|
| 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