]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
1a4d82fc | 11 | // ignore-test FIXME(#5121) |
223e47cc | 12 | |
1a4d82fc JJ |
13 | extern crate rbml; |
14 | extern crate serialize; | |
15 | extern crate time; | |
223e47cc LB |
16 | |
17 | // These tests used to be separate files, but I wanted to refactor all | |
18 | // the common code. | |
19 | ||
1a4d82fc | 20 | use std::collections::{HashMap, HashSet}; |
970d7e83 | 21 | |
1a4d82fc JJ |
22 | use rbml::reader as EBReader; |
23 | use rbml::writer as EBWriter; | |
970d7e83 LB |
24 | use std::cmp::Eq; |
25 | use std::cmp; | |
26 | use std::io; | |
1a4d82fc | 27 | use serialize::{Decodable, Encodable}; |
223e47cc | 28 | |
1a4d82fc | 29 | fn test_rbml<'a, 'b, A: |
223e47cc | 30 | Eq + |
1a4d82fc JJ |
31 | Encodable<EBWriter::Encoder<'a>> + |
32 | Decodable<EBReader::Decoder<'b>> | |
223e47cc | 33 | >(a1: &A) { |
1a4d82fc JJ |
34 | let mut wr = Vec::new(); |
35 | let mut rbml_w = EBwriter::Encoder::new(&mut wr); | |
36 | a1.encode(&mut rbml_w); | |
37 | ||
85aaf69f | 38 | let d: serialize::rbml::Doc<'a> = EBDoc::new(&wr); |
1a4d82fc | 39 | let mut decoder: EBReader::Decoder<'a> = EBreader::Decoder::new(d); |
970d7e83 | 40 | let a2: A = Decodable::decode(&mut decoder); |
223e47cc LB |
41 | assert!(*a1 == a2); |
42 | } | |
43 | ||
1a4d82fc | 44 | #[derive(Decodable, Encodable)] |
223e47cc | 45 | enum Expr { |
c34b1796 | 46 | Val(usize), |
223e47cc LB |
47 | Plus(@Expr, @Expr), |
48 | Minus(@Expr, @Expr) | |
49 | } | |
50 | ||
51 | impl cmp::Eq for Expr { | |
52 | fn eq(&self, other: &Expr) -> bool { | |
53 | match *self { | |
54 | Val(e0a) => { | |
55 | match *other { | |
56 | Val(e0b) => e0a == e0b, | |
57 | _ => false | |
58 | } | |
59 | } | |
60 | Plus(e0a, e1a) => { | |
61 | match *other { | |
62 | Plus(e0b, e1b) => e0a == e0b && e1a == e1b, | |
63 | _ => false | |
64 | } | |
65 | } | |
66 | Minus(e0a, e1a) => { | |
67 | match *other { | |
68 | Minus(e0b, e1b) => e0a == e0b && e1a == e1b, | |
69 | _ => false | |
70 | } | |
71 | } | |
72 | } | |
73 | } | |
74 | fn ne(&self, other: &Expr) -> bool { !(*self).eq(other) } | |
75 | } | |
76 | ||
77 | impl cmp::Eq for Point { | |
78 | fn eq(&self, other: &Point) -> bool { | |
79 | self.x == other.x && self.y == other.y | |
80 | } | |
81 | fn ne(&self, other: &Point) -> bool { !(*self).eq(other) } | |
82 | } | |
83 | ||
84 | impl<T:cmp::Eq> cmp::Eq for Quark<T> { | |
85 | fn eq(&self, other: &Quark<T>) -> bool { | |
86 | match *self { | |
87 | Top(ref q) => { | |
88 | match *other { | |
89 | Top(ref r) => q == r, | |
90 | Bottom(_) => false | |
91 | } | |
92 | }, | |
93 | Bottom(ref q) => { | |
94 | match *other { | |
95 | Top(_) => false, | |
96 | Bottom(ref r) => q == r | |
97 | } | |
98 | }, | |
99 | } | |
100 | } | |
101 | fn ne(&self, other: &Quark<T>) -> bool { !(*self).eq(other) } | |
102 | } | |
103 | ||
104 | impl cmp::Eq for CLike { | |
105 | fn eq(&self, other: &CLike) -> bool { | |
c34b1796 | 106 | (*self) as isize == *other as isize |
223e47cc LB |
107 | } |
108 | fn ne(&self, other: &CLike) -> bool { !self.eq(other) } | |
109 | } | |
110 | ||
1a4d82fc | 111 | #[derive(Decodable, Encodable, Eq)] |
223e47cc | 112 | struct Spanned<T> { |
c34b1796 AL |
113 | lo: usize, |
114 | hi: usize, | |
223e47cc LB |
115 | node: T, |
116 | } | |
117 | ||
1a4d82fc | 118 | #[derive(Decodable, Encodable)] |
c34b1796 | 119 | struct SomeStruct { v: Vec<usize> } |
223e47cc | 120 | |
1a4d82fc | 121 | #[derive(Decodable, Encodable)] |
c34b1796 | 122 | struct Point {x: usize, y: usize} |
223e47cc | 123 | |
1a4d82fc | 124 | #[derive(Decodable, Encodable)] |
223e47cc LB |
125 | enum Quark<T> { |
126 | Top(T), | |
127 | Bottom(T) | |
128 | } | |
129 | ||
1a4d82fc | 130 | #[derive(Decodable, Encodable)] |
223e47cc LB |
131 | enum CLike { A, B, C } |
132 | ||
133 | pub fn main() { | |
c34b1796 | 134 | let a = &Plus(@Minus(@Val(3), @Val(10)), @Plus(@Val(22), @Val(5))); |
1a4d82fc | 135 | test_rbml(a); |
223e47cc | 136 | |
c34b1796 | 137 | let a = &Spanned {lo: 0, hi: 5, node: 22}; |
1a4d82fc | 138 | test_rbml(a); |
223e47cc | 139 | |
c34b1796 | 140 | let a = &Point {x: 3, y: 5}; |
1a4d82fc JJ |
141 | test_rbml(a); |
142 | ||
c34b1796 | 143 | let a = &Top(22); |
1a4d82fc JJ |
144 | test_rbml(a); |
145 | ||
c34b1796 | 146 | let a = &Bottom(222); |
1a4d82fc JJ |
147 | test_rbml(a); |
148 | ||
149 | let a = &A; | |
150 | test_rbml(a); | |
151 | ||
152 | let a = &B; | |
153 | test_rbml(a); | |
223e47cc LB |
154 | |
155 | let a = &time::now(); | |
1a4d82fc | 156 | test_rbml(a); |
970d7e83 | 157 | |
1a4d82fc JJ |
158 | test_rbml(&1.0f32); |
159 | test_rbml(&1.0f64); | |
160 | test_rbml(&'a'); | |
970d7e83 | 161 | |
970d7e83 | 162 | let mut a = HashMap::new(); |
1a4d82fc | 163 | test_rbml(&a); |
970d7e83 | 164 | a.insert(1, 2); |
1a4d82fc | 165 | test_rbml(&a); |
970d7e83 | 166 | |
1a4d82fc JJ |
167 | let mut a = HashSet::new(); |
168 | test_rbml(&a); | |
169 | a.insert(1); | |
170 | test_rbml(&a); | |
223e47cc | 171 | } |