2012-09-18 14:08:42 -05:00
|
|
|
extern mod std;
|
|
|
|
|
|
|
|
// These tests used to be separate files, but I wanted to refactor all
|
|
|
|
// the common code.
|
|
|
|
|
|
|
|
use cmp::Eq;
|
|
|
|
use std::ebml2;
|
|
|
|
use io::Writer;
|
2012-09-26 23:35:13 -05:00
|
|
|
use std::serialization2::{Serializable, Deserializable, deserialize};
|
2012-09-18 14:08:42 -05:00
|
|
|
use std::prettyprint2;
|
|
|
|
|
2012-09-26 23:35:13 -05:00
|
|
|
fn test_ser_and_deser<A:Eq Serializable Deserializable>(
|
2012-10-01 13:35:27 -05:00
|
|
|
a1: &A,
|
|
|
|
+expected: ~str
|
2012-09-18 14:08:42 -05:00
|
|
|
) {
|
|
|
|
// check the pretty printer:
|
2012-09-25 12:50:54 -05:00
|
|
|
let s = do io::with_str_writer |w| {
|
|
|
|
a1.serialize(&prettyprint2::Serializer(w))
|
|
|
|
};
|
2012-09-18 14:08:42 -05:00
|
|
|
debug!("s == %?", s);
|
|
|
|
assert s == expected;
|
|
|
|
|
|
|
|
// check the EBML serializer:
|
|
|
|
let bytes = do io::with_bytes_writer |wr| {
|
2012-09-25 12:50:54 -05:00
|
|
|
let ebml_w = &ebml2::Serializer(wr);
|
2012-09-18 14:08:42 -05:00
|
|
|
a1.serialize(ebml_w)
|
|
|
|
};
|
|
|
|
let d = ebml2::Doc(@bytes);
|
2012-09-25 12:50:54 -05:00
|
|
|
let a2: A = deserialize(&ebml2::Deserializer(d));
|
2012-10-01 13:35:27 -05:00
|
|
|
assert *a1 == a2;
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
|
|
|
|
2012-09-26 23:35:13 -05:00
|
|
|
#[auto_serialize2]
|
|
|
|
enum Expr {
|
|
|
|
Val(uint),
|
|
|
|
Plus(@Expr, @Expr),
|
|
|
|
Minus(@Expr, @Expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Expr : cmp::Eq {
|
|
|
|
pure fn eq(other: &Expr) -> bool {
|
|
|
|
match self {
|
|
|
|
Val(e0a) => {
|
|
|
|
match *other {
|
|
|
|
Val(e0b) => e0a == e0b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Plus(e0a, e1a) => {
|
|
|
|
match *other {
|
|
|
|
Plus(e0b, e1b) => e0a == e0b && e1a == e1b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Minus(e0a, e1a) => {
|
|
|
|
match *other {
|
|
|
|
Minus(e0b, e1b) => e0a == e0b && e1a == e1b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(other: &Expr) -> bool { !self.eq(other) }
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:08:42 -05:00
|
|
|
impl AnEnum : cmp::Eq {
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn eq(other: &AnEnum) -> bool {
|
2012-09-18 14:08:42 -05:00
|
|
|
self.v == other.v
|
|
|
|
}
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn ne(other: &AnEnum) -> bool { !self.eq(other) }
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Point : cmp::Eq {
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn eq(other: &Point) -> bool {
|
2012-09-18 14:08:42 -05:00
|
|
|
self.x == other.x && self.y == other.y
|
|
|
|
}
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn ne(other: &Point) -> bool { !self.eq(other) }
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:cmp::Eq> Quark<T> : cmp::Eq {
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn eq(other: &Quark<T>) -> bool {
|
2012-09-18 14:08:42 -05:00
|
|
|
match self {
|
2012-09-25 12:50:54 -05:00
|
|
|
Top(ref q) => {
|
|
|
|
match *other {
|
|
|
|
Top(ref r) => q == r,
|
|
|
|
Bottom(_) => false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Bottom(ref q) => {
|
|
|
|
match *other {
|
|
|
|
Top(_) => false,
|
|
|
|
Bottom(ref r) => q == r
|
|
|
|
}
|
|
|
|
},
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn ne(other: &Quark<T>) -> bool { !self.eq(other) }
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CLike : cmp::Eq {
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn eq(other: &CLike) -> bool {
|
|
|
|
self as int == *other as int
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn ne(other: &CLike) -> bool { !self.eq(other) }
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[auto_serialize2]
|
|
|
|
type Spanned<T> = {lo: uint, hi: uint, node: T};
|
|
|
|
|
|
|
|
impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn eq(other: &Spanned<T>) -> bool {
|
|
|
|
self.lo == other.lo && self.hi == other.hi && self.node == other.node
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
2012-09-25 12:50:54 -05:00
|
|
|
pure fn ne(other: &Spanned<T>) -> bool { !self.eq(other) }
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[auto_serialize2]
|
|
|
|
type SomeRec = {v: ~[uint]};
|
|
|
|
|
|
|
|
#[auto_serialize2]
|
|
|
|
enum AnEnum = SomeRec;
|
|
|
|
|
|
|
|
#[auto_serialize2]
|
2012-10-01 13:35:27 -05:00
|
|
|
struct Point {x: uint, y: uint}
|
2012-09-18 14:08:42 -05:00
|
|
|
|
|
|
|
#[auto_serialize2]
|
|
|
|
enum Quark<T> {
|
|
|
|
Top(T),
|
|
|
|
Bottom(T)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[auto_serialize2]
|
|
|
|
enum CLike { A, B, C }
|
|
|
|
|
|
|
|
fn main() {
|
2012-10-01 13:35:27 -05:00
|
|
|
test_ser_and_deser(&Plus(@Minus(@Val(3u), @Val(10u)),
|
|
|
|
@Plus(@Val(22u), @Val(5u))),
|
2012-09-26 23:35:13 -05:00
|
|
|
~"Plus(@Minus(@Val(3u), @Val(10u)), \
|
|
|
|
@Plus(@Val(22u), @Val(5u)))");
|
|
|
|
|
2012-10-01 13:35:27 -05:00
|
|
|
test_ser_and_deser(&{lo: 0u, hi: 5u, node: 22u},
|
2012-09-18 14:08:42 -05:00
|
|
|
~"{lo: 0u, hi: 5u, node: 22u}");
|
|
|
|
|
2012-10-01 13:35:27 -05:00
|
|
|
test_ser_and_deser(&AnEnum({v: ~[1u, 2u, 3u]}),
|
2012-09-26 23:35:13 -05:00
|
|
|
~"AnEnum({v: ~[1u, 2u, 3u]})");
|
2012-09-18 14:08:42 -05:00
|
|
|
|
2012-10-01 13:35:27 -05:00
|
|
|
test_ser_and_deser(&Point {x: 3u, y: 5u}, ~"Point {x: 3u, y: 5u}");
|
2012-09-18 14:08:42 -05:00
|
|
|
|
2012-10-01 13:35:27 -05:00
|
|
|
test_ser_and_deser(&@[1u, 2u, 3u], ~"@[1u, 2u, 3u]");
|
2012-09-18 14:08:42 -05:00
|
|
|
|
2012-10-01 13:35:27 -05:00
|
|
|
test_ser_and_deser(&Top(22u), ~"Top(22u)");
|
|
|
|
test_ser_and_deser(&Bottom(222u), ~"Bottom(222u)");
|
2012-09-18 14:08:42 -05:00
|
|
|
|
2012-10-01 13:35:27 -05:00
|
|
|
test_ser_and_deser(&A, ~"A");
|
|
|
|
test_ser_and_deser(&B, ~"B");
|
2012-09-18 14:08:42 -05:00
|
|
|
}
|