2014-02-07 13:08:32 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-05-30 16:58:16 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// This actually tests a lot more than just encodable/decodable, but it gets the
|
|
|
|
// job done at least
|
|
|
|
|
2014-02-07 13:08:32 -06:00
|
|
|
// ignore-test FIXME(#5121)
|
2013-05-31 15:44:44 -05:00
|
|
|
|
2014-04-14 10:30:31 -05:00
|
|
|
#![feature(struct_variant, managed_boxes)]
|
2013-10-02 22:00:54 -05:00
|
|
|
|
2014-03-01 18:33:24 -06:00
|
|
|
extern crate rand;
|
2014-02-14 12:10:06 -06:00
|
|
|
extern crate serialize;
|
2013-05-30 16:58:16 -05:00
|
|
|
|
2014-01-15 15:25:09 -06:00
|
|
|
use std::io::MemWriter;
|
2014-03-01 18:33:24 -06:00
|
|
|
use rand::{random, Rand};
|
2014-02-05 10:52:54 -06:00
|
|
|
use serialize::{Encodable, Decodable};
|
|
|
|
use serialize::ebml;
|
2014-05-25 20:32:04 -05:00
|
|
|
use serialize::ebml::Doc;
|
2014-02-05 10:52:54 -06:00
|
|
|
use serialize::ebml::writer::Encoder;
|
|
|
|
use serialize::ebml::reader::Decoder;
|
2013-05-30 16:58:16 -05:00
|
|
|
|
|
|
|
#[deriving(Encodable, Decodable, Eq, Rand)]
|
|
|
|
struct A;
|
|
|
|
#[deriving(Encodable, Decodable, Eq, Rand)]
|
|
|
|
struct B(int);
|
|
|
|
#[deriving(Encodable, Decodable, Eq, Rand)]
|
|
|
|
struct C(int, int, uint);
|
|
|
|
|
|
|
|
#[deriving(Encodable, Decodable, Eq, Rand)]
|
|
|
|
struct D {
|
|
|
|
a: int,
|
|
|
|
b: uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Encodable, Decodable, Eq, Rand)]
|
|
|
|
enum E {
|
|
|
|
E1,
|
|
|
|
E2(uint),
|
|
|
|
E3(D),
|
|
|
|
E4{ x: uint },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Encodable, Decodable, Eq, Rand)]
|
|
|
|
enum F { F1 }
|
|
|
|
|
|
|
|
#[deriving(Encodable, Decodable, Eq, Rand)]
|
|
|
|
struct G<T> {
|
|
|
|
t: T
|
|
|
|
}
|
|
|
|
|
2014-02-21 17:41:51 -06:00
|
|
|
fn roundtrip<'a, T: Rand + Eq + Encodable<Encoder<'a>> +
|
2013-12-18 16:10:28 -06:00
|
|
|
Decodable<Decoder<'a>>>() {
|
2013-05-30 16:58:16 -05:00
|
|
|
let obj: T = random();
|
2013-12-31 17:46:27 -06:00
|
|
|
let mut w = MemWriter::new();
|
2014-05-25 20:32:04 -05:00
|
|
|
let mut e = Encoder::new(&mut w);
|
2013-10-21 19:11:42 -05:00
|
|
|
obj.encode(&mut e);
|
2014-05-25 20:32:04 -05:00
|
|
|
let doc = ebml::Doc::new(@w.get_ref());
|
|
|
|
let mut dec = Decoder::new(doc);
|
2013-05-30 16:58:16 -05:00
|
|
|
let obj2 = Decodable::decode(&mut dec);
|
|
|
|
assert!(obj == obj2);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
roundtrip::<A>();
|
|
|
|
roundtrip::<B>();
|
|
|
|
roundtrip::<C>();
|
|
|
|
roundtrip::<D>();
|
|
|
|
|
2014-01-29 18:20:34 -06:00
|
|
|
for _ in range(0, 20) {
|
2013-05-30 16:58:16 -05:00
|
|
|
roundtrip::<E>();
|
|
|
|
roundtrip::<F>();
|
|
|
|
roundtrip::<G<int>>();
|
2014-01-29 18:20:34 -06:00
|
|
|
}
|
2013-05-30 16:58:16 -05:00
|
|
|
}
|