@@ -3,6 +3,7 @@ import std::list;
3
3
import std:: list:: car;
4
4
import std:: list:: cdr;
5
5
import std:: list:: from_vec;
6
+ import std:: option;
6
7
7
8
fn test_from_vec ( ) {
8
9
auto l = from_vec ( [ 0 , 1 , 2 ] ) ;
@@ -11,6 +12,46 @@ fn test_from_vec() {
11
12
assert ( car ( cdr ( cdr ( l) ) ) == 2 ) ;
12
13
}
13
14
15
+ fn test_foldl ( ) {
16
+ auto l = from_vec ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
17
+ fn add ( & int a, & uint b) -> uint {
18
+ ret ( a as uint ) + b;
19
+ }
20
+ auto res = list:: foldl ( l, 0 u, add) ;
21
+ assert ( res == 10 u) ;
22
+ }
23
+
24
+ fn test_find_success ( ) {
25
+ auto l = from_vec ( [ 0 , 1 , 2 ] ) ;
26
+ fn match ( & int i) -> option:: t [ int ] {
27
+ ret if ( i == 2 ) {
28
+ option:: some ( i)
29
+ } else {
30
+ option:: none[ int]
31
+ } ;
32
+ }
33
+ auto res = list:: find ( l, match ) ;
34
+ assert ( res == option:: some ( 2 ) ) ;
35
+ }
36
+
37
+ fn test_find_fail ( ) {
38
+ auto l = from_vec ( [ 0 , 1 , 2 ] ) ;
39
+ fn match ( & int i) -> option:: t [ int ] {
40
+ ret option:: none[ int] ;
41
+ }
42
+ auto res = list:: find ( l, match ) ;
43
+ assert ( res == option:: none[ int] ) ;
44
+ }
45
+
46
+ fn test_length ( ) {
47
+ auto l = from_vec ( [ 0 , 1 , 2 ] ) ;
48
+ assert ( list:: length ( l) == 3 u) ;
49
+ }
50
+
14
51
fn main ( ) {
15
52
test_from_vec ( ) ;
53
+ test_foldl ( ) ;
54
+ test_find_success ( ) ;
55
+ test_find_fail ( ) ;
56
+ test_length ( ) ;
16
57
}
0 commit comments