ConfirmTests.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python
  2. # This script confirms both that the register report "adds up", and that its
  3. # final balance is the same as what the balance report shows.
  4. import sys
  5. import os
  6. import re
  7. from LedgerHarness import LedgerHarness
  8. harness = LedgerHarness(sys.argv)
  9. tests = sys.argv[3]
  10. if not os.path.isdir(tests) and not os.path.isfile(tests):
  11. sys.stderr.write("'%s' is not a directory or file (cwd %s)" %
  12. (tests, os.getcwd()))
  13. sys.exit(1)
  14. commands = [
  15. "-f '$tests/standard.dat' -O 0ecbb1b15e2cf3e515cc0f8533e5bb0fb2326728",
  16. "-f '$tests/standard.dat' -B c56a21d23a6535184e7152ee138c28974f14280c",
  17. "-f '$tests/standard.dat' -V c56a21d23a6535184e7152ee138c28974f14280c",
  18. "-f '$tests/standard.dat' -G c56a21d23a6535184e7152ee138c28974f14280c",
  19. "-f '$tests/standard.dat' -B c0226fafdf9e6711ac9121cf263e2d50791859cb",
  20. "-f '$tests/standard.dat' -V c0226fafdf9e6711ac9121cf263e2d50791859cb",
  21. "-f '$tests/standard.dat' -G c0226fafdf9e6711ac9121cf263e2d50791859cb"
  22. ]
  23. def clean(num):
  24. num = re.sub("(\s+|\$|,)","", num)
  25. m = re.search("([-0-9.]+)", num)
  26. if m:
  27. return float(m.group(1))
  28. else:
  29. return float(num)
  30. def confirm_report(command):
  31. index = 1
  32. last_line = ""
  33. failure = False
  34. running_total = 0.0
  35. p = harness.run(re.sub('\$cmd', 'reg', command))
  36. for line in harness.readlines(p.stdout):
  37. match = re.match("\\s*([-$,0-9.]+)\\s+([-$,0-9.]+)", line[54:])
  38. if not match:
  39. continue
  40. value = clean(match.group(1))
  41. total = clean(match.group(2))
  42. running_total += value
  43. diff = abs(running_total - total)
  44. if re.search(' -[VGB] ', command) and diff < 0.015:
  45. diff = 0.0
  46. if diff > 0.001:
  47. print "DISCREPANCY: %.3f (%.3f - %.3f) at line %d:" % \
  48. (running_total - total, running_total, total, index)
  49. print line,
  50. running_total = total
  51. failure = True
  52. index += 1
  53. last_line = line
  54. balance_total = 0.0
  55. p = harness.run(re.sub('\$cmd', 'bal', command))
  56. for line in harness.readlines(p.stdout):
  57. if line[0] != '-':
  58. balance_total = clean(line[:20])
  59. diff = abs(balance_total - running_total)
  60. if re.search(' -[VGB] ', command) and diff < 0.015:
  61. diff = 0.0
  62. if diff > 0.001:
  63. print
  64. print "DISCREPANCY: %.3f (%.3f - %.3f) between register and balance" % \
  65. (balance_total - running_total, balance_total, running_total)
  66. print last_line,
  67. failure = True
  68. return not failure
  69. for cmd in commands:
  70. if confirm_report('$ledger --rounding $cmd ' + re.sub('\$tests', tests, cmd)):
  71. harness.success()
  72. else:
  73. harness.failure()
  74. harness.exit()