-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscillogram_test.lua
More file actions
210 lines (166 loc) · 5.61 KB
/
Copy pathoscillogram_test.lua
File metadata and controls
210 lines (166 loc) · 5.61 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
local luaunit = require("luaunit")
local checks = require("luatypechecks.checks")
local json = require("luaserialization.json")
local Range = require("luamath.models.range")
local Oscillogram = require("luaplot.oscillogram")
-- luacheck: globals TestOscillogram
TestOscillogram = {}
function TestOscillogram.test_from_json_success()
local plot, err = json.from_json(
[=[{
"__name": "Oscillogram",
"kind": "linear",
"points": [0.1, 0.2, 0.3],
"default": 0.5,
"range": {"__name": "Range", "min": 0, "max": 1}
}]=],
Oscillogram.schema(),
{ Range = Range.from_options, Oscillogram = Oscillogram.from_options }
)
luaunit.assert_is_table(plot)
luaunit.assert_true(checks.is_instance(plot, Oscillogram))
luaunit.assert_is_string(plot._kind)
luaunit.assert_equals(plot._kind, "linear")
luaunit.assert_is_table(plot._points)
luaunit.assert_equals(plot._points, {0.1, 0.2, 0.3})
luaunit.assert_is_number(plot._default)
luaunit.assert_equals(plot._default, 0.5)
luaunit.assert_is_table(plot._range)
luaunit.assert_true(checks.is_instance(plot._range, Range))
luaunit.assert_equals(plot._range, Range:new(0, 1))
luaunit.assert_nil(err)
end
function TestOscillogram.test_from_json_error()
local plot, err = json.from_json(
[=[{
"__name": "Oscillogram",
"kind": "linear",
"points": [0.1, "invalid", 0.3],
"default": 0.5,
"range": {"__name": "Range", "min": 0, "max": 1}
}]=],
Oscillogram.schema(),
{ Range = Range.from_options, Oscillogram = Oscillogram.from_options }
)
luaunit.assert_nil(plot)
luaunit.assert_is_string(err)
luaunit.assert_str_matches(
err,
"^invalid data: " ..
[[property "points" validation failed: ]] ..
"failed to validate item 2: " ..
"wrong type: " ..
"expected number, got string$"
)
end
function TestOscillogram.test_from_options_copies_inputs()
local options = {
kind = "custom",
points = {0.1, 0.2, 0.3},
default = 0.5,
range = Range:new(0, 1),
}
local plot = Oscillogram.from_options(options)
options.points[1] = 0.9
options.range.max = 2
luaunit.assert_equals(plot._points, {0.1, 0.2, 0.3})
luaunit.assert_equals(plot._range, Range:new(0, 1))
end
function TestOscillogram.test_new_full()
local range = Range:new(23, 42)
local plot = Oscillogram:new("random", 5, 32, range)
luaunit.assert_is_table(plot)
luaunit.assert_true(checks.is_instance(plot, Oscillogram))
luaunit.assert_is_string(plot._kind)
luaunit.assert_equals(plot._kind, "random")
luaunit.assert_is_table(plot._points)
luaunit.assert_equals(plot._points, {32, 32, 32, 32, 32})
luaunit.assert_is_number(plot._default)
luaunit.assert_equals(plot._default, 32)
luaunit.assert_is_table(plot._range)
luaunit.assert_true(checks.is_instance(plot._range, Range))
luaunit.assert_equals(plot._range, range)
end
function TestOscillogram.test_new_partial()
local plot = Oscillogram:new("random", 5)
luaunit.assert_is_table(plot)
luaunit.assert_true(checks.is_instance(plot, Oscillogram))
luaunit.assert_is_string(plot._kind)
luaunit.assert_equals(plot._kind, "random")
luaunit.assert_is_table(plot._points)
luaunit.assert_equals(plot._points, {0, 0, 0, 0, 0})
luaunit.assert_is_number(plot._default)
luaunit.assert_equals(plot._default, 0)
luaunit.assert_is_table(plot._range)
luaunit.assert_true(checks.is_instance(plot._range, Range))
luaunit.assert_equals(plot._range, Range:new(0, 1))
end
function TestOscillogram.test_new_copies_inputs()
local range = Range:new(23, 42)
local plot = Oscillogram:new("random", 5, 32, range)
range.max = 50
luaunit.assert_equals(plot._range, Range:new(23, 42))
end
function TestOscillogram.test_tostring()
local plot = Oscillogram:new("random", 5, 32, Range:new(23, 42))
local text = tostring(plot)
luaunit.assert_is_string(text)
luaunit.assert_equals(text, "{" ..
"__name = \"Oscillogram\"," ..
"default = 32," ..
"kind = \"random\"," ..
"points = { 32, 32, 32, 32, 32 }," ..
"range = {" ..
"__name = \"Range\"," ..
"max = 42," ..
"min = 23" ..
"}" ..
"}")
end
function TestOscillogram.test_update_custom()
local plot = Oscillogram:new("custom", 0, 0.5)
for i = 1, 5 do
plot:push(i / 10)
end
local first_point = plot:update(0.2)
luaunit.assert_is_table(plot._points)
luaunit.assert_equals(plot._points, {0.2, 0.3, 0.4, 0.5, 0.2})
luaunit.assert_is_number(first_point)
luaunit.assert_equals(first_point, 0.1)
end
function TestOscillogram.test_update_linear()
local plot = Oscillogram:new("linear", 0, 0.5)
for i = 1, 5 do
plot:push(i / 10)
end
local first_point = plot:update(0.2)
luaunit.assert_is_table(plot._points)
luaunit.assert_equals(plot._points, {0.2, 0.3, 0.4, 0.5, 0.7})
luaunit.assert_is_number(first_point)
luaunit.assert_equals(first_point, 0.1)
end
function TestOscillogram.test_update_random()
math.randomseed(1)
local plot = Oscillogram:new("random", 0, 0.5)
for i = 1, 5 do
plot:push(i / 10)
end
local first_point = plot:update(0.2)
local last_point
if _VERSION == "Lua 5.5" or _VERSION == "Lua 5.4" then
last_point = 0.626235
elseif _VERSION == "Lua 5.3" or _VERSION == "Lua 5.2" then
last_point = 0.457753
elseif _VERSION == "Lua 5.1" then
if checks.is_table(jit) then -- check for LuaJIT
last_point = 0.429524
else
last_point = 0.636075
end
end
luaunit.assert_is_table(plot._points)
luaunit.assert_equals(#plot._points, 5)
luaunit.assert_almost_equals(plot._points[5], last_point, 1e-6)
luaunit.assert_is_number(first_point)
luaunit.assert_equals(first_point, 0.1)
end