def testF(): """Test Part F (of Part I) of the assignment. This test procedure checks out the simple additions to class Cluster to support market analysis (ratings and str support). It is perhaps the most straightforward unit test of the lot.""" print ' Testing Part F' # For all unit tests, create a new cluster. cluster = Cluster([0.0, 0.0, 0.0]) # TEST CASE 1 (rating) cornelltest.assert_equals(None, cluster.getRating()) cluster.addRating(1.0) cornelltest.assert_floats_equal(1.0, cluster.getRating()) cluster.addRating(0.5) cornelltest.assert_floats_equal(0.75, cluster.getRating()) cluster.addRating(0.6) cornelltest.assert_floats_equal(0.7, cluster.getRating()) cluster.clearRating() cornelltest.assert_equals(None, cluster.getRating()) print ' Rating methods look ok' # TEST CASE 2 (str) cornelltest.assert_equals(str([0.0, 0.0, 0.0]), str(cluster)) cornelltest.assert_equals( str(cluster.__class__) + str([0.0, 0.0, 0.0]), ` cluster `) cluster.setCentroid([0.0, 0.5, 4.2]) cornelltest.assert_equals(str([0.0, 0.5, 4.2]), str(cluster)) print ' str support look ok' print ' Part F appears correct'
def testD(): """testD() tests the functions iscurrency(currency) and exchange(amount_from, currency_from, currency_to) in module a1.py""" #test iscurrency(currency) currency = 'USD' result = iscurrency(currency) cornelltest.assert_equals(True, result) currency = 'US' result = iscurrency(currency) cornelltest.assert_equals(False, result) #test exchange(amount_from, currency_from, currency_to) amount_from = 5.0 currency_from = 'USD' currency_to = 'EUR' result = exchange(amount_from, currency_from, currency_to) cornelltest.assert_floats_equal(3.77216145, result) amount_from = -5.0 currency_from = 'KRW' currency_to = 'USD' result = exchange(amount_from, currency_from, currency_to) cornelltest.assert_floats_equal(-0.004609999993546, result) amount_from = 576.5 currency_from = 'TRY' currency_to = 'KRW' result = exchange(amount_from, currency_from, currency_to) cornelltest.assert_floats_equal(308288.69086087, result)
def testF(): """Test Part F (of Part I) of the assignment. This test procedure checks out the simple additions to class Cluster to support market analysis (ratings and str support). It is perhaps the most straightforward unit test of the lot.""" print ' Testing Part F' # For all unit tests, create a new cluster. cluster = Cluster([0.0,0.0,0.0]) # TEST CASE 1 (rating) cornelltest.assert_equals(None,cluster.getRating()) cluster.addRating(1.0) cornelltest.assert_floats_equal(1.0,cluster.getRating()) cluster.addRating(0.5) cornelltest.assert_floats_equal(0.75,cluster.getRating()) cluster.addRating(0.6) cornelltest.assert_floats_equal(0.7,cluster.getRating()) cluster.clearRating() cornelltest.assert_equals(None,cluster.getRating()) print ' Rating methods look ok' # TEST CASE 2 (str) cornelltest.assert_equals(str([0.0,0.0,0.0]),str(cluster)) cornelltest.assert_equals(str(cluster.__class__)+str([0.0,0.0,0.0]),`cluster`) cluster.setCentroid([0.0,0.5,4.2]) cornelltest.assert_equals(str([0.0,0.5,4.2]),str(cluster)) print ' str support look ok' print ' Part F appears correct'
def testD(): result = a1.iscurrency('AED') cornelltest.assert_true(result == True) result = a1.iscurrency('LOL') cornelltest.assert_true(result == False) result = a1.iscurrency('MOM') cornelltest.assert_true(result == False) result = a1.iscurrency('USD') cornelltest.assert_true(result == True) #Test exchange(currency_from, currency_to, amount_from) result = a1.exchange('USD', 'EUR', 2.5) cornelltest.assert_floats_equal(2.24075, result) result = a1.exchange('CAD', 'CNY', 1.0) cornelltest.assert_floats_equal(5.1369278716282, result) result = a1.exchange('CAD', 'CNY', 1.09) cornelltest.assert_floats_equal(5.5992513800748, result) result = a1.exchange('CAD', 'CNY', 1.09999) cornelltest.assert_floats_equal(5.6505692895124, result)
def test_cluster_b(): """Test 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 = a6.Dataset(3, items) # Create two clusters cluster2 = a6.Cluster(dset, [0.5, 0.5, 0.0]) cluster3 = a6.Cluster(dset, [0.0, 0.0, 0.5]) # TEST CASE 1 (distance) dist = cluster2.distance([1.0, 0.0, -1.0]) cornelltest.assert_floats_equal(1.22474487139, dist) # TEST CASE 2 (distance) dist = cluster2.distance([0.5, 0.5, 0.0]) cornelltest.assert_floats_equal(0.0, dist) # TEST CASE 3 (distance) dist = cluster3.distance([0.5, 0.0, 0.5]) cornelltest.assert_floats_equal(0.5, dist) print ' Method Cluster.distance() looks okay' # TEST CASE 1 (updateCentroid): centroid remains the same cluster2.addIndex(0) cluster2.addIndex(1) stable = cluster2.updateCentroid() cornelltest.assert_float_lists_equal([0.5, 0.5, 0.0], cluster2.getCentroid()) cornelltest.assert_true(stable) # TEST CASE 2 (updateCentroid): centroid changes cluster2.addIndex(2) cluster2.addIndex(3) stable = cluster2.updateCentroid() cornelltest.assert_float_lists_equal([0.25, 0.25, 0.25], cluster2.getCentroid()) cornelltest.assert_false(stable) # updating again without changing points: centroid stable stable = cluster2.updateCentroid() cornelltest.assert_float_lists_equal([0.25, 0.25, 0.25], cluster2.getCentroid()) cornelltest.assert_true(stable) print ' Method Cluster.updateCentroid() looks okay' print ' Part B of class Cluster appears correct' print ''
def test_cluster_b(): """Test 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 = a6.Dataset(3, items) # Create two clusters cluster2 = a6.Cluster(dset, [0.5,0.5,0.0]) cluster3 = a6.Cluster(dset, [0.0,0.0,0.5]) # TEST CASE 1 (distance) dist = cluster2.distance([1.0,0.0,-1.0]) cornelltest.assert_floats_equal(1.22474487139,dist) # TEST CASE 2 (distance) dist = cluster2.distance([0.5,0.5,0.0]) cornelltest.assert_floats_equal(0.0,dist) # TEST CASE 3 (distance) dist = cluster3.distance([0.5,0.0,0.5]) cornelltest.assert_floats_equal(0.5,dist) print ' Method Cluster.distance() looks okay' # TEST CASE 1 (updateCentroid): centroid remains the same cluster2.addIndex(0) cluster2.addIndex(1) stable = cluster2.updateCentroid() cornelltest.assert_float_lists_equal([0.5, 0.5, 0.0], cluster2.getCentroid()) cornelltest.assert_true(stable) # TEST CASE 2 (updateCentroid): centroid changes cluster2.addIndex(2) cluster2.addIndex(3) stable = cluster2.updateCentroid() cornelltest.assert_float_lists_equal([0.25, 0.25, 0.25], cluster2.getCentroid()) cornelltest.assert_false(stable) # updating again without changing points: centroid stable stable = cluster2.updateCentroid() cornelltest.assert_float_lists_equal([0.25, 0.25, 0.25], cluster2.getCentroid()) cornelltest.assert_true(stable) print ' Method Cluster.updateCentroid() looks okay' print ' Part B of class Cluster appears correct' print ''
def testD(): """Prints: "Module al passed all tests" if all tests successful. Precondition: a1 is a module that contains functions iscurrency() and exchange()""" print "Testing functions iscurrency() and exchange()" A = "LOL" B = "USD" C = "XBT" D = "LOLOL" E = "usd" cornelltest.assert_equals(False, a1.iscurrency(A)) cornelltest.assert_equals(True, a1.iscurrency(B)) cornelltest.assert_equals(True, a1.iscurrency(C)) cornelltest.assert_equals(False, a1.iscurrency(D)) cornelltest.assert_equals(False, a1.iscurrency(E)) cornelltest.assert_floats_equal(36.88680000000000, a1.exchange("USD", "RUB", 1.0)) cornelltest.assert_floats_equal(1.00000000000000, a1.exchange("USD", "USD", 1.0)) cornelltest.assert_floats_equal(0.08735622000000, a1.exchange("USD", "XBT", 42.0))
def testC(): """Test Part C (of Part I) of the assignment. This test checks the methods both nearest and partition. For these checks, it has to go ahead and initialize some clusters (which is done in Part D). We do this by accessing your hidden attributes. Normally, this is bad programming (hidden attributes can be used inside of the class definition, but not outside). But sometimes rules are meant to be broken, and testing is a good time to break rules.""" print ' Testing Part C' # FOR BOTH TEST CASES # Create and test a non-empty database 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]] dbase = Database(3, items) # Create two clusters cluster1 = Cluster([0.5, 0.5, 0.0]) cluster2 = Cluster([0.0, 0.0, 0.5]) # Initialize the database to use these clusters (access hidden attributes) # THIS VIOLATES GOOD PROGRAMMING. But sometimes rules must be broken. dbase._clusters = [None, None] # Make sure the list can hold two clusters dbase._clusters[0] = cluster1 dbase._clusters[1] = cluster2 dbase._ksize = 2 # TEST CASE 1 (distance) dist = cluster1.distance([1.0, 0.0, -1.0]) cornelltest.assert_floats_equal(1.22474487139, dist) # TEST CASE 2 (distance) dist = cluster1.distance([0.5, 0.5, 0.0]) cornelltest.assert_floats_equal(0.0, dist) print ' Method distance() looks okay' # TEST CASE 3 (nearest) nearest = dbase.nearest([1.0, 0.0, 0.0]) cornelltest.assert_equals(id(cluster1), id(nearest)) # TEST CASE 4 (nearest) nearest = dbase.nearest([0.0, 0.0, 1.0]) cornelltest.assert_equals(id(cluster2), id(nearest)) print ' Method nearest() looks okay' # TEST CASE 5 (partition) dbase.partition() # First half of list is in first cluster cornelltest.assert_float_lists_equal(items[:2], cluster1.getContents()) # Second half of list is in second cluster cornelltest.assert_float_lists_equal(items[2:], cluster2.getContents()) # TEST CASE 6 (partition) # Change the clusters dbase._clusters[0].setCentroid([0.0, 0.0, 0.5]) dbase._clusters[1].setCentroid([0.5, 0.5, 0.0]) dbase.partition() # Second half of list is in first cluster cornelltest.assert_float_lists_equal(items[2:], cluster1.getContents()) # First half of list is in second cluster cornelltest.assert_float_lists_equal(items[:2], cluster2.getContents()) print ' Method partition() looks okay' print ' Part C appears correct'
def testC(): """Test Part C (of Part I) of the assignment. This test checks the methods both nearest and partition. For these checks, it has to go ahead and initialize some clusters (which is done in Part D). We do this by accessing your hidden attributes. Normally, this is bad programming (hidden attributes can be used inside of the class definition, but not outside). But sometimes rules are meant to be broken, and testing is a good time to break rules.""" print ' Testing Part C' # FOR BOTH TEST CASES # Create and test a non-empty database 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]] dbase = Database(3,items) # Create two clusters cluster1 = Cluster([0.5,0.5,0.0]) cluster2 = Cluster([0.0,0.0,0.5]) # Initialize the database to use these clusters (access hidden attributes) # THIS VIOLATES GOOD PROGRAMMING. But sometimes rules must be broken. dbase._clusters = [None,None] # Make sure the list can hold two clusters dbase._clusters[0] = cluster1 dbase._clusters[1] = cluster2 dbase._ksize = 2 # TEST CASE 1 (distance) dist = cluster1.distance([1.0,0.0,-1.0]) cornelltest.assert_floats_equal(1.22474487139,dist) # TEST CASE 2 (distance) dist = cluster1.distance([0.5,0.5,0.0]) cornelltest.assert_floats_equal(0.0,dist) print ' Method distance() looks okay' # TEST CASE 3 (nearest) nearest = dbase.nearest([1.0,0.0,0.0]) cornelltest.assert_equals(id(cluster1),id(nearest)) # TEST CASE 4 (nearest) nearest = dbase.nearest([0.0,0.0,1.0]) cornelltest.assert_equals(id(cluster2),id(nearest)) print ' Method nearest() looks okay' # TEST CASE 5 (partition) dbase.partition() # First half of list is in first cluster cornelltest.assert_float_lists_equal(items[:2],cluster1.getContents()) # Second half of list is in second cluster cornelltest.assert_float_lists_equal(items[2:],cluster2.getContents()) # TEST CASE 6 (partition) # Change the clusters dbase._clusters[0].setCentroid([0.0,0.0,0.5]) dbase._clusters[1].setCentroid([0.5,0.5,0.0]) dbase.partition() # Second half of list is in first cluster cornelltest.assert_float_lists_equal(items[2:],cluster1.getContents()) # First half of list is in second cluster cornelltest.assert_float_lists_equal(items[:2],cluster2.getContents()) print ' Method partition() looks okay' print ' Part C appears correct'