]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/deriving-encodable-decodable.rs
Merge tag 'debian/0.7-0_exp1'
[rustc.git] / src / test / run-pass / deriving-encodable-decodable.rs
1 // Copyright 2013 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 // This actually tests a lot more than just encodable/decodable, but it gets the
12 // job done at least
13
14 // xfail-fast
15
16 extern mod extra;
17
18 use std::io;
19 use std::rand::{random, Rand};
20 use extra::serialize::*;
21 use extra::ebml;
22 use extra::ebml::writer::Encoder;
23 use extra::ebml::reader::Decoder;
24
25 #[deriving(Encodable, Decodable, Eq, Rand)]
26 struct A;
27 #[deriving(Encodable, Decodable, Eq, Rand)]
28 struct B(int);
29 #[deriving(Encodable, Decodable, Eq, Rand)]
30 struct C(int, int, uint);
31
32 #[deriving(Encodable, Decodable, Eq, Rand)]
33 struct D {
34 a: int,
35 b: uint,
36 }
37
38 #[deriving(Encodable, Decodable, Eq, Rand)]
39 enum E {
40 E1,
41 E2(uint),
42 E3(D),
43 E4{ x: uint },
44 }
45
46 #[deriving(Encodable, Decodable, Eq, Rand)]
47 enum F { F1 }
48
49 #[deriving(Encodable, Decodable, Eq, Rand)]
50 struct G<T> {
51 t: T
52 }
53
54 fn roundtrip<T: Rand + Eq + Encodable<Encoder> + Decodable<Decoder>>() {
55 let obj: T = random();
56 let bytes = do io::with_bytes_writer |w| {
57 let mut e = Encoder(w);
58 obj.encode(&mut e);
59 };
60 let doc = ebml::reader::Doc(@bytes);
61 let mut dec = Decoder(doc);
62 let obj2 = Decodable::decode(&mut dec);
63 assert!(obj == obj2);
64 }
65
66 pub fn main() {
67 roundtrip::<A>();
68 roundtrip::<B>();
69 roundtrip::<C>();
70 roundtrip::<D>();
71
72 for 20.times {
73 roundtrip::<E>();
74 roundtrip::<F>();
75 roundtrip::<G<int>>();
76 }
77 }