2010-07-28 16:00:44 -05:00
|
|
|
// -*- rust -*-
|
|
|
|
|
|
|
|
use std;
|
|
|
|
import std.deque;
|
|
|
|
|
2010-07-28 18:34:22 -05:00
|
|
|
fn test_simple() {
|
|
|
|
let deque.t[int] d = deque.create[int]();
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 0u);
|
2010-07-28 18:34:22 -05:00
|
|
|
d.add_front(17);
|
|
|
|
d.add_front(42);
|
|
|
|
d.add_back(137);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 3u);
|
2010-07-28 18:34:22 -05:00
|
|
|
d.add_back(137);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 4u);
|
2010-07-28 18:34:22 -05:00
|
|
|
|
|
|
|
log d.peek_front();
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.peek_front() == 42);
|
2010-07-28 18:34:22 -05:00
|
|
|
|
|
|
|
log d.peek_back();
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.peek_back() == 137);
|
2010-07-28 18:34:22 -05:00
|
|
|
|
|
|
|
let int i = d.pop_front();
|
|
|
|
log i;
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (i == 42);
|
2010-07-28 18:34:22 -05:00
|
|
|
|
|
|
|
i = d.pop_back();
|
|
|
|
log i;
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (i == 137);
|
2010-07-28 18:34:22 -05:00
|
|
|
|
|
|
|
i = d.pop_back();
|
|
|
|
log i;
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (i == 137);
|
2010-07-28 18:34:22 -05:00
|
|
|
|
|
|
|
i = d.pop_back();
|
|
|
|
log i;
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (i == 17);
|
2010-07-28 18:34:22 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 0u);
|
2010-08-13 13:55:25 -05:00
|
|
|
d.add_back(3);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 1u);
|
2010-08-13 13:55:25 -05:00
|
|
|
d.add_front(2);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 2u);
|
2010-08-13 13:55:25 -05:00
|
|
|
d.add_back(4);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 3u);
|
2010-08-13 13:55:25 -05:00
|
|
|
d.add_front(1);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.size() == 4u);
|
2010-08-13 13:55:25 -05:00
|
|
|
|
|
|
|
log d.get(0);
|
|
|
|
log d.get(1);
|
|
|
|
log d.get(2);
|
|
|
|
log d.get(3);
|
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (d.get(0) == 1);
|
|
|
|
assert (d.get(1) == 2);
|
|
|
|
assert (d.get(2) == 3);
|
|
|
|
assert (d.get(3) == 4);
|
2010-07-28 18:34:22 -05:00
|
|
|
}
|
|
|
|
|
2010-07-28 19:31:41 -05:00
|
|
|
fn test_boxes(@int a, @int b, @int c, @int d) {
|
|
|
|
let deque.t[@int] deq = deque.create[@int]();
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 0u);
|
2010-07-28 19:31:41 -05:00
|
|
|
deq.add_front(a);
|
|
|
|
deq.add_front(b);
|
|
|
|
deq.add_back(c);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 3u);
|
2010-07-28 19:31:41 -05:00
|
|
|
deq.add_back(d);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 4u);
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.peek_front() == b);
|
|
|
|
assert (deq.peek_back() == d);
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.pop_front() == b);
|
|
|
|
assert (deq.pop_back() == d);
|
|
|
|
assert (deq.pop_back() == c);
|
|
|
|
assert (deq.pop_back() == a);
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 0u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_back(c);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 1u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_front(b);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 2u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_back(d);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 3u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_front(a);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 4u);
|
2010-08-13 13:55:25 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.get(0) == a);
|
|
|
|
assert (deq.get(1) == b);
|
|
|
|
assert (deq.get(2) == c);
|
|
|
|
assert (deq.get(3) == d);
|
2010-07-28 19:31:41 -05:00
|
|
|
}
|
|
|
|
|
2011-04-13 14:29:34 -05:00
|
|
|
type eqfn[T] = fn(&T a, &T b) -> bool;
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2010-10-28 17:02:00 -05:00
|
|
|
fn test_parameterized[T](eqfn[T] e, &T a, &T b, &T c, &T d) {
|
2010-07-28 19:31:41 -05:00
|
|
|
let deque.t[T] deq = deque.create[T]();
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 0u);
|
2010-07-28 19:31:41 -05:00
|
|
|
deq.add_front(a);
|
|
|
|
deq.add_front(b);
|
|
|
|
deq.add_back(c);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 3u);
|
2010-07-28 19:31:41 -05:00
|
|
|
deq.add_back(d);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 4u);
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (e(deq.peek_front(), b));
|
|
|
|
assert (e(deq.peek_back(), d));
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (e(deq.pop_front(), b));
|
|
|
|
assert (e(deq.pop_back(), d));
|
|
|
|
assert (e(deq.pop_back(), c));
|
|
|
|
assert (e(deq.pop_back(), a));
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 0u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_back(c);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 1u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_front(b);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 2u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_back(d);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 3u);
|
2010-08-13 13:55:25 -05:00
|
|
|
deq.add_front(a);
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (deq.size() == 4u);
|
2010-08-13 13:55:25 -05:00
|
|
|
|
2011-05-02 13:23:07 -05:00
|
|
|
assert (e(deq.get(0), a));
|
|
|
|
assert (e(deq.get(1), b));
|
|
|
|
assert (e(deq.get(2), c));
|
|
|
|
assert (e(deq.get(3), d));
|
2010-07-28 19:31:41 -05:00
|
|
|
}
|
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
tag taggy {
|
|
|
|
one(int);
|
|
|
|
two(int, int);
|
|
|
|
three(int, int, int);
|
|
|
|
}
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
tag taggypar[T] {
|
|
|
|
onepar(int);
|
|
|
|
twopar(int, int);
|
|
|
|
threepar(int, int, int);
|
|
|
|
}
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2010-07-28 20:14:33 -05:00
|
|
|
type reccy = rec(int x, int y, taggy t);
|
|
|
|
|
2010-07-28 16:00:44 -05:00
|
|
|
fn main() {
|
2011-04-13 14:29:34 -05:00
|
|
|
fn inteq(&int a, &int b) -> bool {
|
2010-07-28 19:31:41 -05:00
|
|
|
ret a == b;
|
|
|
|
}
|
|
|
|
|
2011-04-13 14:29:34 -05:00
|
|
|
fn intboxeq(&@int a, &@int b) -> bool {
|
2010-07-28 19:31:41 -05:00
|
|
|
ret a == b;
|
|
|
|
}
|
|
|
|
|
2011-04-13 14:29:34 -05:00
|
|
|
fn taggyeq(&taggy a, &taggy b) -> bool {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (a) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (one(?a1)) {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (b) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (one(?b1)) { ret a1 == b1; }
|
2010-07-28 19:31:41 -05:00
|
|
|
case (_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
case (two(?a1, ?a2)) {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (b) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (two(?b1, ?b2)) { ret (a1 == b1 && a2 == b2); }
|
2010-07-28 19:31:41 -05:00
|
|
|
case (_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
case (three(?a1, ?a2, ?a3)) {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (b) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (three(?b1, ?b2, ?b3)) {
|
|
|
|
ret (a1 == b1 && a2 == b2 && a3 == b3);
|
|
|
|
}
|
2010-07-28 19:31:41 -05:00
|
|
|
case (_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-13 14:29:34 -05:00
|
|
|
fn taggypareq[T](&taggypar[T] a, &taggypar[T] b) -> bool {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (a) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (onepar[T](?a1)) {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (b) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (onepar[T](?b1)) { ret a1 == b1; }
|
2010-07-28 19:31:41 -05:00
|
|
|
case (_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
case (twopar[T](?a1, ?a2)) {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (b) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (twopar[T](?b1, ?b2)) { ret (a1 == b1 && a2 == b2); }
|
2010-07-28 19:31:41 -05:00
|
|
|
case (_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
case (threepar[T](?a1, ?a2, ?a3)) {
|
2010-07-28 19:31:41 -05:00
|
|
|
alt (b) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (threepar[T](?b1, ?b2, ?b3)) {
|
2010-07-28 19:31:41 -05:00
|
|
|
ret (a1 == b1 && a2 == b2 && a3 == b3);
|
|
|
|
}
|
|
|
|
case (_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-13 14:29:34 -05:00
|
|
|
fn reccyeq(&reccy a, &reccy b) -> bool {
|
2010-07-28 20:14:33 -05:00
|
|
|
ret (a.x == b.x && a.y == b.y && taggyeq(a.t, b.t));
|
|
|
|
}
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** starting";
|
|
|
|
|
|
|
|
log "*** test simple";
|
2010-07-28 18:34:22 -05:00
|
|
|
test_simple();
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** end test simple";
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** test boxes";
|
2010-07-28 19:31:41 -05:00
|
|
|
test_boxes(@5, @72, @64, @175);
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** end test boxes";
|
2010-07-28 19:31:41 -05:00
|
|
|
|
|
|
|
log "test parameterized: int";
|
2010-08-13 13:55:25 -05:00
|
|
|
let eqfn[int] eq1 = inteq;
|
2010-07-28 19:31:41 -05:00
|
|
|
test_parameterized[int](eq1, 5, 72, 64, 175);
|
|
|
|
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** test parameterized: @int";
|
|
|
|
let eqfn[@int] eq2 = intboxeq;
|
2010-07-28 19:31:41 -05:00
|
|
|
test_parameterized[@int](eq2, @5, @72, @64, @175);
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** end test parameterized @int";
|
2010-07-28 19:31:41 -05:00
|
|
|
|
|
|
|
log "test parameterized: taggy";
|
2010-08-13 13:55:25 -05:00
|
|
|
let eqfn[taggy] eq3 = taggyeq;
|
2010-07-28 19:31:41 -05:00
|
|
|
test_parameterized[taggy](eq3,
|
|
|
|
one(1), two(1, 2), three(1, 2, 3), two(17, 42));
|
|
|
|
|
|
|
|
/*
|
2010-08-13 13:55:25 -05:00
|
|
|
* FIXME: Segfault. Also appears to be caused only after upcall_grow_task
|
2010-07-28 19:31:41 -05:00
|
|
|
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** test parameterized: taggypar[int]";
|
|
|
|
let eqfn[taggypar[int]] eq4 = taggypareq[int];
|
2010-07-28 19:31:41 -05:00
|
|
|
test_parameterized[taggypar[int]](eq4,
|
|
|
|
onepar[int](1),
|
|
|
|
twopar[int](1, 2),
|
|
|
|
threepar[int](1, 2, 3),
|
|
|
|
twopar[int](17, 42));
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** end test parameterized: taggypar[int]";
|
2010-07-28 19:31:41 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** test parameterized: reccy";
|
2010-07-28 20:14:33 -05:00
|
|
|
let reccy reccy1 = rec(x=1, y=2, t=one(1));
|
|
|
|
let reccy reccy2 = rec(x=345, y=2, t=two(1, 2));
|
|
|
|
let reccy reccy3 = rec(x=1, y=777, t=three(1, 2, 3));
|
|
|
|
let reccy reccy4 = rec(x=19, y=252, t=two(17, 42));
|
2010-08-13 13:55:25 -05:00
|
|
|
let eqfn[reccy] eq5 = reccyeq;
|
2010-07-28 20:14:33 -05:00
|
|
|
test_parameterized[reccy](eq5,
|
|
|
|
reccy1, reccy2, reccy3, reccy4);
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** end test parameterized: reccy";
|
2010-07-28 20:14:33 -05:00
|
|
|
|
|
|
|
|
2010-08-13 13:55:25 -05:00
|
|
|
log "*** done";
|
2010-07-28 16:00:44 -05:00
|
|
|
}
|