2019-06-29 22:02:27 +00:00
|
|
|
pub mod value;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-06-30 22:43:32 +00:00
|
|
|
mod ieee754_section_5_10_total_order_tests {
|
|
|
|
use std::cmp::Ordering::{Less, Equal, Greater};
|
|
|
|
|
2019-09-16 23:58:32 +00:00
|
|
|
use crate::value::Value;
|
2019-06-30 22:43:32 +00:00
|
|
|
// fn v<T>(val: T) -> Value where Value: std::convert::From<T> { Value::from(val) }
|
|
|
|
fn f(val: f32) -> Value { Value::from(val) }
|
|
|
|
fn d(val: f64) -> Value { Value::from(val) }
|
|
|
|
|
2019-07-11 01:52:29 +00:00
|
|
|
// TODO: Test cases with a few different signalling and non-signalling NaNs
|
|
|
|
|
2019-06-30 22:43:32 +00:00
|
|
|
#[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)
|
2019-06-29 22:02:27 +00:00
|
|
|
}
|
2019-06-30 22:43:32 +00:00
|
|
|
#[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) }
|
2019-06-29 22:02:27 +00:00
|
|
|
}
|
2019-09-09 13:46:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod value_tests {
|
2019-09-16 23:58:32 +00:00
|
|
|
use crate::value::Value;
|
2019-09-09 13:46:53 +00:00
|
|
|
use num::bigint::BigInt;
|
|
|
|
|
|
|
|
#[test] fn boolean_mut() {
|
|
|
|
let mut b = Value::Boolean(true);
|
|
|
|
assert!(b.is_boolean());
|
|
|
|
*(b.as_boolean_mut().unwrap()) = false;
|
|
|
|
assert_eq!(b, Value::Boolean(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn float_mut() {
|
|
|
|
let mut f = Value::from(1.0f32);
|
|
|
|
assert!(f.is_float());
|
|
|
|
*(f.as_float_mut().unwrap()) = 123.45;
|
|
|
|
assert_eq!(f, Value::from(123.45f32));
|
|
|
|
assert_eq!(f.as_float().unwrap(), 123.45f32);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn double_mut() {
|
|
|
|
let mut f = Value::from(1.0);
|
|
|
|
assert!(f.is_double());
|
|
|
|
*(f.as_double_mut().unwrap()) = 123.45;
|
|
|
|
assert_eq!(f, Value::from(123.45));
|
|
|
|
assert_eq!(f.as_double().unwrap(), 123.45);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn signedinteger_mut() {
|
|
|
|
let mut i = Value::from(123);
|
|
|
|
assert!(i.is_signedinteger());
|
|
|
|
*(i.as_signedinteger_mut().unwrap()) = BigInt::from(234);
|
|
|
|
assert_eq!(i, Value::from(234));
|
|
|
|
|
|
|
|
use num::traits::cast::ToPrimitive;
|
|
|
|
assert_eq!(i.as_signedinteger().unwrap().to_i64().unwrap(), 234);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn string_mut() {
|
|
|
|
let mut s = Value::from("hello, world!");
|
|
|
|
assert!(s.is_string());
|
|
|
|
s.as_string_mut().unwrap().replace_range(7..12, "there");
|
|
|
|
assert_eq!(s, Value::from("hello, there!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn bytes_mut() {
|
|
|
|
let mut b = Value::from(&b"hello, world!"[..]);
|
|
|
|
assert!(b.is_bytestring());
|
|
|
|
b.as_bytestring_mut().unwrap().splice(7..12, Vec::from(&b"there"[..]));
|
|
|
|
assert_eq!(b, Value::from(&b"hello, there!"[..]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn symbol_mut() {
|
|
|
|
let mut s = Value::symbol("abcd");
|
|
|
|
assert!(s.is_symbol());
|
|
|
|
s.as_symbol_mut().unwrap().replace_range(..2, "AB");
|
|
|
|
assert_eq!(s, Value::symbol("ABcd"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn record_mut() {
|
|
|
|
let says = Value::symbol("says").wrap().rc();
|
|
|
|
let mut r = Value::record(&says, vec![Value::from("Tony").wrap(), Value::from("Hello!").wrap()]);
|
|
|
|
r.as_record_mut().unwrap().1[0] = Value::from("Alice").wrap();
|
|
|
|
assert_eq!(r, Value::record(&says, vec![Value::from("Alice").wrap(), Value::from("Hello!").wrap()]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn sequence_mut() {
|
|
|
|
let mut s = Value::Sequence(vec![Value::from(1).wrap(),
|
|
|
|
Value::from(2).wrap(),
|
|
|
|
Value::from(3).wrap()]);
|
|
|
|
let r = Value::Sequence(vec![Value::from(1).wrap(),
|
|
|
|
Value::from(99).wrap(),
|
|
|
|
Value::from(3).wrap()]);
|
|
|
|
s.as_sequence_mut().unwrap()[1] = Value::from(99).wrap();
|
|
|
|
assert_eq!(r, s);
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 21:08:26 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod decoder_tests {
|
2019-09-16 23:58:32 +00:00
|
|
|
use crate::value::Decoder;
|
|
|
|
use crate::value::Value;
|
2019-09-09 21:08:26 +00:00
|
|
|
|
|
|
|
#[test] fn read_123() {
|
|
|
|
let mut d = Decoder::new(&b"abc"[..], None);
|
|
|
|
assert_eq!(d.read().ok(), Some(97));
|
|
|
|
assert_eq!(d.read().ok(), Some(98));
|
|
|
|
assert_eq!(d.read().ok(), Some(99));
|
|
|
|
assert!(d.read().err().unwrap().is_eof())
|
|
|
|
}
|
|
|
|
|
2019-09-15 11:25:23 +00:00
|
|
|
#[test] fn skip_annotations_noskip() {
|
|
|
|
let mut d = Decoder::new(&b"\x0521"[..], None);
|
2019-09-16 23:58:32 +00:00
|
|
|
let v = d.next().unwrap();
|
2019-09-15 11:25:23 +00:00
|
|
|
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 d = Decoder::new(&b"\x0521"[..], None);
|
|
|
|
d.set_read_annotations(false);
|
2019-09-16 23:58:32 +00:00
|
|
|
let v = d.next().unwrap();
|
2019-09-15 11:25:23 +00:00
|
|
|
assert_eq!(v.annotations().len(), 0);
|
|
|
|
assert_eq!(v.value(), &Value::from(1));
|
|
|
|
}
|
2019-09-16 23:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod samples_tests {
|
|
|
|
use crate::value::Decoder;
|
|
|
|
use crate::value::{Value, AValue};
|
|
|
|
use crate::value::DecodePlaceholderMap;
|
|
|
|
use crate::value::to_value;
|
|
|
|
use crate::value::from_value;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
struct ExpectedPlaceholderMapping(DecodePlaceholderMap);
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
struct TestCases {
|
|
|
|
decode_placeholders: ExpectedPlaceholderMapping,
|
|
|
|
tests: BTreeMap<// #[serde(with = "crate::symbol")] <--- HERE
|
|
|
|
String, TestCase>
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
enum TestCase {
|
|
|
|
Test(#[serde(with = "serde_bytes")] Vec<u8>, AValue),
|
|
|
|
NondeterministicTest(#[serde(with = "serde_bytes")] Vec<u8>, AValue),
|
|
|
|
StreamingTest(#[serde(with = "serde_bytes")] Vec<u8>, AValue),
|
|
|
|
ParseError(String),
|
|
|
|
ParseShort(String),
|
|
|
|
DecodeError(#[serde(with = "serde_bytes")] Vec<u8>),
|
|
|
|
DecodeShort(#[serde(with = "serde_bytes")] Vec<u8>),
|
|
|
|
}
|
2019-09-15 11:25:23 +00:00
|
|
|
|
2019-09-16 23:58:32 +00:00
|
|
|
#[test] fn run() {
|
2019-09-09 21:08:26 +00:00
|
|
|
let mut d = Decoder::new(std::fs::File::open("../../tests/samples.bin").unwrap(), None);
|
2019-09-16 23:58:32 +00:00
|
|
|
let v = d.next().unwrap();
|
|
|
|
// println!("{:#?}", v);
|
|
|
|
|
|
|
|
let tests: TestCases = from_value(&v).unwrap();
|
|
|
|
println!("{:#?}", tests);
|
|
|
|
|
2019-09-15 15:16:17 +00:00
|
|
|
// let mut buf = Vec::new();
|
|
|
|
// let mut e = Encoder::new(&mut buf, None);
|
2019-09-16 23:58:32 +00:00
|
|
|
// e.write(&v).unwrap();
|
2019-09-15 15:16:17 +00:00
|
|
|
// println!("{:?}", Value::from(buf))
|
2019-09-09 21:08:26 +00:00
|
|
|
}
|
2019-09-16 23:58:32 +00:00
|
|
|
|
|
|
|
#[test] fn simple_to_value() {
|
|
|
|
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
|
|
struct SimpleValue<'a>(String,
|
|
|
|
&'a str,
|
|
|
|
#[serde(with = "serde_bytes")] &'a [u8],
|
|
|
|
#[serde(with = "serde_bytes")] Vec<u8>,
|
|
|
|
i16,
|
|
|
|
AValue);
|
|
|
|
let v = SimpleValue("hello".to_string(),
|
|
|
|
"world",
|
|
|
|
&b"slice"[..],
|
|
|
|
b"vec".to_vec(),
|
|
|
|
12345,
|
|
|
|
Value::from("hi").wrap());
|
|
|
|
println!("{:#?}", v);
|
|
|
|
let w = to_value(&v).unwrap();
|
|
|
|
println!("{:#?}", w);
|
|
|
|
let x = from_value(&w).unwrap();
|
|
|
|
println!("{:#?}", &x);
|
|
|
|
assert_eq!(v, x);
|
|
|
|
}
|
2019-09-09 21:08:26 +00:00
|
|
|
}
|