]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/auto-encode.rs
test: Automatically remove all `~[T]` from tests.
[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 // ignore-fast
13
14 #[feature(managed_boxes)];
15
16 extern crate time;
17 extern crate serialize;
18
19 // These tests used to be separate files, but I wanted to refactor all
20 // the common code.
21
22 use std::hashmap::{HashMap, HashSet};
23
24 use EBReader = serialize::ebml::reader;
25 use EBWriter = serialize::ebml::writer;
26 use std::cmp::Eq;
27 use std::cmp;
28 use std::io;
29 use serialize::{Decodable, Encodable};
30
31 fn test_ebml<'a, 'b, A:
32     Eq +
33     Encodable<EBWriter::Encoder<'a>> +
34     Decodable<EBReader::Decoder<'b>>
35 >(a1: &A) {
36     let mut wr = std::io::MemWriter::new();
37     let mut ebml_w = EBWriter::Encoder(&mut wr);
38     a1.encode(&mut ebml_w);
39     let bytes = wr.get_ref();
40
41     let d: serialize::ebml::Doc<'a> = EBReader::Doc(bytes);
42     let mut decoder: EBReader::Decoder<'a> = EBReader::Decoder(d);
43     let a2: A = Decodable::decode(&mut decoder);
44     assert!(*a1 == a2);
45 }
46
47 #[deriving(Decodable, Encodable)]
48 enum Expr {
49     Val(uint),
50     Plus(@Expr, @Expr),
51     Minus(@Expr, @Expr)
52 }
53
54 impl cmp::Eq for Expr {
55     fn eq(&self, other: &Expr) -> bool {
56         match *self {
57             Val(e0a) => {
58                 match *other {
59                     Val(e0b) => e0a == e0b,
60                     _ => false
61                 }
62             }
63             Plus(e0a, e1a) => {
64                 match *other {
65                     Plus(e0b, e1b) => e0a == e0b && e1a == e1b,
66                     _ => false
67                 }
68             }
69             Minus(e0a, e1a) => {
70                 match *other {
71                     Minus(e0b, e1b) => e0a == e0b && e1a == e1b,
72                     _ => false
73                 }
74             }
75         }
76     }
77     fn ne(&self, other: &Expr) -> bool { !(*self).eq(other) }
78 }
79
80 impl cmp::Eq for Point {
81     fn eq(&self, other: &Point) -> bool {
82         self.x == other.x && self.y == other.y
83     }
84     fn ne(&self, other: &Point) -> bool { !(*self).eq(other) }
85 }
86
87 impl<T:cmp::Eq> cmp::Eq for Quark<T> {
88     fn eq(&self, other: &Quark<T>) -> bool {
89         match *self {
90             Top(ref q) => {
91                 match *other {
92                     Top(ref r) => q == r,
93                     Bottom(_) => false
94                 }
95             },
96             Bottom(ref q) => {
97                 match *other {
98                     Top(_) => false,
99                     Bottom(ref r) => q == r
100                 }
101             },
102         }
103     }
104     fn ne(&self, other: &Quark<T>) -> bool { !(*self).eq(other) }
105 }
106
107 impl cmp::Eq for CLike {
108     fn eq(&self, other: &CLike) -> bool {
109         (*self) as int == *other as int
110     }
111     fn ne(&self, other: &CLike) -> bool { !self.eq(other) }
112 }
113
114 #[deriving(Decodable, Encodable, Eq)]
115 struct Spanned<T> {
116     lo: uint,
117     hi: uint,
118     node: T,
119 }
120
121 #[deriving(Decodable, Encodable)]
122 struct SomeStruct { v: Vec<uint> }
123
124 #[deriving(Decodable, Encodable)]
125 struct Point {x: uint, y: uint}
126
127 #[deriving(Decodable, Encodable)]
128 enum Quark<T> {
129     Top(T),
130     Bottom(T)
131 }
132
133 #[deriving(Decodable, Encodable)]
134 enum CLike { A, B, C }
135
136 pub fn main() {
137     let a = &Plus(@Minus(@Val(3u), @Val(10u)), @Plus(@Val(22u), @Val(5u)));
138     test_ebml(a);
139
140     let a = &Spanned {lo: 0u, hi: 5u, node: 22u};
141     test_ebml(a);
142
143     let a = &Point {x: 3u, y: 5u};
144     test_ebml(a);
145
146     let a = &Top(22u);
147     test_ebml(a);
148
149     let a = &Bottom(222u);
150     test_ebml(a);
151
152     let a = &A;
153     test_ebml(a);
154
155     let a = &B;
156     test_ebml(a);
157
158     let a = &time::now();
159     test_ebml(a);
160
161     test_ebml(&1.0f32);
162     test_ebml(&1.0f64);
163     test_ebml(&'a');
164
165     let mut a = HashMap::new();
166     test_ebml(&a);
167     a.insert(1, 2);
168     test_ebml(&a);
169
170     let mut a = HashSet::new();
171     test_ebml(&a);
172     a.insert(1);
173     test_ebml(&a);
174 }