def test_exchange(): """Test procedure for exchange""" print("Testing exchange") case1 = currency.exchange('USD', 'EUR', 2) introcs.assert_floats_equal(1.772814, case1) case2 = currency.exchange('USD', 'EUR', -2) introcs.assert_floats_equal(-1.772814, case2)
def test_exchange(): """ Test procedure for exchange """ print('Testing exchange') result = currency.exchange('USD', 'EUR', 2.5) introcs.assert_floats_equal(2.2160175, result) result = currency.exchange('USD', 'EUR', -2.5) introcs.assert_floats_equal(-2.2160175, result)
def test_asserts(): """ This is a simple test procedure to help you understand how assert works """ print('Testing the introcs asserts') introcs.assert_equals('b c', 'ab cd'[1:4]) #introcs.assert_equals('b c', 'ab cd'[1:3]) # UNCOMMENT ONLY WHEN DIRECTED introcs.assert_true(3 < 4) introcs.assert_equals(3, 1+2) introcs.assert_equals(3.0, 1.0+2.0) introcs.assert_floats_equal(6.3, 3.1+3.2)
def test_circ_area(): """ Test procedure for function circ_area(). """ print('Testing circ_area()') result = func.circ_area(radius=3) introcs.assert_floats_equal(28.27433, result) result = func.circ_area(radius=2) introcs.assert_floats_equal(12.56637, result) result = func.circ_area(diameter=4) introcs.assert_floats_equal(12.56637, result) # Test for crashes try: func.circ_area() introcs.assert_true(False) # We should never reach this line! except: pass try: func.circ_area(radius=3, diameter=6) introcs.assert_true(False) # We should never reach this line! except: pass # Add a test with additional arguments dict1 = {'radius': 3, 'foo': 20, 'bar': 10} result = func.circ_area(**dict1) introcs.assert_floats_equal(28.274333882308138, result)
def testD(): """ Test for part D """ #-----------tests for iscurrency------------------------------------------------ value1 = pro.iscurrency('USD') introcs.assert_true(value1) value2 = pro.iscurrency('GHS') introcs.assert_true(value2) value3 = pro.iscurrency('EUR') introcs.assert_true(value3) value4 = pro.iscurrency('CAD') introcs.assert_true(value4) #--------------------tests for exchange----------------------------------------- compare1 = pro.exchange('USD', 'GHS', 256.84) introcs.assert_floats_equal(compare1, 1279.0632) compare2 = pro.exchange('EUR', 'CAD', -0.96) introcs.assert_floats_equal(compare2, -1.4492613128365) compare3 = pro.exchange('CAD', 'GHS', 1200.01) introcs.assert_floats_equal(compare3, 4543.3533104394) compare4 = pro.exchange('EUR', 'USD', 780.98) introcs.assert_floats_equal(compare4, 896.35012033895)
def test_average_grade(): """ Test procedure for function average_grade """ print('Testing function average_grade') netids = ['wmw2', 'abc123', 'jms45', 'qoz15', 'xyz2345', 'jms46', 'jms47'] grades = [ 55, 90, 85, 72, 100, 63, 77 ] inputs = dict(zip(netids[:1],grades[:1])) result = funcs.average_grade(inputs) introcs.assert_floats_equal(55.0,result) introcs.assert_equals(dict(zip(netids[:1],grades[:1])), inputs) # Check unmodified inputs = dict(zip(netids[:3],grades[:3])) result = funcs.average_grade(inputs) introcs.assert_floats_equal(76.666666667,result) introcs.assert_equals(dict(zip(netids[:3],grades[:3])), inputs) # Check unmodified inputs = dict(zip(netids[:5],grades[:5])) result = funcs.average_grade(inputs) introcs.assert_floats_equal(80.4,result) introcs.assert_equals(dict(zip(netids[:5],grades[:5])), inputs) # Check unmodified inputs = dict(zip(netids,grades)) result = funcs.average_grade(inputs) introcs.assert_floats_equal(77.428571428,result) introcs.assert_equals(dict(zip(netids,grades)), inputs) # Check unmodified
def testD(): """ Test procedure for Part D """ print('Testing function iscurrency(currency)') result = a1.iscurrency('United') introcs.assert_equals(False, result) result = a1.iscurrency('USD') introcs.assert_equals(True, result) print('The module is working correctly') print('Testing function exchange(currency_from, currency_to, amount_from)') result = a1.exchange('USD', 'EUR', 2.0) introcs.assert_floats_equal(1.727138, result) print('The module is working correctly')
def testD(): """ Test procedure for Part D """ #tests valid currency result = a1.is_currency('USD') introcs.assert_equals(True, result) #tests invalid currency result = a1.is_currency('CBD') introcs.assert_equals(False, result) #testing exchange result = a1.exchange('USD', 'CUP', 2.5) introcs.assert_floats_equal(64.375, result) #testing longer exchange result = a1.exchange('TTD', 'JMD', 2.5) introcs.assert_floats_equal(48.690190282653, result)
def test_avg(): """ Test procedure for function avg(). """ print('Testing avg()') result = func.avg() introcs.assert_floats_equal(0, result) result = func.avg(7, 1, 4, 3, 6, 8) introcs.assert_floats_equal(4.833333333333333, result) result = func.avg(-1, 1, 3, 5) introcs.assert_floats_equal(2.0, result) result = func.avg(2.5) introcs.assert_floats_equal(2.5, result) result = func.avg(1.0, 1.0, 1.0) introcs.assert_floats_equal(1.0, result) # Test range(10,20) here a = range(10, 20) result = func.avg(*a)
def test_to_float_list(): """ Test conversion function to_float_list """ seq = ['1.0', '2.2', '3.5'] lst = a2.to_float_list(seq) introcs.assert_floats_equal(1.0, lst[0]) introcs.assert_floats_equal(2.2, lst[1]) introcs.assert_floats_equal(3.5, lst[2]) seq = ['2.2', '3.5'] lst = a2.to_float_list(seq) introcs.assert_floats_equal(2.2, lst[0]) introcs.assert_floats_equal(3.5, lst[1]) seq = ['1.0', '2.2', '3.5', '-7.5'] lst = a2.to_float_list(seq) introcs.assert_floats_equal(1.0, lst[0]) introcs.assert_floats_equal(2.2, lst[1]) introcs.assert_floats_equal(3.5, lst[2]) introcs.assert_floats_equal(-7.5, lst[3])
def test_avg(): """ Test procedure for function avg(). """ print('Testing avg()') result = funcs.avg(()) introcs.assert_floats_equal(0, result) result = funcs.avg((7, 1, 4, 3, 6, 8)) introcs.assert_floats_equal(4.833333333333333, result) result = funcs.avg((-1, 1, 3, 5)) introcs.assert_floats_equal(2.0, result) result = funcs.avg((2.5, )) introcs.assert_floats_equal(2.5, result) result = funcs.avg((1.0, 1.0, 1.0)) introcs.assert_floats_equal(1.0, result) tup = tuple(range(10, 20)) result = funcs.avg(tup) introcs.assert_floats_equal(14.5, result)
def test_cluster_b(): """ Tests Part B of the Cluster class assignment. """ print(' Testing Part B of class Cluster') # A dataset with four points items = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]] dset = a6dataset.Dataset(3, items) # Create some clusters cluster1 = a6cluster.Cluster(dset, [0.0, 0.0, 0.2]) cluster2 = a6cluster.Cluster(dset, [0.5, 0.5, 0.0]) cluster3 = a6cluster.Cluster(dset, [0.0, 0.0, 0.5]) # TEST CASE 1 (distance) dist = cluster2.distance([1.0, 0.0, -1.0]) introcs.assert_floats_equal(1.22474487139, dist) # TEST CASE 2 (distance) dist = cluster2.distance([0.5, 0.5, 0.0]) introcs.assert_floats_equal(0.0, dist) # TEST CASE 3 (distance) dist = cluster3.distance([0.5, 0.0, 0.5]) introcs.assert_floats_equal(0.5, dist) print(' Method Cluster.distance() looks okay') # Add some indices cluster1.addIndex(0) cluster1.addIndex(1) cluster2.addIndex(0) cluster2.addIndex(1) cluster3.addIndex(0) cluster3.addIndex(1) cluster3.addIndex(2) # TEST CASE 1 (radius) rads = cluster1.getRadius() introcs.assert_floats_equal(1.0198039, rads) # TEST CASE 2 (radius) rads = cluster2.getRadius() introcs.assert_floats_equal(0.7071068, rads) # TEST CASE 3 (radius) rads = cluster3.getRadius() introcs.assert_floats_equal(1.1180340, rads) print(' Method Cluster.getRadius() looks okay') # TEST CASE 1 (updateCentroid): centroid remains the same stable = cluster2.update() introcs.assert_float_lists_equal([0.5, 0.5, 0.0], cluster2.getCentroid()) introcs.assert_true(stable) # TEST CASE 2 (updateCentroid): centroid changes cluster2.addIndex(2) cluster2.addIndex(3) stable = cluster2.update() introcs.assert_float_lists_equal([0.25, 0.25, 0.25], cluster2.getCentroid()) introcs.assert_false(stable) # updating again without changing points: centroid stable stable = cluster2.update() introcs.assert_float_lists_equal([0.25, 0.25, 0.25], cluster2.getCentroid()) introcs.assert_true(stable) print(' Method Cluster.update() looks okay') print(' Part B of class Cluster appears correct') print()
def test_average_grade(): """ Test procedure for function average_grade(). """ print('Testing average_grade()') grades = {'wmw2': 55, 'abc3': 90, 'jms45': 86} # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(77.0, result) introcs.assert_equals(orignl, grades) grades = {'abc123': 0, 'abc456': 65, 'jms457': 50} # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(38.333, result) introcs.assert_equals(orignl, grades) grades = { 'abc123': 0, 'abc456': 65, 'jms457': 50, 'jms123': 60, 'xyz123': 70 } # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(49.0, result) introcs.assert_equals(orignl, grades) grades = { 'abc123': 0, 'abc456': 65, 'jms457': 50, 'jms123': 60, 'xyz123': 70, 'xyz456': 80, 'wmw4': 90 } # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(59.286, result) introcs.assert_equals(orignl, grades) grades = { 'abc123': 0, 'abc456': 65, 'jms457': 50, 'jms123': 60, 'xyz123': 70, 'xyz456': 80, 'wmw4': 90, 'wmw5': 100 } # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(64.375, result) introcs.assert_equals(orignl, grades) grades = { 'abc123': 0, 'abc456': 65, 'jms457': 50, 'jms123': 60, 'xyz123': 70, 'xyz456': 80, 'wmw4': 90, 'wmw5': 100, 'tor3': 88 } # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(67.0, result) introcs.assert_equals(orignl, grades) grades = {'wmw2': 55, 'abc3': 90} # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(72.5, result) introcs.assert_equals(orignl, grades) grades = {'abc3': 90} # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(90.0, result) introcs.assert_equals(orignl, grades) grades = {} # Snapshot table to make sure we do not modify orignl = copy.deepcopy(grades) result = funcs.average_grade(grades) introcs.assert_floats_equal(0.0, result) introcs.assert_equals(orignl, grades)
def test_contrast_value(): """ Test translation function contrast_value """ print('Testing contrast_value') # contrast == -1.0 (extreme) result = a3.contrast_value(0.0,-1.0) introcs.assert_floats_equal(0.5,result) # contrast extreme with color result = a3.contrast_value(1.0,-1.0) introcs.assert_floats_equal(0.5,result) # contrast < 0, bottom part of sawtooth result = a3.contrast_value(0.1,-0.5) introcs.assert_floats_equal(0.3,result) # contrast < 0, middle of sawtooth result = a3.contrast_value(0.4,-0.4) introcs.assert_floats_equal(0.4571429,result) # contrast < 0, upper part of sawtooth result = a3.contrast_value(0.9,-0.3) introcs.assert_floats_equal(0.8142857,result) # contrast == 0.0, bottom part of sawtooth result = a3.contrast_value(0.1,0.0) introcs.assert_floats_equal(0.1,result) # contrast == 0, middle of sawtooth result = a3.contrast_value(0.6,0.0) introcs.assert_floats_equal(0.6,result) # contrast == 0.0, upper part of sawtooth result = a3.contrast_value(0.9,0.0) introcs.assert_floats_equal(0.9,result) # contrast > 0, bottom part of sawtooth result = a3.contrast_value(0.1,0.3) introcs.assert_floats_equal(0.05384615,result) # contrast > 0, middle of sawtooth result = a3.contrast_value(0.4,0.5) introcs.assert_floats_equal(0.2,result) # contrast > 0, upper part of sawtooth result = a3.contrast_value(0.9,0.4) introcs.assert_floats_equal(0.95714286,result) # contrast == 1.0 (extreme) result = a3.contrast_value(0.2,1.0) introcs.assert_floats_equal(0.0,result) result = a3.contrast_value(0.6,1.0) introcs.assert_floats_equal(1.0,result) print('Tests for contrast_value passed')
def test_exp(): """ Tests procedure for function exp(). """ print('Testing exp()') # Note that we round the result to ignore anything outside margin of error result = round(func.exp(1), 5) introcs.assert_floats_equal(2.71828, result) result = round(func.exp(1, 1e-12), 11) introcs.assert_floats_equal(2.71828182846, result) result = round(func.exp(8), 5) introcs.assert_floats_equal(2980.95799, result) result = round(func.exp(8), 11) introcs.assert_floats_equal(2980.95798664543, result) result = round(func.exp(-2), 5) introcs.assert_floats_equal(0.13534, result) result = round(func.exp(-2, 1e-12), 11) introcs.assert_floats_equal(0.13533528324, result) result = round(func.exp(8, 1e-1), 0) introcs.assert_floats_equal(2981.0, result)