]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/deriving-encodable-decodable.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / test / run-pass / deriving-encodable-decodable.rs
CommitLineData
1a4d82fc 1// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
970d7e83
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
11// This actually tests a lot more than just encodable/decodable, but it gets the
12// job done at least
13
1a4d82fc 14// ignore-test FIXME(#5121)
970d7e83 15
1a4d82fc
JJ
16extern crate rand;
17extern crate rbml;
18extern crate serialize;
970d7e83 19
1a4d82fc
JJ
20use rand::{random, Rand};
21use rbml;
22use rbml::Doc;
23use rbml::writer::Encoder;
24use rbml::reader::Decoder;
25use serialize::{Encodable, Decodable};
970d7e83 26
1a4d82fc 27#[derive(Encodable, Decodable, Eq, Rand)]
970d7e83 28struct A;
1a4d82fc 29#[derive(Encodable, Decodable, Eq, Rand)]
c34b1796 30struct B(isize);
1a4d82fc 31#[derive(Encodable, Decodable, Eq, Rand)]
c34b1796 32struct C(isize, isize, usize);
970d7e83 33
1a4d82fc 34#[derive(Encodable, Decodable, Eq, Rand)]
970d7e83 35struct D {
c34b1796
AL
36 a: isize,
37 b: usize,
970d7e83
LB
38}
39
1a4d82fc 40#[derive(Encodable, Decodable, Eq, Rand)]
970d7e83
LB
41enum E {
42 E1,
c34b1796 43 E2(usize),
970d7e83 44 E3(D),
c34b1796 45 E4{ x: usize },
970d7e83
LB
46}
47
1a4d82fc 48#[derive(Encodable, Decodable, Eq, Rand)]
970d7e83
LB
49enum F { F1 }
50
1a4d82fc 51#[derive(Encodable, Decodable, Eq, Rand)]
970d7e83
LB
52struct G<T> {
53 t: T
54}
55
1a4d82fc
JJ
56fn roundtrip<'a, T: Rand + Eq + Encodable<Encoder<'a>> +
57 Decodable<Decoder<'a>>>() {
970d7e83 58 let obj: T = random();
1a4d82fc
JJ
59 let mut w = Vec::new();
60 let mut e = Encoder::new(&mut w);
61 obj.encode(&mut e);
85aaf69f 62 let doc = rbml::Doc::new(&w);
1a4d82fc 63 let mut dec = Decoder::new(doc);
970d7e83
LB
64 let obj2 = Decodable::decode(&mut dec);
65 assert!(obj == obj2);
66}
67
68pub fn main() {
69 roundtrip::<A>();
70 roundtrip::<B>();
71 roundtrip::<C>();
72 roundtrip::<D>();
73
85aaf69f 74 for _ in 0..20 {
970d7e83
LB
75 roundtrip::<E>();
76 roundtrip::<F>();
c34b1796 77 roundtrip::<G<isize>>();
970d7e83
LB
78 }
79}