rust/src/test/run-pass/auto-encode.rs

206 lines
4.9 KiB
Rust
Raw Normal View History

// xfail-fast
// Copyright 2012 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.
#[forbid(deprecated_pattern)];
2012-09-18 14:08:42 -05:00
extern mod std;
// These tests used to be separate files, but I wanted to refactor all
2012-12-13 16:18:47 -06:00
// the oldcommon code.
2012-09-18 14:08:42 -05:00
use cmp::Eq;
use std::ebml;
use EBReader = std::ebml::reader;
use EBWriter = std::ebml::writer;
2012-09-18 14:08:42 -05:00
use io::Writer;
use std::serialize::{Encodable, Decodable, decode};
use std::prettyprint;
use std::time;
2012-09-18 14:08:42 -05:00
fn test_prettyprint<A: Encodable<prettyprint::Encoder>>(
a: &A,
expected: &~str
2012-09-18 14:08:42 -05:00
) {
let s = do io::with_str_writer |w| {
a.encode(&prettyprint::Encoder(w))
};
2012-09-18 14:08:42 -05:00
debug!("s == %?", s);
assert s == *expected;
}
2012-09-18 14:08:42 -05:00
fn test_ebml<A:
Eq
Encodable<EBWriter::Encoder>
Decodable<EBReader::Decoder>
>(a1: &A) {
2012-09-18 14:08:42 -05:00
let bytes = do io::with_bytes_writer |wr| {
let ebml_w = &EBWriter::Encoder(wr);
a1.encode(ebml_w)
2012-09-18 14:08:42 -05:00
};
let d = EBReader::Doc(@move bytes);
let a2: A = decode(&EBReader::Decoder(d));
2012-10-01 13:35:27 -05:00
assert *a1 == a2;
2012-09-18 14:08:42 -05:00
}
#[auto_encode]
#[auto_decode]
enum Expr {
Val(uint),
Plus(@Expr, @Expr),
Minus(@Expr, @Expr)
}
impl Expr : cmp::Eq {
pure 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
}
}
}
}
pure fn ne(&self, other: &Expr) -> bool { !(*self).eq(other) }
}
2012-09-18 14:08:42 -05:00
impl AnEnum : cmp::Eq {
pure fn eq(&self, other: &AnEnum) -> bool {
(*self).v == other.v
2012-09-18 14:08:42 -05:00
}
pure fn ne(&self, other: &AnEnum) -> bool { !(*self).eq(other) }
2012-09-18 14:08:42 -05:00
}
impl Point : cmp::Eq {
pure fn eq(&self, other: &Point) -> bool {
2012-09-18 14:08:42 -05:00
self.x == other.x && self.y == other.y
}
pure fn ne(&self, other: &Point) -> bool { !(*self).eq(other) }
2012-09-18 14:08:42 -05:00
}
impl<T:cmp::Eq> Quark<T> : cmp::Eq {
pure 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
}
},
2012-09-18 14:08:42 -05:00
}
}
pure fn ne(&self, other: &Quark<T>) -> bool { !(*self).eq(other) }
2012-09-18 14:08:42 -05:00
}
impl CLike : cmp::Eq {
pure fn eq(&self, other: &CLike) -> bool {
(*self) as int == *other as int
2012-09-18 14:08:42 -05:00
}
pure fn ne(&self, other: &CLike) -> bool { !(*self).eq(other) }
2012-09-18 14:08:42 -05:00
}
#[auto_encode]
#[auto_decode]
2012-09-18 14:08:42 -05:00
type Spanned<T> = {lo: uint, hi: uint, node: T};
impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
pure fn eq(&self, other: &Spanned<T>) -> bool {
(*self).lo == other.lo &&
(*self).hi == other.hi &&
(*self).node == other.node
2012-09-18 14:08:42 -05:00
}
pure fn ne(&self, other: &Spanned<T>) -> bool { !(*self).eq(other) }
2012-09-18 14:08:42 -05:00
}
#[auto_encode]
#[auto_decode]
2012-09-18 14:08:42 -05:00
type SomeRec = {v: ~[uint]};
#[auto_encode]
#[auto_decode]
2012-09-18 14:08:42 -05:00
enum AnEnum = SomeRec;
#[auto_encode]
#[auto_decode]
2012-10-01 13:35:27 -05:00
struct Point {x: uint, y: uint}
2012-09-18 14:08:42 -05:00
#[auto_encode]
#[auto_decode]
2012-09-18 14:08:42 -05:00
enum Quark<T> {
Top(T),
Bottom(T)
}
#[auto_encode]
#[auto_decode]
2012-09-18 14:08:42 -05:00
enum CLike { A, B, C }
fn main() {
let a = &Plus(@Minus(@Val(3u), @Val(10u)), @Plus(@Val(22u), @Val(5u)));
test_prettyprint(a, &~"Plus(@Minus(@Val(3u), @Val(10u)), \
@Plus(@Val(22u), @Val(5u)))");
test_ebml(a);
let a = &{lo: 0u, hi: 5u, node: 22u};
test_prettyprint(a, &~"{lo: 0u, hi: 5u, node: 22u}");
test_ebml(a);
let a = &AnEnum({v: ~[1u, 2u, 3u]});
test_prettyprint(a, &~"AnEnum({v: ~[1u, 2u, 3u]})");
test_ebml(a);
let a = &Point {x: 3u, y: 5u};
test_prettyprint(a, &~"Point {x: 3u, y: 5u}");
test_ebml(a);
let a = &@[1u, 2u, 3u];
test_prettyprint(a, &~"@[1u, 2u, 3u]");
test_ebml(a);
let a = &Top(22u);
test_prettyprint(a, &~"Top(22u)");
test_ebml(a);
let a = &Bottom(222u);
test_prettyprint(a, &~"Bottom(222u)");
test_ebml(a);
let a = &A;
test_prettyprint(a, &~"A");
test_ebml(a);
let a = &B;
test_prettyprint(a, &~"B");
test_ebml(a);
let a = &time::now();
test_ebml(a);
2012-09-18 14:08:42 -05:00
}