-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoverloads_spec.lua
312 lines (252 loc) · 7.75 KB
/
overloads_spec.lua
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
local vips = require "vips"
-- test all operator overloads
-- make a table of x repeated n times
local function replicate(x, n)
local result = {}
for i = 1, n do
result[i] = x
end
return result
end
-- apply an operation to a nested table, or to a number
local function map(op, a)
local result
if type(a) == "table" then
result = {}
for i = 1, #a do
result[i] = map(op, a[i])
end
else
result = op(a)
end
return result
end
-- apply an operation pairwise to two nested tables, or to a table and a number,
-- or to two numbers
local function map2(op, a, b)
if type(a) == "table" and type(b) == "table" then
assert.are.equal(#a, #b)
end
local result
if type(a) == "table" or type(b) == "table" then
if type(a) ~= "table" then
a = replicate(a, #b)
end
if type(b) ~= "table" then
b = replicate(b, #a)
end
result = {}
for i = 1, #a do
result[i] = map2(op, a[i], b[i])
end
else
result = op(a, b)
end
return result
end
-- find the sum and number of elements in a nested table
local function sum(a)
local total = 0
local n = 0
if type(a) == "table" then
for i = 1, #a do
local new_total
local new_n
new_total, new_n = sum(a[i])
total = total + new_total
n = n + new_n
end
else
total = a
n = 1
end
return total, n
end
-- find the average of a nested table
local function avg(a)
local total
local n
total, n = sum(a)
return total / n
end
local function test_binary(name, vop, lop)
local array, im
setup(function()
array = { 1, 2, 3, 4 }
im = vips.Image.new_from_array(array)
end)
describe(name, function()
it("can " .. name .. " image and single constant", function()
local im2 = vop(im, 12)
local a2 = map2(lop, array, 12)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 1)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
it("can " .. name .. " image and single constant, reversed", function()
local im2 = vop(12, im)
local a2 = map2(lop, 12, array)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 1)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
it("can " .. name .. " an image and an array", function()
local array_constant = { 12, 13, 14 }
local im2 = vop(im, array_constant)
local a2 = map2(lop, array, replicate(array_constant, #array))
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 3)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
it("can " .. name .. " an image and an array, reversed", function()
local array_constant = { 12, 13, 14 }
local im2 = vop(array_constant, im)
local a2 = map2(lop, replicate(array_constant, #array), array)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 3)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
it("can " .. name .. " two images", function()
local im2 = vop(im, im)
local a2 = map2(lop, array, array)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 1)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
end)
end
local function test_binary_noreverse(name, vop, lop)
local array, im
setup(function()
array = { 1, 2, 3, 4 }
im = vips.Image.new_from_array(array)
end)
describe(name, function()
it("can " .. name .. " image and single constant", function()
local im2 = vop(im, 12)
local a2 = map2(lop, array, 12)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 1)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
it("can " .. name .. " an image and an array", function()
local im2 = vop(im, { 12, 13, 14 })
local a2 = map2(lop, array, 13)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 3)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
it("can " .. name .. " two images", function()
local im2 = vop(im, im)
local a2 = map2(lop, array, array)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 1)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
end)
end
local function test_unary(name, vop, lop)
local array, im
setup(function()
array = { 1, 2, 3, 4 }
im = vips.Image.new_from_array(array)
end)
describe(name, function()
it("can " .. name .. " an image", function()
local im2 = vop(im)
local a2 = map(lop, array)
assert.are.equal(im2:width(), 4)
assert.are.equal(im2:height(), 1)
assert.are.equal(im2:bands(), 1)
assert.are.almost_equal(im2:avg(), avg(a2))
end)
end)
end
describe("test overload", function()
test_binary("add",
function(a, b)
return vips.Image.mt.__add(a, b)
end,
function(a, b)
return a + b
end)
test_binary("sub",
function(a, b)
return vips.Image.mt.__sub(a, b)
end,
function(a, b)
return a - b
end)
test_binary("mul",
function(a, b)
return vips.Image.mt.__mul(a, b)
end,
function(a, b)
return a * b
end)
test_binary("div",
function(a, b)
return vips.Image.mt.__div(a, b)
end,
function(a, b)
return a / b
end)
test_binary_noreverse("mod",
function(a, b)
return vips.Image.mt.__mod(a, b)
end,
function(a, b)
return a % b
end)
test_binary("pow",
function(a, b)
return vips.Image.mt.__pow(a, b)
end,
function(a, b)
return a ^ b
end)
test_unary("unm",
function(a)
return vips.Image.mt.__unm(a)
end,
function(a)
return -a
end)
describe("band overloads", function()
local array, im, im2
setup(function()
array = { 1, 2, 3, 4 }
im = vips.Image.new_from_array(array)
im2 = im:bandjoin({ im + 1, im + 2 })
end)
it("can bandjoin with '..'", function()
local b = im .. im2
assert.are.equal(b:width(), 4)
assert.are.equal(b:height(), 1)
assert.are.equal(b:bands(), 4)
assert.are.equal(b:extract_band(0):avg(), 2.5)
end)
end)
describe("call overload", function()
local array, im, im2
setup(function()
array = { 1, 2, 3, 4 }
im = vips.Image.new_from_array(array)
im2 = im:bandjoin({ im + 1, im + 2 })
end)
it("can extract a pixel with '()'", function()
local a, b, c = im2(1, 0)
assert.are.equal(a, 2)
assert.are.equal(b, 3)
assert.are.equal(c, 4)
end)
end)
end)