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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
|
-- Lua 5.2 compatibility
local unpack = unpack or table.unpack
local check = {} -- helper functions, defined at the bottom of the file
local Tester = torch.class('torch.Tester')
function Tester:__init()
self.errors = {}
self.tests = {}
self.warnings = {}
self._warningCount = {}
self.disabledTests = {}
self._currentTestName = ''
-- To maintain backwards compatibility (at least for a short while),
-- disable exact dimension checking of tensors when :assertTensorEq is
-- called. Thus {{1}} == {1} when this flag is true.
--
-- Note that other methods that suppose tensor checking (such as
-- :assertGeneralEq) ignore this flag, since previously they didn't
-- exist or support tensor equality checks at all, so there is no
-- old code that uses these functions and relies on the behaviour.
--
-- Note also that if the dimension check fails with this flag is true, then
-- will show a warning.
self._assertTensorEqIgnoresDims = true
end
function Tester:setEarlyAbort(earlyAbort)
self.earlyAbort = earlyAbort
end
function Tester:setRethrowErrors(rethrow)
self.rethrow = rethrow
end
function Tester:setSummaryOnly(summaryOnly)
self.summaryOnly = summaryOnly
end
-- Add a success to the test.
function Tester:_success()
local name = self._currentTestName
self.assertionPass[name] = self.assertionPass[name] + 1
return true
end
function Tester:_addDebugInfo(message)
local ss = debug.traceback('tester', 3) or ''
ss = ss:match('.-\n([^\n]+\n[^\n]+)\n[^\n]+xpcall') or ''
local name = self._currentTestName
return (name ~= '' and name .. '\n' or '') .. message .. '\n' .. ss
end
-- Add a failure to the test.
function Tester:_failure(message)
if self.rethrow then error(message, 2) end
local name = self._currentTestName
self.assertionFail[name] = self.assertionFail[name] + 1
self.errors[#self.errors + 1] = self:_addDebugInfo(message)
return false
end
-- Add a warning to the test
function Tester:_warning(message)
local name = self._currentTestName
self._warningCount[name] = (self._warningCount[name] or 0) + 1
self.warnings[#self.warnings + 1] = self:_addDebugInfo(message)
end
-- Call this during a test run with `condition = true` to log a success, or with
-- `condition = false` to log a failure (using `message`).
function Tester:_assert_sub(condition, message)
if condition then
return self:_success()
else
return self:_failure(message)
end
end
local function getMessage(message, ...)
assert(next{...} == nil, "Unexpected arguments passed to test function")
if message then
assert(type(message) == 'string', 'message parameter must be a string')
if message ~= '' then
return message .. '\n'
end
end
return ''
end
--[[ Historically, some test functions have accepted both a message and a
tolerance, and some just a message (e.g., assertTableEq). Now assertTableEq
accepts both a tolerance and a message, so allow the two arguments to be passed
in either order to maintain backwards compatibility (and more generally,
for convenience). (We still document the ordering as "tolerance, message" for
clarity.) This function also sanitizes them (ensures they are non-nil, etc).
]]
local function getToleranceAndMessage(defaultTolerance, ...)
local args = {...}
local message = nil
local tolerance = nil
for _, a in ipairs(args) do
if type(a) == 'string' then
if message then
error("Unexpected string argument; already have message", a)
end
message = a .. '\n'
elseif type(a) == 'number' then
if tolerance then
error("Unexpected number argument; already have tolerance", a)
end
tolerance = a
assert(tolerance >= 0, "tolerance cannot be negative")
else
error("Unrecognized argument; should be a tolerance or message", a)
end
end
message = message or ''
tolerance = tolerance or defaultTolerance
return tolerance, message
end
function Tester:assert(condition, ...)
local message = getMessage(...)
if type(condition) ~= 'boolean' then
self:_warning(" :assert should only be used for boolean conditions. "
.. "To check for non-nil variables, do this explicitly: "
.. "Tester:assert(var ~= nil).")
end
return self:_assert_sub(condition,
string.format('%sBOOL violation condition=%s',
message, tostring(condition)))
end
function Tester:assertGeneralEq(got, expected, ...)
return self:_eqOrNeq(got, expected, false, ...)
end
function Tester:eq(got, expected, ...)
return self:assertGeneralEq(got, expected, ...)
end
function Tester:assertGeneralNe(got, unexpected, ...)
return self:_eqOrNeq(got, unexpected, true, ...)
end
function Tester:ne(got, unexpected, ...)
return self:assertGeneralNe(got, unexpected, ...)
end
function Tester:_eqOrNeq(got, expected, negate, ...)
local tolerance, message = getToleranceAndMessage(0, ...)
local success, subMessage = check.areEq(got, expected, tolerance, negate)
subMessage = subMessage or ''
return self:_assert_sub(success, message .. subMessage)
end
function Tester:assertlt(a, b, ...)
local message = getMessage(...)
return self:_assert_sub(a < b,
string.format('%sLT failed: %s >= %s',
message, tostring(a), tostring(b)))
end
function Tester:assertgt(a, b, ...)
local message = getMessage(...)
return self:_assert_sub(a > b,
string.format('%sGT failed: %s <= %s',
message, tostring(a), tostring(b)))
end
function Tester:assertle(a, b, ...)
local message = getMessage(...)
return self:_assert_sub(a <= b,
string.format('%sLE failed: %s > %s',
message, tostring(a), tostring(b)))
end
function Tester:assertge(a, b, ...)
local message = getMessage(...)
return self:_assert_sub(a >= b,
string.format('%sGE failed: %s < %s',
message, tostring(a), tostring(b)))
end
function Tester:assertalmosteq(a, b, ...)
local tolerance, message = getToleranceAndMessage(1e-16, ...)
local diff = math.abs(a - b)
return self:_assert_sub(
diff <= tolerance,
string.format(
'%sALMOST_EQ failed: %s ~= %s with tolerance=%s',
message, tostring(a), tostring(b), tostring(tolerance)))
end
function Tester:asserteq(a, b, ...)
local message = getMessage(...)
return self:_assert_sub(a == b,
string.format('%sEQ failed: %s ~= %s',
message, tostring(a), tostring(b)))
end
function Tester:assertne(a, b, ...)
local message = getMessage(...)
if type(a) == type(b) and type(a) == 'table' or type(a) == 'userdata' then
self:_warning(" :assertne should only be used to compare basic lua "
.. "objects (numbers, booleans, etc). Consider using "
.. "either :assertGeneralNe or :assert(a ~= b).")
end
return self:_assert_sub(a ~= b,
string.format('%sNE failed: %s == %s',
message, tostring(a), tostring(b)))
end
function Tester:assertTensorEq(ta, tb, ...)
return self:_assertTensorEqOrNeq(ta, tb, false, ...)
end
function Tester:assertTensorNe(ta, tb, ...)
return self:_assertTensorEqOrNeq(ta, tb, true, ...)
end
function Tester:_assertTensorEqOrNeq(ta, tb, negate, ...)
assert(torch.isTensor(ta), "First argument should be a Tensor")
assert(torch.isTensor(tb), "Second argument should be a Tensor")
local tolerance, message = getToleranceAndMessage(0, ...)
local success, subMessage =
check.areTensorsEq(ta, tb, tolerance, negate,
self._assertTensorEqIgnoresDims)
subMessage = subMessage or ''
if self._assertTensorEqIgnoresDims and (not negate) and success
and not ta:isSameSizeAs(tb) then
self:_warning("Tensors have the same content but different dimensions. "
.. "For backwards compatibility, they are considered equal, "
.. "but this may change in the future. Consider using :eq "
.. "to check for equality instead.")
end
return self:_assert_sub(success, message .. subMessage)
end
function Tester:assertTableEq(ta, tb, ...)
return self:_assertTableEqOrNeq(ta, tb, false, ...)
end
function Tester:assertTableNe(ta, tb, ...)
return self:_assertTableEqOrNeq(ta, tb, true, ...)
end
function Tester:_assertTableEqOrNeq(ta, tb, negate, ...)
assert(type(ta) == 'table', "First argument should be a Table")
assert(type(tb) == 'table', "Second argument should be a Table")
return self:_eqOrNeq(ta, tb, negate, ...)
end
function Tester:assertError(f, ...)
return self:assertErrorObj(f, function() return true end, ...)
end
function Tester:assertNoError(f, ...)
local message = getMessage(...)
local status, err = pcall(f)
return self:_assert_sub(status,
string.format('%sERROR violation: err=%s', message,
tostring(err)))
end
function Tester:assertErrorMsg(f, errmsg, ...)
return self:assertErrorObj(f, function(err) return err == errmsg end, ...)
end
function Tester:assertErrorPattern(f, errPattern, ...)
local function errcomp(err)
return string.find(err, errPattern) ~= nil
end
return self:assertErrorObj(f, errcomp, ...)
end
function Tester:assertErrorObj(f, errcomp, ...)
local message = getMessage(...)
local status, err = pcall(f)
return self:_assert_sub((not status) and errcomp(err),
string.format('%sERROR violation: err=%s', message,
tostring(err)))
end
function Tester:add(f, name)
if type(f) == "table" then
assert(name == nil, "Name parameter is forbidden for a table of tests, "
.. "since its use is ambiguous")
if f.__isTestSuite then
f = f.__tests
else
self:_warning("Should use TestSuite rather than plain lua table")
end
for i, v in pairs(f) do
-- We forbid nested tests because the "expected" behaviour when a named
-- test is run in the case that the named test is in fact a table of
-- tests is not supported. Similar issue with _setUp and _tearDown
-- functions inside nested tests.
assert(type(v) ~= 'table', "Nested sets of tests are not supported")
self:add(v, i)
end
return self
end
assert(type(f) == 'function',
"Only tables of functions and functions supported")
if name == '_setUp' then
assert(not self._setUp, "Only one set-up function allowed")
self._setUp = f
elseif name == '_tearDown' then
assert(not self._tearDown, "Only one tear-down function allowed")
self._tearDown = f
else
name = name or 'unknown'
if self.tests[name] ~= nil then
error('Test with name ' .. name .. ' already exists!')
end
self.tests[name] = f
end
return self
end
function Tester:disable(testNames)
if type(testNames) == 'string' then
testNames = {testNames}
end
assert(type(testNames) == 'table', "Expecting name or list for disable")
for _, name in ipairs(testNames) do
assert(self.tests[name], "Unrecognized test '" .. name .. "'")
self.disabledTests[name] = true
end
return self
end
function Tester:run(testNames)
local tests = self:_getTests(testNames)
self.assertionPass = {}
self.assertionFail = {}
self.haveWarning = {}
self.testError = {}
for name in pairs(tests) do
self.assertionPass[name] = 0
self.assertionFail[name] = 0
self.testError[name] = 0
self._warningCount[name] = 0
end
self:_run(tests)
self:_report(tests)
-- Throws an error on test failure/error, so that test script returns
-- with nonzero return value.
for name in pairs(tests) do
assert(self.assertionFail[name] == 0,
'An error was found while running tests!')
assert(self.testError[name] == 0,
'An error was found while running tests!')
end
return 0
end
local function pluralize(num, str)
local stem = num .. ' ' .. str
if num == 1 then
return stem
else
return stem .. 's'
end
end
local NCOLS = 80
local coloured
local enable_colors, c = pcall(require, 'sys.colors')
if arg and enable_colors then -- have we been invoked from the commandline?
coloured = function(str, colour)
return colour .. str .. c.none
end
else
c = {}
coloured = function(str)
return str
end
end
function Tester:_run(tests)
local ntests = 0
for _ in pairs(tests) do
ntests = ntests + 1
end
local ntestsAsString = string.format('%u', ntests)
local cfmt = string.format('%%%uu/%u ', ntestsAsString:len(), ntestsAsString)
local cfmtlen = ntestsAsString:len() * 2 + 2
local function bracket(str)
return '[' .. str .. ']'
end
io.write('Running ' .. pluralize(ntests, 'test') .. '\n')
local i = 1
for name, fn in pairs(tests) do
self._currentTestName = name
-- TODO: compute max length of name and cut it down to size if needed
local strinit = coloured(string.format(cfmt, i), c.cyan)
.. self._currentTestName .. ' '
.. string.rep('.',
NCOLS - 6 - 2 -
cfmtlen - self._currentTestName:len())
.. ' '
io.write(strinit .. bracket(coloured('WAIT', c.cyan)))
io.flush()
local status, message, pass, skip
if self.disabledTests[name] then
skip = true
else
skip = false
if self._setUp then
self._setUp(name)
end
if self.rethrow then
status = true
local nerr = #self.errors
message = fn()
pass = nerr == #self.errors
else
status, message, pass = self:_pcall(fn)
end
if self._tearDown then
self._tearDown(name)
end
end
io.write('\r')
io.write(strinit)
if skip then
io.write(bracket(coloured('SKIP', c.yellow)))
elseif not status then
self.testError[name] = 1
io.write(bracket(coloured('ERROR', c.magenta)))
elseif not pass then
io.write(bracket(coloured('FAIL', c.red)))
else
io.write(bracket(coloured('PASS', c.green)))
if self._warningCount[name] > 0 then
io.write('\n' .. string.rep(' ', NCOLS - 10))
io.write(bracket(coloured('+warning', c.yellow)))
end
end
io.write('\n')
io.flush()
if self.earlyAbort and (i < ntests) and (not status or not pass)
and (not skip) then
io.write('Aborting on first error, not all tests have been executed\n')
break
end
i = i + 1
collectgarbage()
end
end
function Tester:_pcall(f)
local nerr = #self.errors
local stat, result = xpcall(f, debug.traceback)
if not stat then
self.errors[#self.errors + 1] =
self._currentTestName .. '\n Function call failed\n' .. result .. '\n'
end
return stat, result, stat and (nerr == #self.errors)
end
function Tester:_getTests(testNames)
if testNames == nil then
return self.tests
end
if type(testNames) == 'string' then
testNames = {testNames}
end
assert(type(testNames) == 'table',
"Only accept a name or table of test names (or nil for all tests)")
local function getMatchingNames(pattern)
local matchingNames = {}
for name in pairs(self.tests) do
if string.match(name, pattern) then
table.insert(matchingNames, name)
end
end
return matchingNames
end
local tests = {}
for _, pattern in ipairs(testNames) do
local matchingNames = getMatchingNames(pattern)
assert(#matchingNames > 0, "Couldn't find test '" .. pattern .. "'")
for _, name in ipairs(matchingNames) do
tests[name] = self.tests[name]
end
end
return tests
end
function Tester:_report(tests)
local ntests = 0
local nfailures = 0
local nerrors = 0
local nskipped = 0
local nwarnings = 0
self.countasserts = 0
for name in pairs(tests) do
ntests = ntests + 1
self.countasserts = self.countasserts + self.assertionFail[name]
+ self.assertionPass[name]
if self.assertionFail[name] > 0 then
nfailures = nfailures + 1
end
if self.testError[name] > 0 then
nerrors = nerrors + 1
end
if self._warningCount[name] > 0 then
nwarnings = nwarnings + 1
end
if self.disabledTests[name] then
nskipped = nskipped + 1
end
end
if self._warningCount[''] then
nwarnings = nwarnings + self._warningCount['']
end
io.write('Completed ' .. pluralize(self.countasserts, 'assert'))
io.write(' in ' .. pluralize(ntests, 'test') .. ' with ')
io.write(coloured(pluralize(nfailures, 'failure'),
nfailures == 0 and c.green or c.red))
io.write(' and ')
io.write(coloured(pluralize(nerrors, 'error'),
nerrors == 0 and c.green or c.magenta))
if nwarnings > 0 then
io.write(' and ')
io.write(coloured(pluralize(nwarnings, 'warning'), c.yellow))
end
if nskipped > 0 then
io.write(' and ')
io.write(coloured(nskipped .. ' disabled', c.yellow))
end
io.write('\n')
-- Prints off a message separated by -----
local haveSection = false
local function addSection(text)
local function printDashes()
io.write(string.rep('-', NCOLS) .. '\n')
end
if not haveSection then
printDashes()
haveSection = true
end
io.write(text .. '\n')
printDashes()
end
if not self.summaryOnly then
for _, v in ipairs(self.errors) do
addSection(v)
end
for _, v in ipairs(self.warnings) do
addSection(v)
end
end
end
--[[ Tests for tensor equality between two tensors of matching sizes and types.
Tests whether the maximum element-wise difference between `ta` and `tb` is less
than or equal to `tolerance`.
Arguments:
* `ta` (tensor)
* `tb` (tensor)
* `tolerance` (number) maximum elementwise difference between `ta` and `tb`.
* `negate` (boolean) if true, we invert success and failure.
* `storage` (boolean) if true, we print an error message referring to Storages
rather than Tensors.
Returns:
1. success, boolean that indicates success
2. failure_message, string or nil
]]
function check.areSameFormatTensorsEq(ta, tb, tolerance, negate, storage)
local function ensureHasAbs(t)
-- Byte, Char and Short Tensors don't have abs
return t.abs and t or t:double()
end
ta = ensureHasAbs(ta)
tb = ensureHasAbs(tb)
local diff = ta:clone():add(-1, tb):abs()
local err = diff:max()
local success = err <= tolerance
if negate then
success = not success
end
local errMessage
if not success then
local prefix = storage and 'Storage' or 'Tensor'
local violation = negate and 'NE(==)' or 'EQ(==)'
errMessage = string.format('%s%s violation: max diff=%s, tolerance=%s',
prefix,
violation,
tostring(err),
tostring(tolerance))
end
return success, errMessage
end
--[[ Tests for tensor equality.
Tests whether the maximum element-wise difference between `ta` and `tb` is less
than or equal to `tolerance`.
Arguments:
* `ta` (tensor)
* `tb` (tensor)
* `tolerance` (number) maximum elementwise difference between `ta` and `tb`.
* `negate` (boolean) if negate is true, we invert success and failure.
* `ignoreTensorDims` (boolean, default false) if true, then tensors of the same
size but different dimensions can still be considered equal, e.g.,
{{1}} == {1}. For backwards compatibility.
Returns:
1. success, boolean that indicates success
2. failure_message, string or nil
]]
function check.areTensorsEq(ta, tb, tolerance, negate, ignoreTensorDims)
ignoreTensorDims = ignoreTensorDims or false
if not ignoreTensorDims and ta:dim() ~= tb:dim() then
return negate, 'The tensors have different dimensions'
end
if ta:type() ~= tb:type() then
return negate, 'The tensors have different types'
end
-- If we are comparing two empty tensors, return true.
-- This is needed because some functions below cannot be applied to tensors
-- of dimension 0.
if ta:dim() == 0 and tb:dim() == 0 then
return not negate, 'Both tensors are empty'
end
local sameSize
if ignoreTensorDims then
sameSize = ta:nElement() == tb:nElement()
else
sameSize = ta:isSameSizeAs(tb)
end
if not sameSize then
return negate, 'The tensors have different sizes'
end
return check.areSameFormatTensorsEq(ta, tb, tolerance, negate, false)
end
local typesMatching = {
['torch.ByteStorage'] = torch.ByteTensor,
['torch.CharStorage'] = torch.CharTensor,
['torch.ShortStorage'] = torch.ShortTensor,
['torch.IntStorage'] = torch.IntTensor,
['torch.LongStorage'] = torch.LongTensor,
['torch.FloatStorage'] = torch.FloatTensor,
['torch.DoubleStorage'] = torch.DoubleTensor,
['torch.HalfStorage'] = torch.HalfTensor,
}
--[[ Tests for storage equality.
Tests whether the maximum element-wise difference between `sa` and `sb` is less
than or equal to `tolerance`.
Arguments:
* `sa` (storage)
* `sb` (storage)
* `tolerance` (number) maximum elementwise difference between `a` and `b`.
* `negate` (boolean) if negate is true, we invert success and failure.
Returns:
1. success, boolean that indicates success
2. failure_message, string or nil
]]
function check.areStoragesEq(sa, sb, tolerance, negate)
if sa:size() ~= sb:size() then
return negate, 'The storages have different sizes'
end
local typeOfsa = torch.type(sa)
local typeOfsb = torch.type(sb)
if typeOfsa ~= typeOfsb then
return negate, 'The storages have different types'
end
local ta = typesMatching[typeOfsa](sa)
local tb = typesMatching[typeOfsb](sb)
return check.areSameFormatTensorsEq(ta, tb, tolerance, negate, true)
end
--[[ Tests for general (deep) equality.
The types of `got` and `expected` must match.
Tables are compared recursively. Keys and types of the associated values must
match, recursively. Numbers are compared with the given tolerance.
Torch tensors and storages are compared with the given tolerance on their
elementwise difference. Other types are compared for strict equality with the
regular Lua == operator.
Arguments:
* `got`
* `expected`
* `tolerance` (number) maximum elementwise difference between `a` and `b`.
* `negate` (boolean) if negate is true, we invert success and failure.
Returns:
1. success, boolean that indicates success
2. failure_message, string or nil
]]
function check.areEq(got, expected, tolerance, negate)
local errMessage
if type(got) ~= type(expected) then
if not negate then
errMessage = 'EQ failed: values have different types (first: '
.. type(got) .. ', second: ' .. type(expected) .. ')'
end
return negate, errMessage
elseif type(got) == 'number' then
local diff = math.abs(got - expected)
local ok = (diff <= tolerance)
if negate then
ok = not ok
end
if not ok then
if negate then
errMessage = string.format("NE failed: %s == %s",
tostring(got), tostring(expected))
else
errMessage = string.format("EQ failed: %s ~= %s",
tostring(got), tostring(expected))
end
if tolerance > 0 then
errMessage = errMessage .. " with tolerance=" .. tostring(tolerance)
end
end
return ok, errMessage
elseif type(expected) == "table" then
return check.areTablesEq(got, expected, tolerance, negate)
elseif torch.isTensor(got) then
return check.areTensorsEq(got, expected, tolerance, negate)
elseif torch.isStorage(got) then
return check.areStoragesEq(got, expected, tolerance, negate)
else
-- Below: we have the same type which is either userdata or a lua type
-- which is not a number.
local ok = (got == expected)
if negate then
ok = not ok
end
if not ok then
if negate then
errMessage = string.format("NE failed: %s (%s) == %s (%s)",
tostring(got), type(got),
tostring(expected), type(expected))
else
errMessage = string.format("EQ failed: %s (%s) ~= %s (%s)",
tostring(got), type(got),
tostring(expected), type(expected))
end
end
return ok, errMessage
end
end
--[[ Tests for (deep) table equality.
Tables are compared recursively. Keys and types of the associated values must
match, recursively. Numbers are compared with the given tolerance.
Torch tensors and storages are compared with the given tolerance on their
elementwise difference. Other types are compared for strict equality with the
regular Lua == operator.
Arguments:
* `t1` (table)
* `t2` (table)
* `tolerance` (number) maximum elementwise difference between `a` and `b`.
* `negate` (boolean) if negate is true, we invert success and failure.
Returns:
1. success, boolean that indicates success
2. failure_message, string or nil
]]
function check.areTablesEq(t1, t2, tolerance, negate)
-- Implementation detail: Instead of doing a depth-first table comparison
-- check (for example, using recursion), let's do a breadth-first search
-- using a queue. Why? Because if we have two tables that are quite deep
-- (e.g., a gModule from nngraph), then if they are different then it's
-- more useful to the user to show how they differ at as-shallow-a-depth
-- as possible.
local queue = {}
queue._head = 1
queue._tail = 1
function queue.isEmpty()
return queue._tail == queue._head
end
function queue.pop()
queue._head = queue._head + 1
return queue[queue._head - 1]
end
function queue.push(value)
queue[queue._tail] = value
queue._tail = queue._tail + 1
end
queue.push({t1, t2})
while not queue.isEmpty() do
local location
t1, t2, location = unpack(queue.pop())
local function toSublocation(key)
local keyAsString = tostring(key)
return (location and location .. "." .. keyAsString) or keyAsString
end
for key, value1 in pairs(t1) do
local sublocation = toSublocation(key)
if t2[key] == nil then
return negate, string.format(
"Entry %s missing in second table (is %s in first)",
sublocation, tostring(value1))
end
local value2 = t2[key]
if type(value1) == 'table' and type(value2) == 'table' then
queue.push({value1, value2, sublocation})
else
local ok, message = check.areEq(value1, value2, tolerance, false)
if not ok then
message = 'At table location ' .. sublocation .. ': ' .. message
return negate, message
end
end
end
for key, value2 in pairs(t2) do
local sublocation = toSublocation(key)
if t1[key] == nil then
return negate, string.format(
"Entry %s missing in first table (is %s in second)",
sublocation, tostring(value2))
end
end
end
return not negate, 'The tables are equal'
end
|