Advertisement
gur111

Kinda Working But Slow Tester Mtm Ex0 Pt1

Mar 22nd, 2020
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. import string
  2. import unittest
  3. import random
  4. import psutil
  5. import subprocess as sp
  6.  
  7. TOT_PATH = './cmake-build-debug/mtm_tot'
  8. SOL_PATH = './part1/mtm_sol'
  9.  
  10. REPEAT_COUNT = 50000
  11.  
  12.  
  13. def generate_invalid():
  14.     """
  15.    This is likely an invalid number
  16.    :return:
  17.    """
  18.     return ''.join(random.choices('+-' + string.ascii_lowercase + string.digits, k=random.randint(1, 10))).encode()
  19.  
  20.  
  21. def generate_valid(length=10):
  22.     return (random.choice(['', '-', '+']) + ''.join(random.choices(string.digits, k=random.randint(1, length)))).encode()
  23.  
  24.  
  25. def is_int(s):
  26.     try:
  27.         return int(s, base=10)
  28.     except ValueError:
  29.         return False
  30.  
  31.  
  32. class MyTestCase(unittest.TestCase):
  33.     def test_invalids(self):
  34.  
  35.         for _ in range(REPEAT_COUNT):
  36.             count = random.choice([generate_invalid(), generate_valid(length=6)])
  37.             num_as_int = is_int(count)
  38.  
  39.             tot = sp.Popen([TOT_PATH], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
  40.             sol = sp.Popen([SOL_PATH], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
  41.  
  42.             # print('Checking num: {}'.format(num.decode()))
  43.             tot.stdin.write(count)
  44.             sol.stdin.write(count)
  45.  
  46.             tot_out = tot.communicate()
  47.             sol_out = sol.communicate()
  48.             self.assertEqual(sol_out, tot_out, "Output incompatible. Count: {}\nFree memory: {}MB".format(count, psutil.virtual_memory().available / (1024 ** 2)))
  49.             # print('Output was: {}'.format(tot_out))
  50.  
  51.             if num_as_int:
  52.                 for _ in range(num_as_int + random.randint(-2, 3)):
  53.                     sol_out = sol.poll()
  54.                     tot_out = tot.poll()
  55.                     self.assertEqual(sol_out, tot_out, "Poll incompatible")
  56.                     if not tot.poll():
  57.                         break
  58.  
  59.                     num = random.choice([generate_invalid(), generate_valid(),
  60.                                          generate_valid(), generate_valid(), generate_valid()])
  61.                     tot.stdin.write(num)
  62.                     sol.stdin.write(num)
  63.                     sol_out = sol.communicate()
  64.                     tot_out = tot.communicate()
  65.                     self.assertEqual(sol_out, tot_out, "Output incompatible. Num: {}. Count: {}".format(num, count))
  66.  
  67.     def test_valids(self):
  68.  
  69.         for _ in range(REPEAT_COUNT):
  70.             num = generate_valid(length=6)
  71.  
  72.             tot = sp.Popen([TOT_PATH], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
  73.             sol = sp.Popen([SOL_PATH], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
  74.  
  75.             # print('Checking num: {}'.format(num.decode()))
  76.             tot.stdin.write(num)
  77.             sol.stdin.write(num)
  78.  
  79.             tot_out = tot.communicate()
  80.             sol_out = sol.communicate()
  81.             self.assertEqual(sol_out, tot_out, "Output incompatible. Count: {}\nFree memory: {}MB".format(num, psutil.virtual_memory().available / (1024 ** 2)))
  82.             # print('Output was: {}'.format(tot_out))
  83.  
  84.             for _ in range(int(num) + random.randint(-2, 3)):
  85.                 sol_out = sol.poll()
  86.                 tot_out = tot.poll()
  87.                 self.assertEqual(sol_out, tot_out, "Poll incompatible")
  88.                 if not tot.poll():
  89.                     break
  90.  
  91.                 num = generate_valid()
  92.  
  93.                 tot.stdin.write(num)
  94.                 sol.stdin.write(num)
  95.                 sol_out = sol.communicate()
  96.                 tot_out = tot.communicate()
  97.                 self.assertEqual(sol_out, tot_out, "Output incompatible. Num: {}, Count: {}".format(num, count))
  98.  
  99.  
  100. if __name__ == '__main__':
  101.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement