forked from wilson1yan/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint_and_click_test.lua
More file actions
174 lines (153 loc) · 5.67 KB
/
point_and_click_test.lua
File metadata and controls
174 lines (153 loc) · 5.67 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
--[[ Copyright (C) 2018-2019 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
]]
local asserts = require 'testing.asserts'
local test_runner = require 'testing.test_runner'
local tensor = require 'dmlab.system.tensor'
local point_and_click = require 'factories.psychlab.point_and_click'
local tests = {}
local function environment(opts)
return {
_inits = 0,
_steps = 0,
_resets = 0,
init = function(self, opts) self._inits = self._inits + 1 end,
step = function(self, opts) self._steps = self._steps + 1 end,
reset = function(self, opts) self._resets = self._resets + 1 end,
}
end
function tests.addWidgetAbsolute()
local SIZE = 8
local pac = point_and_click{
environment = environment,
screenSize = {height = SIZE, width = SIZE},
}
pac:reset(0, 0)
local lookCenter = {{0, 0}, {0.5, 0.5}}
pac:step(lookCenter)
local relativeMousePos = {}
for i = 1, SIZE do
local size = (SIZE - i + 1)
pac:addWidget{
name = 'GREY_' .. i,
posAbs = {i - 1, i - 1},
sizeAbs = {size, size},
image = tensor.ByteTensor(size, size, 3):fill(i),
imageLayer = i,
mouseHoverCallback = function(self, name, mousePos, hoverTime, userData)
userData[name] = {mousePos[1], mousePos[2]}
end,
userData = relativeMousePos,
}
end
local surface, reward, pcontinue, wasDirty = pac:step(lookCenter)
local result = tensor.ByteTensor(SIZE, SIZE, 4)
result:applyIndexed(function(val, index)
local y, x, c = unpack(index)
return c < 4 and (x < y and x or y) or 255
end)
asserts.EQ(result, surface, 'Invalid surface')
asserts.EQ(reward, 0, 'Invalid reward')
asserts.EQ(pcontinue, 1, 'Invalid pcontinue')
asserts.EQ(wasDirty, true, 'Invalid wasDirty')
asserts.tablesEQ(relativeMousePos.GREY_1, {0.5, 0.5}, 'Bad widget position!')
local surface, reward, pcontinue, wasDirty = pac:step{{0, 0}, {0.1, 0.1}}
asserts.tablesEQ(relativeMousePos.GREY_1, {0.1, 0.1}, 'Bad widget position!')
asserts.EQ(wasDirty, false, 'Invalid wasDirty')
end
function tests.addWidgetsRelative()
local SIZE = 7
local pac = point_and_click{
environment = environment,
screenSize = {height = SIZE, width = SIZE},
}
pac:reset(0, 0)
local relativeMousePos = {}
for i = 1, SIZE do
local p = (i - 1) / SIZE
local size = (SIZE - i + 1)
pac:addWidget{
name = 'GREY_' .. i,
pos = {p, p},
size = {size / SIZE, size / SIZE},
image = tensor.ByteTensor(size, size, 3):fill(i),
imageLayer = i,
mouseHoverCallback = function(self, name, mousePos, hoverTime, userData)
userData[name] = {mousePos[1], mousePos[2]}
end,
userData = relativeMousePos,
}
end
local surface, reward, pcontinue, wasDirty = pac:step{{0, 0}, {0.5, 0.5}}
local result = tensor.ByteTensor(SIZE, SIZE, 4)
result:applyIndexed(function(val, index)
local y, x, c = unpack(index)
return c < 4 and (x < y and x or y) or 255
end)
asserts.EQ(result, surface, 'Invalid surface')
asserts.EQ(reward, 0, 'Invalid reward')
asserts.EQ(pcontinue, 1, 'Invalid pcontinue')
asserts.EQ(wasDirty, true, 'Invalid wasDirty')
asserts.tablesEQ(relativeMousePos.GREY_1, {0.5, 0.5}, 'Bad widget position!')
local surface, reward, pcontinue, wasDirty = pac:step{{0, 0}, {0.1, 0.1}}
asserts.tablesEQ(relativeMousePos.GREY_1, {0.1, 0.1}, 'Bad widget position!')
asserts.EQ(wasDirty, false, 'Invalid wasDirty')
end
function tests.addWidgetsClipped()
local SIZE = 10
local pac = point_and_click{
environment = environment,
screenSize = {height = SIZE, width = SIZE},
}
pac:reset(0, 0)
local relativeMousePos = {}
local positions = {{0, 0}, {1, 0}, {0, 1}, {1, 1}}
for i, p in ipairs(positions) do
local layer = i
pac:addWidget{
name = 'GREY_' .. layer,
pos = p,
posAbs = {-5, -5},
sizeAbs = {10, 10},
image = tensor.ByteTensor(10, 10, 3):fill(layer),
imageLayer = layer,
mouseHoverCallback = function(self, name, mousePos, hoverTime, userData)
userData[name] = {mousePos[1], mousePos[2]}
end,
userData = relativeMousePos,
}
end
local surface, reward, pcontinue, wasDirty = pac:step{{0, 0}, {0.5, 0.5}}
local result = tensor.ByteTensor(SIZE, SIZE, 4)
result:applyIndexed(function(val, index)
local y, x, c = unpack(index)
if c == 4 then return 255 end
if x <= 5 and y <= 5 then return 1 end
if x > 5 and y <= 5 then return 2 end
if x <= 5 and y > 5 then return 3 end
if x > 5 and y > 5 then return 4 end
end)
asserts.EQ(result, surface, 'Invalid surface')
asserts.EQ(reward, 0, 'Invalid reward')
asserts.EQ(pcontinue, 1, 'Invalid pcontinue')
asserts.EQ(wasDirty, true, 'Invalid wasDirty')
for i, p in ipairs(positions) do
for k in pairs(relativeMousePos) do
relativeMousePos[k] = nil
end
pac:step{{0, 0}, p}
asserts.tablesEQ(relativeMousePos['GREY_' .. i], {0.5, 0.5},
'Bad widget position! - GREY_' .. i)
end
end
return test_runner.run(tests)