auto-encode.rs 4.0 KB
Newer Older
1
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// 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.

11
// ignore-test FIXME(#5121)
12

13
extern crate rbml;
14
extern crate serialize;
15
extern crate time;
16 17

// These tests used to be separate files, but I wanted to refactor all
18
// the common code.
19

20
use std::collections::{HashMap, HashSet};
21

22 23
use rbml::reader as EBReader;
use rbml::writer as EBWriter;
24
use std::cmp::Eq;
25 26
use std::cmp;
use std::io;
27
use serialize::{Decodable, Encodable};
28

29
fn test_rbml<'a, 'b, A:
30
    Eq +
31 32
    Encodable<EBWriter::Encoder<'a>> +
    Decodable<EBReader::Decoder<'b>>
33
>(a1: &A) {
D
Daniel Micay 已提交
34
    let mut wr = Vec::new();
35 36
    let mut rbml_w = EBwriter::Encoder::new(&mut wr);
    a1.encode(&mut rbml_w);
37

D
Daniel Micay 已提交
38
    let d: serialize::rbml::Doc<'a> = EBDoc::new(wr[]);
39
    let mut decoder: EBReader::Decoder<'a> = EBreader::Decoder::new(d);
40
    let a2: A = Decodable::decode(&mut decoder);
P
Patrick Walton 已提交
41
    assert!(*a1 == a2);
42 43
}

44
#[deriving(Decodable, Encodable)]
45 46 47 48 49 50
enum Expr {
    Val(uint),
    Plus(@Expr, @Expr),
    Minus(@Expr, @Expr)
}

51
impl cmp::Eq for Expr {
52
    fn eq(&self, other: &Expr) -> bool {
53
        match *self {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            Val(e0a) => {
                match *other {
                    Val(e0b) => e0a == e0b,
                    _ => false
                }
            }
            Plus(e0a, e1a) => {
                match *other {
                    Plus(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
            Minus(e0a, e1a) => {
                match *other {
                    Minus(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
        }
    }
74
    fn ne(&self, other: &Expr) -> bool { !(*self).eq(other) }
75 76
}

77
impl cmp::Eq for Point {
78
    fn eq(&self, other: &Point) -> bool {
79 80
        self.x == other.x && self.y == other.y
    }
81
    fn ne(&self, other: &Point) -> bool { !(*self).eq(other) }
82 83
}

84
impl<T:cmp::Eq> cmp::Eq for Quark<T> {
85
    fn eq(&self, other: &Quark<T>) -> bool {
86
        match *self {
87 88 89 90 91 92 93 94 95 96 97 98
            Top(ref q) => {
                match *other {
                    Top(ref r) => q == r,
                    Bottom(_) => false
                }
            },
            Bottom(ref q) => {
                match *other {
                    Top(_) => false,
                    Bottom(ref r) => q == r
                }
            },
99 100
        }
    }
101
    fn ne(&self, other: &Quark<T>) -> bool { !(*self).eq(other) }
102 103
}

104
impl cmp::Eq for CLike {
105
    fn eq(&self, other: &CLike) -> bool {
106
        (*self) as int == *other as int
107
    }
108
    fn ne(&self, other: &CLike) -> bool { !self.eq(other) }
109 110
}

111
#[deriving(Decodable, Encodable, Eq)]
112 113 114 115 116
struct Spanned<T> {
    lo: uint,
    hi: uint,
    node: T,
}
117

118
#[deriving(Decodable, Encodable)]
119
struct SomeStruct { v: Vec<uint> }
120

121
#[deriving(Decodable, Encodable)]
122
struct Point {x: uint, y: uint}
123

124
#[deriving(Decodable, Encodable)]
125 126 127 128 129
enum Quark<T> {
    Top(T),
    Bottom(T)
}

130
#[deriving(Decodable, Encodable)]
131 132
enum CLike { A, B, C }

133
pub fn main() {
134
    let a = &Plus(@Minus(@Val(3u), @Val(10u)), @Plus(@Val(22u), @Val(5u)));
135
    test_rbml(a);
136

137
    let a = &Spanned {lo: 0u, hi: 5u, node: 22u};
138
    test_rbml(a);
139

140
    let a = &Point {x: 3u, y: 5u};
141
    test_rbml(a);
142 143

    let a = &Top(22u);
144
    test_rbml(a);
145 146

    let a = &Bottom(222u);
147
    test_rbml(a);
148 149

    let a = &A;
150
    test_rbml(a);
151 152

    let a = &B;
153
    test_rbml(a);
154 155

    let a = &time::now();
156
    test_rbml(a);
157

158 159 160
    test_rbml(&1.0f32);
    test_rbml(&1.0f64);
    test_rbml(&'a');
161 162

    let mut a = HashMap::new();
163
    test_rbml(&a);
164
    a.insert(1, 2);
165
    test_rbml(&a);
166

167
    let mut a = HashSet::new();
168
    test_rbml(&a);
169
    a.insert(1);
170
    test_rbml(&a);
171
}