Example #1
0
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)
Example #2
0
def testC():
    """testC() tests the function
    currency_response(amount_from, currency_from, currency_to) in module a1.py"""

    amount_from = 5.0
    currency_from = 'USD'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "5 U.S. dollars",rhs: "3.77216145 Euros",error: "",icc: true}', result)

    amount_from = -5.0
    currency_from = 'USD'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "-5 U.S. dollars",rhs: "-3.77216145 Euros",error: "",icc: true}', result)

    amount_from = 'hello'
    currency_from = 'USD'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "",rhs: "",error: "4",icc: false}', result)

    amount_from = 5.0
    currency_from = 'US'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "",rhs: "",error: "4",icc: false}', result)

    amount_from = 5.0
    currency_from = 'USD'
    currency_to = 'EU'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "",rhs: "",error: "4",icc: false}', result)
Example #3
0
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 test_replace_first():
    """Test procedure for repeat_first"""
    print 'Testing the function replace_first'

    print "\tTesting crane, a-> o"
    result = lab03.replace_first('crane', 'a', 'o')
    cornelltest.assert_equals('crone', result)
def test_first_in_double_quotes():
    """Test procedure for first_in_double_quotes"""
    print 'Testing the function first_in_double_quotes'

    print '\tTesting \'A \"B C\" D\''
    result = lab03.first_in_double_quotes('A "B C" D')
    cornelltest.assert_equals('B C', result)
def test_str5_color():
    """Test the str5 functions for cmyk and hsv."""
    cornelltest.assert_equals('(98.45, 25.36, 99.99, 21.99)',\
            a3.str5_cmyk(colormodel.CMYK(98.448, 25.362, 99.99, 21.994)));
    
    cornelltest.assert_equals('(32.33, 6.355, 0.001, 10.01)',\
    a3.str5_cmyk(colormodel.CMYK(32.3256, 6.3546, .0013, 10.013567)))
Example #7
0
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)
Example #8
0
def testE():
    """Test Part E (of Part I) of the assignment. 

    This tests the final part of K-means.  It gets a lot easier from here.
    As with the test for Part D, we have to use random.seed to fix
    the random number generator."""
    print '  Testing Part E'
    # Force the random number generator to not be random
    random.seed(3) # More interesting result than a seed of 1
    
    # FOR ALL TEST CASES    
	# Create and initialize a non-empty database
    items = [[0.5,0.5,0.5],[0.5,0.6,0.6],[0.6,0.5,0.6],[0.5,0.6,0.5],[0.5,0.4,0.5],[0.5,0.4,0.4]]
    dbase = Database(3,items)
    dbase.setKSize(2)
    
    # PRE-TEST: Check first cluster (should be okay if passed part D)
    cluster1 = dbase.getCluster(0)
    cornelltest.assert_float_lists_equal([0.5, 0.6, 0.6],cluster1.getCentroid())
    cornelltest.assert_float_lists_equal(items[1:3],cluster1.getContents())

    # PRE-TEST: Check second cluster (should be okay if passed part D)
    cluster2 = dbase.getCluster(1)
    cornelltest.assert_float_lists_equal([0.5, 0.6, 0.5],cluster2.getCentroid())
    cornelltest.assert_float_lists_equal([items[0]]+items[3:],cluster2.getContents())
    
    # Make a copy of a cluster (to test update() method)
    clustertest = Cluster(cluster1.getCentroid())
    for point in cluster1.getContents():
        clustertest.appendContents(point)
    
    # TEST CASE 1 (update)
    stable = clustertest.update()
    cornelltest.assert_float_lists_equal([0.55, 0.55, 0.6],clustertest.getCentroid())
    cornelltest.assert_false(stable) # Not yet stable

    # TEST CASE 2 (update)
    stable = clustertest.update()
    cornelltest.assert_float_lists_equal([0.55, 0.55, 0.6],clustertest.getCentroid())
    cornelltest.assert_true(stable) # Now it is stable
    print '    Method update() looks okay'
    
    # TEST CASE 3 (step)
    dbase.step()
    
    # K size should be unchanged
    cornelltest.assert_equals(2,dbase.getKSize())

    # Check first cluster (WHICH HAS CHANGED!)  
    cluster1 = dbase.getCluster(0)
    cornelltest.assert_float_lists_equal([0.55, 0.55, 0.6],cluster1.getCentroid())
    cornelltest.assert_float_lists_equal(items[1:4],cluster1.getContents())

    # Check second cluster (WHICH HAS CHANGED!)  
    cluster2 = dbase.getCluster(1)
    cornelltest.assert_float_lists_equal([0.5, 0.475, 0.475],cluster2.getCentroid())
    cornelltest.assert_float_lists_equal([items[0]]+items[4:],cluster2.getContents())
    print '    Method step() looks okay'
    print '  Part E appears correct'
def test_complement():
    """Test function complement"""
    cornelltest.assert_equals(
        colormodel.RGB(255 - 250, 255 - 0, 255 - 71), a3.complement_rgb(colormodel.RGB(250, 0, 71))
    )

    # Add another test
    cornelltest.assert_equals(colormodel.RGB(254, 175, 42), a3.complement_rgb(colormodel.RGB(1, 80, 213)))
Example #10
0
def test_num_runs():
    """Test the function num_space_runs"""
    print '  Testing num_space_runs'
    cornelltest.assert_equals(4, lab12.num_space_runs('  a  f   g    '))
    cornelltest.assert_equals(2, lab12.num_space_runs('a  f   g'))
    cornelltest.assert_equals(3, lab12.num_space_runs('  a  bc   d'))
    cornelltest.assert_equals(1, lab12.num_space_runs('  a'))
    cornelltest.assert_equals(0, lab12.num_space_runs('ab'))
    print '  num_space_runs looks okay'
Example #11
0
def test_numberof():
    """Test the function numberof"""
    print '  Testing numberof'
    mylist = [5, 3, 3455, 74, 74, 74, 3]
    cornelltest.assert_equals(3, lab11.numberof(mylist, 74))
    cornelltest.assert_equals(2, lab11.numberof(mylist, 3))
    cornelltest.assert_equals(0, lab11.numberof(mylist, 4))
    cornelltest.assert_equals(1, lab11.numberof([4], 4))
    cornelltest.assert_equals(0, lab11.numberof([], 4))
    print '  numberof looks okay'
Example #12
0
def testC():
    #   TEST ONE
    print 'Testing function currency_response 1'
    result = a1.currency_response('USD', 'EUR', 2.5)
    cornelltest.assert_equals(
        '{ "from" : "2.5 United States Dollars", "to" : "2.24075 Euros", "success" : true, "error" : "" }',
        result)
    #   TEST TWO
    print 'Testing function currency_response 2'
    result = a1.currency_response('TND', 'BDT', 3453.67)
    cornelltest.assert_equals(
        '{ "from" : "3453.67 Tunisian Dinar", "to" : "122560.93186266 Bangladeshi Taka", "success" : true, "error" : "" }',
        result)
Example #13
0
def testB():
    """Test Part B (of Part I) of the assignment. 
    
    This test procedure includes getCluster from part A.  To test getCluster,
    we have to initialize your _clusters attribute.  We can only do this by
    accessing the hidden attribute _clusters in this function.  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 B'
    # TEST CASE 1
    # Create and test a cluster (always empty)
    point = [0.0,1.0,0.0]
    cluster1 = Cluster(point)
    
    # Compare centroid and contents
    cornelltest.assert_float_lists_equal(point,cluster1.getCentroid())
    cornelltest.assert_float_lists_equal([],cluster1.getContents())
    # Make sure centroid COPIED
    cornelltest.assert_not_equals(id(point),id(cluster1.getContents()))
	
    # Add something to cluster (and check it was added)
    extra = [0.0,0.5,4.2]
    cluster1.appendContents(extra)
    # Cluster is a 2D-list.
    cornelltest.assert_float_lists_equal([extra],cluster1.getContents())
    # Check the point was COPIED
    cornelltest.assert_false(id(extra) in map(id,cluster1.getContents()))

    # And clear it
    cluster1.clearContents()
    cornelltest.assert_float_lists_equal([],cluster1.getContents())
    print '    Basic cluster methods look okay'
    
    # TEST CASE 2 (getCluster)
    # Make a second cluster
    cluster2 = Cluster([0.0,0.0,0.0])
    
    # Now make a database and put these in _clusters attribute
    dbase = Database(3)
    # THIS VIOLATES GOOD PROGRAMMING. But sometimes rules must be broken.    
    dbase._clusters = [cluster1,cluster2]
    dbase._ksize = 2
    
    # Check that I get the right objects back
    # MUST COMPARE FOLDER IDENTIFIERS.  Use the id function.
    cornelltest.assert_equals(id(cluster1),id(dbase.getCluster(0)))
    cornelltest.assert_equals(id(cluster2),id(dbase.getCluster(1)))
    print '    Method getCluster() looks okay'
    print '  Part B appears correct'
Example #14
0
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'
Example #15
0
def testB():
    """Test Part B (of Part I) of the assignment. 
    
    This test procedure includes getCluster from part A.  To test getCluster,
    we have to initialize your _clusters attribute.  We can only do this by
    accessing the hidden attribute _clusters in this function.  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 B'
    # TEST CASE 1
    # Create and test a cluster (always empty)
    point = [0.0, 1.0, 0.0]
    cluster1 = Cluster(point)

    # Compare centroid and contents
    cornelltest.assert_float_lists_equal(point, cluster1.getCentroid())
    cornelltest.assert_float_lists_equal([], cluster1.getContents())
    # Make sure centroid COPIED
    cornelltest.assert_not_equals(id(point), id(cluster1.getContents()))

    # Add something to cluster (and check it was added)
    extra = [0.0, 0.5, 4.2]
    cluster1.appendContents(extra)
    # Cluster is a 2D-list.
    cornelltest.assert_float_lists_equal([extra], cluster1.getContents())
    # Check the point was COPIED
    cornelltest.assert_false(id(extra) in map(id, cluster1.getContents()))

    # And clear it
    cluster1.clearContents()
    cornelltest.assert_float_lists_equal([], cluster1.getContents())
    print '    Basic cluster methods look okay'

    # TEST CASE 2 (getCluster)
    # Make a second cluster
    cluster2 = Cluster([0.0, 0.0, 0.0])

    # Now make a database and put these in _clusters attribute
    dbase = Database(3)
    # THIS VIOLATES GOOD PROGRAMMING. But sometimes rules must be broken.
    dbase._clusters = [cluster1, cluster2]
    dbase._ksize = 2

    # Check that I get the right objects back
    # MUST COMPARE FOLDER IDENTIFIERS.  Use the id function.
    cornelltest.assert_equals(id(cluster1), id(dbase.getCluster(0)))
    cornelltest.assert_equals(id(cluster2), id(dbase.getCluster(1)))
    print '    Method getCluster() looks okay'
    print '  Part B appears correct'
Example #16
0
def test_split():
    """Test the function split"""
    print '  Testing split'
    cornelltest.assert_equals(['a', 'b', 'c', 'd'], lab12.split('a b c d '))
    cornelltest.assert_equals(['ab', 'cd'], lab12.split('ab cd '))
    cornelltest.assert_equals(['ab', 'c', 'de'], lab12.split('ab c de '))
    cornelltest.assert_equals(['a'], lab12.split('a '))
    print '  split looks okay'
def testC():
    """Returns:'Module name is working correctly' if function currency_response
    test cases work."""
    #First test case
    result = a1.currency_response('USD', 'EUR', 2.5)
    cornelltest.assert_equals(
        '{ "from" : "2.5 United States Dollars", "to" ' +
        ': "2.24075 Euros", "success" : true, "error" ' + ': "" }', result)

    #Second test case
    result = a1.currency_response('BBD', 'USD', 3000)
    cornelltest.assert_equals(
        '{ "from" : "3000 Barbadian Dollars", "to" : ' +
        '"1500 United States Dollars", "success" : ' + 'true, "error" : "" }',
        result)
def test_dataset():
    """Test the Dataset class."""
    print '  Testing class Dataset'

    # TEST CASE 1
    # Create and test an empty dataset
    dset1 = a6.Dataset(3)
    cornelltest.assert_equals(3, dset1.getDimension())
    cornelltest.assert_equals(0, dset1.getSize())

    # We use this assert function to compare lists
    cornelltest.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 = a6.Dataset(3, items)
    cornelltest.assert_equals(3, dset2.getDimension())
    cornelltest.assert_equals(4, dset2.getSize())

    # Check that contents is initialized correctly
    # Make sure items is COPIED
    cornelltest.assert_float_lists_equal(items, dset2.getContents())
    cornelltest.assert_false(dset2.getContents() is items)
    cornelltest.assert_false(dset2.getContents()[0] is items[0])

    print '    User-provided initialization looks okay'

    # Check that getPoint() is correct AND that it copies
    cornelltest.assert_float_lists_equal([0.0, 1.0, 0.0], dset2.getPoint(2))
    cornelltest.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])
    cornelltest.assert_float_lists_equal([[0.0, 0.5, 4.2]],
                                         dset1.getContents())
    cornelltest.assert_float_lists_equal([0.0, 0.5, 4.2], dset1.getPoint(0))
    # Check the point is COPIED
    cornelltest.assert_false(dset1.getPoint(0) is dset1.getContents()[0])

    extra = [0.0, 0.5, 4.2]
    dset2.addPoint(extra)
    items.append(extra)
    cornelltest.assert_float_lists_equal(items, dset2.getContents())
    # Check the point was COPIED
    cornelltest.assert_false(id(extra) in map(id, dset2.getContents()))

    print '    Method Dataset.addPoint looks okay'
    print '  class Dataset appears correct'
    print ''
Example #19
0
def test_replace_copy():
    """Test the function replace_copy"""
    print '  Testing replace_copy'
    cornelltest.assert_equals([4], lab11.replace_copy([5], 5, 4))
    cornelltest.assert_equals([], lab11.replace_copy([], 1, 2))
    mylist = [5, 3, 3455, 74, 74, 74, 3]
    cornelltest.assert_equals([5, 20, 3455, 74, 74, 74, 20],
                              lab11.replace_copy(mylist, 3, 20))
    cornelltest.assert_equals([5, 3, 3455, 74, 74, 74, 3],
                              lab11.replace_copy(mylist, 1, 3))
    print '  replace_copy looks okay'
Example #20
0
def test_uniques():
    """Test procedure for function uniques"""
    print 'Testing function uniques'
    thelist = [5, 9, 5, 7]
    cornelltest.assert_equals(3, lab07.uniques(thelist))

    thelist = [5, 5, 1, 'a', 5, 'a']
    cornelltest.assert_equals(3, lab07.uniques(thelist))

    thelist = [1, 2, 3, 4, 5]
    cornelltest.assert_equals(5, lab07.uniques(thelist))

    thelist = []
    cornelltest.assert_equals(0, lab07.uniques(thelist))

    # Make sure the function does not modify the original
    thelist = [5, 9, 5, 7]
    result = lab07.uniques(thelist)
    cornelltest.assert_equals([5, 9, 5, 7], thelist)
Example #21
0
def test_dataset():
    """Test the Dataset class."""
    print '  Testing class Dataset'
    
    # TEST CASE 1
    # Create and test an empty dataset
    dset1 = a6.Dataset(3)
    cornelltest.assert_equals(3,dset1.getDimension())
    cornelltest.assert_equals(0,dset1.getSize())
    
    # We use this assert function to compare lists
    cornelltest.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 = a6.Dataset(3,items)
    cornelltest.assert_equals(3,dset2.getDimension())
    cornelltest.assert_equals(4,dset2.getSize())
    
    # Check that contents is initialized correctly
    # Make sure items is COPIED
    cornelltest.assert_float_lists_equal(items,dset2.getContents())
    cornelltest.assert_false(dset2.getContents() is items)
    cornelltest.assert_false(dset2.getContents()[0] is items[0])
    
    print '    User-provided initialization looks okay'
    
    # Check that getPoint() is correct AND that it copies
    cornelltest.assert_float_lists_equal([0.0,1.0,0.0],dset2.getPoint(2))
    cornelltest.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])
    cornelltest.assert_float_lists_equal([[0.0,0.5,4.2]],dset1.getContents())
    cornelltest.assert_float_lists_equal([0.0,0.5,4.2],dset1.getPoint(0))
    # Check the point is COPIED
    cornelltest.assert_false(dset1.getPoint(0) is dset1.getContents()[0])
    
    extra = [0.0,0.5,4.2]
    dset2.addPoint(extra)
    items.append(extra)
    cornelltest.assert_float_lists_equal(items,dset2.getContents())
    # Check the point was COPIED
    cornelltest.assert_false(id(extra) in map(id,dset2.getContents()))
    
    print '    Method Dataset.addPoint looks okay'
    print '  class Dataset appears correct'
    print ''
def test_round5_color():
    """Test the round5 functions for cmyk and hsv."""
    # Tests for round5_cmyk (add one more)
    cornelltest.assert_equals(
        "(98.45, 25.36, 72.80, 25.00)", a3.round5_cmyk(colormodel.CMYK(98.448, 25.362, 72.8, 25.0))
    )
    cornelltest.assert_equals(
        "(30.64, 89.03, 3.090, 26.90)", a3.round5_cmyk(colormodel.CMYK(30.643, 89.029, 3.0900, 26.901))
    )
    # Tests for round5_hsv (add two)
    cornelltest.assert_equals("(300.0, 0.123, 0.576)", a3.round5_hsv(colormodel.HSV(300.0001, 0.12321, 0.5759)))
    cornelltest.assert_equals("(125.4, 0.875, 0.537)", a3.round5_hsv(colormodel.HSV(125.433, 0.8748, 0.53741)))
Example #23
0
def test_str5_color():
    """Test the str5 functions for cmyk and hsv."""
    cornelltest.assert_equals(
        '(98.45, 25.36, 72.80, 1.000)',
        a3.str5_cmyk(colormodel.CMYK(98.448, 25.362, 72.8, 1.0)))
    cornelltest.assert_equals(
        '(1.000, 100.0, 23.12, 54.44)',
        a3.str5_cmyk(colormodel.CMYK(1, 100, 23.12345, 54.435)))
    cornelltest.assert_equals('(98.45, 0.000, 0.123)',
                              a3.str5_hsv(colormodel.HSV(98.448, 0, .123456)))
    cornelltest.assert_equals('(312.0, 0.500, 0.568)',
                              a3.str5_hsv(colormodel.HSV(312, 0.5, 0.56789)))
Example #24
0
def testA():
    """Test Part A (of Part I) of the assignment. 
    
    This test procedure cannot test getCluster, as there are
    no clusters yet.  That test is moved to part B."""
    print '  Testing Part A'
    # TEST CASE 1
    # Create and test an empty database
    dbase = Database(3)
    cornelltest.assert_equals(3, dbase.getDimension())
    cornelltest.assert_equals(0, dbase.getKSize())

    # We use this BRAND NEW ASSERT to compare lists
    cornelltest.assert_float_lists_equal([], dbase.getContents())

    # Add something to the database (and check it was added)
    dbase.appendContents([0.0, 0.5, 4.2])
    # Database is a 2D-list.
    cornelltest.assert_float_lists_equal([[0.0, 0.5, 4.2]],
                                         dbase.getContents())

    # And clear it
    dbase.clearContents()
    cornelltest.assert_float_lists_equal([], dbase.getContents())
    print '    Default initialization looks okay'

    # TEST CASE 2
    # Create and test a non-empty database
    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]]
    dbase = Database(3, items)
    cornelltest.assert_equals(3, dbase.getDimension())
    cornelltest.assert_equals(0, dbase.getKSize())

    # Check that contents is initialized correctly
    # Make sure items is COPIED
    cornelltest.assert_float_lists_equal(items, dbase.getContents())
    cornelltest.assert_not_equals(id(items), id(dbase.getContents()))

    # Add something to the database (and check it was added)
    extra = [0.0, 0.5, 4.2]
    dbase.appendContents(extra)
    items.append(extra)
    cornelltest.assert_float_lists_equal(items, dbase.getContents())
    # Check the point was COPIED
    cornelltest.assert_false(id(extra) in map(id, dbase.getContents()))

    # And clear it
    dbase.clearContents()
    cornelltest.assert_float_lists_equal([], dbase.getContents())
    print '    User-given contents looks okay'
    print '  Part A appears correct'
def test_str5_color():
    """Test the str5 functions for cmyk and hsv."""
    cornelltest.assert_equals(
        '(98.45, 25.36, 72.80, 1.000)',
        a3.str5_cmyk(colormodel.CMYK(98.448, 25.362, 72.8, 1.0)))
    cornelltest.assert_equals(
        '(100.0, 0.225, 83.50, 0.000)',
        a3.str5_cmyk(colormodel.CMYK(100.0, .2245, 83.5, 0.0)))
    cornelltest.assert_equals(
        '(56.00, 0.000, 0.987)',
        a3.str5_hsv(colormodel.HSV(55.999, 0.0, 0.9874234353442)))
    cornelltest.assert_equals(
        '(250.4, 1.000, 0.488)',
        a3.str5_hsv(colormodel.HSV(250.356, 1.0, .4878325345)))
Example #26
0
def testA():
    """Test Part A (of Part I) of the assignment. 
    
    This test procedure cannot test getCluster, as there are
    no clusters yet.  That test is moved to part B."""
    print '  Testing Part A'
    # TEST CASE 1
    # Create and test an empty database
    dbase = Database(3)
    cornelltest.assert_equals(3,dbase.getDimension())
    cornelltest.assert_equals(0,dbase.getKSize())

    # We use this BRAND NEW ASSERT to compare lists
    cornelltest.assert_float_lists_equal([],dbase.getContents())
	
	# Add something to the database (and check it was added)
    dbase.appendContents([0.0,0.5,4.2])
    # Database is a 2D-list.
    cornelltest.assert_float_lists_equal([[0.0,0.5,4.2]],dbase.getContents())
	
	# And clear it
    dbase.clearContents()
    cornelltest.assert_float_lists_equal([],dbase.getContents())
    print '    Default initialization looks okay'
	
    # TEST CASE 2	
	# Create and test a non-empty database
    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]]
    dbase = Database(3,items)
    cornelltest.assert_equals(3,dbase.getDimension())
    cornelltest.assert_equals(0,dbase.getKSize())
	
	# Check that contents is initialized correctly
	# Make sure items is COPIED
    cornelltest.assert_float_lists_equal(items,dbase.getContents())
    cornelltest.assert_not_equals(id(items),id(dbase.getContents()))

	# Add something to the database (and check it was added)
    extra = [0.0,0.5,4.2]
    dbase.appendContents(extra)
    items.append(extra)
    cornelltest.assert_float_lists_equal(items,dbase.getContents())
    # Check the point was COPIED
    cornelltest.assert_false(id(extra) in map(id,dbase.getContents()))
	
	# And clear it
    dbase.clearContents()
    cornelltest.assert_float_lists_equal([],dbase.getContents())
    print '    User-given contents looks okay'
    print '  Part A appears correct'
Example #27
0
def testC():
    """testC() tests the function
    currency_response(amount_from, currency_from, currency_to) in module a1.py"""

    amount_from = 5.0
    currency_from = 'USD'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals(
        '{lhs: "5 U.S. dollars",rhs: "3.77216145 Euros",error: "",icc: true}',
        result)

    amount_from = -5.0
    currency_from = 'USD'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals(
        '{lhs: "-5 U.S. dollars",rhs: "-3.77216145 Euros",error: "",icc: true}',
        result)

    amount_from = 'hello'
    currency_from = 'USD'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "",rhs: "",error: "4",icc: false}',
                              result)

    amount_from = 5.0
    currency_from = 'US'
    currency_to = 'EUR'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "",rhs: "",error: "4",icc: false}',
                              result)

    amount_from = 5.0
    currency_from = 'USD'
    currency_to = 'EU'
    result = currency_response(amount_from, currency_from, currency_to)
    cornelltest.assert_equals('{lhs: "",rhs: "",error: "4",icc: false}',
                              result)
Example #28
0
def test_pmap_to_word_list():
    """Test function pmap_to_word_list"""
    print 'Testing function pmap_to_word_list'
    
    # Start with an empty prefix map
    pmap = {}
    words = a4.pmap_to_word_list(pmap)
    
    # Should be empty list
    cornelltest.assert_equals(list, type(words))
    cornelltest.assert_equals(0, len(words))
    
    # Build a pmap manually
    pmap = { '':['a'], 'a':['','t'], 'at':[''] }
    words =  a4.pmap_to_word_list(pmap)
    assert_lists_equal(['a', 'at'], words)
    
    # See if we can go back and forth
    expected = ['the', 'be','to','of','and','a','in','that','have','it']
    pmap  = a4.word_list_to_pmap(expected)
    words = a4.pmap_to_word_list(pmap)
    assert_lists_equal(expected, words)
Example #29
0
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))
Example #30
0
def test_minpos1():
    """Test the function minpos1"""
    print '  Testing minpos1'
    a = [0, 3, 4, 2, 5, 1]
    cornelltest.assert_equals(0, lab13.minpos1(a, 0, 5))
    cornelltest.assert_equals(5, lab13.minpos1(a, 1, 5))
    cornelltest.assert_equals(3, lab13.minpos1(a, 1, 4))
    print '  minpos1 looks okay'
Example #31
0
def test_complement():
    """Test function complement"""
    cornelltest.assert_equals(colormodel.RGB(255 - 250, 255 - 0, 255 - 71),
                              a3.complement_rgb(colormodel.RGB(250, 0, 71)))
    cornelltest.assert_equals(colormodel.RGB(255 - 255, 255 - 255, 255 - 255),
                              a3.complement_rgb(colormodel.RGB(255, 255, 255)))
    cornelltest.assert_equals(colormodel.RGB(255 - 0, 255 - 0, 255 - 0),
                              a3.complement_rgb(colormodel.RGB(0, 0, 0)))
def testD():
    """Returns:'Module name is working correctly' if functions iscurrency and
    exchange test cases work."""
    #First is_currency test case
    result = a1.iscurrency('USD')
    cornelltest.assert_equals(True, result)

    #Second is_currency test case suggested by Consultant Nancy Shen (nws37)
    result = a1.iscurrency('hi')
    cornelltest.assert_equals(False, result)
    #End Nancy's suggestion

    #Third is_currency test case
    result = a1.iscurrency('usd')
    cornelltest.assert_equals(False, result)

    #First exchange test case
    result = a1.exchange('USD', 'EUR', 2.5)
    cornelltest.assert_equals(2.24075, result)

    #Second test case
    result = a1.exchange('BBD', 'USD', 3000)
    cornelltest.assert_equals(1500, result)
Example #33
0
def testD():
    #   TEST ONE
    print 'Testing function iscurrency 1'
    result = a1.iscurrency('USD')
    cornelltest.assert_equals(True, result)
    #   TEST TWO
    print 'Testing function iscurrency 2'
    result = a1.iscurrency('ZZK')
    cornelltest.assert_equals(False, result)
    #   TEST ONE
    print 'Testing function exchange 1'
    result = a1.exchange('USD', 'EUR', 2.5)
    cornelltest.assert_equals(2.24075, result)
    #   TEST ONE
    print 'Testing function exchange 2'
    result = a1.exchange('GEL', 'SOS', 0.0)
    cornelltest.assert_equals(0.0, result)
Example #34
0
def test_first_inside_quotes():
    print('Testing function first_inside_quotes()')
    
    s='The instructions say "Dry-clean only".'
    result = lab03.first_inside_quotes(s)
    cornelltest.assert_equals('Dry-clean only',result)
    
    s='A "B C" D "E F" G'
    result = lab03.first_inside_quotes(s)
    cornelltest.assert_equals('B C',result)
    
    s='"The" instructions say Dry-clean only.'
    result = lab03.first_inside_quotes(s)
    cornelltest.assert_equals('The',result)
    
    s='"The" "instructions" "say" "Dry-clean only".'
    result = lab03.first_inside_quotes(s)
    cornelltest.assert_equals('The',result)
Example #35
0
def test_replace():
    """Test the function replace"""
    print '  Testing replace'
    mylist = [5]
    lab11.replace(mylist, 5, 4)
    cornelltest.assert_equals([4], mylist)

    mylist = []
    lab11.replace(mylist, 1, 2)
    cornelltest.assert_equals([], mylist)

    mylist = [5, 3, 3455, 74, 74, 74, 3]
    lab11.replace(mylist, 3, 20)
    cornelltest.assert_equals([5, 20, 3455, 74, 74, 74, 20], mylist)

    lab11.replace(mylist, 1, 3)
    cornelltest.assert_equals([5, 20, 3455, 74, 74, 74, 20], mylist)
    print '  replace looks okay'
Example #36
0
def test_pmap_add_word():
    """Test function pmap_add_word"""
    print 'Testing pmap_add_word'
    
    # Start with an empty prefix map
    pmap = {}
    a4.pmap_add_word(pmap,'a')
    
    # Verify that pmap now has two keys (space and 'a')
    # Note use of helper to make this test easier to read
    cornelltest.assert_equals(2, len(pmap))
    cornelltest.assert_true('' in pmap)
    assert_lists_equal(['a'], pmap[''])
    cornelltest.assert_true('a' in pmap)
    assert_lists_equal([''], pmap['a'])
    
    # Add something with two letters
    a4.pmap_add_word(pmap,'by')
    
    # Verify the keys again
    cornelltest.assert_equals(4, len(pmap))
    cornelltest.assert_true('' in pmap)
    assert_lists_equal(['a','b'], pmap[''])
    cornelltest.assert_true('a' in pmap)
    assert_lists_equal([''], pmap['a'])
    cornelltest.assert_true('b' in pmap)
    assert_lists_equal(['y'], pmap['b'])
    cornelltest.assert_true('by' in pmap)
    assert_lists_equal([''], pmap['by'])
    
    # One last time with overlap
    a4.pmap_add_word(pmap,'at')
    
    # Verify the keys again
    cornelltest.assert_equals(5, len(pmap))
    cornelltest.assert_true('' in pmap)
    assert_lists_equal(['a','b'], pmap[''])
    cornelltest.assert_true('a' in pmap)
    assert_lists_equal(['','t'], pmap['a'])
    cornelltest.assert_true('at' in pmap)
    assert_lists_equal([''], pmap['at'])
    cornelltest.assert_true('b' in pmap)
    assert_lists_equal(['y'], pmap['b'])
    cornelltest.assert_true('by' in pmap)
    assert_lists_equal([''], pmap['by'])
    
    a4.pmap_add_word(pmap,'that')
    cornelltest.assert_true('that' in pmap)
    cornelltest.assert_true('' in pmap['that'])
    
    a4.pmap_add_word(pmap,'goop')
    cornelltest.assert_true('goop' in pmap)
    cornelltest.assert_true('o' in pmap['go'])
    cornelltest.assert_true('p' in pmap['goo'])
Example #37
0
def test_sort2():
    """Test the function sort2"""
    print '  Testing sort2'
    a = [0, 3, 2, 6, 4, 8, 7, 5]
    lab13.sort1(a)
    cornelltest.assert_equals([0, 2, 3, 4, 5, 6, 7, 8], a)
    lab13.sort1(a)
    cornelltest.assert_equals([0, 2, 3, 4, 5, 6, 7, 8], a)
    a = [6, 5, 4, 3, 2, 1, 0]
    lab13.sort1(a)
    cornelltest.assert_equals([0, 1, 2, 3, 4, 5, 6], a)
    print '  sort2 looks okay'
Example #38
0
def test_clamp():
    """Test procedure for function clamp"""
    print 'Testing function clamp'

    thelist = [-1, 1, 3, 5]
    lab07.clamp(thelist, 0, 4)
    # You CAN use assert_equals to compare lists
    # Though we will see a better way in A4
    cornelltest.assert_equals([0, 1, 3, 4], thelist)

    thelist = [1, 3]
    lab07.clamp(thelist, 0, 4)
    cornelltest.assert_equals([1, 3], thelist)

    thelist = [-1, 1, 3, 5]
    lab07.clamp(thelist, 1, 1)
    cornelltest.assert_equals([1, 1, 1, 1], thelist)

    thelist = []
    lab07.clamp(thelist, 0, 4)
    cornelltest.assert_equals([], thelist)
Example #39
0
def test_word_list_to_pmap():
    """Test function word_list_to_pmap"""
    print 'Testing function word_list_to_pmap'

    # Start with an empty word list
    words = []
    pmap = a4.word_list_to_pmap(words)

    # Should be empty dictionary
    cornelltest.assert_equals(dict, type(pmap))
    cornelltest.assert_equals(0, len(pmap))

    # One word, two letters
    words = ['at']
    pmap = a4.word_list_to_pmap(words)

    # Similar test format to pmap_add_word
    cornelltest.assert_equals(3, len(pmap))
    assert_lists_equal(['a'], pmap[''])
    cornelltest.assert_true('a' in pmap)
    assert_lists_equal(['t'], pmap['a'])
    cornelltest.assert_true('at' in pmap)
    assert_lists_equal([''], pmap['at'])

    # Several words
    words = ['at', 'by', 'a']
    pmap = a4.word_list_to_pmap(words)

    # Similar test format to pmap_add_word
    cornelltest.assert_equals(5, len(pmap))
    cornelltest.assert_true('' in pmap)
    assert_lists_equal(['a', 'b'], pmap[''])
    cornelltest.assert_true('a' in pmap)
    assert_lists_equal(['', 't'], pmap['a'])
    cornelltest.assert_true('at' in pmap)
    assert_lists_equal([''], pmap['at'])
    cornelltest.assert_true('b' in pmap)
    assert_lists_equal(['y'], pmap['b'])
    cornelltest.assert_true('by' in pmap)
    assert_lists_equal([''], pmap['by'])
Example #40
0
def test_word_list_to_pmap():
    """Test function word_list_to_pmap"""
    print 'Testing function word_list_to_pmap'
    
    # Start with an empty word list
    words = []
    pmap = a4.word_list_to_pmap(words)
    
    # Should be empty dictionary
    cornelltest.assert_equals(dict, type(pmap))
    cornelltest.assert_equals(0, len(pmap))
    
    # One word, two letters
    words = ['at']
    pmap = a4.word_list_to_pmap(words)
    
    # Similar test format to pmap_add_word
    cornelltest.assert_equals(3, len(pmap))
    assert_lists_equal(['a'], pmap[''])
    cornelltest.assert_true('a' in pmap)
    assert_lists_equal(['t'], pmap['a'])
    cornelltest.assert_true('at' in pmap)
    assert_lists_equal([''], pmap['at'])
    
    # Several words
    words = ['at', 'by', 'a']
    pmap = a4.word_list_to_pmap(words)
    
    # Similar test format to pmap_add_word
    cornelltest.assert_equals(5, len(pmap))
    cornelltest.assert_true('' in pmap)
    assert_lists_equal(['a','b'], pmap[''])
    cornelltest.assert_true('a' in pmap)
    assert_lists_equal(['','t'], pmap['a'])
    cornelltest.assert_true('at' in pmap)
    assert_lists_equal([''], pmap['at'])
    cornelltest.assert_true('b' in pmap)
    assert_lists_equal(['y'], pmap['b'])
    cornelltest.assert_true('by' in pmap)
    assert_lists_equal([''], pmap['by'])
Example #41
0
def testC():
    #Test case 1
    result = a1.currency_response('USD', 'EUR', 2.5)
    cornelltest.assert_equals(
        '{ "from" : "2.5 United States Dollars", "to"' +
        ' : "2.24075 Euros", "success" : true, "error" : "" }', result)
    result = a1.currency_response('USD', 'ETB', 1.0)
    cornelltest.assert_equals(
        '{ "from" : "1 United States Dollar", "to"' +
        ' : "22.09099 Ethiopian Birr", "success" : true, "error" : "" }',
        result)
    result = a1.currency_response('ETB', 'USD', 22.09099)
    cornelltest.assert_equals(
        '{ "from" : "22.09099 Ethiopian Birr", "to"' +
        ' : "1 United States Dollar", "success" : true, "error" : "" }',
        result)
def test_truncate5():
    """Test function truncate5"""
    cornelltest.assert_equals("130.5", a3.truncate5(130.59))
    cornelltest.assert_equals("130.5", a3.truncate5(130.54))
    cornelltest.assert_equals("100.0", a3.truncate5(100))
    cornelltest.assert_equals("99.56", a3.truncate5(99.566))
    cornelltest.assert_equals("99.99", a3.truncate5(99.99))
    cornelltest.assert_equals("99.99", a3.truncate5(99.995))
    cornelltest.assert_equals("21.99", a3.truncate5(21.99575))
    cornelltest.assert_equals("21.99", a3.truncate5(21.994))
    cornelltest.assert_equals("10.01", a3.truncate5(10.013567))
    cornelltest.assert_equals("10.00", a3.truncate5(10.000000005))
    cornelltest.assert_equals("9.999", a3.truncate5(9.9999))
    cornelltest.assert_equals("9.999", a3.truncate5(9.9993))
    cornelltest.assert_equals("1.354", a3.truncate5(1.3546))
    cornelltest.assert_equals("1.354", a3.truncate5(1.3544))
    cornelltest.assert_equals("0.045", a3.truncate5(0.0456))
    cornelltest.assert_equals("0.045", a3.truncate5(0.0453))
    cornelltest.assert_equals("0.005", a3.truncate5(0.0056))
    cornelltest.assert_equals("0.001", a3.truncate5(0.0013))
    cornelltest.assert_equals("0.000", a3.truncate5(0.0004))
    cornelltest.assert_equals("0.000", a3.truncate5(0.0009999))
def test_hsv_to_rgb():
    """Test translation function hsv_to_rgb"""
    # pass # ADD TESTS TO ME
    hsv = colormodel.HSV(1.000, 0.500, 0.500)
    rgb = a3.hsv_to_rgb(hsv)
    cornelltest.assert_equals(128, rgb.red)
    cornelltest.assert_equals(65, rgb.green)
    cornelltest.assert_equals(64, rgb.blue)

    hsv = colormodel.HSV(200.0, 0.250, 0.250)
    rgb = a3.hsv_to_rgb(hsv)
    cornelltest.assert_equals(48, rgb.red)
    cornelltest.assert_equals(58, rgb.green)
    cornelltest.assert_equals(64, rgb.blue)

    hsv = colormodel.HSV(312.3, 0.667, 0.913)
    rgb = a3.hsv_to_rgb(hsv)
    cornelltest.assert_equals(233, rgb.red)
    cornelltest.assert_equals(78, rgb.green)
    cornelltest.assert_equals(201, rgb.blue)

    hsv = colormodel.HSV(87.20, 0.117, 0.699)
    rgb = a3.hsv_to_rgb(hsv)
    cornelltest.assert_equals(169, rgb.red)
    cornelltest.assert_equals(178, rgb.green)
    cornelltest.assert_equals(157, rgb.blue)

    hsv = colormodel.HSV(0.000, 0.000, 0.000)
    rgb = a3.hsv_to_rgb(hsv)
    cornelltest.assert_equals(0, rgb.red)
    cornelltest.assert_equals(0, rgb.green)
    cornelltest.assert_equals(0, rgb.blue)
def test_rgb_to_hsv():
    """Test translation function rgb_to_hsv"""
    # pass # ADD TESTS TO ME
    rgb = colormodel.RGB(15, 5, 75)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals(248.6, hsv.hue)
    cornelltest.assert_equals(0.933, hsv.saturation)
    cornelltest.assert_equals(0.294, hsv.value)

    rgb = colormodel.RGB(0, 0, 0)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals(0.000, hsv.hue)
    cornelltest.assert_equals(0.000, hsv.saturation)
    cornelltest.assert_equals(0.000, hsv.value)

    rgb = colormodel.RGB(100, 100, 100)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals(0.000, hsv.hue)
    cornelltest.assert_equals(0.000, hsv.saturation)
    cornelltest.assert_equals(0.392, hsv.value)

    rgb = colormodel.RGB(235, 19, 25)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals(358.3, hsv.hue)
    cornelltest.assert_equals(0.919, hsv.saturation)
    cornelltest.assert_equals(0.922, hsv.value)

    rgb = colormodel.RGB(1, 2, 3)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals(210.0, hsv.hue)
    cornelltest.assert_equals(0.667, hsv.saturation)
    cornelltest.assert_equals(0.012, hsv.value)
Example #45
0
def test_cluster_a():
    """Test Part A of the Cluster class assignment."""
    print '  Testing Part A of class Cluster'
    
    # TEST CASE 1
    # Create and test a cluster (always empty)
    dset = a6.Dataset(3)
    point = [0.0,1.0,0.0]
    cluster1 = a6.Cluster(dset, point)
    
    # Compare centroid and contents
    cornelltest.assert_float_lists_equal(point,cluster1.getCentroid())
    cornelltest.assert_equals([],cluster1.getIndices())
    # Make sure centroid COPIED
    cornelltest.assert_not_equals(id(point),id(cluster1.getCentroid()))
    
    print '    Basic cluster methods look okay'    
    
    # Add something to cluster (and check it was added)
    extra = [[0.0,0.5,4.2],[0.0,1.0,0.0]]
    dset.addPoint(extra[0])
    dset.addPoint(extra[1])
    cluster1.addIndex(1)
    cornelltest.assert_equals([1],cluster1.getIndices())
    cluster1.addIndex(0)
    cornelltest.assert_equals([1,0],cluster1.getIndices())
    # Make sure we can handle duplicates!
    cluster1.addIndex(1)
    cornelltest.assert_equals([1,0],cluster1.getIndices())
    
    print '    Method Cluster.addIndex look okay'
    
    # And clear it
    contents = cluster1.getContents()
    cornelltest.assert_equals(2,len(contents))
    cornelltest.assert_float_lists_equal(extra[1],contents[0])
    cornelltest.assert_float_lists_equal(extra[0],contents[1])
    
    print '    Method Cluster.getContents look okay'
    
    # And clear it
    cluster1.clear()
    cornelltest.assert_equals([],cluster1.getIndices())
    
    print '    Method Cluster.clear look okay'
    print '  Part A of class Cluster appears correct'
    print ''
def test_cmyk_to_rgb():
    """Test translation function cmyk_to_rgb"""
    cmyk = colormodel.CMYK(0, 0, 0, 0)
    rgb = a3.cmyk_to_rgb(cmyk)
    cornelltest.assert_equals(255, rgb.red)
    cornelltest.assert_equals(255, rgb.green)
    cornelltest.assert_equals(255, rgb.blue)

    cmyk = colormodel.CMYK(15.00, 32.00, 85.00, 6.000)
    rgb = a3.cmyk_to_rgb(cmyk)
    cornelltest.assert_equals(204, rgb.red)
    cornelltest.assert_equals(163, rgb.green)
    cornelltest.assert_equals(36, rgb.blue)

    cmyk = colormodel.CMYK(50.00, 50.00, 50.00, 50.00)
    rgb = a3.cmyk_to_rgb(cmyk)
    cornelltest.assert_equals(64, rgb.red)
    cornelltest.assert_equals(64, rgb.green)
    cornelltest.assert_equals(64, rgb.blue)

    cmyk = colormodel.CMYK(69.99, 69.69, 96.96, 66.99)
    rgb = a3.cmyk_to_rgb(cmyk)
    cornelltest.assert_equals(25, rgb.red)
    cornelltest.assert_equals(26, rgb.green)
    cornelltest.assert_equals(3, rgb.blue)

    cmyk = colormodel.CMYK(100.0, 0.000, 50.00, 0.000)
    rgb = a3.cmyk_to_rgb(cmyk)
    cornelltest.assert_equals(0, rgb.red)
    cornelltest.assert_equals(255, rgb.green)
    cornelltest.assert_equals(128, rgb.blue)
Example #47
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'
Example #48
0
def test_kmeans_d():
    """Test Part D of the ClusterGroup class."""
    print '  Testing Part D of class ClusterGroup'
    items = [[0.5,0.5,0.5],[0.5,0.6,0.6],[0.6,0.5,0.6],[0.5,0.6,0.5],[0.5,0.4,0.5],[0.5,0.4,0.4]]
    dset = a6.Dataset(3,items)
    
    # Try the same test case straight from the top using perform_k_means
    km1 = a6.ClusterGroup(dset, 2, [1, 3])
    km1.run(10)
    
    # Check first cluster
    cluster1 = km1.getClusters()[0]
    cornelltest.assert_float_lists_equal([8./15, 17./30, 17./30], cluster1.getCentroid())
    cornelltest.assert_equals(set([1, 2, 3]), set(cluster1.getIndices()))
    
    # Check second cluster
    cluster2 = km1.getClusters()[1]
    cornelltest.assert_float_lists_equal([0.5, 13./30, 14./30],cluster2.getCentroid())
    cornelltest.assert_equals(set([0, 4, 5]), set(cluster2.getIndices()))
    print '    Method run looks okay'
    
    # Test on a real world data set
    km2 = candy_to_kmeans('datasets/small_candy.csv',3,[23, 54, 36])
    km2.run(20)
    
    # The actual results
    cluster0 = km2.getClusters()[0]
    cluster1 = km2.getClusters()[1]
    cluster2 = km2.getClusters()[2]
    
    # The "correct" answers
    contents0 = [[0.88, 0.84, 0.8, 0.3], [0.02, 0.67, 0.75, 0.61], [0.81, 0.69, 0.65, 0.65], 
                 [0.62, 0.75, 0.65, 0.43], [0.35, 0.63, 0.65, 0.12], [0.61, 0.85, 0.81, 0.44], 
                 [0.95, 0.94, 0.98, 0.69], [0.04, 0.69, 0.38, 0.39], [0.28, 0.91, 0.63, 0.08], 
                 [0.38, 0.94, 0.53, 0.07], [0.08, 0.62, 0.32, 0.27], [0.69, 0.82, 0.75, 0.65], 
                 [0.84, 0.89, 0.91, 0.38], [0.22, 0.88, 0.39, 0.33], [0.71, 0.78, 0.64, 0.57], 
                 [0.15, 0.87, 0.62, 0.22], [0.65, 0.81, 0.69, 0.55], [0.27, 0.63, 0.69, 0.39], 
                 [0.35, 0.7, 0.41, 0.15], [0.91, 0.98, 0.61, 0.58], [0.9, 0.63, 0.83, 0.6], 
                 [0.95, 0.83, 0.64, 0.5], [0.76, 0.86, 0.74, 0.61], [0.27, 0.65, 0.52, 0.28], 
                 [0.86, 0.91, 0.88, 0.62], [0.1, 0.79, 0.5, 0.12], [0.99, 0.68, 0.8, 0.42], 
                 [0.09, 0.85, 0.55, 0.21], [0.79, 0.94, 0.83, 0.48], [0.73, 0.92, 0.74, 0.39], 
                 [0.89, 0.72, 0.78, 0.38], [0.39, 0.9, 0.52, 0.26], [0.46, 0.35, 0.96, 0.05], 
                 [0.21, 0.62, 0.33, 0.09], [0.58, 0.37, 0.9, 0.08], [0.54, 0.92, 0.36, 0.35], 
                 [0.67, 0.32, 0.66, 0.2], [0.36, 0.64, 0.57, 0.26], [0.9, 0.7, 0.74, 0.63], 
                 [0.4, 0.69, 0.74, 0.7]]
    contents1 = [[0.32, 0.87, 0.14, 0.68], [0.73, 0.31, 0.15, 0.08], [0.87, 0.99, 0.2, 0.8], 
                 [0.77, 0.45, 0.31, 0.31], [0.96, 0.09, 0.49, 0.3], [0.86, 0.03, 0.3, 0.39], 
                 [0.86, 0.86, 0.32, 0.88], [0.8, 0.4, 0.23, 0.33], [0.81, 0.66, 0.26, 0.82], 
                 [0.95, 0.62, 0.28, 0.01], [0.35, 0.71, 0.01, 0.32], [0.73, 0.65, 0.23, 0.02], 
                 [0.84, 0.88, 0.04, 0.86], [0.8, 0.62, 0.09, 0.65], [0.72, 0.55, 0.1, 0.17], 
                 [0.61, 0.42, 0.24, 0.33], [0.72, 0.88, 0.02, 0.95], [0.88, 0.96, 0.09, 0.88], 
                 [0.9, 0.05, 0.34, 0.41], [0.9, 0.41, 0.27, 0.36]]
    contents2 = [[0.4, 0.21, 0.78, 0.68], [0.54, 0.06, 0.81, 0.98], [0.2, 0.54, 0.73, 0.85], 
                 [0.14, 0.31, 0.86, 0.74], [0.39, 0.14, 0.99, 0.24], [0.23, 0.32, 0.7, 0.75], 
                 [0.65, 0.05, 0.39, 0.49], [0.04, 0.52, 0.99, 0.75], [0.14, 0.55, 0.67, 0.63], 
                 [0.5, 0.2, 0.69, 0.95], [0.79, 0.09, 0.41, 0.69], [0.4, 0.3, 0.78, 0.74], 
                 [0.65, 0.24, 0.63, 0.27], [0.35, 0.3, 0.94, 0.92], [0.39, 0.38, 0.85, 0.32], 
                 [0.38, 0.07, 0.82, 0.01], [0.66, 0.09, 0.69, 0.46], [0.26, 0.39, 0.95, 0.63], 
                 [0.54, 0.06, 0.74, 0.86], [0.2, 0.48, 0.98, 0.84], [0.62, 0.24, 0.77, 0.17], 
                 [0.27, 0.38, 0.76, 0.63], [0.7, 0.04, 0.7, 0.82], [0.41, 0.11, 0.61, 0.78], 
                 [0.22, 0.44, 0.67, 0.99], [0.51, 0.05, 0.95, 0.66], [0.44, 0.1, 0.61, 0.98], 
                 [0.31, 0.16, 0.95, 0.9], [0.31, 0.5, 0.87, 0.85], [0.5, 0.09, 0.84, 0.78], 
                 [0.62, 0.01, 0.88, 0.1], [0.44, 0.28, 0.88, 0.99], [0.57, 0.23, 0.6, 0.85], 
                 [0.72, 0.14, 0.63, 0.37], [0.39, 0.08, 0.77, 0.96], [0.09, 0.47, 0.63, 0.8], 
                 [0.63, 0.05, 0.52, 0.63], [0.62, 0.27, 0.67, 0.77], [0.35, 0.04, 0.85, 0.86], 
                 [0.36, 0.34, 0.75, 0.37]]
    centroid0 = [0.54125, 0.7545, 0.66125, 0.3775]
    centroid1 = [0.76900, 0.5705, 0.20550, 0.4775]
    centroid2 = [0.42325, 0.2330, 0.75775, 0.6765]

    cornelltest.assert_float_lists_equal(centroid0,cluster0.getCentroid())
    cornelltest.assert_float_lists_equal(centroid1,cluster1.getCentroid())
    cornelltest.assert_float_lists_equal(centroid2,cluster2.getCentroid())
    cornelltest.assert_float_lists_equal(contents0,cluster0.getContents()) 
    cornelltest.assert_float_lists_equal(contents1,cluster1.getContents()) 
    cornelltest.assert_float_lists_equal(contents2,cluster2.getContents()) 
    print '    Candy analysis test looks okay'
    print '  Part D of class ClusterGroup appears correct'
    print ''
def test_rgb_to_cmyk():
    """Test rgb_to_cmyk"""
    rgb = colormodel.RGB(255, 255, 255)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals("0.000", a3.round5(cmyk.cyan))
    cornelltest.assert_equals("0.000", a3.round5(cmyk.magenta))
    cornelltest.assert_equals("0.000", a3.round5(cmyk.yellow))
    cornelltest.assert_equals("0.000", a3.round5(cmyk.black))

    rgb = colormodel.RGB(0, 0, 0)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals("0.000", a3.round5(cmyk.cyan))
    cornelltest.assert_equals("0.000", a3.round5(cmyk.magenta))
    cornelltest.assert_equals("0.000", a3.round5(cmyk.yellow))
    cornelltest.assert_equals("100.0", a3.round5(cmyk.black))

    rgb = colormodel.RGB(217, 43, 164)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals("0.000", a3.round5(cmyk.cyan))
    cornelltest.assert_equals("80.18", a3.round5(cmyk.magenta))
    cornelltest.assert_equals("24.42", a3.round5(cmyk.yellow))
    cornelltest.assert_equals("14.90", a3.round5(cmyk.black))

    rgb = colormodel.RGB(175, 234, 18)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals("25.21", a3.round5(cmyk.cyan))
    cornelltest.assert_equals("0.000", a3.round5(cmyk.magenta))
    cornelltest.assert_equals("92.31", a3.round5(cmyk.yellow))
    cornelltest.assert_equals("8.235", a3.round5(cmyk.black))

    rgb = colormodel.RGB(15, 5, 75)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals("80.00", a3.round5(cmyk.cyan))
    cornelltest.assert_equals("93.33", a3.round5(cmyk.magenta))
    cornelltest.assert_equals("0.000", a3.round5(cmyk.yellow))
    cornelltest.assert_equals("70.59", a3.round5(cmyk.black))
def test_round5():
    """Test function round5"""
    cornelltest.assert_equals("130.6", a3.round5(130.59))
    cornelltest.assert_equals("130.5", a3.round5(130.54))
    cornelltest.assert_equals("100.0", a3.round5(100))
    cornelltest.assert_equals("99.57", a3.round5(99.566))
    cornelltest.assert_equals("99.99", a3.round5(99.99))
    cornelltest.assert_equals("100.0", a3.round5(99.995))
    cornelltest.assert_equals("22.00", a3.round5(21.99575))
    cornelltest.assert_equals("21.99", a3.round5(21.994))
    cornelltest.assert_equals("10.01", a3.round5(10.013567))
    cornelltest.assert_equals("10.00", a3.round5(10.000000005))
    cornelltest.assert_equals("10.00", a3.round5(9.9999))
    cornelltest.assert_equals("9.999", a3.round5(9.9993))
    cornelltest.assert_equals("1.355", a3.round5(1.3546))
    cornelltest.assert_equals("1.354", a3.round5(1.3544))
    cornelltest.assert_equals("0.046", a3.round5(0.0456))
    cornelltest.assert_equals("0.045", a3.round5(0.0453))
    cornelltest.assert_equals("0.006", a3.round5(0.0056))
    cornelltest.assert_equals("0.001", a3.round5(0.0013))
    cornelltest.assert_equals("0.000", a3.round5(0.0004))
    cornelltest.assert_equals("0.001", a3.round5(0.0009999))
Example #51
0
def test_kmeans_b():
    """Test Part B of the ClusterGroup class."""
    # This function tests the methods _nearest_cluster and _partition,
    # both of which are private methods.  Normally it's not good form to
    # directly call these methods from outside the class, but we make an
    # exception for testing code, which often has to be more tightly
    # integrated with the implementation of a class than other code that
    # just uses the class.
    print '  Testing Part B of class ClusterGroup'
    # Reinitialize data set
    items = [[0.,0.], [10.,1.], [10.,10.], [0.,9.]]
    dset = a6.Dataset(2, items)
    km1 = a6.ClusterGroup(dset, 2, [0,2])
    km2 = a6.ClusterGroup(dset, 3, [0,2,3])

    nearest = km1._nearest_cluster([1.,1.])
    cornelltest.assert_true(nearest is km1.getClusters()[0])

    nearest = km1._nearest_cluster([1.,10.])
    cornelltest.assert_true(nearest is km1.getClusters()[1])

    nearest = km2._nearest_cluster([1.,1.])
    cornelltest.assert_true(nearest is km2.getClusters()[0])

    nearest = km2._nearest_cluster([1.,10.])
    cornelltest.assert_true(nearest is km2.getClusters()[2])
    print '    Method ClusterGroup._nearest_cluster() looks okay'

    # Testing partition()
    # For this example points 0 and 3 are closer, as are 1 and 2
    km1._partition()
    cornelltest.assert_equals(set([0,3]), set(km1.getClusters()[0].getIndices()))
    cornelltest.assert_equals(set([1,2]), set(km1.getClusters()[1].getIndices()))
    # partition and repeat -- should not change clusters.
    km1._partition()
    cornelltest.assert_equals(set([0,3]), set(km1.getClusters()[0].getIndices()))
    cornelltest.assert_equals(set([1,2]), set(km1.getClusters()[1].getIndices()))
    
    # Reset the cluster centroids; now it changes
    cluster = km1.getClusters()
    cluster[0]._centroid = [5.0, 10.0]
    cluster[1]._centroid = [0.0, 2.0]
    km1._partition()
    cornelltest.assert_equals(set([2,3]), set(km1.getClusters()[0].getIndices()))
    cornelltest.assert_equals(set([0,1]), set(km1.getClusters()[1].getIndices()))
    
    print '    Method ClusterGroup._partition() looks okay'
    print '  Part B of class ClusterGroup appears correct'
    print ''
Example #52
0
def testB():
    """Prints: "Module al passed all tests" if all tests successful.
    
    Precondition: a1 is a module that contains functions get_keyword_index(),
    has_error(), and get_value()"""

    print "Testing functions get_keyword_index(), has_error(), and get_value()"

    A = 'A "word" B'
    B = 'A word B "word" C'
    C = 'A x B "word" C'
    cornelltest.assert_equals(2, a1.get_keyword_index(A, "word"))
    cornelltest.assert_equals(9, a1.get_keyword_index(B, "word"))
    cornelltest.assert_equals(-1, a1.get_keyword_index(C, "x"))

    D = '{"to": "EUR", "rate": 0.75, "warning": "invalid quantity, ignored.",\
        "from": "USD"}'
    E = '{"err": "failed to parse response from xe.com."}'
    F = '{"to": "EUR", "rate": 0.77203, "from": "USD", "v": 1.93007500000000}'
    cornelltest.assert_equals(True, a1.has_error(D))
    cornelltest.assert_equals(True, a1.has_error(E))
    cornelltest.assert_equals(False, a1.has_error(F))

    G = '{"to": "EUR", "rate": 0.75443, "from": "USD", "v": 1.88}'
    H = '{"to": "EUR", "rate": 0.75443, "from": "USD", "v": 1.88}'
    cornelltest.assert_equals('"EUR"', a1.get_value(G, "to"))
    cornelltest.assert_equals("1.88", a1.get_value(H, "v"))
Example #53
0
def testC():
    """Prints: "Module al passed all tests" if all tests successful.
    
    Precondition: a1 is a module that contains function currency_response()"""

    print "Testing function currency_response()"

    A = '{"to": "EUR", "rate": 0.77203, "from": "USD", "v": 1.93007500000000}'
    B = '{"to": "MNT", "rate": 0.17376, "from": "BYR", "v": 17.37634752695054}'
    C = '{"to": "CHF", "rate": 828.86915, "from": "XPD", "v": 0.08288691535559}'
    D = '{"to": "XBT", "rate": 1.00000, "from": "XBT", "v": 42.00000000000000}'
    E = '{"err": "failed to parse response from xe.com."}'
    F = '{"to": "USD", "rate": 1.29529, "from": "EUR", "v": -12.95286452598992}'
    cornelltest.assert_equals(A, a1.currency_response("USD", "EUR", 2.5))
    cornelltest.assert_equals(B, a1.currency_response("BYR", "MNT", 100.0))
    cornelltest.assert_equals(C, a1.currency_response("XPD", "CHF", 0.0001))
    cornelltest.assert_equals(D, a1.currency_response("XBT", "XBT", 42.0))
    cornelltest.assert_equals(E, a1.currency_response("LOL", "USD", 10.0))
    cornelltest.assert_equals(F, a1.currency_response("EUR", "USD", -10.0))
Example #54
0
def test_kmeans_a():
    """Test Part A of the ClusterGroup class."""
    print '  Testing Part A of class ClusterGroup'
    
    # A dataset with four points almost in a square
    items = [[0.,0.], [10.,1.], [10.,10.], [0.,9.]]
    dset = a6.Dataset(2, items)

    # Test creating a clustering with random seeds
    km = a6.ClusterGroup(dset, 3)
    # Should have 3 clusters
    cornelltest.assert_equals(len(km.getClusters()), 3)
    for clust in km.getClusters():
        # cluster centroids should have been chosen from items
        cornelltest.assert_true(clust.getCentroid() in items)
        # cluster centroids should be distinct (since items are)
        for clust2 in km.getClusters():
            if clust2 is not clust:
                cornelltest.assert_float_lists_not_equal(clust.getCentroid(), clust2.getCentroid())

    print '    Random ClusterGroup initialization looks okay'

    # Clusterings of that dataset, with two and three deterministic clusters
    km = a6.ClusterGroup(dset, 2, [0,2])
    cornelltest.assert_equals(items[0], km.getClusters()[0].getCentroid())
    cornelltest.assert_equals(items[2], km.getClusters()[1].getCentroid())
    km = a6.ClusterGroup(dset, 3, [0,2,3])
    cornelltest.assert_equals(items[0], km.getClusters()[0].getCentroid())
    cornelltest.assert_equals(items[2], km.getClusters()[1].getCentroid())
    cornelltest.assert_equals(items[3], km.getClusters()[2].getCentroid())

    print '    Seeded ClusterGroup initialization looks okay'
    print '  Part A of class ClusterGroup appears correct'
    print ''
Example #55
0
def testD():
    """Test Part D (of Part I) of the assignment. 

    This test procedure shows why we are providing you with the 
    unit tests, rather than asking you to write your own.  The
    method setKSize() has a randomization element inside of it.
    It is hard to check random things, because you do not get
    the same answer each time.
    
    To get around that, we use the function random.seed(). This
    function essentially turns off the random number generator,
    and makes it return predicatable values.  For more information,
    see http://en.wikipedia.org/wiki/Pseudorandomness."""
    print '  Testing Part D (setKSize)'
    # Force the random number generator to not be random
    random.seed(1)

    # 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)

    # TEST CASE 1 (Change k)
    dbase.setKSize(0)
    cornelltest.assert_equals(0,dbase.getKSize())
    cornelltest.assert_float_lists_equal([],dbase._clusters)
    
    # TEST CASE 2 (Change k)
    dbase.setKSize(2)
    
    # Should create two clusters
    cornelltest.assert_equals(2,dbase.getKSize())

    # Check first cluster    
    cluster1 = dbase.getCluster(0)
    cornelltest.assert_float_lists_equal([1.0,0.0,0.0],cluster1.getCentroid())
    cornelltest.assert_float_lists_equal(items[:3],cluster1.getContents())

    # Check second cluster    
    cluster2 = dbase.getCluster(1)
    cornelltest.assert_float_lists_equal([0.0,0.0,1.0],cluster2.getCentroid())
    cornelltest.assert_float_lists_equal(items[3:],cluster2.getContents())

    # TEST CASE 3 (Change k)
    dbase.setKSize(3)

    # Should create three clusters
    cornelltest.assert_equals(3,dbase.getKSize())

    # Check first cluster    
    cluster1 = dbase.getCluster(0)
    cornelltest.assert_float_lists_equal([0.0,0.0,1.0],cluster1.getCentroid())
    cornelltest.assert_float_lists_equal(items[3:4],cluster1.getContents())

    # Check second cluster    
    cluster2 = dbase.getCluster(1)
    cornelltest.assert_float_lists_equal([0.0,1.0,0.0],cluster2.getCentroid())
    cornelltest.assert_float_lists_equal(items[1:2],cluster2.getContents())

    # Check third cluster    
    cluster3 = dbase.getCluster(2)
    cornelltest.assert_float_lists_equal([0.0,0.0,0.0],cluster3.getCentroid())
    cornelltest.assert_float_lists_equal(items[0:1]+items[2:3],cluster3.getContents())
    print '  Part D appears correct'
Example #56
0
def test_kmeans_c():
    """Test Part C of the ClusterGroup class."""
    print '  Testing Part C of class ClusterGroup'
    items = [[0.,0.], [10.,1.], [10.,10.], [0.,9.]]
    dset = a6.Dataset(2, items)
    km1 = a6.ClusterGroup(dset, 2, [0,2])
    km1._partition()
    
    # Test update()
    stable = km1._update()
    cornelltest.assert_float_lists_equal([0,4.5], km1.getClusters()[0].getCentroid())
    cornelltest.assert_float_lists_equal([10.0,5.5], km1.getClusters()[1].getCentroid())
    cornelltest.assert_false(stable)
    
    # updating again should not change anything, but should return stable
    stable = km1._update()
    cornelltest.assert_float_lists_equal([0,4.5], km1.getClusters()[0].getCentroid())
    cornelltest.assert_float_lists_equal([10.0,5.5], km1.getClusters()[1].getCentroid())
    cornelltest.assert_true(stable)

    print '    Method ClusterGroup._update() looks okay'

    # Now test the k-means process itself.

    # FOR ALL TEST CASES
    # Create and initialize a non-empty dataset
    items = [[0.5,0.5,0.5],[0.5,0.6,0.6],[0.6,0.5,0.6],[0.5,0.6,0.5],[0.5,0.4,0.5],[0.5,0.4,0.4]]
    dset = a6.Dataset(3,items)

    # Create a clustering, providing non-random seed indices so the test is deterministic
    km2 = a6.ClusterGroup(dset, 2, [1, 3])

    # PRE-TEST: Check first cluster (should be okay if passed part D)
    cluster1 = km2.getClusters()[0]
    cornelltest.assert_float_lists_equal([0.5, 0.6, 0.6], cluster1.getCentroid())
    cornelltest.assert_equals(set([]), set(cluster1.getIndices()))

    # PRE-TEST: Check second cluster (should be okay if passed part D)
    cluster2 = km2.getClusters()[1]
    cornelltest.assert_float_lists_equal([0.5, 0.6, 0.5], cluster2.getCentroid())
    cornelltest.assert_equals(set([]), set(cluster2.getIndices()))

    # Make a fake cluster to test update_centroid() method
    clustertest = a6.Cluster(dset, [0.5, 0.6, 0.6])
    for ind in [1, 2]:
        clustertest.addIndex(ind)

    # TEST CASE 1 (update)
    stable = clustertest.updateCentroid()
    cornelltest.assert_float_lists_equal([0.55, 0.55, 0.6],clustertest.getCentroid())
    cornelltest.assert_false(stable) # Not yet stable

    # TEST CASE 2 (update)
    stable = clustertest.updateCentroid()
    cornelltest.assert_float_lists_equal([0.55, 0.55, 0.6],clustertest.getCentroid())
    cornelltest.assert_true(stable) # Now it is stable

    # TEST CASE 3 (step)
    km2.step()

    # Check first cluster (WHICH HAS CHANGED!)
    cluster1 = km2.getClusters()[0]
    cornelltest.assert_float_lists_equal([0.55, 0.55, 0.6], cluster1.getCentroid())
    cornelltest.assert_equals(set([1, 2]), set(cluster1.getIndices()))

    # Check second cluster (WHICH HAS CHANGED!)
    cluster2 = km2.getClusters()[1]
    cornelltest.assert_float_lists_equal([0.5, 0.475, 0.475],cluster2.getCentroid())
    cornelltest.assert_equals(set([0, 3, 4, 5]), set(cluster2.getIndices()))

    # TEST CASE 3 (step)
    km2.step()

    # Check first cluster (WHICH HAS CHANGED!)
    cluster1 = km2.getClusters()[0]
    cornelltest.assert_float_lists_equal([8./15, 17./30, 17./30], cluster1.getCentroid())
    cornelltest.assert_equals(set([1, 2, 3]), set(cluster1.getIndices()))

    # Check second cluster (WHICH HAS CHANGED!)
    cluster2 = km2.getClusters()[1]
    cornelltest.assert_float_lists_equal([0.5, 13./30, 14./30],cluster2.getCentroid())
    cornelltest.assert_equals(set([0, 4, 5]), set(cluster2.getIndices()))
    print '    Method ClusterGroup.step looks okay'
    print '  Part C of class ClusterGroup appears correct'
    print ''
Example #57
0
def testB():
    """testB() tests the functions first_inside_quotes(s), get_lhs(query),
    get_rhs(query), and get_error(query) in module a1.py"""

    #test first_inside_quotes(s)
    s = 'A "B C" D'
    result = first_inside_quotes(s)
    cornelltest.assert_equals('B C', result)

    s = 'A "B C" D "E F"'
    result = first_inside_quotes(s)
    cornelltest.assert_equals('B C', result)

    s = '""'
    result = first_inside_quotes(s)
    cornelltest.assert_equals('', result)

    s = 'the teacher said "hello!!!!!" to the students'
    result = first_inside_quotes(s)
    cornelltest.assert_equals('hello!!!!!', result)

    #test get_lhs(query)
    query = '{lhs: "5 U.S. dollars",rhs: "18.3650007 United Arab Emirates dirhams",error: "",icc: true}'
    result = get_lhs(query)
    cornelltest.assert_equals('5 U.S. dollars', result)

    query = '{lhs: "",rhs: "",error: "4",icc: false}'
    result = get_lhs(query)
    cornelltest.assert_equals('', result)

    #test get_rhs(query)
    query = '{lhs: "5 U.S. dollars",rhs: "18.3650007 United Arab Emirates dirhams",error: "",icc: true}'
    result = get_rhs(query)
    cornelltest.assert_equals('18.3650007 United Arab Emirates dirhams', result)

    query = '{lhs: "",rhs: "",error: "4",icc: false}'
    result = get_lhs(query)
    cornelltest.assert_equals('', result)

    #test get_error(query)
    query='{lhs: "5 U.S. dollars",rhs: "18.3650007 United Arab Emirates dirhams",error: "",icc: true}'
    result = get_error(query)
    cornelltest.assert_equals('', result)

    query = '{lhs: "",rhs: "",error: "4",icc: false}'
    result = get_error(query)
    cornelltest.assert_equals('4', result)
Example #58
0
def testA():
    """Prints: "Module al passed all tests" if all tests successful.
    
    Precondition: a1 is a module that contains functions after_space()
    and before_comma_or_end()."""

    print "Testing functions after_space() and before_comma_or_end()"

    A = "I have homework."
    B = "  But of course!"
    C = "AndwhywouldIdothat? "
    D = " "
    cornelltest.assert_equals("have homework.", a1.after_space(A))
    cornelltest.assert_equals(" But of course!", a1.after_space(B))
    cornelltest.assert_equals("", a1.after_space(C))
    cornelltest.assert_equals("", a1.after_space(D))

    E = "a,b,c"
    F = "abc}"
    G = ", what even,"
    H = "And I said,"
    I = ","
    J = "A,,,,b"
    cornelltest.assert_equals("a", a1.before_comma_or_end(E))
    cornelltest.assert_equals("abc", a1.before_comma_or_end(F))
    cornelltest.assert_equals("", a1.before_comma_or_end(G))
    cornelltest.assert_equals("And I said", a1.before_comma_or_end(H))
    cornelltest.assert_equals("", a1.before_comma_or_end(I))
    cornelltest.assert_equals("A", a1.before_comma_or_end(J))