aboutsummaryrefslogtreecommitdiff
path: root/gen/test/gleam@list_test.erl
blob: dcb4ceb34abf690c4bb4155d0689288d29e2944d (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
-module(gleam@list_test).
-compile(no_auto_import).

-export([length_test/0, reverse_test/0, is_empty_test/0, contains_test/0, head_test/0, tail_test/0, filter_test/0, map_test/0, traverse_test/0, drop_test/0, take_test/0, new_test/0, append_test/0, flatten_test/0, fold_test/0, fold_right_test/0, find_test/0, all_test/0, any_test/0, zip_test/0, strict_zip_test/0, intersperse_test/0, at_test/0, unique_test/0, sort_test/0, index_map_test/0, range_test/0, repeat_test/0, split_test/0, split_while_test/0, key_find_test/0]).

length_test() ->
    gleam@expect:equal(gleam@list:length([]), 0),
    gleam@expect:equal(gleam@list:length([1]), 1),
    gleam@expect:equal(gleam@list:length([1, 1]), 2),
    gleam@expect:equal(gleam@list:length([1, 1, 1]), 3).

reverse_test() ->
    gleam@expect:equal(gleam@list:reverse([]), []),
    gleam@expect:equal(gleam@list:reverse([1, 2, 3, 4, 5]), [5, 4, 3, 2, 1]).

is_empty_test() ->
    gleam@expect:true(gleam@list:is_empty([])),
    gleam@expect:false(gleam@list:is_empty([1])).

contains_test() ->
    gleam@expect:true(gleam@list:contains([0, 4, 5, 1], 1)),
    gleam@expect:false(gleam@list:contains([0, 4, 5, 7], 1)),
    gleam@expect:false(gleam@list:contains([], 1)).

head_test() ->
    gleam@expect:equal(gleam@list:head([0, 4, 5, 7]), {ok, 0}),
    gleam@expect:is_error(gleam@list:head([])).

tail_test() ->
    gleam@expect:equal(gleam@list:tail([0, 4, 5, 7]), {ok, [4, 5, 7]}),
    gleam@expect:equal(gleam@list:tail([0]), {ok, []}),
    gleam@expect:is_error(gleam@list:tail([])).

filter_test() ->
    gleam@expect:equal(gleam@list:filter([], fun(_) -> true end), []),
    gleam@expect:equal(
        gleam@list:filter([0, 4, 5, 7, 3], fun(_) -> true end),
        [0, 4, 5, 7, 3]
    ),
    gleam@expect:equal(
        gleam@list:filter([0, 4, 5, 7, 3], fun(X) -> X > 4 end),
        [5, 7]
    ),
    gleam@expect:equal(
        gleam@list:filter([0, 4, 5, 7, 3], fun(X) -> X < 4 end),
        [0, 3]
    ).

map_test() ->
    gleam@expect:equal(gleam@list:map([], fun(X) -> X * 2 end), []),
    gleam@expect:equal(
        gleam@list:map([0, 4, 5, 7, 3], fun(X) -> X * 2 end),
        [0, 8, 10, 14, 6]
    ).

traverse_test() ->
    Fun = fun(X) -> case X =:= 6 orelse X =:= 5 orelse X =:= 4 of
            true ->
                {ok, X * 2};

            false ->
                {error, X}
        end end,
    gleam@expect:equal(
        gleam@list:traverse([5, 6, 5, 6], Fun),
        {ok, [10, 12, 10, 12]}
    ),
    gleam@expect:equal(gleam@list:traverse([4, 6, 5, 7, 3], Fun), {error, 7}).

drop_test() ->
    gleam@expect:equal(gleam@list:drop([], 5), []),
    gleam@expect:equal(gleam@list:drop([1, 2, 3, 4, 5, 6, 7, 8], 5), [6, 7, 8]).

take_test() ->
    gleam@expect:equal(gleam@list:take([], 5), []),
    gleam@expect:equal(
        gleam@list:take([1, 2, 3, 4, 5, 6, 7, 8], 5),
        [1, 2, 3, 4, 5]
    ).

new_test() ->
    gleam@expect:equal(gleam@list:new(), []).

append_test() ->
    gleam@expect:equal(gleam@list:append([1], [2, 3]), [1, 2, 3]).

flatten_test() ->
    gleam@expect:equal(gleam@list:flatten([]), []),
    gleam@expect:equal(gleam@list:flatten([[]]), []),
    gleam@expect:equal(gleam@list:flatten([[], [], []]), []),
    gleam@expect:equal(gleam@list:flatten([[1, 2], [], [3, 4]]), [1, 2, 3, 4]).

fold_test() ->
    gleam@expect:equal(
        gleam@list:fold([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end),
        [3, 2, 1]
    ).

fold_right_test() ->
    gleam@expect:equal(
        gleam@list:fold_right([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end),
        [1, 2, 3]
    ).

find_test() ->
    F = fun(X) -> case X of
            2 ->
                {ok, 4};

            _ ->
                {error, 0}
        end end,
    gleam@expect:equal(gleam@list:find([1, 2, 3], F), {ok, 4}),
    gleam@expect:equal(gleam@list:find([1, 3, 2], F), {ok, 4}),
    gleam@expect:is_error(gleam@list:find([1, 3], F)).

all_test() ->
    gleam@expect:equal(
        gleam@list:all([1, 2, 3, 4, 5], fun(X) -> X > 0 end),
        true
    ),
    gleam@expect:equal(
        gleam@list:all([1, 2, 3, 4, 5], fun(X) -> X < 0 end),
        false
    ),
    gleam@expect:equal(gleam@list:all([], fun(_) -> false end), true).

any_test() ->
    gleam@expect:equal(
        gleam@list:any([1, 2, 3, 4, 5], fun(X) -> X =:= 2 end),
        true
    ),
    gleam@expect:equal(
        gleam@list:any([1, 2, 3, 4, 5], fun(X) -> X < 0 end),
        false
    ),
    gleam@expect:equal(gleam@list:any([], fun(_) -> false end), false).

zip_test() ->
    gleam@expect:equal(gleam@list:zip([], [1, 2, 3]), []),
    gleam@expect:equal(gleam@list:zip([1, 2], []), []),
    gleam@expect:equal(
        gleam@list:zip([1, 2, 3], [4, 5, 6]),
        [{1, 4}, {2, 5}, {3, 6}]
    ),
    gleam@expect:equal(gleam@list:zip([5, 6], [1, 2, 3]), [{5, 1}, {6, 2}]),
    gleam@expect:equal(gleam@list:zip([5, 6, 7], [1, 2]), [{5, 1}, {6, 2}]).

strict_zip_test() ->
    gleam@expect:is_error(gleam@list:strict_zip([], [1, 2, 3])),
    gleam@expect:is_error(gleam@list:strict_zip([1, 2], [])),
    gleam@expect:equal(
        gleam@list:strict_zip([1, 2, 3], [4, 5, 6]),
        {ok, [{1, 4}, {2, 5}, {3, 6}]}
    ),
    gleam@expect:is_error(gleam@list:strict_zip([5, 6], [1, 2, 3])),
    gleam@expect:is_error(gleam@list:strict_zip([5, 6, 7], [1, 2])).

intersperse_test() ->
    gleam@expect:equal(gleam@list:intersperse([1, 2, 3], 4), [1, 4, 2, 4, 3]),
    gleam@expect:equal(gleam@list:intersperse([], 2), []).

at_test() ->
    gleam@expect:equal(gleam@list:at([1, 2, 3], 2), {ok, 3}),
    gleam@expect:is_error(gleam@list:at([1, 2, 3], 5)),
    gleam@expect:is_error(gleam@list:at([], 0)),
    gleam@expect:is_error(gleam@list:at([1, 2, 3, 4, 5, 6], -1)).

unique_test() ->
    gleam@expect:equal(
        gleam@list:unique([1, 1, 2, 3, 4, 4, 4, 5, 6]),
        [1, 2, 3, 4, 5, 6]
    ),
    gleam@expect:equal(
        gleam@list:unique([7, 1, 45, 6, 2, 47, 2, 7, 5]),
        [7, 1, 45, 6, 2, 47, 5]
    ),
    gleam@expect:equal(gleam@list:unique([3, 4, 5]), [3, 4, 5]),
    gleam@expect:equal(gleam@list:unique([]), []).

sort_test() ->
    gleam@expect:equal(
        gleam@list:sort([4, 3, 6, 5, 4], fun gleam@int:compare/2),
        [3, 4, 4, 5, 6]
    ),
    gleam@expect:equal(
        gleam@list:sort([4, 3, 6, 5, 4, 1], fun gleam@int:compare/2),
        [1, 3, 4, 4, 5, 6]
    ),
    gleam@expect:equal(
        gleam@list:sort([4.1, 3.1, 6.1, 5.1, 4.1], fun gleam@float:compare/2),
        [3.1, 4.1, 4.1, 5.1, 6.1]
    ),
    gleam@expect:equal(gleam@list:sort([], fun gleam@int:compare/2), []).

index_map_test() ->
    gleam@expect:equal(
        gleam@list:index_map([3, 4, 5], fun(I, X) -> {I, X} end),
        [{0, 3}, {1, 4}, {2, 5}]
    ),
    F = fun(I, X) -> gleam@string:append(X, gleam@int:to_string(I)) end,
    gleam@expect:equal(
        gleam@list:index_map([<<"a">>, <<"b">>, <<"c">>], F),
        [<<"a0">>, <<"b1">>, <<"c2">>]
    ).

range_test() ->
    gleam@expect:equal(gleam@list:range(0, 0), []),
    gleam@expect:equal(gleam@list:range(1, 1), []),
    gleam@expect:equal(gleam@list:range(-1, -1), []),
    gleam@expect:equal(gleam@list:range(0, 1), [0]),
    gleam@expect:equal(gleam@list:range(0, 5), [0, 1, 2, 3, 4]),
    gleam@expect:equal(gleam@list:range(1, -5), [1, 0, -1, -2, -3, -4]).

repeat_test() ->
    gleam@expect:equal(gleam@list:repeat(1, -10), []),
    gleam@expect:equal(gleam@list:repeat(1, 0), []),
    gleam@expect:equal(gleam@list:repeat(2, 3), [2, 2, 2]),
    gleam@expect:equal(
        gleam@list:repeat(<<"x">>, 5),
        [<<"x">>, <<"x">>, <<"x">>, <<"x">>, <<"x">>]
    ).

split_test() ->
    gleam@expect:equal(gleam@list:split([], 0), {[], []}),
    gleam@expect:equal(
        gleam@list:split([0, 1, 2, 3, 4], 0),
        {[], [0, 1, 2, 3, 4]}
    ),
    gleam@expect:equal(
        gleam@list:split([0, 1, 2, 3, 4], -2),
        {[], [0, 1, 2, 3, 4]}
    ),
    gleam@expect:equal(
        gleam@list:split([0, 1, 2, 3, 4], 1),
        {[0], [1, 2, 3, 4]}
    ),
    gleam@expect:equal(
        gleam@list:split([0, 1, 2, 3, 4], 3),
        {[0, 1, 2], [3, 4]}
    ),
    gleam@expect:equal(
        gleam@list:split([0, 1, 2, 3, 4], 9),
        {[0, 1, 2, 3, 4], []}
    ).

split_while_test() ->
    gleam@expect:equal(
        gleam@list:split_while([], fun(X) -> X =< 5 end),
        {[], []}
    ),
    gleam@expect:equal(
        gleam@list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< 5 end),
        {[1, 2, 3, 4, 5], []}
    ),
    gleam@expect:equal(
        gleam@list:split_while([1, 2, 3, 4, 5], fun(X) -> X =:= 2 end),
        {[], [1, 2, 3, 4, 5]}
    ),
    gleam@expect:equal(
        gleam@list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< 3 end),
        {[1, 2, 3], [4, 5]}
    ),
    gleam@expect:equal(
        gleam@list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< -3 end),
        {[], [1, 2, 3, 4, 5]}
    ).

key_find_test() ->
    Proplist = [{0, <<"1">>}, {1, <<"2">>}],
    gleam@expect:equal(gleam@list:key_find(Proplist, 0), {ok, <<"1">>}),
    gleam@expect:equal(gleam@list:key_find(Proplist, 1), {ok, <<"2">>}),
    gleam@expect:is_error(gleam@list:key_find(Proplist, 2)).