GenerateTests.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 re
  6. from difflib import ndiff
  7. multiproc = False
  8. try:
  9. from multiprocessing import Pool
  10. multiproc = True
  11. except:
  12. pass
  13. args = sys.argv
  14. jobs = 1
  15. match = re.match('-j([0-9]+)?', args[1])
  16. if match:
  17. args = [args[0]] + args[2:]
  18. if match.group(1):
  19. jobs = int(match.group(1))
  20. if jobs == 1:
  21. multiproc = False
  22. from LedgerHarness import LedgerHarness
  23. harness = LedgerHarness(args)
  24. #def normalize(line):
  25. # match = re.match("((\s*)([A-Za-z]+)?(\s*)([-0-9.]+)(\s*)([A-Za-z]+)?)( (.+))?$", line)
  26. # if match:
  27. # if match.group(3):
  28. # prefix = match.group(3) + " " + match.group(5)
  29. # if match.group(8):
  30. # return prefix + match.group(8)
  31. # return prefix
  32. # elif match.group(7):
  33. # prefix = match.group(7) + " " + match.group(5)
  34. # if match.group(8):
  35. # return prefix + match.group(8)
  36. # return prefix
  37. # return line
  38. def generation_test(seed):
  39. p_gen = harness.run('$ledger --seed=%d generate' % seed)
  40. cout = harness.read(p_gen.stdout)
  41. if not harness.wait(p_gen, msg=("Generation for seed %d failed:" % seed)):
  42. return False
  43. p_print = harness.run('$ledger --actual -f - print')
  44. p_print.stdin.write(cout)
  45. p_print.stdin.close()
  46. p_print_out = p_print.stdout.read()
  47. if not harness.wait(p_print, msg=("Print for seed %d failed:" % seed)):
  48. return False
  49. #p_cerr_bal = Popen("%s --args-only -f - bal" % ledger, shell=True,
  50. # stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
  51. #p_cerr_bal.stdin.write(cerr)
  52. #p_cerr_bal.stdin.close()
  53. #
  54. #cerr_lines = [normalize(line) for line in p_cerr_bal.stdout.readlines()]
  55. #
  56. #if p_cerr_bal.wait() != 0:
  57. # print "Stderr balance for seed %d failed due to error:" % seed
  58. # print p_cerr_bal.stderr.read()
  59. # del p_cerr_bal
  60. # return False
  61. #del p_cerr_bal
  62. p_cout_bal = harness.run('$ledger -f - bal')
  63. p_cout_bal.stdin.write(cout)
  64. p_cout_bal.stdin.close()
  65. cout_lines = harness.readlines(p_cout_bal.stdout)
  66. if len(cout_lines) == 0:
  67. return False
  68. #norm_cout_lines = [normalize(line) for line in cout_lines]
  69. if not harness.wait(p_cout_bal, msg=("Stdout balance for seed %d failed:" % seed)):
  70. return False
  71. p_print_bal = harness.run('$ledger -f - bal')
  72. p_print_bal.stdin.write(p_print_out)
  73. p_print_bal.stdin.close()
  74. print_lines = harness.readlines(p_print_bal.stdout)
  75. if len(print_lines) == 0:
  76. return False
  77. if not harness.wait(p_print_bal, msg=("Print balance for seed %d failed:" % seed)):
  78. return False
  79. success = True
  80. #printed = False
  81. #for line in ndiff(cerr_lines, norm_cout_lines, charjunk=None):
  82. # if line[:2] == " ":
  83. # continue
  84. # if not printed:
  85. # if success: print
  86. # print "Generation failure in output from seed %d (cerr vs. cout):" % seed
  87. # if success: failed += 1
  88. # success = False
  89. # printed = True
  90. # print " ", line
  91. printed = False
  92. for line in ndiff(cout_lines, print_lines, charjunk=None):
  93. if line[:2] == " ":
  94. continue
  95. if not printed:
  96. if success: print
  97. print "Generation failure in output from seed %d (cout vs. print):" % seed
  98. success = False
  99. printed = True
  100. print " ", line
  101. return success
  102. beg_range = 1
  103. end_range = 20
  104. if len(args) > 4:
  105. beg_range = int(args[3])
  106. end_range = int(args[4])
  107. def run_gen_test(i):
  108. if generation_test(i):
  109. harness.success()
  110. else:
  111. harness.failure()
  112. return harness.failed
  113. if multiproc:
  114. pool = Pool(jobs*2)
  115. else:
  116. pool = None
  117. if pool:
  118. pool.map(run_gen_test, range(beg_range, end_range))
  119. else:
  120. for i in range(beg_range, end_range):
  121. run_gen_test(i)
  122. if pool:
  123. pool.close()
  124. pool.join()
  125. harness.exit()