-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathArrayTests.res
More file actions
115 lines (102 loc) · 3.04 KB
/
ArrayTests.res
File metadata and controls
115 lines (102 loc) · 3.04 KB
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
open RescriptCore
let eq = (a, b) => a == b
Test.run(__POS_OF__("make"), Array.make(~length=6, 7), eq, [7, 7, 7, 7, 7, 7])
Test.run(
__POS_OF__("fromInitializer"),
Array.fromInitializer(~length=7, i => i + 3),
eq,
[3, 4, 5, 6, 7, 8, 9],
)
Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], list{}, List.add), eq, list{3, 2, 1})
Test.run(__POS_OF__("reduce - empty"), Array.reduce([], list{}, List.add), eq, list{})
Test.run(
__POS_OF__("reduceWithIndex"),
Array.reduceWithIndex([1, 2, 3], list{}, (acc, v, i) => list{v + i, ...acc}),
eq,
list{5, 3, 1},
)
Test.run(
__POS_OF__("reduceWithIndex - empty"),
Array.reduceWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}),
eq,
list{},
)
Test.run(
__POS_OF__("reduceRight"),
Array.reduceRight([1, 2, 3], list{}, List.add),
eq,
list{1, 2, 3},
)
Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], list{}, List.add), eq, list{})
Test.run(
__POS_OF__("reduceEightWithIndex"),
Array.reduceRightWithIndex([1, 2, 3], list{}, (acc, v, i) => list{v + i, ...acc}),
eq,
list{1, 3, 5},
)
Test.run(
__POS_OF__("reduceWithIndex - empty"),
Array.reduceRightWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}),
eq,
list{},
)
Test.run(__POS_OF__("toShuffled - length"), Array.toShuffled([1, 2, 3])->Array.length, eq, 3)
Test.run(
__POS_OF__("shuffle - length"),
{
let arr = [1, 2, 3]
Array.shuffle(arr)
arr->Array.length
},
eq,
3,
)
Test.run(
__POS_OF__("filterMap"),
Array.filterMap([1, 2, 3, 4, 5, 6], n => mod(n, 2) == 0 ? Some(n * n) : None),
eq,
[4, 16, 36],
)
Test.run(__POS_OF__("filterMap - no match"), Array.filterMap([1, 2, 3, 4, 5, 6], _ => None), eq, [])
Test.run(
__POS_OF__("filterMap - empty"),
Array.filterMap([], n => mod(n, 2) == 0 ? Some(n * n) : None),
eq,
[],
)
Test.run(__POS_OF__("keepSome"), Array.keepSome([Some(1), None, Some(3)]), eq, [1, 3])
Test.run(
__POS_OF__("keepSome - all Some"),
Array.keepSome([Some(1), Some(2), Some(3)]),
eq,
[1, 2, 3],
)
Test.run(__POS_OF__("keepSome - all None"), Array.keepSome([None, None, None]), eq, [])
Test.run(__POS_OF__("keepSome - empty"), Array.keepSome([]), eq, [])
Test.run(
__POS_OF__("findMap"),
Array.findMap([1, 2, 3, 4, 5, 6], n => mod(n, 2) == 0 ? Some(n - 8) : None),
eq,
Some(-6),
)
Test.run(__POS_OF__("findMap - no match"), Array.findMap([1, 2, 3, 4, 5, 6], _ => None), eq, None)
Test.run(
__POS_OF__("findMap - empty"),
Array.findMap([], n => mod(n, 2) == 0 ? Some(n * n) : None),
eq,
None,
)
let of1Test = (a, title) => {
Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a), (i, j) => i == j, [a])
Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a)->Array.length, eq, 1)
Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a)->Array.getUnsafe(0), (i, j) => i === j, a)
}
let m = {"x": 3, "y": 5}->Array.of1
3->of1Test("Single integer")
"abc"->of1Test("Single string")
undefined->of1Test("undefined")
null->of1Test("null")
[1, 2, 3]->of1Test("full array of integers")
[]->of1Test("empty array")
Some(5)->of1Test("Some")
None->of1Test("None")