Advertisement
Guest User

grademe.py

a guest
Jan 31st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import math
  4. import unittest
  5.  
  6. try: import a2 as sol
  7. except: print >> sys.stderr, "Missing ass02"
  8.  
  9. try: import aggregate
  10. except: print >> sys.stderr, "Missing aggregate"
  11. try: import cube
  12. except: print >> sys.stderr, "Missing cube"
  13. try: import tax
  14. except: print >> sys.stderr, "Missing tax"
  15. try: import snakeoil
  16. except: print >> sys.stderr, "Missing snakeoil"
  17. try: import nyctime
  18. except: print >> sys.stderr, "Missing nyctime"
  19. try: import digit
  20. except: print >> sys.stderr, "Missing digit"
  21.  
  22. all_perms = [
  23.     (3, 11, -2),
  24.     (3, -2, 11),
  25.     (11, 3, -2),
  26.     (11, -2, 3),
  27.     (-2, 3, 11),
  28.     (-2, 11, 3),
  29.     (100, 2, 121),
  30.     (1, 10.0, 21.0),
  31.     (7.0, 2.5, 3.1)
  32. ]
  33.  
  34.  
  35. class assignment02(unittest.TestCase):
  36.     def test_min(self):
  37.         """ Testing my_min (Q1a) """
  38.         for p, q, r in all_perms:
  39.             self.assertEqual(aggregate.my_min(p,q,r), sol.my_min(p,q,r))
  40.            
  41.     def test_mean(self):
  42.         """ Testing my_mean (Q1b) """
  43.         for p, q, r in all_perms:
  44.             self.assertAlmostEqual(aggregate.my_mean(p,q,r), sol.my_mean(p,q,r))
  45.            
  46.     def test_med(self):
  47.         """ Testing my_mean (Q1c) """
  48.         for p, q, r in all_perms:
  49.             self.assertEqual(aggregate.my_med(p,q,r), sol.my_med(p,q,r))
  50.                
  51.  
  52.     def test_perfectcube(self):
  53.         """ Testing isPerfectCube (Q2) """
  54.         tests = range(1, 45**3 + 1)
  55.         for test in tests:
  56.             self.assertEqual(cube.isPerfectCube(test), sol.isPerfectCube(test), test)
  57.  
  58.     def test_tax(self):
  59.         """ Testing Tax brackets (Q3) """
  60.         K = 1000
  61.         deductions = [20000, 500 , 12345]
  62.         borders = [50000, 150*K, 300*K, 500*K, 750*K, 1000*K, 2000*K, 4000*K]
  63.         genpm = [ v + a for a in [-7, -2, 0, 2, 9] for v in borders ] + [ b + 100*K for b in borders ]
  64.         for test in genpm:
  65.             for deduct in deductions:
  66.                 self.assertAlmostEqual(tax.calculateTax(test, deduct), sol.calculateTax(test, deduct),2)
  67.  
  68.     def test_snakeoil(self):
  69.         tests = [4.8, 5, 7, 9, 11, 15.0, 71, 99, 99.95, 100 , 100.425, 101, 230, 1024.1]
  70.         for test in tests:
  71.             self.assertAlmostEqual(snakeoil.price(test), sol.price(test), 2, "Case: {0}".format(test))
  72.  
  73.     def test_nyctime(self):
  74.         tests = range(24)
  75.         for hr in tests:
  76.             self.assertEqual(nyctime.nycHour(hr), sol.nycHour(hr), "Testing {0}".format(hr))
  77.        
  78.     def test_kthDigit(self):
  79.         tests = [ 498698787, 356, 29851, 2768175, 828581, 1, 17682 ]
  80.  
  81.         for num in tests:
  82.             for b in range(2, 78):
  83.                 for k in range(0, int(math.log10(num))+1):
  84.                     self.assertEqual(digit.kthDigit(num, b, k), sol.kthDigit(num, b, k))
  85.    
  86.            
  87. if __name__ == '__main__':
  88.     unittest.main(verbosity=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement