-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib_completeness.as
More file actions
140 lines (140 loc) · 7.19 KB
/
stdlib_completeness.as
File metadata and controls
140 lines (140 loc) · 7.19 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
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
import * as array from "std/array"
import * as string from "std/string"
import * as math from "std/math"
import * as object from "std/object"
import * as map from "std/map"
import * as crypto from "std/crypto"
let nums = [3, 1, 2, 1, 4]
assert(array.find(nums, x => x > 2) == 3, "find: first > 2 is 3")
let found_nil = array.find(nums, x => x > 10)
assert(found_nil == nil, "find: nothing > 10 => nil")
assert(array.findIndex(nums, x => x == 2) == 2, "findIndex: 2 is at index 2")
assert(array.findIndex(nums, x => x == 9) == -1, "findIndex: 9 missing => -1")
assert(array.some(nums, x => x == 4), "some: contains 4")
assert(!array.some(nums, x => x > 10), "some: nothing > 10")
assert(array.every(nums, x => x > 0), "every: all positive")
assert(!array.every(nums, x => x > 2), "every: not all > 2")
assert(array.every([], x => false), "every: vacuously true on empty")
assert(array.indexOf(nums, 4) == 4, "indexOf: 4 at index 4")
assert(array.indexOf(nums, 1) == 1, "indexOf: first 1 at index 1")
assert(array.indexOf(nums, 9) == -1, "indexOf: 9 missing => -1")
let nested = [[1], [2, 3], [4]]
let flatted = array.flat(nested)
assert(flatted[0] == 1 && flatted[2] == 3, "flat: depth 1")
assert(array.flat([[1, [2]], [3]], 2)[1] == 2, "flat: depth 2")
let fm = array.flatMap([1, 2, 3], x => [x, x * 10])
assert(fm[0] == 1 && fm[1] == 10 && fm[3] == 20, "flatMap: pairs")
assert(array.reverse([1, 2, 3])[0] == 3, "reverse: first element is last")
let cat = array.concat([1, 2], [3, 4], [5])
assert(len(cat) == 5 && cat[4] == 5, "concat: three arrays")
assert(array.first(nums) == 3, "first")
assert(array.last(nums) == 4, "last")
let first_empty = array.first([])
assert(first_empty == nil, "first of empty => nil")
let last_empty = array.last([])
assert(last_empty == nil, "last of empty => nil")
let uniq = array.unique(nums)
assert(len(uniq) == 4, "unique: 4 distinct values from [3,1,2,1,4]")
assert(array.take(nums, 2)[1] == 1, "take 2: second is 1")
assert(len(array.take(nums, 0)) == 0, "take 0: empty")
assert(array.drop(nums, 3)[0] == 1, "drop 3: first remaining is 1")
let chunks = array.chunk(nums, 2)
assert(len(chunks) == 3, "chunk: 5 items / 2 = 3 chunks")
assert(chunks[0][0] == 3 && chunks[0][1] == 1, "chunk[0] = [3, 1]")
assert(len(chunks[2]) == 1, "last chunk has 1 item")
let zipped = array.zip([1, 2, 3], ["a", "b", "c"])
assert(zipped[0][0] == 1 && zipped[0][1] == "a", "zip[0] = [1, a]")
assert(zipped[2][1] == "c", "zip[2][1] = c")
let groups = array.groupBy(nums, x => x % 2)
assert(len(map.get(groups, 1)) == 3, "groupBy: 3 odd numbers")
assert(len(map.get(groups, 0)) == 2, "groupBy: 2 even numbers")
let parts = array.partition(nums, x => x > 2)
assert(len(parts[0]) == 2, "partition: 2 pass (3,4)")
assert(len(parts[1]) == 3, "partition: 3 fail (1,2,1)")
assert(string.startsWith("hello world", "hello"), "startsWith: true")
assert(!string.startsWith("hello world", "world"), "startsWith: false")
assert(string.endsWith("hello world", "world"), "endsWith: true")
assert(!string.endsWith("hello world", "hello"), "endsWith: false")
assert(string.contains("hello world", "lo wo"), "contains: true")
assert(!string.contains("hello world", "xyz"), "contains: false")
assert(string.replace("a.b.c", ".", "-") == "a-b.c", "replace: first only")
assert(string.replaceAll("a.b.c", ".", "-") == "a-b-c", "replaceAll: all")
let chars = string.chars("abc")
assert(len(chars) == 3 && chars[0] == "a", "chars: length 3, first a")
let lines = string.lines("one\ntwo\nthree")
assert(len(lines) == 3 && lines[1] == "two", "lines: 3 lines")
assert(string.reverse("abc") == "cba", "reverse string")
assert(string.reverse("") == "", "reverse empty string")
assert(string.count("banana", "a") == 3, "count: 3 a's in banana")
assert(string.count("hello", "x") == 0, "count: 0 x's")
let sp2 = string.splitN("a:b:c:d", ":", 2)
assert(len(sp2) == 2, "splitN 2: 2 parts")
assert(sp2[0] == "a" && sp2[1] == "b:c:d", "splitN 2: second part has remainder")
assert(math.sin(0) == 0, "sin(0) = 0")
assert(math.cos(0) == 1, "cos(0) = 1")
assert(math.tan(0) == 0, "tan(0) = 0")
assert(math.asin(0) == 0, "asin(0) = 0")
assert(math.acos(1) == 0, "acos(1) = 0")
assert(math.atan(0) == 0, "atan(0) = 0")
let quarter = math.atan2(1, 1)
assert(quarter > 0.785 && quarter < 0.786, "atan2(1,1) ≈ pi/4")
assert(math.exp(0) == 1, "exp(0) = 1")
assert(math.ln(1) == 0, "ln(1) = 0")
assert(math.log2(8) == 3, "log2(8) = 3")
assert(math.log10(1000) == 3, "log10(1000) = 3")
assert(math.sign(-5) == -1, "sign negative")
assert(math.sign(0) == 0, "sign zero")
assert(math.sign(3) == 1, "sign positive")
assert(math.trunc(3.9) == 3, "trunc positive")
assert(math.trunc(-3.9) == -3, "trunc negative")
assert(math.clamp(5, 0, 3) == 3, "clamp above hi")
assert(math.clamp(-1, 0, 3) == 0, "clamp below lo")
assert(math.clamp(2, 0, 3) == 2, "clamp in range")
assert(math.hypot(3, 4) == 5, "hypot 3-4-5")
assert(math.gcd(12, 8) == 4, "gcd(12,8) = 4")
assert(math.lcm(4, 6) == 12, "lcm(4,6) = 12")
assert(math.sum([1, 2, 3, 4]) == 10, "sum")
assert(math.mean([1, 2, 3, 4]) == 2.5, "mean")
assert(math.median([3, 1, 2]) == 2, "median odd")
assert(math.median([1, 2, 3, 4]) == 2.5, "median even")
let pop_var = math.variance([2, 4, 4, 4, 5, 5, 7, 9])
assert(pop_var == 4, "population variance")
let samp_var = math.variance([2, 4], true)
assert(samp_var == 2, "sample variance")
let pop_sd = math.stddev([2, 4, 4, 4, 5, 5, 7, 9])
assert(pop_sd == 2, "population stddev")
assert(math.randomInt(5, 5) == 5, "randomInt single value")
let ri = math.randomInt(1, 100)
assert(ri >= 1 && ri <= 100, "randomInt in [1,100]")
let shuffled = math.shuffle([1, 2, 3, 4, 5])
assert(len(shuffled) == 5, "shuffle preserves length")
let ch = math.choice([10, 20, 30])
assert(ch == 10 || ch == 20 || ch == 30, "choice picks from array")
assert(math.min(...[3, 1, 2]) == 1, "min with spread")
assert(math.max(...[3, 1, 2]) == 3, "max with spread")
let fe = object.fromEntries([["x", 10], ["y", 20]])
assert(fe.x == 10 && fe.y == 20, "fromEntries: x=10, y=20")
let o = { a: 1, b: 2, c: 3 }
let picked = object.pick(o, ["a", "c"])
assert(picked.a == 1 && picked.c == 3, "pick: a and c")
assert(!object.has(picked, "b"), "pick: b not included")
let omitted = object.omit(o, ["b"])
assert(omitted.a == 1 && omitted.c == 3, "omit: a and c remain")
assert(!object.has(omitted, "b"), "omit: b removed")
let doubled = object.mapValues(o, (v, k) => v * 2)
assert(doubled.a == 2 && doubled.b == 4 && doubled.c == 6, "mapValues: all doubled")
let with_key = object.mapValues({ x: 1 }, (v, k) => k)
assert(with_key.x == "x", "mapValues: key passed as second arg")
let nested_o = { a: 1, b: { c: [1, 2, 3] } }
let cloned = object.deepClone(nested_o)
assert(object.deepEqual(nested_o, cloned), "deepClone + deepEqual: equal")
cloned.a = 99
assert(nested_o.a == 1, "deepClone: mutations don't propagate")
assert(!object.deepEqual(nested_o, cloned), "deepEqual: detects difference after mutation")
assert(object.deepEqual({ a: 1, b: 2 }, { a: 1, b: 2 }), "deepEqual: same structure")
assert(!object.deepEqual({ a: 1 }, { a: 2 }), "deepEqual: different values")
assert(crypto.crc32("hello") == 907060870, "crc32 hello = 907060870")
let xxh = crypto.xxhash("hello")
assert(len(xxh) == 16, "xxhash: 16 chars")
assert(xxh == "26c7827d889f6da3", "xxhash hello pinned vector")
print("stdlib completeness: all assertions passed")