forked from google-deepmind/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.lua
More file actions
262 lines (215 loc) · 6.12 KB
/
helpers_test.lua
File metadata and controls
262 lines (215 loc) · 6.12 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
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
local asserts = require 'testing.asserts'
local test_runner = require 'testing.test_runner'
local helpers = require 'common.helpers'
local random = require 'common.random'
local tests = {}
function tests.findFileInLuaPath_shouldFindCommonHelpers()
-- This must be present, because we require it above:
local path = helpers.findFileInLuaPath('common.helpers')
assert(path)
assert(path:find('/game_scripts/common/helpers.lua'))
end
function tests.findFileInLuaPath_shouldNotFindMadeUpFile()
local path = helpers.findFileInLuaPath('wiggly.biggly.boo')
asserts.EQ(path, nil)
end
local function toKeyArrays(table)
if table == nil then return {} end
local arr = {}
for k, v in pairs(table) do
arr[#arr + 1] = k
end
return arr
end
local function shallowEqual(tableA, tableB)
if tableA == nil and tableB == nil then return true end
-- Ensure tables don't reference the same object.
if tableA == tableB then return false end
-- Convert to arrays
local a = toKeyArrays(tableA)
local b = toKeyArrays(tableB)
if #a ~= #b then return false end
-- Check for simple equality between key values.
for _, k in ipairs(a) do
if tableA[k] ~= tableB[k] then return false end
end
return true
end
local function assertShallowCopyIsEqual(orig)
local copy = helpers.shallowCopy(orig)
assert(shallowEqual(orig, copy))
end
function tests.shallowCopy_nil()
assertShallowCopyIsEqual(nil)
end
function tests.shallowCopy_emptyTable()
assertShallowCopyIsEqual({})
end
function tests.shallowCopy_oneItemArray()
assertShallowCopyIsEqual({'hello'})
end
function tests.shallowCopy_flatArray()
assertShallowCopyIsEqual({'hello', 7, 'swha!?'})
end
function tests.shallowCopy_oneItemTable()
assertShallowCopyIsEqual({myKey = 'hello'})
end
function tests.shallowCopy_flatTable()
assertShallowCopyIsEqual({
['myKey'] = 'hello',
[7] = 'there',
['thinking'] = 1
})
end
function tests.shallowCopy_parentChildTable()
local leaf = {myKey = 'hello'}
assertShallowCopyIsEqual({root = leaf})
end
function tests.shallowCopy_complexKeyTable()
local key = {myKey = 'hello'}
assertShallowCopyIsEqual({[key] = 'yo'})
end
function tests.shallowCopy_bigTable()
local key = {myKey = 'hello'}
local leaf = {fancy = 'string'}
local mid = {midKey = leaf}
assertShallowCopyIsEqual({
'try',
'something',
'here',
[key] = leaf,
anotherKey = mid
})
end
local function deepEqual(tableA, tableB)
if tableA == nil and tableB == nil then return true end
-- Ensure tables don't reference the same object.
if tableA == tableB then return false end
-- Convert to arrays
local a = toKeyArrays(tableA)
local b = toKeyArrays(tableB)
if #a ~= #b then return false end
for _, k in ipairs(a) do
-- Check for simple equality between keys
local va = tableA[k]
local vb = tableB[k]
if type(va) ~= type(vb) then return false end
if type(va) == 'table' then
-- Check for deep equality between table values
if not deepEqual(va, vb) then return false end
else
-- Check for simple equality between simple values
if va ~= vb then return false end
end
end
return true
end
local function assertDeepCopyIsEqual(orig)
local copy = helpers.deepCopy(orig)
assert(deepEqual(orig, copy))
end
function tests.deepCopy_nil()
assertDeepCopyIsEqual(nil)
end
function tests.deepCopy_emptyTable()
assertDeepCopyIsEqual({})
end
function tests.deepCopy_oneItemArray()
assertDeepCopyIsEqual({'hello'})
end
function tests.deepCopy_flatArray()
assertDeepCopyIsEqual({'hello', 7, 'swha!?'})
end
function tests.deepCopy_oneItemTable()
assertDeepCopyIsEqual({myKey = 'hello'})
end
function tests.deepCopy_flatTable()
assertDeepCopyIsEqual({
['myKey'] = 'hello',
[7] = 'there',
['thinking'] = 1
})
end
function tests.deepCopy_parentChildTable()
local leaf = {myKey = 'hello'}
assertDeepCopyIsEqual({root = leaf})
end
function tests.deepCopy_complexKeyTable()
local key = {myKey = 'hello'}
assertDeepCopyIsEqual({[key] = 'yo'})
end
function tests.deepCopy_bigTable()
local key = {myKey = 'hello'}
local leaf = {fancy = 'string'}
local mid = {midKey = leaf}
assertDeepCopyIsEqual({
'try',
'something',
'here',
[key] = leaf,
anotherKey = mid
})
end
function tests.fromString_boolean()
asserts.EQ(helpers.fromString("true"), true)
asserts.EQ(helpers.fromString("false"), false)
end
function tests.fromString_nil()
asserts.EQ(helpers.fromString(nil), nil)
end
function tests.fromString_number()
asserts.EQ(helpers.fromString("0"), 0)
asserts.EQ(helpers.fromString("1"), 1)
asserts.EQ(helpers.fromString("-1"), -1)
asserts.EQ(helpers.fromString("2"), 2)
asserts.EQ(helpers.fromString("0.0000000001"), 0.0000000001)
end
function tests.fromString_string()
asserts.EQ(helpers.fromString(""), "")
asserts.EQ(helpers.fromString("random string"), "random string")
end
function tests.fromString_table()
local random_table = {random_name = 1}
asserts.EQ(helpers.fromString(random_table), random_table)
end
function tests.pathJoin()
asserts.EQ(helpers.pathJoin('base', 'path'), 'base/path')
asserts.EQ(helpers.pathJoin('base/', 'path'), 'base/path')
asserts.EQ(helpers.pathJoin('base', '/path'), '/path')
asserts.EQ(helpers.pathJoin('base/', '/path'), '/path')
end
function tests.pairsByKeys()
local keys = {'a', 'b', 'c', 'd'}
local vals = {'10', '20', '80', '40'}
local gen = random:shuffledIndexGenerator(#keys)
local dict = {}
for i = 1, #keys do
local id = gen()
dict[keys[id]] = vals[id]
end
local i = 0
for k, v in helpers.pairsByKeys(dict) do
i = i + 1
asserts.EQ(k, keys[i])
asserts.EQ(v, vals[i])
end
asserts.EQ(i, #keys)
end
function tests.pairsByKeysReversed()
local keys = {'a', 'b', 'c', 'd'}
local vals = {'10', '20', '80', '40'}
local gen = random:shuffledIndexGenerator(#keys)
local dict = {}
for i = 1, #keys do
local id = gen()
dict[keys[id]] = vals[id]
end
local i = #keys
for k, v in helpers.pairsByKeys(dict, function(a, b) return a > b end) do
asserts.EQ(k, keys[i])
asserts.EQ(v, vals[i])
i = i - 1
end
asserts.EQ(i, 0)
end
return test_runner.run(tests)