]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/auto-encode.rs
Merge branch 'master' into cfg_tmp_dir
[rust.git] / src / test / run-pass / auto-encode.rs
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
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
11 // ignore-test FIXME(#5121)
12
13 extern crate rbml;
14 extern crate serialize;
15 extern crate time;
16
17 // These tests used to be separate files, but I wanted to refactor all
18 // the common code.
19
20 use std::collections::{HashMap, HashSet};
21
22 use rbml::reader as EBReader;
23 use rbml::writer as EBWriter;
24 use std::cmp::Eq;
25 use std::cmp;
26 use std::io;
27 use serialize::{Decodable, Encodable};
28
29 fn test_rbml<'a, 'b, A:
30     Eq +
31     Encodable<EBWriter::Encoder<'a>> +
32     Decodable<EBReader::Decoder<'b>>
33 >(a1: &A) {
34     let mut wr = Vec::new();
35     let mut rbml_w = EBwriter::Encoder::new(&mut wr);
36     a1.encode(&mut rbml_w);
37
38     let d: serialize::rbml::Doc<'a> = EBDoc::new(wr[]);
39     let mut decoder: EBReader::Decoder<'a> = EBreader::Decoder::new(d);
40     let a2: A = Decodable::decode(&mut decoder);
41     assert!(*a1 == a2);
42 }
43
44 #[deriving(Decodable, Encodable)]
45 enum Expr {
46     Val(uint),
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 {
106         (*self) as int == *other as int
107     }
108     fn ne(&self, other: &CLike) -> bool { !self.eq(other) }
109 }
110
111 #[deriving(Decodable, Encodable, Eq)]
112 struct Spanned<T> {
113     lo: uint,
114     hi: uint,
115     node: T,
116 }
117
118 #[deriving(Decodable, Encodable)]
119 struct SomeStruct { v: Vec<uint> }
120
121 #[deriving(Decodable, Encodable)]
122 struct Point {x: uint, y: uint}
123
124 #[deriving(Decodable, Encodable)]
125 enum Quark<T> {
126     Top(T),
127     Bottom(T)
128 }
129
130 #[deriving(Decodable, Encodable)]
131 enum CLike { A, B, C }
132
133 pub fn main() {
134     let a = &Plus(@Minus(@Val(3u), @Val(10u)), @Plus(@Val(22u), @Val(5u)));
135     test_rbml(a);
136
137     let a = &Spanned {lo: 0u, hi: 5u, node: 22u};
138     test_rbml(a);
139
140     let a = &Point {x: 3u, y: 5u};
141     test_rbml(a);
142
143     let a = &Top(22u);
144     test_rbml(a);
145
146     let a = &Bottom(222u);
147     test_rbml(a);
148
149     let a = &A;
150     test_rbml(a);
151
152     let a = &B;
153     test_rbml(a);
154
155     let a = &time::now();
156     test_rbml(a);
157
158     test_rbml(&1.0f32);
159     test_rbml(&1.0f64);
160     test_rbml(&'a');
161
162     let mut a = HashMap::new();
163     test_rbml(&a);
164     a.insert(1, 2);
165     test_rbml(&a);
166
167     let mut a = HashSet::new();
168     test_rbml(&a);
169     a.insert(1);
170     test_rbml(&a);
171 }