2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-21 23:11:28 -05:00
|
|
|
use std;
|
|
|
|
import std::list;
|
|
|
|
import std::list::car;
|
|
|
|
import std::list::cdr;
|
|
|
|
import std::list::from_vec;
|
2011-05-21 23:40:55 -05:00
|
|
|
import std::option;
|
2011-05-21 23:11:28 -05:00
|
|
|
|
|
|
|
fn test_from_vec() {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto l = from_vec([0, 1, 2]);
|
|
|
|
assert (car(l) == 0);
|
|
|
|
assert (car(cdr(l)) == 1);
|
|
|
|
assert (car(cdr(cdr(l))) == 2);
|
2011-05-21 23:11:28 -05:00
|
|
|
}
|
|
|
|
|
2011-05-21 23:40:55 -05:00
|
|
|
fn test_foldl() {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto l = from_vec([0, 1, 2, 3, 4]);
|
|
|
|
fn add(&int a, &uint b) -> uint { ret (a as uint) + b; }
|
2011-06-25 10:45:49 -05:00
|
|
|
auto rs = list::foldl(l, 0u, add);
|
|
|
|
assert (rs == 10u);
|
2011-05-21 23:40:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_find_success() {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto l = from_vec([0, 1, 2]);
|
|
|
|
fn match(&int i) -> option::t[int] {
|
|
|
|
ret if (i == 2) { option::some(i) } else { option::none[int] };
|
|
|
|
}
|
2011-06-25 10:45:49 -05:00
|
|
|
auto rs = list::find(l, match);
|
|
|
|
assert (rs == option::some(2));
|
2011-05-21 23:40:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_find_fail() {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto l = from_vec([0, 1, 2]);
|
|
|
|
fn match(&int i) -> option::t[int] { ret option::none[int]; }
|
2011-06-25 10:45:49 -05:00
|
|
|
auto rs = list::find(l, match);
|
|
|
|
assert (rs == option::none[int]);
|
2011-05-21 23:40:55 -05:00
|
|
|
}
|
|
|
|
|
2011-05-25 11:54:43 -05:00
|
|
|
fn test_has() {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto l = from_vec([5, 8, 6]);
|
|
|
|
auto empty = list::nil[int];
|
|
|
|
assert (list::has(l, 5));
|
|
|
|
assert (!list::has(l, 7));
|
|
|
|
assert (list::has(l, 8));
|
|
|
|
assert (!list::has(empty, 5));
|
2011-05-25 11:54:43 -05:00
|
|
|
}
|
|
|
|
|
2011-05-21 23:40:55 -05:00
|
|
|
fn test_length() {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto l = from_vec([0, 1, 2]);
|
|
|
|
assert (list::length(l) == 3u);
|
2011-05-21 23:40:55 -05:00
|
|
|
}
|
|
|
|
|
2011-05-21 23:11:28 -05:00
|
|
|
fn main() {
|
2011-06-15 13:19:50 -05:00
|
|
|
test_from_vec();
|
|
|
|
test_foldl();
|
|
|
|
test_find_success();
|
|
|
|
test_find_fail();
|
|
|
|
test_length();
|
|
|
|
test_has();
|
|
|
|
}
|