Skip to content

Commit a479c43

Browse files
ericktgraydon
authored andcommitted
---
yaml --- r: 30779 b: refs/heads/incoming c: d2506a1 h: refs/heads/master i: 30777: 53d6ec6 30775: 81bb914 v: v3
1 parent 22d42fb commit a479c43

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: 2ba9d2a888aeb8978d94050bf01e6b68271fd82e
9+
refs/heads/incoming: d2506a1787f27741dc9b577531d72db2b50ca446
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
extern mod std;
2+
3+
// These tests used to be separate files, but I wanted to refactor all
4+
// the common code.
5+
6+
use cmp::Eq;
7+
use std::ebml2;
8+
use io::Writer;
9+
use std::serialization2::{Serializer, Serializable, deserialize};
10+
use std::prettyprint2;
11+
12+
fn test_ser_and_deser<A:Eq Serializable>(
13+
a1: A,
14+
expected: ~str
15+
) {
16+
17+
// check the pretty printer:
18+
let s = io::with_str_writer(|w| a1.serialize(w));
19+
debug!("s == %?", s);
20+
assert s == expected;
21+
22+
// check the EBML serializer:
23+
let bytes = do io::with_bytes_writer |wr| {
24+
let ebml_w = ebml2::Serializer(wr);
25+
a1.serialize(ebml_w)
26+
};
27+
let d = ebml2::Doc(@bytes);
28+
let a2: A = deserialize(ebml2::Deserializer(d));
29+
assert a1 == a2;
30+
}
31+
32+
#[auto_serialize2]
33+
enum Expr {
34+
Val(uint),
35+
Plus(@Expr, @Expr),
36+
Minus(@Expr, @Expr)
37+
}
38+
39+
impl AnEnum : cmp::Eq {
40+
pure fn eq(&&other: AnEnum) -> bool {
41+
self.v == other.v
42+
}
43+
pure fn ne(&&other: AnEnum) -> bool { !self.eq(other) }
44+
}
45+
46+
impl Point : cmp::Eq {
47+
pure fn eq(&&other: Point) -> bool {
48+
self.x == other.x && self.y == other.y
49+
}
50+
pure fn ne(&&other: Point) -> bool { !self.eq(other) }
51+
}
52+
53+
impl<T:cmp::Eq> Quark<T> : cmp::Eq {
54+
pure fn eq(&&other: Quark<T>) -> bool {
55+
match self {
56+
Top(ref q) => match other {
57+
Top(ref r) => q == r,
58+
Bottom(_) => false
59+
},
60+
Bottom(ref q) => match other {
61+
Top(_) => false,
62+
Bottom(ref r) => q == r
63+
}
64+
}
65+
}
66+
pure fn ne(&&other: Quark<T>) -> bool { !self.eq(other) }
67+
}
68+
69+
impl CLike : cmp::Eq {
70+
pure fn eq(&&other: CLike) -> bool {
71+
self as int == other as int
72+
}
73+
pure fn ne(&&other: CLike) -> bool { !self.eq(other) }
74+
}
75+
76+
impl Expr : cmp::Eq {
77+
pure fn eq(&&other: Expr) -> bool {
78+
match self {
79+
Val(e0a) => {
80+
match other {
81+
Val(e0b) => e0a == e0b,
82+
_ => false
83+
}
84+
}
85+
Plus(e0a, e1a) => {
86+
match other {
87+
Plus(e0b, e1b) => e0a == e0b && e1a == e1b,
88+
_ => false
89+
}
90+
}
91+
Minus(e0a, e1a) => {
92+
match other {
93+
Minus(e0b, e1b) => e0a == e0b && e1a == e1b,
94+
_ => false
95+
}
96+
}
97+
}
98+
}
99+
pure fn ne(&&other: Expr) -> bool { !self.eq(other) }
100+
}
101+
102+
#[auto_serialize2]
103+
type Spanned<T> = {lo: uint, hi: uint, node: T};
104+
105+
impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
106+
pure fn eq(&&other: Spanned<T>) -> bool {
107+
self.lo == other.lo && self.hi == other.hi && self.node.eq(other.node)
108+
}
109+
pure fn ne(&&other: Spanned<T>) -> bool { !self.eq(other) }
110+
}
111+
112+
#[auto_serialize2]
113+
type SomeRec = {v: ~[uint]};
114+
115+
#[auto_serialize2]
116+
enum AnEnum = SomeRec;
117+
118+
#[auto_serialize2]
119+
type Point = {x: uint, y: uint};
120+
121+
#[auto_serialize2]
122+
enum Quark<T> {
123+
Top(T),
124+
Bottom(T)
125+
}
126+
127+
#[auto_serialize2]
128+
enum CLike { A, B, C }
129+
130+
fn main() {
131+
132+
test_ser_and_deser(Plus(@Minus(@Val(3u), @Val(10u)),
133+
@Plus(@Val(22u), @Val(5u))),
134+
~"Plus(@Minus(@Val(3u), @Val(10u)), \
135+
@Plus(@Val(22u), @Val(5u)))");
136+
137+
test_ser_and_deser({lo: 0u, hi: 5u, node: 22u},
138+
~"{lo: 0u, hi: 5u, node: 22u}");
139+
140+
test_ser_and_deser(AnEnum({v: ~[1u, 2u, 3u]}),
141+
~"AnEnum({v: [1u, 2u, 3u]})");
142+
143+
test_ser_and_deser({x: 3u, y: 5u}, ~"{x: 3u, y: 5u}");
144+
145+
test_ser_and_deser(~[1u, 2u, 3u], ~"[1u, 2u, 3u]");
146+
147+
test_ser_and_deser(Top(22u), ~"Top(22u)");
148+
test_ser_and_deser(Bottom(222u), ~"Bottom(222u)");
149+
150+
test_ser_and_deser(A, ~"A");
151+
test_ser_and_deser(B, ~"B");
152+
}

0 commit comments

Comments
 (0)