]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/auto-encode.rs
bfc15acaa763c52ec9b24c41984f065cb19dd1d2
[rust.git] / src / test / run-pass / auto-encode.rs
1 // xfail-fast
2
3 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
4 // file at the top-level directory of this distribution and at
5 // http://rust-lang.org/COPYRIGHT.
6 //
7 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 // option. This file may not be copied, modified, or distributed
11 // except according to those terms.
12
13 #[forbid(deprecated_pattern)];
14
15 extern mod std;
16
17 // These tests used to be separate files, but I wanted to refactor all
18 // the common code.
19
20 use EBReader = std::ebml::reader;
21 use EBWriter = std::ebml::writer;
22 use core::cmp::Eq;
23 use core::io::Writer;
24 use std::ebml;
25 use std::serialize::{Decodable, Encodable};
26 use std::time;
27
28 fn test_ebml<A:
29     Eq +
30     Encodable<EBWriter::Encoder> +
31     Decodable<EBReader::Decoder>
32 >(a1: &A) {
33     let bytes = do io::with_bytes_writer |wr| {
34         let ebml_w = &EBWriter::Encoder(wr);
35         a1.encode(ebml_w)
36     };
37     let d = EBReader::Doc(@bytes);
38     let a2: A = Decodable::decode(&EBReader::Decoder(d));
39     assert!(*a1 == a2);
40 }
41
42 #[deriving(Decodable, Encodable)]
43 enum Expr {
44     Val(uint),
45     Plus(@Expr, @Expr),
46     Minus(@Expr, @Expr)
47 }
48
49 impl cmp::Eq for Expr {
50     fn eq(&self, other: &Expr) -> bool {
51         match *self {
52             Val(e0a) => {
53                 match *other {
54                     Val(e0b) => e0a == e0b,
55                     _ => false
56                 }
57             }
58             Plus(e0a, e1a) => {
59                 match *other {
60                     Plus(e0b, e1b) => e0a == e0b && e1a == e1b,
61                     _ => false
62                 }
63             }
64             Minus(e0a, e1a) => {
65                 match *other {
66                     Minus(e0b, e1b) => e0a == e0b && e1a == e1b,
67                     _ => false
68                 }
69             }
70         }
71     }
72     fn ne(&self, other: &Expr) -> bool { !(*self).eq(other) }
73 }
74
75 impl cmp::Eq for Point {
76     fn eq(&self, other: &Point) -> bool {
77         self.x == other.x && self.y == other.y
78     }
79     fn ne(&self, other: &Point) -> bool { !(*self).eq(other) }
80 }
81
82 impl<T:cmp::Eq> cmp::Eq for Quark<T> {
83     fn eq(&self, other: &Quark<T>) -> bool {
84         match *self {
85             Top(ref q) => {
86                 match *other {
87                     Top(ref r) => q == r,
88                     Bottom(_) => false
89                 }
90             },
91             Bottom(ref q) => {
92                 match *other {
93                     Top(_) => false,
94                     Bottom(ref r) => q == r
95                 }
96             },
97         }
98     }
99     fn ne(&self, other: &Quark<T>) -> bool { !(*self).eq(other) }
100 }
101
102 impl cmp::Eq for CLike {
103     fn eq(&self, other: &CLike) -> bool {
104         (*self) as int == *other as int
105     }
106     fn ne(&self, other: &CLike) -> bool { !self.eq(other) }
107 }
108
109 #[deriving(Decodable, Encodable, Eq)]
110 struct Spanned<T> {
111     lo: uint,
112     hi: uint,
113     node: T,
114 }
115
116 #[deriving(Decodable, Encodable)]
117 struct SomeStruct { v: ~[uint] }
118
119 #[deriving(Decodable, Encodable)]
120 struct Point {x: uint, y: uint}
121
122 #[deriving(Decodable, Encodable)]
123 enum Quark<T> {
124     Top(T),
125     Bottom(T)
126 }
127
128 #[deriving(Decodable, Encodable)]
129 enum CLike { A, B, C }
130
131 pub fn main() {
132     let a = &Plus(@Minus(@Val(3u), @Val(10u)), @Plus(@Val(22u), @Val(5u)));
133     test_ebml(a);
134
135     let a = &Spanned {lo: 0u, hi: 5u, node: 22u};
136     test_ebml(a);
137
138     let a = &Point {x: 3u, y: 5u};
139     test_ebml(a);
140
141     let a = &@[1u, 2u, 3u];
142     test_ebml(a);
143
144     let a = &Top(22u);
145     test_ebml(a);
146
147     let a = &Bottom(222u);
148     test_ebml(a);
149
150     let a = &A;
151     test_ebml(a);
152
153     let a = &B;
154     test_ebml(a);
155
156     let a = &time::now();
157     test_ebml(a);
158 }