def test_image_str(): """ Tests the __str__ method in class Image """ print('Testing image __str__ method') p = [(255, 64, 0), (0, 255, 64), (64, 0, 255), (64, 255, 128), (128, 64, 255), (255, 128, 64)] str0 = '[[' + str(p[0]) + ', ' + str(p[1]) + '],\n[' + str( p[2]) + ', ' + str(p[3]) + ']]' str1 = '[[' + str(p[0]) + ', ' + str(p[1]) + '],\n[' + str( p[2]) + ', ' + str(p[3]) + '],\n[' + str(p[4]) + ', ' + str( p[5]) + ']]' str2 = '[[' + str(p[0]) + ', ' + str(p[1]) + ', ' + str( p[2]) + '],\n[' + str(p[3]) + ', ' + str(p[4]) + ', ' + str( p[5]) + ']]' str3 = '[[' + str(p[0]) + ', ' + str(p[1]) + ', ' + str(p[2]) + ', ' + str( p[3]) + ', ' + str(p[4]) + ', ' + str(p[5]) + ']]' str4 = '[[' + str(p[0]) + '],\n[' + str(p[1]) + '],\n[' + str( p[2]) + '],\n[' + str(p[3]) + '],\n[' + str(p[4]) + '],\n[' + str( p[5]) + ']]' image = a6image.Image(p[:4], 2) introcs.assert_equals(str0, str(image)) image = a6image.Image(p, 2) introcs.assert_equals(str1, str(image)) image.setWidth(3) introcs.assert_equals(str2, str(image)) image.setWidth(6) introcs.assert_equals(str3, str(image)) image.setWidth(1) introcs.assert_equals(str4, str(image))
def test_has_a_vowel(): """ This procedure tests the has_a_vowel module""" print('Testing function has_a_vowel') input = 'aeiou' result = funcs.has_a_vowel(input) introcs.assert_equals(True, result)
def testC(): """ Test functions currency_response() """ #testing converting to the same currency introcs.assert_equals( '{ "ok":true, "lhs":"2.5 Australian Dollars",' + ' "rhs":"2.5 Australian Dollars", "err":""' + ' }', a1.currency_response("AUD", "AUD", 2.5)) #testing converting between 2 different currencies introcs.assert_equals( '{ "ok":true, "lhs":"2.5 Australian Dollars",' + ' "rhs":"1.7551668602031 United States Dollars", "err":"" }' + '', a1.currency_response("AUD", "USD", 2.5)) #testing invalid source currency introcs.assert_equals( '{ "ok":false, "lhs":"", "rhs":"", "err":"Source ' + 'currency code is invalid." }', a1.currency_response("abc", "USD", 2.5)) #testing 2 invalid currencies introcs.assert_equals( '{ "ok":false, "lhs":"", "rhs":"", "err":"Source' + ' currency code is invalid." }', a1.currency_response("abc", "def", 2.5)) #testing invalid exchange currency introcs.assert_equals( '{ "ok":false, "lhs":"", "rhs":"", "err":"' + 'Exchange currency code is invalid.' + '" }', a1.currency_response("USD", "def", 2.5))
def test_has_a_vowel(): """ This is a simple test procedure to help you understand how vowels work """ print('Testing function has_a_vowel') input = 'aeiou' result = funcs.has_a_vowel(input) introcs.assert_equals(True, result)
def test_crossout(): """ Test procedure for function crossout(). Note the use of assert_float_lists_equal for testing (nested) lists of floats. """ print('Testing crossout()') table = [[0.1, 0.3, 0.5], [0.6, 0.2, 0.7], [1.5, 2.3, 0.4]] # Snapshot table to make sure we do not modify orig = copy.deepcopy(table) result = funcs.crossout(table, 1, 2) introcs.assert_float_lists_equal([[0.1, 0.3], [1.5, 2.3]], result) introcs.assert_float_lists_equal(orig, table) result = funcs.crossout(table, 0, 0) introcs.assert_float_lists_equal([[0.2, 0.7], [2.3, 0.4]], result) introcs.assert_float_lists_equal(orig, table) result = funcs.crossout(table, 2, 1) introcs.assert_float_lists_equal([[0.1, 0.5], [0.6, 0.7]], result) introcs.assert_float_lists_equal(orig, table) table = [[0.1, 0.3, 0.5], [0.6, 0.2, 0.7], [1.5, 2.3, 0.4], [0.1, 0.2, 0.3]] # Snapshot table to make sure we do not modify orig = copy.deepcopy(table) result = funcs.crossout(table, 1, 2) introcs.assert_float_lists_equal([[0.1, 0.3], [1.5, 2.3], [0.1, 0.2]], result) introcs.assert_float_lists_equal(orig, table) table = [[0.1, 0.3, 0.5, 1.0], [0.6, 0.2, 0.7, 2.0], [1.5, 2.3, 0.4, 3.0]] # Snapshot table to make sure we do not modify orig = copy.deepcopy(table) result = funcs.crossout(table, 1, 2) introcs.assert_float_lists_equal([[0.1, 0.3, 1.0], [1.5, 2.3, 3.0]], result) introcs.assert_float_lists_equal(orig, table) table = [[1, 2], [3, 4]] # Snapshot table to make sure we do not modify orig = copy.deepcopy(table) result = funcs.crossout(table, 1, 0) introcs.assert_float_lists_equal([[2]], result) introcs.assert_float_lists_equal(orig, table) result = funcs.crossout(table, 0, 1) introcs.assert_float_lists_equal([[3]], result) introcs.assert_float_lists_equal(orig, table) table = [[5]] # Snapshot table to make sure we do not modify orig = copy.deepcopy(table) result = funcs.crossout(table, 0, 0) introcs.assert_equals([], result) introcs.assert_float_lists_equal(orig, table)
def test_into(): """ Tests for function into """ introcs.assert_equals(0, lab07.into(5, 3)) introcs.assert_equals(1, lab07.into(6, 3)) introcs.assert_equals(2, lab07.into(9, 3)) introcs.assert_equals(2, lab07.into(18, 3)) introcs.assert_equals(4, lab07.into(3*3*3*3*7,3))
def test_number2(): """ Tests for function number2 """ introcs.assert_equals(0, lab07.number2(0)) introcs.assert_equals(1, lab07.number2(2)) introcs.assert_equals(2, lab07.number2(232)) introcs.assert_equals(0, lab07.number2(333)) introcs.assert_equals(3, lab07.number2(234252))
def test_remove_first(): """ Tests for function remove_first """ introcs.assert_equals([], lab07.remove_first([],3)) introcs.assert_equals([], lab07.remove_first([3],3)) introcs.assert_equals([3], lab07.remove_first([3],4)) introcs.assert_equals([3, 4, 4, 5], lab07.remove_first([3, 4, 4, 4, 5],4)) introcs.assert_equals([3, 5, 4, 4, 4], lab07.remove_first([3, 4, 5, 4, 4, 4],4))
def testB(): """ test for preprocessed functions. """ print("Test the preprocessed functions.") input = "I went to the school, and didn't see any one." output = "i went to the school and didn't see any one" print(ngrammodel.prep(input, 1)) introcs.assert_equals(ngrammodel.prep(input, 1), output)
def test_iscurrency(): """Test procedure for iscurrency""" print("Testing iscurrency") case1 = currency.iscurrency("USD") introcs.assert_equals(True, case1) case2 = currency.iscurrency("AB") introcs.assert_equals(False, case2)
def test_numberof(): """ Tests for function numberof """ mylist = [5, 3, 3455, 74, 74, 74, 3] introcs.assert_equals(0, lab07.numberof([],4)) introcs.assert_equals(1, lab07.numberof([4],4)) introcs.assert_equals(3, lab07.numberof(mylist,74)) introcs.assert_equals(2, lab07.numberof(mylist,3)) introcs.assert_equals(0, lab07.numberof(mylist,4))
def test_number_not(): """ Tests for function number_not """ mylist = [5, 3, 3455, 74, 74, 74, 3] introcs.assert_equals(0, lab07.number_not([],4)) introcs.assert_equals(0, lab07.number_not([4],4)) introcs.assert_equals(4, lab07.number_not(mylist,74)) introcs.assert_equals(5, lab07.number_not(mylist,3)) introcs.assert_equals(7, lab07.number_not(mylist,4))
def test_complement(): """ Test function complement """ introcs.assert_equals(introcs.RGB(255-250, 255-0, 255-71), a2.complement_rgb(introcs.RGB(250, 0, 71))) introcs.assert_equals(introcs.RGB(255-92, 255-128, 255-255), a2.complement_rgb(introcs.RGB(92, 128, 255))) # Make sure we are not modifying the color rgb = introcs.RGB(128,128,128) introcs.assert_not_equals(id(rgb),id(a2.complement_rgb(rgb)))
def test_playerScore(): """ Tests the playerScore method for Blackjack objects """ deck = [card.Card(0, 12), card.Card(1, 10), card.Card(2, 9)] game = bjack.Blackjack(deck) introcs.assert_equals(20, game.playerScore()) game.playerHand = [card.Card(2, 2), card.Card(3, 1)] game.dealerHand = [card.Card(1, 13), card.Card(0, 3)] introcs.assert_equals(13, game.playerScore()) print('The playerScore tests passed')
def test_iscurrency(): """ Test procedure for iscurrency """ print('Testing iscurrency') result = currency.iscurrency('EUR') introcs.assert_equals(True, result) result = currency.iscurrency('EAR') introcs.assert_equals(False, result) pass
def test_read_csv(): """ Test procedure for the function read_csv() """ print('Testing read_csv()') # Access the file relative to this one, not the user's terminal parent = os.path.split(__file__)[0] # First test fpath = os.path.join(parent, 'files', 'readcsv1.csv') table = func.read_csv(fpath) introcs.assert_equals(type(table), list) introcs.assert_true(len(table) > 0 and type(table[0]) == list) introcs.assert_true(len(table[0]) > 0 and type(table[0][0]) == str) introcs.assert_equals(table, FILE1) # Second test fpath = os.path.join(parent, 'files', 'readcsv2.csv') table = func.read_csv(fpath) introcs.assert_equals(type(table), list) introcs.assert_true(len(table) > 0 and type(table[0]) == list) introcs.assert_true(len(table[0]) > 0 and type(table[0][0]) == str) introcs.assert_equals(table, FILE2)
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 test_service_response(): """ Test procedure for service_response """ print('Testing service_response') result = currency.service_response('USD', 'EUR', 2.5) introcs.assert_equals( '{"success": true, "src": "2.5 United States Dollars",' + ' "dst": "2.2160175 Euros", "error": ""}', result) result = currency.service_response('USD', 'ER', 2.5) introcs.assert_equals( '{"success": false, "src": "", "dst": "", "error": "The' + ' rate for currency ER is not present."}', result) result = currency.service_response('Dollars', 'EUR', 2.5) introcs.assert_equals( '{"success": false, "src": "", "dst": "", "error": "The' + ' rate for currency DOLLARS is not present."}', result) result = currency.service_response('USD', 'EUR', -2.5) introcs.assert_equals( '{"success": true, "src": "-2.5 United States Dollars",' + ' "dst": "-2.2160175 Euros", "error": ""}', result)
def test_place_sums(): """ Test procedure for function place_sums """ print('Testing function place_sums') table = [['I1','I2','I3'], [0.8, 0.2], [0.6, 0.9], [0.4, 0.3]] goal = [['I1','I2','I3','Sum'], [0.8, 0.2, 1.0], [0.6, 0.9, 1.5], [0.4, 0.3, 0.7]] funcs.place_sums(table) introcs.assert_equals(goal[0],table[0]) for pos in range(1,len(table)): introcs.assert_float_lists_equal(goal[pos],table[pos]) table = [['I1','I2'], [0.2, -0.6, 0.1], [0.9, 0.8, -1.0]] goal = [['I1','I2','Sum'], [0.2, -0.6, 0.1, -0.3], [0.9, 0.8, -1.0, 0.7]] funcs.place_sums(table) introcs.assert_equals(goal[0],table[0]) for pos in range(1,len(table)): introcs.assert_float_lists_equal(goal[pos],table[pos]) table = [['I1'], [0.4, 0.8, 0.5, 0.4]] goal = [['I1','Sum'], [0.4, 0.8, 0.5, 0.4, 2.1]] funcs.place_sums(table) introcs.assert_equals(goal[0],table[0]) for pos in range(1,len(table)): introcs.assert_float_lists_equal(goal[pos],table[pos]) table = [['I1','I2','I3','I4'], [0.3], [0.5], [0.8], [0.4]] goal = [['I1','I2','I3','I4','Sum'], [0.3, 0.3], [0.5, 0.5], [0.8, 0.8], [0.4, 0.4]] funcs.place_sums(table) introcs.assert_equals(goal[0],table[0]) for pos in range(1,len(table)): introcs.assert_float_lists_equal(goal[pos],table[pos])
def testC(): """ Test for part C """ #----------------tests for currency_response------------------------------------ compare1 = pro.currency_response('USD', 'EUR', 770) introcs.assert_equals( compare1, '{ "valid" : true, "from" : "770 United States Dollars", "to" : "670.89253 Euros", "error" : "" }' ) compare2 = pro.currency_response('USD', 'EUR', -95) introcs.assert_equals( compare2, '{ "valid" : true, "from" : "-95 United States Dollars", "to" : "-82.772455 Euros", "error" : "" }' ) compare3 = pro.currency_response('GHS', 'CAD', 200) introcs.assert_equals( compare3, '{ "valid" : true, "from" : "200 Ghanaian Cedis", "to" : "52.824859437751 Canadian Dollars", "error" : "" }' ) compare4 = pro.currency_response('CAD', 'GHS', 3.35) introcs.assert_equals( compare4, '{ "valid" : true, "from" : "3.35 Canadian Dollars", "to" : "12.683422296457 Ghanaian Cedis", "error" : "" }' )
def compare_images(image1,image2,file1,file2): """ Compares image1 and image2 via assert functions. If the images are the same, nothing happens. Otherwise this function produces an error and quits python. We provide the file names to give use proper error messages Parameter image1: The first image to compare Precondition: image1 is an Image object Parameter image2: The second image to compare Precondition: image2 is an Image object Parameter file1: The file name of the first image Precondition: file1 is an Image object Parameter file2: The file name of the second image Precondition: file2 is an Image object """ introcs.assert_equals(len(image2),len(image1), file1+' and '+file2+' do not have the same pixel size') introcs.assert_equals(image2.getWidth(),image1.getWidth(), file1+' and '+file2+' do not have the same width') introcs.assert_equals(image2.getHeight(),image1.getHeight(), file1+' and '+file2+' do not have the same height') for col in range(image2.getWidth()): for row in range(image2.getHeight()): introcs.assert_equals(image2.getPixel(row,col),image1.getPixel(row,col), 'Pixel mismatch between '+file1+' and '+file2+ ' at ('+str(col)+','+str(row)+')')
def test_image_access(): """ Tests the methods the two-dimensional get/setPixel methods in class Image """ print('Testing image get/setPixel methods') p = [(255,0,0),(0,255,0),(0,0,255),(0,255,255),(255,0,255),(255,255,0)] rgb1 = (255,255,255) rgb2 = (64,128,192) image = a6image.Image(p,2) for n in range(6): introcs.assert_equals(p[n],image.getPixel(n // 2, n % 2)) introcs.assert_equals(id(p[n]),id(image.getPixel(n // 2, n % 2))) image.setPixel(2,1,rgb1) introcs.assert_equals(rgb1,image.getPixel(2,1)) image.setPixel(2,1,rgb2) introcs.assert_equals(rgb2,image.getPixel(2,1)) # Test enforcement introcs.assert_error(image.getPixel, 'a', 1, message='getPixel does not enforce the precondition on row type') introcs.assert_error(image.getPixel, 8, 1, message='getPixel does not enforce the precondition on row value') introcs.assert_error(image.getPixel, 2, 'a', message='getPixel does not enforce the precondition on col value') introcs.assert_error(image.getPixel, 2, 8, message='getPixel does not enforce the precondition on col value') introcs.assert_error(image.setPixel, 'a', 1, (0,0,255), message='setPixel does not enforce the precondition on row type') introcs.assert_error(image.setPixel, 8, 1, (0,0,255), message='setPixel does not enforce the precondition on row value') introcs.assert_error(image.setPixel, 2, 'a', (0,0,255), message='setPixel does not enforce the precondition on col value') introcs.assert_error(image.setPixel, 2, 8, (0,0,255), message='setPixel does not enforce the precondition on col value') introcs.assert_error(image.setPixel, 2, 1, (0,0,'255'), message='setPixel does not enforce the precondition on pixel value')
def test_num_digits(): """ Tests for function num_digits """ introcs.assert_equals(1, lab07.num_digits(0)) introcs.assert_equals(1, lab07.num_digits(3)) introcs.assert_equals(2, lab07.num_digits(34)) introcs.assert_equals(4, lab07.num_digits(1356))
def test_sum_digits(): """ Tests for function sum_digits """ introcs.assert_equals(0, lab07.sum_digits(0)) introcs.assert_equals(3, lab07.sum_digits(3)) introcs.assert_equals(7, lab07.sum_digits(34)) introcs.assert_equals(12, lab07.sum_digits(345))
def test_wild_str(): """ Tests that __str__ works properly for WildCard. """ import wild card = wild.WildCard(1, 12) introcs.assert_equals('Queen of Diamonds', str(card)) card = wild.WildCard(0, 3) introcs.assert_equals('3 of Clubs', str(card)) card = wild.WildCard(3, 1, True) introcs.assert_equals('Ace of Spades [WILD]', str(card)) card = wild.WildCard(1, 12, True) introcs.assert_equals('Queen of Diamonds [WILD]', str(card)) card = wild.WildCard(0, 3, True) introcs.assert_equals('3 of Clubs [WILD]', str(card))
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_files_to_dictionary(): """ Test loading function files_to_dictionary """ files = ['colorblind/normal.dat','colorblind/tritanomaly.dat'] maps = a2.files_to_dictionary(files) introcs.assert_equals(2,len(maps)) introcs.assert_true('Normal' in maps) introcs.assert_true('Tritanomaly' in maps) introcs.assert_float_lists_equal([1,0,0],maps['Normal'][0]) introcs.assert_float_lists_equal([0,1,0],maps['Normal'][1]) introcs.assert_float_lists_equal([0,0,1],maps['Normal'][2]) introcs.assert_float_lists_equal([0.967, 0.033, 0],maps['Tritanomaly'][0]) introcs.assert_float_lists_equal([0, 0.733, 0.267],maps['Tritanomaly'][1]) introcs.assert_float_lists_equal([0, 0.183, 0.817],maps['Tritanomaly'][2])
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 test_file_to_data(): """ Test file function file_to_data """ file = 'colorblind/normal.dat' data = a2.file_to_data(file) introcs.assert_equals('Normal',data[0]) introcs.assert_float_lists_equal([1,0,0],data[1]) introcs.assert_float_lists_equal([0,1,0],data[2]) introcs.assert_float_lists_equal([0,0,1],data[3]) file = 'colorblind/tritanomaly.dat' data = a2.file_to_data(file) introcs.assert_equals('Tritanomaly',data[0]) introcs.assert_float_lists_equal([0.967, 0.033, 0],data[1]) introcs.assert_float_lists_equal([0, 0.733, 0.267],data[2]) introcs.assert_float_lists_equal([0, 0.183, 0.817],data[3])
def test_dataset(): """ Tests the Dataset class. """ print(' Testing class Dataset') # TEST CASE 1 # Create and test an empty dataset dset1 = a6dataset.Dataset(3) introcs.assert_equals(3, dset1.getDimension()) introcs.assert_equals(0, dset1.getSize()) # We use this assert function to compare lists introcs.assert_float_lists_equal([], dset1.getContents()) print(' Default initialization looks okay') # TEST CASE 2 # Create and test a non-empty dataset items = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] dset2 = a6dataset.Dataset(3, items) introcs.assert_equals(3, dset2.getDimension()) introcs.assert_equals(4, dset2.getSize()) # Check that contents is initialized correctly # Make sure items is COPIED assert_point_sets_equal(items, dset2.getContents()) introcs.assert_false(dset2.getContents() is items) introcs.assert_false(dset2.getContents()[0] is items[0]) print(' User-provided initialization looks okay') # Check that getPoint() is correct AND that it copies introcs.assert_float_lists_equal([0.0, 1.0, 0.0], dset2.getPoint(2)) introcs.assert_false(dset2.getContents()[2] is dset2.getPoint(2)) print(' Method Dataset.getPoint looks okay') # Add something to the dataset (and check it was added) dset1.addPoint([0.0, 0.5, 4.2]) assert_point_sets_equal([[0.0, 0.5, 4.2]], dset1.getContents()) introcs.assert_float_lists_equal([0.0, 0.5, 4.2], dset1.getPoint(0)) # Check the point is COPIED introcs.assert_false(dset1.getPoint(0) is dset1.getContents()[0]) extra = [0.0, 0.5, 4.2] dset2.addPoint(extra) items.append(extra) assert_point_sets_equal(items, dset2.getContents()) # Check the point was COPIED introcs.assert_false(id(extra) in map(id, dset2.getContents())) print(' Method Dataset.addPoint looks okay') print(' class Dataset appears correct') print()