1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// 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.
// ignore-test FIXME(#5121)
extern crate rbml;
extern crate serialize;
extern crate time;
// These tests used to be separate files, but I wanted to refactor all
// the common code.
use std::collections::{HashMap, HashSet};
use rbml::reader as EBReader;
use rbml::writer as EBWriter;
use std::cmp::Eq;
use std::cmp;
use std::io;
use serialize::{Decodable, Encodable};
fn test_rbml<'a, 'b, A:
Eq +
Encodable<EBWriter::Encoder<'a>> +
Decodable<EBReader::Decoder<'b>>
>(a1: &A) {
let mut wr = Vec::new();
let mut rbml_w = EBwriter::Encoder::new(&mut wr);
a1.encode(&mut rbml_w);
let d: serialize::rbml::Doc<'a> = EBDoc::new(&wr);
let mut decoder: EBReader::Decoder<'a> = EBreader::Decoder::new(d);
let a2: A = Decodable::decode(&mut decoder);
assert!(*a1 == a2);
}
#[derive(Decodable, Encodable)]
enum Expr {
Val(usize),
Plus(@Expr, @Expr),
Minus(@Expr, @Expr)
}
impl cmp::Eq for Expr {
fn eq(&self, other: &Expr) -> bool {
match *self {
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
}
}
}
}
fn ne(&self, other: &Expr) -> bool { !(*self).eq(other) }
}
impl cmp::Eq for Point {
fn eq(&self, other: &Point) -> bool {
self.x == other.x && self.y == other.y
}
fn ne(&self, other: &Point) -> bool { !(*self).eq(other) }
}
impl<T:cmp::Eq> cmp::Eq for Quark<T> {
fn eq(&self, other: &Quark<T>) -> bool {
match *self {
Top(ref q) => {
match *other {
Top(ref r) => q == r,
Bottom(_) => false
}
},
Bottom(ref q) => {
match *other {
Top(_) => false,
Bottom(ref r) => q == r
}
},
}
}
fn ne(&self, other: &Quark<T>) -> bool { !(*self).eq(other) }
}
impl cmp::Eq for CLike {
fn eq(&self, other: &CLike) -> bool {
(*self) as isize == *other as isize
}
fn ne(&self, other: &CLike) -> bool { !self.eq(other) }
}
#[derive(Decodable, Encodable, Eq)]
struct Spanned<T> {
lo: usize,
hi: usize,
node: T,
}
#[derive(Decodable, Encodable)]
struct SomeStruct { v: Vec<usize> }
#[derive(Decodable, Encodable)]
struct Point {x: usize, y: usize}
#[derive(Decodable, Encodable)]
enum Quark<T> {
Top(T),
Bottom(T)
}
#[derive(Decodable, Encodable)]
enum CLike { A, B, C }
pub fn main() {
let a = &Plus(@Minus(@Val(3), @Val(10)), @Plus(@Val(22), @Val(5)));
test_rbml(a);
let a = &Spanned {lo: 0, hi: 5, node: 22};
test_rbml(a);
let a = &Point {x: 3, y: 5};
test_rbml(a);
let a = &Top(22);
test_rbml(a);
let a = &Bottom(222);
test_rbml(a);
let a = &A;
test_rbml(a);
let a = &B;
test_rbml(a);
let a = &time::now();
test_rbml(a);
test_rbml(&1.0f32);
test_rbml(&1.0f64);
test_rbml(&'a');
let mut a = HashMap::new();
test_rbml(&a);
a.insert(1, 2);
test_rbml(&a);
let mut a = HashSet::new();
test_rbml(&a);
a.insert(1);
test_rbml(&a);
}
|