about summary refs log tree commit diff
path: root/src/test/stdtest/list.rs
blob: da1643e92749cb005bad4a3246c9b1c6eb3f900b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import core::*;

use std;
import std::list;
import std::list::head;
import std::list::tail;
import std::list::from_vec;
import option;

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

#[test]
fn test_from_vec_mut() {
    let l = from_vec([mutable 0, 1, 2]);
    assert (head(l) == 0);
    assert (head(tail(l)) == 1);
    assert (head(tail(tail(l))) == 2);
}

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

#[test]
fn test_foldl2() {
    fn sub(&&a: int, &&b: int) -> int {
        a - b
    }
    let l = from_vec([1, 2, 3, 4]);
    let sum = list::foldl(l, 0, sub);
    assert sum == -10;
}

#[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_len() {
    let l = from_vec([0, 1, 2]);
    assert (list::len(l) == 3u);
}