492 lines
24 KiB
Rust
492 lines
24 KiB
Rust
#[macro_use]
|
|
extern crate lazy_static;
|
|
|
|
pub mod de;
|
|
pub mod ser;
|
|
pub mod error;
|
|
pub mod set;
|
|
pub mod symbol;
|
|
pub mod value;
|
|
|
|
#[cfg(test)]
|
|
mod dom {
|
|
use super::value::{
|
|
Value, IOValue, NestedValue, PlainValue, Domain,
|
|
encoder::encode_bytes,
|
|
};
|
|
|
|
#[derive(Debug, Hash, Clone, Ord, PartialEq, Eq, PartialOrd)]
|
|
pub enum Dom {
|
|
One,
|
|
Two,
|
|
}
|
|
|
|
impl Domain for Dom {
|
|
fn as_preserves(&self) -> Result<IOValue, std::io::Error> {
|
|
Ok(match self {
|
|
// TODO: How to nicely expose streaming values?
|
|
Dom::One => Value::ByteString(vec![255, 255, 255, 255]).wrap(),
|
|
Dom::Two => Value::symbol(&format!("Dom::{:?}", self)).wrap(),
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test] fn test_one() {
|
|
let v: PlainValue<_> = Value::from(vec![Value::from(1).wrap(),
|
|
Value::Domain(Dom::One).wrap(),
|
|
Value::from(2).wrap()])
|
|
.wrap();
|
|
assert_eq!(encode_bytes(&v.to_io_value()).unwrap(),
|
|
[147, 49, 100, 255, 255, 255, 255, 50]);
|
|
}
|
|
|
|
#[test] fn test_two() {
|
|
let v: PlainValue<_> = Value::from(vec![Value::from(1).wrap(),
|
|
Value::Domain(Dom::Two).wrap(),
|
|
Value::from(2).wrap()])
|
|
.wrap();
|
|
assert_eq!(encode_bytes(&v.to_io_value()).unwrap(),
|
|
[147, 49, 120, 68, 111, 109, 58, 58, 84, 119, 111, 50]);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod ieee754_section_5_10_total_order_tests {
|
|
use std::cmp::Ordering::{Less, Equal, Greater};
|
|
use super::dom::Dom;
|
|
|
|
use crate::value::{Value, PlainValue};
|
|
fn f(val: f32) -> Value<PlainValue<Dom>, Dom> { Value::from(val) }
|
|
fn d(val: f64) -> Value<PlainValue<Dom>, Dom> { Value::from(val) }
|
|
|
|
// TODO: Test cases with a few different signalling and non-signalling NaNs
|
|
|
|
#[test] fn case32_a_1() { assert_eq!(f(1.0).cmp(&f(2.0)), Less) }
|
|
#[test] fn case32_a_2() { assert_eq!(f(-1.0).cmp(&f(1.0)), Less) }
|
|
#[test] fn case32_a_3() { assert_eq!(f(0.0).cmp(&f(1.0)), Less) }
|
|
#[test] fn case32_a_4() { assert_eq!(f(-1.0).cmp(&f(0.0)), Less) }
|
|
#[test] fn case32_a_5() { assert_eq!(f(-1e32).cmp(&f(-1e31)), Less) }
|
|
#[test] fn case32_a_6() { assert_eq!(f(-1e32).cmp(&f(1e33)), Less) }
|
|
#[test] fn case32_a_7() {
|
|
assert_eq!(f(std::f32::NEG_INFINITY).cmp(&f(std::f32::INFINITY)), Less)
|
|
}
|
|
#[test] fn case32_a_8() { assert_eq!(f(std::f32::NEG_INFINITY).cmp(&f(0.0)), Less) }
|
|
#[test] fn case32_a_9() { assert_eq!(f(std::f32::NEG_INFINITY).cmp(&f(1.0)), Less) }
|
|
#[test] fn case32_a_10() { assert_eq!(f(std::f32::NEG_INFINITY).cmp(&f(1e33)), Less) }
|
|
#[test] fn case32_a_11() { assert_eq!(f(0.0).cmp(&f(std::f32::INFINITY)), Less) }
|
|
#[test] fn case32_a_12() { assert_eq!(f(1.0).cmp(&f(std::f32::INFINITY)), Less) }
|
|
#[test] fn case32_a_13() { assert_eq!(f(1e33).cmp(&f(std::f32::INFINITY)), Less) }
|
|
|
|
#[test] fn case32_b_1() { assert_eq!(f(2.0).cmp(&f(1.0)), Greater) }
|
|
#[test] fn case32_b_2() { assert_eq!(f(1.0).cmp(&f(-1.0)), Greater) }
|
|
#[test] fn case32_b_3() { assert_eq!(f(1.0).cmp(&f(0.0)), Greater) }
|
|
#[test] fn case32_b_4() { assert_eq!(f(0.0).cmp(&f(-1.0)), Greater) }
|
|
#[test] fn case32_b_5() { assert_eq!(f(-1e31).cmp(&f(-1e32)), Greater) }
|
|
#[test] fn case32_b_6() { assert_eq!(f(1e33).cmp(&f(-1e32)), Greater) }
|
|
#[test] fn case32_b_7() {
|
|
assert_eq!(f(std::f32::INFINITY).cmp(&f(std::f32::NEG_INFINITY)), Greater)
|
|
}
|
|
#[test] fn case32_b_8() { assert_eq!(f(std::f32::INFINITY).cmp(&f(0.0)), Greater) }
|
|
#[test] fn case32_b_9() { assert_eq!(f(std::f32::INFINITY).cmp(&f(1.0)), Greater) }
|
|
#[test] fn case32_b_10() { assert_eq!(f(std::f32::INFINITY).cmp(&f(1e33)), Greater) }
|
|
#[test] fn case32_b_11() { assert_eq!(f(0.0).cmp(&f(std::f32::NEG_INFINITY)), Greater) }
|
|
#[test] fn case32_b_12() { assert_eq!(f(1.0).cmp(&f(std::f32::NEG_INFINITY)), Greater) }
|
|
#[test] fn case32_b_13() { assert_eq!(f(1e33).cmp(&f(std::f32::NEG_INFINITY)), Greater) }
|
|
|
|
#[test] fn case32_c1() { assert_eq!(f(-0.0).cmp(&f( 0.0)), Less) }
|
|
#[test] fn case32_c2() { assert_eq!(f( 0.0).cmp(&f(-0.0)), Greater) }
|
|
#[test] fn case32_c3_1() { assert_eq!(f(-0.0).cmp(&f(-0.0)), Equal) }
|
|
#[test] fn case32_c3_2() { assert_eq!(f( 0.0).cmp(&f( 0.0)), Equal) }
|
|
#[test] fn case32_c3_3() { assert_eq!(f(1.0).cmp(&f(1.0)), Equal) }
|
|
#[test] fn case32_c3_4() { assert_eq!(f(-1.0).cmp(&f(-1.0)), Equal) }
|
|
#[test] fn case32_c3_5() { assert_eq!(f(-1e32).cmp(&f(-1e32)), Equal) }
|
|
#[test] fn case32_c3_6() { assert_eq!(f(1e33).cmp(&f(1e33)), Equal) }
|
|
|
|
|
|
#[test] fn case64_a_1() { assert_eq!(d(1.0).cmp(&d(2.0)), Less) }
|
|
#[test] fn case64_a_2() { assert_eq!(d(-1.0).cmp(&d(1.0)), Less) }
|
|
#[test] fn case64_a_3() { assert_eq!(d(0.0).cmp(&d(1.0)), Less) }
|
|
#[test] fn case64_a_4() { assert_eq!(d(-1.0).cmp(&d(0.0)), Less) }
|
|
#[test] fn case64_a_5() { assert_eq!(d(-1e32).cmp(&d(-1e31)), Less) }
|
|
#[test] fn case64_a_6() { assert_eq!(d(-1e32).cmp(&d(1e33)), Less) }
|
|
#[test] fn case64_a_7() {
|
|
assert_eq!(d(std::f64::NEG_INFINITY).cmp(&d(std::f64::INFINITY)), Less)
|
|
}
|
|
#[test] fn case64_a_8() { assert_eq!(d(std::f64::NEG_INFINITY).cmp(&d(0.0)), Less) }
|
|
#[test] fn case64_a_9() { assert_eq!(d(std::f64::NEG_INFINITY).cmp(&d(1.0)), Less) }
|
|
#[test] fn case64_a_10() { assert_eq!(d(std::f64::NEG_INFINITY).cmp(&d(1e33)), Less) }
|
|
#[test] fn case64_a_11() { assert_eq!(d(0.0).cmp(&d(std::f64::INFINITY)), Less) }
|
|
#[test] fn case64_a_12() { assert_eq!(d(1.0).cmp(&d(std::f64::INFINITY)), Less) }
|
|
#[test] fn case64_a_13() { assert_eq!(d(1e33).cmp(&d(std::f64::INFINITY)), Less) }
|
|
|
|
#[test] fn case64_b_1() { assert_eq!(d(2.0).cmp(&d(1.0)), Greater) }
|
|
#[test] fn case64_b_2() { assert_eq!(d(1.0).cmp(&d(-1.0)), Greater) }
|
|
#[test] fn case64_b_3() { assert_eq!(d(1.0).cmp(&d(0.0)), Greater) }
|
|
#[test] fn case64_b_4() { assert_eq!(d(0.0).cmp(&d(-1.0)), Greater) }
|
|
#[test] fn case64_b_5() { assert_eq!(d(-1e31).cmp(&d(-1e32)), Greater) }
|
|
#[test] fn case64_b_6() { assert_eq!(d(1e33).cmp(&d(-1e32)), Greater) }
|
|
#[test] fn case64_b_7() {
|
|
assert_eq!(d(std::f64::INFINITY).cmp(&d(std::f64::NEG_INFINITY)), Greater)
|
|
}
|
|
#[test] fn case64_b_8() { assert_eq!(d(std::f64::INFINITY).cmp(&d(0.0)), Greater) }
|
|
#[test] fn case64_b_9() { assert_eq!(d(std::f64::INFINITY).cmp(&d(1.0)), Greater) }
|
|
#[test] fn case64_b_10() { assert_eq!(d(std::f64::INFINITY).cmp(&d(1e33)), Greater) }
|
|
#[test] fn case64_b_11() { assert_eq!(d(0.0).cmp(&d(std::f64::NEG_INFINITY)), Greater) }
|
|
#[test] fn case64_b_12() { assert_eq!(d(1.0).cmp(&d(std::f64::NEG_INFINITY)), Greater) }
|
|
#[test] fn case64_b_13() { assert_eq!(d(1e33).cmp(&d(std::f64::NEG_INFINITY)), Greater) }
|
|
|
|
#[test] fn case64_c1() { assert_eq!(d(-0.0).cmp(&d( 0.0)), Less) }
|
|
#[test] fn case64_c2() { assert_eq!(d( 0.0).cmp(&d(-0.0)), Greater) }
|
|
#[test] fn case64_c3_1() { assert_eq!(d(-0.0).cmp(&d(-0.0)), Equal) }
|
|
#[test] fn case64_c3_2() { assert_eq!(d( 0.0).cmp(&d( 0.0)), Equal) }
|
|
#[test] fn case64_c3_3() { assert_eq!(d(1.0).cmp(&d(1.0)), Equal) }
|
|
#[test] fn case64_c3_4() { assert_eq!(d(-1.0).cmp(&d(-1.0)), Equal) }
|
|
#[test] fn case64_c3_5() { assert_eq!(d(-1e32).cmp(&d(-1e32)), Equal) }
|
|
#[test] fn case64_c3_6() { assert_eq!(d(1e33).cmp(&d(1e33)), Equal) }
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod value_tests {
|
|
use crate::value::{Value, PlainValue, value::Record, signed_integer::SignedInteger};
|
|
use super::dom::Dom;
|
|
|
|
type VV = Value<PlainValue<Dom>, Dom>;
|
|
|
|
#[test] fn boolean_mut() {
|
|
let mut b = VV::Boolean(true);
|
|
assert!(b.is_boolean());
|
|
*(b.as_boolean_mut().unwrap()) = false;
|
|
assert_eq!(b, VV::Boolean(false));
|
|
}
|
|
|
|
#[test] fn float_mut() {
|
|
let mut f = VV::from(1.0f32);
|
|
assert!(f.is_float());
|
|
*(f.as_float_mut().unwrap()) = 123.45;
|
|
assert_eq!(f, VV::from(123.45f32));
|
|
assert_eq!(f.as_float().unwrap(), 123.45f32);
|
|
}
|
|
|
|
#[test] fn double_mut() {
|
|
let mut f = VV::from(1.0);
|
|
assert!(f.is_double());
|
|
*(f.as_double_mut().unwrap()) = 123.45;
|
|
assert_eq!(f, VV::from(123.45));
|
|
assert_eq!(f.as_double().unwrap(), 123.45);
|
|
}
|
|
|
|
#[test] fn signedinteger_mut() {
|
|
let mut i = VV::from(123);
|
|
assert!(i.is_signedinteger());
|
|
*(i.as_signedinteger_mut().unwrap()) = SignedInteger::from(234i128);
|
|
assert_eq!(i, VV::from(234));
|
|
assert_eq!(i.as_i().unwrap(), 234);
|
|
}
|
|
|
|
#[test] fn string_mut() {
|
|
let mut s = VV::from("hello, world!");
|
|
assert!(s.is_string());
|
|
s.as_string_mut().unwrap().replace_range(7..12, "there");
|
|
assert_eq!(s, VV::from("hello, there!"));
|
|
}
|
|
|
|
#[test] fn bytes_mut() {
|
|
let mut b = VV::from(&b"hello, world!"[..]);
|
|
assert!(b.is_bytestring());
|
|
b.as_bytestring_mut().unwrap().splice(7..12, Vec::from(&b"there"[..]));
|
|
assert_eq!(b, VV::from(&b"hello, there!"[..]));
|
|
}
|
|
|
|
#[test] fn symbol_mut() {
|
|
let mut s = VV::symbol("abcd");
|
|
assert!(s.is_symbol());
|
|
s.as_symbol_mut().unwrap().replace_range(..2, "AB");
|
|
assert_eq!(s, VV::symbol("ABcd"));
|
|
}
|
|
|
|
#[test] fn record_mut() {
|
|
let says = VV::symbol("says").wrap();
|
|
let mut r = VV::Record(Record(vec![says.clone(), VV::from("Tony").wrap(), VV::from("Hello!").wrap()]));
|
|
assert_eq!(r.as_record_mut(Some(0)), None);
|
|
assert_eq!(r.as_record_mut(Some(1)), None);
|
|
assert!(r.as_record_mut(Some(2)).is_some());
|
|
assert_eq!(r.as_record_mut(Some(3)), None);
|
|
r.as_record_mut(None).unwrap().fields_mut()[0] = VV::from("Alice").wrap();
|
|
assert_eq!(r, VV::Record(Record(vec![says, VV::from("Alice").wrap(), VV::from("Hello!").wrap()])));
|
|
}
|
|
|
|
#[test] fn sequence_mut() {
|
|
let mut s = VV::Sequence(vec![VV::from(1).wrap(),
|
|
VV::from(2).wrap(),
|
|
VV::from(3).wrap()]);
|
|
let r = VV::Sequence(vec![VV::from(1).wrap(),
|
|
VV::from(99).wrap(),
|
|
VV::from(3).wrap()]);
|
|
s.as_sequence_mut().unwrap()[1] = VV::from(99).wrap();
|
|
assert_eq!(r, s);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod decoder_tests {
|
|
use crate::value::{Value, NestedValue, decoder};
|
|
use crate::de::from_bytes;
|
|
use crate::error::{Error, ExpectedKind, is_eof_io_error};
|
|
|
|
fn expect_number_out_of_range<T: core::fmt::Debug>(r: Result<T, Error>) {
|
|
match r {
|
|
Ok(v) => panic!("Expected NumberOutOfRange, but got a parse of {:?}", v),
|
|
Err(Error::NumberOutOfRange(_)) => (),
|
|
Err(e) => panic!("Expected NumberOutOfRange, but got an error of {:?}", e),
|
|
}
|
|
}
|
|
|
|
fn expect_expected<T: core::fmt::Debug>(k: ExpectedKind, r: Result<T, Error>) {
|
|
match r {
|
|
Ok(v) => panic!("Expected Expected({:?}), but got a parse of {:?}", k, v),
|
|
Err(Error::Expected(k1, _)) if k1 == k => (),
|
|
Err(e) => panic!("Expected Expected({:?}, but got an error of {:?}", k, e),
|
|
}
|
|
}
|
|
|
|
#[test] fn skip_annotations_noskip() {
|
|
let mut buf = &b"\x0521"[..];
|
|
let mut d = decoder::from_bytes(&mut buf);
|
|
let v = d.demand_next().unwrap();
|
|
assert_eq!(v.annotations().len(), 1);
|
|
assert_eq!(v.annotations()[0], Value::from(2).wrap());
|
|
assert_eq!(v.value(), &Value::from(1));
|
|
}
|
|
|
|
#[test] fn skip_annotations_skip() {
|
|
let mut buf = &b"\x0521"[..];
|
|
let mut d = decoder::from_bytes(&mut buf);
|
|
d.set_read_annotations(false);
|
|
let v = d.demand_next().unwrap();
|
|
assert_eq!(v.annotations().len(), 0);
|
|
assert_eq!(v.value(), &Value::from(1));
|
|
}
|
|
|
|
#[test] fn multiple_values_buf_advanced() {
|
|
let mut buf = &b"\x81tPing\x81tPong"[..];
|
|
assert_eq!(buf.len(), 12);
|
|
let mut d = decoder::from_bytes(&mut buf);
|
|
assert_eq!(d.read.source.index, 0);
|
|
assert_eq!(d.demand_next().unwrap().value(), &Value::simple_record0("Ping"));
|
|
assert_eq!(d.read.source.index, 6);
|
|
assert_eq!(d.demand_next().unwrap().value(), &Value::simple_record0("Pong"));
|
|
assert_eq!(d.read.source.index, 12);
|
|
assert!(d.next().is_none());
|
|
assert!(if let Err(e) = d.demand_next() { is_eof_io_error(&e) } else { false });
|
|
}
|
|
|
|
#[test] fn direct_i8_format_a_positive() { assert_eq!(from_bytes::<i8>(b"1").unwrap(), 1) }
|
|
#[test] fn direct_i8_format_a_zero() { assert_eq!(from_bytes::<i8>(b"0").unwrap(), 0) }
|
|
#[test] fn direct_i8_format_a_negative() { assert_eq!(from_bytes::<i8>(b"?").unwrap(), -1) }
|
|
#[test] fn direct_i8_format_b() { assert_eq!(from_bytes::<i8>(b"A\xfe").unwrap(), -2) }
|
|
#[test] fn direct_i8_format_b_too_long() { assert_eq!(from_bytes::<i8>(b"C\xff\xff\xfe").unwrap(), -2) }
|
|
#[test] fn direct_i8_format_b_much_too_long() { assert_eq!(from_bytes::<i8>(b"J\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe").unwrap(), -2) }
|
|
#[test] fn direct_i8_format_c() { assert_eq!(from_bytes::<i8>(b"$a\xfe\x04").unwrap(), -2) }
|
|
#[test] fn direct_i8_format_c_too_long() { assert_eq!(from_bytes::<i8>(b"$a\xffa\xfe\x04").unwrap(), -2) }
|
|
#[test] fn direct_i8_format_c_much_too_long() { assert_eq!(from_bytes::<i8>(b"$a\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xfe\x04").unwrap(), -2) }
|
|
|
|
#[test] fn direct_u8_format_a_positive() { assert_eq!(from_bytes::<u8>(b"1").unwrap(), 1) }
|
|
#[test] fn direct_u8_format_a_zero() { assert_eq!(from_bytes::<u8>(b"0").unwrap(), 0) }
|
|
#[test] fn direct_u8_format_b() { assert_eq!(from_bytes::<u8>(b"A1").unwrap(), 49) }
|
|
#[test] fn direct_u8_format_b_too_long() { assert_eq!(from_bytes::<u8>(b"D\0\0\01").unwrap(), 49) }
|
|
#[test] fn direct_u8_format_b_much_too_long() { assert_eq!(from_bytes::<u8>(b"J\0\0\0\0\0\0\0\0\01").unwrap(), 49) }
|
|
#[test] fn direct_u8_format_c() { assert_eq!(from_bytes::<u8>(b"$a1\x04").unwrap(), 49) }
|
|
#[test] fn direct_u8_format_c_too_long() { assert_eq!(from_bytes::<u8>(b"$a\0a1\x04").unwrap(), 49) }
|
|
#[test] fn direct_u8_format_c_much_too_long() { assert_eq!(from_bytes::<u8>(b"$a\0a\0a\0a\0a\0a\0a\0a\0a\0a\0a\0a1\x04").unwrap(), 49) }
|
|
|
|
#[test] fn direct_i16_format_a() { assert_eq!(from_bytes::<i16>(b">").unwrap(), -2) }
|
|
#[test] fn direct_i16_format_b() { assert_eq!(from_bytes::<i16>(b"B\xfe\xff").unwrap(), -257) }
|
|
|
|
#[test] fn direct_u8_wrong_format() {
|
|
expect_expected(ExpectedKind::SignedInteger, from_bytes::<u8>(b"Ubogus"))
|
|
}
|
|
|
|
#[test] fn direct_u8_format_b_too_large() {
|
|
expect_number_out_of_range(from_bytes::<u8>(b"D\0\011"))
|
|
}
|
|
|
|
#[test] fn direct_u8_format_c_too_large() {
|
|
expect_number_out_of_range(from_bytes::<u8>(b"$a1a1\x04"))
|
|
}
|
|
|
|
#[test] fn direct_i8_format_b_too_large() {
|
|
expect_number_out_of_range(from_bytes::<i8>(b"B\xfe\xff"))
|
|
}
|
|
|
|
#[test] fn direct_i8_format_c_too_large() {
|
|
expect_number_out_of_range(from_bytes::<u8>(b"$a\xfea\xff\x04"))
|
|
}
|
|
|
|
#[test] fn direct_i16_format_b_too_large() {
|
|
expect_number_out_of_range(from_bytes::<i16>(b"C\xfe\xff\xff"));
|
|
}
|
|
|
|
#[test] fn direct_i32_format_b_ok() {
|
|
assert_eq!(from_bytes::<i32>(b"C\xfe\xff\xff").unwrap(), -65537);
|
|
}
|
|
|
|
#[test] fn direct_i32_format_b_ok_2() {
|
|
assert_eq!(from_bytes::<i32>(b"D\xfe\xff\xff\xff").unwrap(), -16777217);
|
|
}
|
|
|
|
#[test] fn direct_i64_format_b() {
|
|
assert_eq!(from_bytes::<i64>(b"A\xff").unwrap(), -1);
|
|
assert_eq!(from_bytes::<i64>(b"C\xff\xff\xff").unwrap(), -1);
|
|
assert_eq!(from_bytes::<i64>(b"J\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff").unwrap(), -1);
|
|
assert_eq!(from_bytes::<i64>(b"A\xfe").unwrap(), -2);
|
|
assert_eq!(from_bytes::<i64>(b"C\xff\xfe\xff").unwrap(), -257);
|
|
assert_eq!(from_bytes::<i64>(b"C\xfe\xff\xff").unwrap(), -65537);
|
|
assert_eq!(from_bytes::<i64>(b"J\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff").unwrap(), -16777217);
|
|
assert_eq!(from_bytes::<i64>(b"J\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff").unwrap(), -72057594037927937);
|
|
expect_number_out_of_range(from_bytes::<i64>(b"J\xff\xff\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
|
expect_number_out_of_range(from_bytes::<i64>(b"I\xff\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
|
expect_number_out_of_range(from_bytes::<i64>(b"I\x80\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
|
expect_number_out_of_range(from_bytes::<i64>(b"J\xff\x00\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
|
assert_eq!(from_bytes::<i64>(b"H\xfe\xff\xff\xff\xff\xff\xff\xff").unwrap(), -72057594037927937);
|
|
assert_eq!(from_bytes::<i64>(b"H\x0e\xff\xff\xff\xff\xff\xff\xff").unwrap(), 1080863910568919039);
|
|
assert_eq!(from_bytes::<i64>(b"H\x80\0\0\0\0\0\0\0").unwrap(), -9223372036854775808);
|
|
assert_eq!(from_bytes::<i64>(b"H\0\0\0\0\0\0\0\0").unwrap(), 0);
|
|
assert_eq!(from_bytes::<i64>(b"@").unwrap(), 0);
|
|
assert_eq!(from_bytes::<i64>(b"H\x7f\xff\xff\xff\xff\xff\xff\xff").unwrap(), 9223372036854775807);
|
|
}
|
|
|
|
#[test] fn direct_u64_format_b() {
|
|
expect_number_out_of_range(from_bytes::<u64>(b"A\xff"));
|
|
assert_eq!(from_bytes::<u64>(b"B\0\xff").unwrap(), 255);
|
|
expect_number_out_of_range(from_bytes::<u64>(b"C\xff\xff\xff"));
|
|
assert_eq!(from_bytes::<u64>(b"D\0\xff\xff\xff").unwrap(), 0xffffff);
|
|
expect_number_out_of_range(from_bytes::<u64>(b"J\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"));
|
|
assert_eq!(from_bytes::<u64>(b"A\x02").unwrap(), 2);
|
|
assert_eq!(from_bytes::<u64>(b"C\x00\x01\x00").unwrap(), 256);
|
|
assert_eq!(from_bytes::<u64>(b"C\x01\x00\x00").unwrap(), 65536);
|
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00").unwrap(), 16777216);
|
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00").unwrap(), 72057594037927936);
|
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\xf2\x00\x00\x00\x00\x00\x00\x00").unwrap(), 0xf200000000000000);
|
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\x72\x00\x00\x00\x00\x00\x00\x00").unwrap(), 0x7200000000000000);
|
|
expect_number_out_of_range(from_bytes::<u64>(b"J\x00\xf2\x00\x00\x00\x00\x00\x00\x00\x00"));
|
|
assert_eq!(from_bytes::<u64>(b"I\x00\xf2\x00\x00\x00\x00\x00\x00\x00").unwrap(), 0xf200000000000000);
|
|
expect_number_out_of_range(from_bytes::<u64>(b"I\x7f\xf2\x00\x00\x00\x00\x00\x00\x00"));
|
|
expect_number_out_of_range(from_bytes::<u64>(b"J\x00\xff\xf2\x00\x00\x00\x00\x00\x00\x00"));
|
|
assert_eq!(from_bytes::<u64>(b"H\x01\x00\x00\x00\x00\x00\x00\x00").unwrap(), 72057594037927936);
|
|
assert_eq!(from_bytes::<u64>(b"H\x0e\xff\xff\xff\xff\xff\xff\xff").unwrap(), 1080863910568919039);
|
|
expect_number_out_of_range(from_bytes::<u64>(b"H\x80\0\0\0\0\0\0\0"));
|
|
assert_eq!(from_bytes::<u64>(b"I\0\x80\0\0\0\0\0\0\0").unwrap(), 9223372036854775808);
|
|
assert_eq!(from_bytes::<u64>(b"H\0\0\0\0\0\0\0\0").unwrap(), 0);
|
|
assert_eq!(from_bytes::<u64>(b"@").unwrap(), 0);
|
|
assert_eq!(from_bytes::<u64>(b"H\x7f\xff\xff\xff\xff\xff\xff\xff").unwrap(), 9223372036854775807);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod serde_tests {
|
|
use crate::symbol::Symbol;
|
|
use crate::de::from_bytes as deserialize_from_bytes;
|
|
use crate::value::de::from_value as deserialize_from_value;
|
|
use crate::value::encoder::encode_bytes;
|
|
use crate::value::to_value;
|
|
use crate::value::{Value, IOValue, Map, Set};
|
|
|
|
#[test] fn simple_to_value() {
|
|
use serde::Serialize;
|
|
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
struct Colour{ red: u8, green: u8, blue: u8 }
|
|
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
struct SimpleValue<'a>(String,
|
|
#[serde(with = "crate::symbol")] String,
|
|
Symbol,
|
|
#[serde(with = "crate::symbol")] String,
|
|
Symbol,
|
|
&'a str,
|
|
#[serde(with = "serde_bytes")] &'a [u8],
|
|
#[serde(with = "serde_bytes")] Vec<u8>,
|
|
Vec<bool>,
|
|
#[serde(with = "crate::set")] Set<String>,
|
|
i16,
|
|
IOValue,
|
|
Map<String, Colour>,
|
|
f32,
|
|
f64);
|
|
let mut str_set = Set::new();
|
|
str_set.insert("one".to_owned());
|
|
str_set.insert("two".to_owned());
|
|
str_set.insert("three".to_owned());
|
|
let mut colours = Map::new();
|
|
colours.insert("red".to_owned(), Colour { red: 255, green: 0, blue: 0 });
|
|
colours.insert("green".to_owned(), Colour { red: 0, green: 255, blue: 0 });
|
|
colours.insert("blue".to_owned(), Colour { red: 0, green: 0, blue: 255 });
|
|
let v = SimpleValue("hello".to_string(),
|
|
"sym1".to_string(),
|
|
Symbol("sym2".to_string()),
|
|
"sym3".to_string(),
|
|
Symbol("sym4".to_string()),
|
|
"world",
|
|
&b"slice"[..],
|
|
b"vec".to_vec(),
|
|
vec![false, true, false, true],
|
|
str_set,
|
|
12345,
|
|
Value::from("hi").wrap(),
|
|
colours,
|
|
12.345,
|
|
12.3456789);
|
|
println!("== v: {:#?}", v);
|
|
let w: IOValue = to_value(&v);
|
|
println!("== w: {:#?}", w);
|
|
let x = deserialize_from_value(&w).unwrap();
|
|
println!("== x: {:#?}", &x);
|
|
assert_eq!(v, x);
|
|
|
|
let expected_bytes = vec![
|
|
0x8f, 0x10, // Struct, 15 members + 1 label
|
|
0x7b, 0x53, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, // SimpleValue
|
|
0x55, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
|
|
0x74, 0x73, 0x79, 0x6d, 0x31, // sym1
|
|
0x74, 0x73, 0x79, 0x6d, 0x32, // sym2
|
|
0x74, 0x73, 0x79, 0x6d, 0x33, // sym3
|
|
0x74, 0x73, 0x79, 0x6d, 0x34, // sym4
|
|
0x55, 0x77, 0x6f, 0x72, 0x6c, 0x64, // "world"
|
|
0x65, 0x73, 0x6c, 0x69, 0x63, 0x65, // #"slice"
|
|
0x63, 0x76, 0x65, 0x63, // #"vec"
|
|
0x94, // Sequence, 4 items
|
|
0x0, // false
|
|
0x1, // true
|
|
0x0, // false
|
|
0x1, // true
|
|
0xa3, // Set, 3 items
|
|
0x53, 0x6f, 0x6e, 0x65,
|
|
0x55, 0x74, 0x68, 0x72, 0x65, 0x65,
|
|
0x53, 0x74, 0x77, 0x6f,
|
|
0x42, 0x30, 0x39, // 12345
|
|
0x52, 0x68, 0x69, // "hi"
|
|
0xb6, // Dictionary, 6 items = 3 key/value pairs
|
|
0x54, 0x62, 0x6c, 0x75, 0x65, // "blue"
|
|
0x84, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x75, 0x72, 0x30, 0x30, 0x42, 0x00, 0xff,
|
|
0x55, 0x67, 0x72, 0x65, 0x65, 0x6e, // "green"
|
|
0x84, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x75, 0x72, 0x30, 0x42, 0x00, 0xff, 0x30,
|
|
0x53, 0x72, 0x65, 0x64, // "red"
|
|
0x84, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x75, 0x72, 0x42, 0x00, 0xff, 0x30, 0x30,
|
|
|
|
0x2, 0x41, 0x45, 0x85, 0x1f, // 12.345,
|
|
0x3, 0x40, 0x28, 0xb0, 0xfc, 0xd3, 0x24, 0xd5, 0xa2, // 12.3456789
|
|
];
|
|
|
|
let v_bytes_1 = encode_bytes(&w).unwrap();
|
|
println!("== w bytes = {:?}", v_bytes_1);
|
|
assert_eq!(expected_bytes, v_bytes_1);
|
|
|
|
let mut v_bytes_2 = Vec::new();
|
|
v.serialize(&mut crate::ser::Serializer::new(&mut v_bytes_2)).unwrap();
|
|
println!("== v bytes = {:?}", v_bytes_2);
|
|
assert_eq!(v_bytes_1, v_bytes_2);
|
|
|
|
let y = deserialize_from_bytes(&v_bytes_1).unwrap();
|
|
println!("== y: {:#?}", &y);
|
|
assert_eq!(v, y);
|
|
}
|
|
}
|