Skip to content

Commit 7beb21c

Browse files
Lenny222marijnh
authored andcommitted
tests: add corner case (empty list)
1 parent 341e415 commit 7beb21c

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/test/stdtest/list.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ fn test_from_vec() {
1515
assert (head(tail(tail(l))) == 2);
1616
}
1717

18+
#[test]
19+
fn test_from_vec_empty() {
20+
let empty : list::list<int> = from_vec([]);
21+
assert (empty == list::nil::<int>);
22+
}
23+
1824
#[test]
1925
fn test_from_vec_mut() {
2026
let l = from_vec([mutable 0, 1, 2]);
@@ -25,10 +31,11 @@ fn test_from_vec_mut() {
2531

2632
#[test]
2733
fn test_foldl() {
28-
let l = from_vec([0, 1, 2, 3, 4]);
2934
fn add(&&a: uint, &&b: int) -> uint { ret a + (b as uint); }
30-
let rs = list::foldl(l, 0u, add);
31-
assert (rs == 10u);
35+
let l = from_vec([0, 1, 2, 3, 4]);
36+
let empty = list::nil::<int>;
37+
assert (list::foldl(l, 0u, add) == 10u);
38+
assert (list::foldl(empty, 0u, add) == 0u);
3239
}
3340

3441
#[test]
@@ -37,26 +44,25 @@ fn test_foldl2() {
3744
a - b
3845
}
3946
let l = from_vec([1, 2, 3, 4]);
40-
let sum = list::foldl(l, 0, sub);
41-
assert sum == -10;
47+
assert (list::foldl(l, 0, sub) == -10);
4248
}
4349

4450
#[test]
4551
fn test_find_success() {
46-
let l = from_vec([0, 1, 2]);
4752
fn match(&&i: int) -> option::t<int> {
4853
ret if i == 2 { option::some(i) } else { option::none::<int> };
4954
}
50-
let rs = list::find(l, match);
51-
assert (rs == option::some(2));
55+
let l = from_vec([0, 1, 2]);
56+
assert (list::find(l, match) == option::some(2));
5257
}
5358

5459
#[test]
5560
fn test_find_fail() {
56-
let l = from_vec([0, 1, 2]);
5761
fn match(&&_i: int) -> option::t<int> { ret option::none::<int>; }
58-
let rs = list::find(l, match);
59-
assert (rs == option::none::<int>);
62+
let l = from_vec([0, 1, 2]);
63+
let empty = list::nil::<int>;
64+
assert (list::find(l, match) == option::none::<int>);
65+
assert (list::find(empty, match) == option::none::<int>);
6066
}
6167

6268
#[test]
@@ -72,6 +78,8 @@ fn test_has() {
7278
#[test]
7379
fn test_len() {
7480
let l = from_vec([0, 1, 2]);
81+
let empty = list::nil::<int>;
7582
assert (list::len(l) == 3u);
83+
assert (list::len(empty) == 0u);
7684
}
7785

0 commit comments

Comments
 (0)