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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
local lcm = require('lcm')
-- this line required to make `require` correctly search the working directory
package.path = './?/init.lua;' .. package.path
local lcmtest = require('lcmtest')
local function info(text)
print("client: " .. text)
end
local function check_field(value, expectedvalue, name)
if value ~= expectedvalue then
error(string.format("%s does not equal %s for field %s!", tostring(value), tostring(expectedvalue), name))
end
end
local MultidimTest = {}
MultidimTest.__index = MultidimTest
function MultidimTest:new()
local obj = {}
obj.name = "multidim array test"
obj.msgtype = lcmtest.multidim_array_t
obj.num_iterations = 5
setmetatable(obj, MultidimTest)
return obj
end
function MultidimTest:make_message(iteration)
local msg = self.msgtype:new()
msg.size_a = iteration
msg.size_b = iteration
msg.size_c = iteration
local n = 0
for i = 1, msg.size_a do
msg.data[i] = {}
for j = 1, msg.size_b do
msg.data[i][j] = {}
for k = 1, msg.size_c do
msg.data[i][j][k] = n
n = n + 1
end
end
end
n = 0
for i = 1, 2 do
msg.strarray[i] = {}
for j = 1, msg.size_c do
msg.strarray[i][j] = tostring(n)
n = n + 1
end
end
return msg
end
function MultidimTest:check_reply(msg, iteration)
check_field(msg.size_a, iteration + 1, "size_a")
check_field(msg.size_b, iteration + 1, "size_b")
check_field(msg.size_c, iteration + 1, "size_c")
local n = 0
for i = 1, msg.size_a do
for j = 1, msg.size_b do
for k = 1, msg.size_c do
check_field(msg.data[i][j][k], n, string.format("data[%d][%d][%d]", i, j, k))
n = n + 1
end
end
end
n = 0
for i = 1, 2 do
for j = 1, msg.size_c do
check_field(msg.strarray[i][j], tostring(n), string.format("strarray[%d][%d]", i, j))
n = n + 1
end
end
end
local NodeTest = {}
NodeTest.__index = NodeTest
function NodeTest:new()
local obj = {}
obj.name = "node test"
obj.msgtype = lcmtest.node_t
obj.num_iterations = 7
setmetatable(obj, NodeTest)
return obj
end
function NodeTest:make_message(iteration)
local msg = self.msgtype:new()
msg.num_children = iteration
msg.children = {}
for i = 1, msg.num_children do
msg.children[i] = self:make_message(msg.num_children - 1)
end
return msg
end
function NodeTest:check_reply(msg, iteration, decendant)
if not decendant then
-- this is only used for the root of the message
check_field(msg.num_children, iteration + 1, "num_children")
else
check_field(msg.num_children, iteration, "num_children")
end
for _, childmsg in pairs(msg.children) do
self:check_reply(childmsg, msg.num_children - 1, true)
end
end
local PrimitivesListTest = {}
PrimitivesListTest.__index = PrimitivesListTest
function PrimitivesListTest:new()
local obj = {}
obj.name = "primitives list test"
obj.msgtype = lcmtest.primitives_list_t
obj.num_iterations = 100
setmetatable(obj, PrimitivesListTest)
return obj
end
function PrimitivesListTest:make_message(iteration)
local msg = self.msgtype:new()
msg.num_items = iteration
msg.items = {}
for ix = 1, msg.num_items do
local submsg = lcmtest.primitives_t:new()
-- make i start at zero
local i = ix - 1
submsg.i8 = -(i % 100)
submsg.i16 = -i * 10
submsg.i64 = -i * 10000
submsg.position[1] = -i
submsg.position[2] = -i
submsg.position[3] = -i
submsg.orientation[1] = -i
submsg.orientation[2] = -i
submsg.orientation[3] = -i
submsg.orientation[4] = -i
submsg.num_ranges = i
submsg.ranges = {}
for j = 1, submsg.num_ranges do
submsg.ranges[j] = -(j - 1)
end
-- +0 is to avoid issues with tostring(-0)
submsg.name = tostring(-i + 0)
-- because Lua evaluates 0 as true
submsg.enabled = (((i + 1) % 2) ~= 0)
msg.items[ix] = submsg
end
return msg
end
function PrimitivesListTest:check_reply(msg, iteration)
check_field(msg.num_items, iteration + 1, "num_items")
for ix = 1, msg.num_items do
local submsg = msg.items[ix]
local prefix = string.format("items[%d].", ix)
-- make i start at zero
local i = ix - 1
check_field(submsg.i8, -(i % 100), prefix .. "i8")
check_field(submsg.i16, -(i * 10), prefix .. "i16")
check_field(submsg.i64, -(i * 10000), prefix .. "i64")
check_field(submsg.position[1], -i, prefix .. "position[1]")
check_field(submsg.position[2], -i, prefix .. "position[2]")
check_field(submsg.position[3], -i, prefix .. "position[3]")
check_field(submsg.orientation[1], -i, prefix .. "orientation[1]")
check_field(submsg.orientation[2], -i, prefix .. "orientation[2]")
check_field(submsg.orientation[3], -i, prefix .. "orientation[3]")
check_field(submsg.orientation[4], -i, prefix .. "orientation[4]")
check_field(submsg.num_ranges, i, prefix .. "num_ranges")
for j = 1, submsg.num_ranges do
check_field(submsg.ranges[j], -(j - 1), prefix .. string.format("ranges[%d]", j))
end
-- +0 is to avoid issues with tostring(-0)
check_field(submsg.name, tostring(-i + 0), prefix .. "name")
-- because Lua evaluates 0 as true
check_field(submsg.enabled, (((i + 1) % 2) ~= 0), prefix .. "enabled")
end
end
local PrimitivesTest = {}
PrimitivesTest.__index = PrimitivesTest
function PrimitivesTest:new()
local obj = {}
obj.name = "primitives test"
obj.msgtype = lcmtest.primitives_t
obj.num_iterations = 1000
setmetatable(obj, PrimitivesTest)
return obj
end
function PrimitivesTest:make_message(iteration)
local msg = self.msgtype:new()
local n = iteration
msg.i8 = n % 100
msg.i16 = n * 10
msg.i64 = n * 10000
msg.position[1] = n
msg.position[2] = n
msg.position[3] = n
msg.orientation[1] = n
msg.orientation[2] = n
msg.orientation[3] = n
msg.orientation[4] = n
msg.num_ranges = n
msg.ranges = {}
for i = 1, msg.num_ranges do
msg.ranges[i] = i - 1
end
msg.name = tostring(n)
-- because Lua evaluates 0 as true
msg.enabled = ((n % 2) ~= 0)
return msg
end
function PrimitivesTest:check_reply(msg, iteration)
local n = iteration
check_field(msg.i8, (n + 1) % 100, "i8")
check_field(msg.i16, (n + 1) * 10, "i16");
check_field(msg.i64, (n + 1) * 10000, "i64");
check_field(msg.position[1], n + 1, "position[1]");
check_field(msg.position[2], n + 1, "position[2]");
check_field(msg.position[3], n + 1, "position[3]");
check_field(msg.orientation[1], n + 1, "orientation[1]");
check_field(msg.orientation[2], n + 1, "orientation[2]");
check_field(msg.orientation[3], n + 1, "orientation[3]");
check_field(msg.orientation[4], n + 1, "orientation[4]");
check_field(msg.num_ranges, n + 1, "num_ranges");
for i = 1, n + 1 do
check_field(msg.ranges[i], i - 1, string.format("ranges[%d]", i))
end
check_field(msg.name, tostring(n + 1), "name")
-- because Lua evaluates 0 as true
check_field(msg.enabled, (((n + 1) % 2) ~= 0), "enabled")
end
local StandardTester = {}
StandardTester.__index = StandardTester
function StandardTester:new(lc, test)
local obj = {}
obj.lc = lc
obj.test = test
obj.testerr = nil
obj.response_count = 0
obj.iteration = 0
setmetatable(obj, StandardTester)
return obj
end
function StandardTester:handler(channel, data)
local function process_msg()
-- this will error if something goes wrong
self.test:check_reply(self.test.msgtype.decode(data), self.iteration)
end
local function process_err(argerr)
-- set the objects error, testerr
self.testerr = argerr
end
-- discard status, run() will check testerr
xpcall(process_msg, process_err)
end
function StandardTester:run()
self.testerr = nil
self.response_count = 0
self.iteration = 0
local channel = string.format("test_lcmtest_%s", self.test.msgtype.shortname)
local reply_channel = string.format("test_lcmtest_%s_reply", self.test.msgtype.shortname)
local handler_closure = function(channel, data) self:handler(channel, data) end
local sub = self.lc:subscribe(reply_channel, handler_closure)
for i = 1, self.test.num_iterations do
-- send a message
local msg = self.test:make_message(self.iteration)
self.lc:publish(channel, msg:encode())
-- get the message, check result
local ontime = self.lc:timedhandle(500000)
if not ontime then
self.testerr = "timed out waiting for response!"
end
-- check for an error
if self.testerr then
info(string.format("%s failed on iteration %d!", self.test.name, self.iteration))
info("error: " .. tostring(self.testerr))
self.lc:unsubscribe(sub)
return false
end
self.iteration = self.iteration + 1
end
info(string.format("lcmtest_%-17s : PASSED :-)", self.test.msgtype.shortname))
self.lc:unsubscribe(sub)
return true
end
local EchoTester = {}
EchoTester.__index = EchoTester
function EchoTester:new(lc)
local obj = {}
obj.lc = lc
obj.data = nil
obj.response_count = 0
obj.num_iterations = 100
setmetatable(obj, EchoTester)
return obj
end
function EchoTester:handler(channel, data)
if data == self.data then
self.response_count = self.response_count + 1
end
end
function EchoTester:run()
local handler_closure = function(channel, data) self:handler(channel, data) end
local sub = self.lc:subscribe("TEST_ECHO_REPLY", handler_closure)
for i = 1, self.num_iterations do
local datalen = math.random(10, 10000)
local chars = {}
for j = 1, datalen do
chars[j] = string.char(math.random(0, 255))
end
self.data = table.concat(chars)
self.lc:publish("TEST_ECHO", self.data)
if not self.lc:timedhandle(500000) or self.response_count ~= i then
info(string.format("echo test failed to receive a response on iteration %d!", i))
self.lc:unsubscribe(sub)
return false
end
end
info(string.format("%-25s : PASSED :-)", "echo test"))
self.lc:unsubscribe(sub)
return true
end
local Tester = {}
Tester.__index = Tester
function Tester:new()
local obj = {}
setmetatable(obj, Tester)
return obj
end
function Tester:run()
-- make lcm connection
local lc = lcm.lcm.new()
-- run all tests
EchoTester:new(lc):run()
StandardTester:new(lc, PrimitivesTest:new()):run()
StandardTester:new(lc, PrimitivesListTest:new()):run()
StandardTester:new(lc, NodeTest:new()):run()
StandardTester:new(lc, MultidimTest:new()):run()
-- attempt to garbage collect the lcm connection
lc = nil
for i = 1, 1000 do
collectgarbage()
end
info("All tests passed.")
end
-- now to actually do something
local test = Tester:new()
test:run()
|