add simple test for decoding

This commit is contained in:
Kasey White 2022-05-30 17:12:32 -04:00
parent d4b659c04e
commit 83a784c745
3 changed files with 21 additions and 4 deletions

View File

@ -1,12 +1,12 @@
use crate::builtins::DefaultFunction; use crate::builtins::DefaultFunction;
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub struct Program { pub struct Program {
pub version: (usize, usize, usize), pub version: (usize, usize, usize),
pub term: Term, pub term: Term,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum Term { pub enum Term {
// tag: 0 // tag: 0
Var(String), Var(String),
@ -32,7 +32,7 @@ pub enum Term {
Builtin(DefaultFunction), Builtin(DefaultFunction),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum Constant { pub enum Constant {
// TODO: figure out the right size for this // TODO: figure out the right size for this
// tag: 0 // tag: 0

View File

@ -2,7 +2,7 @@ use strum_macros::EnumString;
#[repr(u8)] #[repr(u8)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[derive(Debug, Clone, EnumString)] #[derive(Debug, Clone, EnumString, PartialEq)]
#[strum(serialize_all = "camelCase")] #[strum(serialize_all = "camelCase")]
pub enum DefaultFunction { pub enum DefaultFunction {
// Integer functions // Integer functions

View File

@ -237,6 +237,8 @@ pub fn decode_constant_tag(d: &mut Decoder) -> Result<u8, String> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use flat::Flat;
use super::{Constant, Program, Term}; use super::{Constant, Program, Term};
#[test] #[test]
@ -253,4 +255,19 @@ mod test {
vec![0b00001011, 0b00010110, 0b00100001, 0b01001000, 0b00000101, 0b10000001] vec![0b00001011, 0b00010110, 0b00100001, 0b01001000, 0b00000101, 0b10000001]
) )
} }
#[test]
fn flat_decode_integer() {
let flat_encoded = vec![
0b00001011, 0b00010110, 0b00100001, 0b01001000, 0b00000101, 0b10000001,
];
let expected_program = Program {
version: (11, 22, 33),
term: Term::Constant(Constant::Integer(11)),
};
let actual_program: Program = Program::unflat(flat_encoded.as_slice()).unwrap();
assert_eq!(actual_program, expected_program)
}
} }