aboutsummaryrefslogtreecommitdiff
path: root/test/list_test.gleam
blob: e7cd786f8dd41e34252a4264a9b6ea5fd41d1fc6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import expect
import list

pub fn length_test() {
  list:length([]) |> expect:equal(_, 0)
  list:length([1]) |> expect:equal(_, 1)
  list:length([1, 1]) |> expect:equal(_, 2)
  list:length([1, 1, 1]) |> expect:equal(_, 3)
}

pub fn reverse_test() {
  list:reverse([]) |> expect:equal(_, [])
  list:reverse([1, 2, 3, 4, 5]) |> expect:equal(_, [5, 4, 3, 2, 1])
}

pub fn is_empty_test() {
  list:is_empty([]) |> expect:true
  list:is_empty([1]) |> expect:false
}

pub fn contains_test() {
  list:contains([0, 4, 5, 1], 1) |> expect:true
  list:contains([0, 4, 5, 7], 1) |> expect:false
  list:contains([], 1) |> expect:false
}

pub fn head_test() {
  list:head([0, 4, 5, 7])
    |> expect:equal(_, Ok(0))

  list:head([])
    |> expect:is_error
}

pub fn tail_test() {
  list:tail([0, 4, 5, 7])
    |> expect:equal(_, Ok([4, 5, 7]))

  list:tail([0])
    |> expect:equal(_, Ok([]))

  list:tail([])
    |> expect:is_error
}

pub fn filter_test() {
  []
    |> list:filter(_, fn(_) { True })
    |> expect:equal(_, [])

  [0, 4, 5, 7, 3]
    |> list:filter(_, fn(_) { True })
    |> expect:equal(_, [0, 4, 5, 7, 3])

  [0, 4, 5, 7, 3]
    |> list:filter(_, fn(x) { x > 4 })
    |> expect:equal(_, [5, 7])

  [0, 4, 5, 7, 3]
    |> list:filter(_, fn(x) { x < 4 })
    |> expect:equal(_, [0, 3])
}

pub fn map_test() {
  []
    |> list:map(_, fn(x) { x * 2 })
    |> expect:equal(_, [])

  [0, 4, 5, 7, 3]
    |> list:map(_, fn(x) { x * 2 })
    |> expect:equal(_, [0, 8, 10, 14, 6])
}

pub fn traverse_test() {
  let fun = fn(x) {
    case x == 6 || x == 5 || x == 4 {
    | True -> Ok(x * 2)
    | False -> Error(x)
    }
  }

  [5, 6, 5, 6]
    |> list:traverse(_, fun)
    |> expect:equal(_, Ok([10, 12, 10, 12]))

  [4, 6, 5, 7, 3]
    |> list:traverse(_, fun)
    |> expect:equal(_, Error(7))
}

pub fn drop_test() {
  []
    |> list:drop(_, 5)
    |> expect:equal(_, [])

  [1, 2, 3, 4, 5, 6, 7, 8]
    |> list:drop(_, 5)
    |> expect:equal(_, [6, 7, 8])
}

pub fn take_test() {
  []
    |> list:take(_, 5)
    |> expect:equal(_, [])
  [1, 2, 3, 4, 5, 6, 7, 8]
    |> list:take(_, 5)
    |> expect:equal(_, [1, 2, 3, 4, 5])
}

pub fn new_test() {
  list:new() |> expect:equal(_, [])
}

pub fn append_test() {
  list:append([1], [2, 3])
    |> expect:equal(_, [1, 2, 3])
}

pub fn flatten_test() {
  list:flatten([])
    |> expect:equal(_, [])

  list:flatten([[]])
    |> expect:equal(_, [])

  list:flatten([[], [], []])
    |> expect:equal(_, [])

  list:flatten([[1, 2], [], [3, 4]])
    |> expect:equal(_, [1, 2, 3, 4])
}

pub fn fold_test() {
  [1, 2, 3]
    |> list:fold(_, [], fn(x, acc) { [x | acc] })
    |> expect:equal(_, [3, 2, 1])
}

pub fn fold_right_test() {
  [1, 2, 3]
    |> list:fold_right(_, [], fn(x, acc) { [x | acc] })
    |> expect:equal(_, [1, 2, 3])
}

pub fn find_test() {
  let f = fn(x) {
    case x {
    | 2 -> Ok(4)
    | _ -> Error(0)
    }
  }

  [1, 2, 3]
  |> list:find(_, f)
  |> expect:equal(_, Ok(4))

  [1, 3, 2]
  |> list:find(_, f)
  |> expect:equal(_, Ok(4))

  [1, 3]
  |> list:find(_, f)
  |> expect:is_error
}

pub fn all_test() {
  list:all([1,2,3,4,5], fn(x) { x > 0 })
  |> expect:equal(_, True)

  list:all([1,2,3,4,5], fn(x) { x < 0 })
  |> expect:equal(_, False)

  list:all([], fn(_) { False })
  |> expect:equal(_, True)
}

pub fn any_test() {
  list:any([1,2,3,4,5], fn(x) { x == 2 })
  |> expect:equal(_, True)

  list:any([1,2,3,4,5], fn(x) { x < 0 })
  |> expect:equal(_, False)

  list:any([], fn(_) { False })
  |> expect:equal(_, False)
}

pub fn zip_test() {
  list:zip([], [1,2,3])
  |> expect:equal(_, [])

  list:zip([1,2], [])
  |> expect:equal(_, [])

  list:zip([1,2,3], [4,5,6])
  |> expect:equal(_, [{1,4}, {2,5}, {3,6}])

  list:zip([5,6], [1,2,3])
  |> expect:equal(_, [{5,1}, {6,2}])

  list:zip([5,6,7], [1,2])
  |> expect:equal(_, [{5,1}, {6,2}])
}