about summary refs log tree commit diff
path: root/src/test/stdtest/list.rs
blob: 3880ee8d6133b8c952f8a1a89cd46255d8fec64b (plain)
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

use std;
import std::list;
import std::list::car;
import std::list::cdr;
import std::list::from_vec;
import std::option;

#[test]
fn test_from_vec() {
    let l = from_vec([0, 1, 2]);
    assert (car(l) == 0);
    assert (car(cdr(l)) == 1);
    assert (car(cdr(cdr(l))) == 2);
}

#[test]
fn test_foldl() {
    let l = from_vec([0, 1, 2, 3, 4]);
    fn add(a: &int, b: &uint) -> uint { ret (a as uint) + b; }
    let rs = list::foldl(l, 0u, add);
    assert (rs == 10u);
}

#[test]
fn test_find_success() {
    let l = from_vec([0, 1, 2]);
    fn match(i: &int) -> option::t<int> {
        ret if i == 2 { option::some(i) } else { option::none::<int> };
    }
    let rs = list::find(l, match);
    assert (rs == option::some(2));
}

#[test]
fn test_find_fail() {
    let l = from_vec([0, 1, 2]);
    fn match(_i: &int) -> option::t<int> { ret option::none::<int>; }
    let rs = list::find(l, match);
    assert (rs == option::none::<int>);
}

#[test]
fn test_has() {
    let l = from_vec([5, 8, 6]);
    let empty = list::nil::<int>;
    assert (list::has(l, 5));
    assert (!list::has(l, 7));
    assert (list::has(l, 8));
    assert (!list::has(empty, 5));
}

#[test]
fn test_length() {
    let l = from_vec([0, 1, 2]);
    assert (list::length(l) == 3u);
}