feat: Move flat en/de from aiken to pallas (#303)

Nothing new is going on within the code itself.
I simply popped the crate into pallas_codec
as a submodule `pallas_codec::flat`. I also moved
over the tests that we had in the crate. In general
this is in solid shape and hasn't had any changes for
months. That said there could be some things that require love
like dealing with BigInt.

Co-authored-by: Kasey White <kwhitemsg@gmail.com>
This commit is contained in:
Lucas 2023-10-04 19:08:52 -04:00 committed by GitHub
parent f82fbcd7d9
commit 11687d7f85
15 changed files with 1122 additions and 15 deletions

123
pallas-codec/tests/flat.rs Normal file
View file

@ -0,0 +1,123 @@
use pallas_codec::flat::filler::Filler;
use pallas_codec::flat::{decode, encode};
use proptest::prelude::*;
prop_compose! {
fn arb_big_vec()(size in 255..300, element in any::<u8>()) -> Vec<u8> {
(0..size).map(|_| element).collect()
}
}
#[test]
fn encode_bool() {
let bytes = encode(&true).unwrap();
assert_eq!(bytes, vec![0b10000001]);
let decoded: bool = decode(bytes.as_slice()).unwrap();
assert!(decoded);
let bytes = encode(&false).unwrap();
assert_eq!(bytes, vec![0b00000001]);
let decoded: bool = decode(bytes.as_slice()).unwrap();
assert!(!decoded);
}
#[test]
fn encode_u8() {
let bytes = encode(&3_u8).unwrap();
assert_eq!(bytes, vec![0b00000011, 0b00000001]);
let decoded: u8 = decode(bytes.as_slice()).unwrap();
assert_eq!(decoded, 3_u8);
}
proptest! {
#[test]
fn encode_isize(x: isize) {
let bytes = encode(&x).unwrap();
let decoded: isize = decode(&bytes).unwrap();
assert_eq!(decoded, x);
}
#[test]
fn encode_usize(x: usize) {
let bytes = encode(&x).unwrap();
let decoded: usize = decode(&bytes).unwrap();
assert_eq!(decoded, x);
}
#[test]
fn encode_char(c: char) {
let bytes = encode(&c).unwrap();
let decoded: char = decode(&bytes).unwrap();
assert_eq!(decoded, c);
}
#[test]
fn encode_string(str: String) {
let bytes = encode(&str).unwrap();
let decoded: String = decode(&bytes).unwrap();
assert_eq!(decoded, str);
}
#[test]
fn encode_vec_u8(xs: Vec<u8>) {
let bytes = encode(&xs).unwrap();
let decoded: Vec<u8> = decode(&bytes).unwrap();
assert_eq!(decoded, xs);
}
#[test]
fn encode_big_vec_u8(xs in arb_big_vec()) {
let bytes = encode(&xs).unwrap();
let decoded: Vec<u8> = decode(&bytes).unwrap();
assert_eq!(decoded, xs);
}
#[test]
fn encode_arr_u8(xs: Vec<u8>) {
let bytes = encode(&xs.as_slice()).unwrap();
let decoded: Vec<u8> = decode(&bytes).unwrap();
assert_eq!(decoded, xs);
}
#[test]
fn encode_big_arr_u8(xs in arb_big_vec()) {
let bytes = encode(&xs.as_slice()).unwrap();
let decoded: Vec<u8> = decode(&bytes).unwrap();
assert_eq!(decoded, xs);
}
#[test]
fn encode_boxed(c: char) {
let boxed = Box::new(c);
let bytes = encode(&boxed).unwrap();
let decoded: char = decode(&bytes).unwrap();
assert_eq!(decoded, c);
}
}
#[test]
fn encode_filler() {
let bytes = encode(&Filler::FillerEnd).unwrap();
assert_eq!(bytes, vec![0b0000001, 0b00000001]);
let bytes = encode(&Filler::FillerStart(Box::new(Filler::FillerEnd))).unwrap();
assert_eq!(bytes, vec![0b0000001, 0b00000001]);
let bytes = encode(&Filler::FillerStart(Box::new(Filler::FillerStart(
Box::new(Filler::FillerEnd),
))))
.unwrap();
assert_eq!(bytes, vec![0b0000001, 0b00000001]);
}

View file

@ -0,0 +1,18 @@
use pallas_codec::flat::zigzag::{to_isize, to_usize};
use proptest::prelude::*;
proptest! {
#[test]
fn zigzag(i: isize) {
let u = to_usize(i);
let converted_i = to_isize(u);
assert_eq!(converted_i, i);
}
#[test]
fn zagzig(u: usize) {
let i = to_isize(u);
let converted_u = to_usize(i);
assert_eq!(converted_u, u);
}
}