# a3test.py # ADD YOUR NAME(S) AND NETID(S) HERE; Lillian Lee (LJL2), Steve Marschner (srm2), Walker White (wmw2) # Feb 25, 2013 """ Unit Test for A3""" import colormodel import cunittest2 import a3 # We *guarantee* that the implementation of equality for RGB objects is # correct (as long as you didn't edit that code). This means that you can test # whether an RGB object's attributes are correct by directly comparing the RGB # object for equality with another RGB object, rather than doing attribute-by- # attribute testing as you did with class Point in the labs; see # test_complement for a concrete example of what we mean. # matrix coefficients from # http://web.archive.org/web/20081014161121/http://www.colorjack.com/labs/colormatrix/, # credited to Matthew Wickline PROTANOPIA = [[0.56667, 0.43333, 0], [0.55833, 0.44167, 0], [0, 0.24167, 0.75833]] CYCLE = [[0, 1, 0], [0, 0, 1], [1, 0, 0]] IDENTITY = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] def test_complement(): """Test function a3.complement""" # you do not need to add any other test cases r = 250 g = 0 b = 71 testcolor = colormodel.RGB(r,g,b) # Because of our guarantee above, we do NOT have to test each component # of the new object individually; that is, for class RGB we do # NOT need to write # # c = a3.complement_rgb(colormodel.RGB(r, g, b) # cunittest2.assert_equals(255-r, c.red) # (etc.) # # Also note that we did not need to compute 255-r, 255-g, or 255-b # explicitly (which helps guard against typos in our testcases) cunittest2.assert_equals(colormodel.RGB(255-r, 255-g, 255-b), a3.complement_rgb(testcolor)) # check that testcolor itself was not changed cunittest2.assert_equals(colormodel.RGB(250,0,71), testcolor) r = 255 g = 1 b = 100 testcolor = colormodel.RGB(r,g,b) cunittest2.assert_equals(colormodel.RGB(255-r, 255-g, 255-b), a3.complement_rgb(testcolor)) # check that testcolor itself was not changed cunittest2.assert_equals(colormodel.RGB(255,1,100), testcolor) print "passed test of complement_rgb" def test_helpers(): """Test a3.list_to_rgb and a3.rgb_to_string""" # you do not need to add any other test cases cunittest2.assert_equals(colormodel.RGB(0, 15, 255), a3.list_to_rgb([0, 15, 255])) print "passed test of list_to_rgb" cunittest2.assert_equals("(30, 240, 230)", a3.rgb_to_string(colormodel.RGB(30, 240, 230))) cunittest2.assert_equals("(0, 0, 255)", a3.rgb_to_string(colormodel.RGB(0, 0, 255))) cunittest2.assert_equals("(10, 255, 3)", a3.rgb_to_string(colormodel.RGB(10, 255, 3))) print "passed test of rgb_to_string" def test_list_applications(): """Test a3.components_to_num and a3.apply_matrix""" ################### ## testing components_to_num ## ## you DO need to add more test cases here. We've done one for you. ## ################### coeffs = [1.5, 0, 0] testcolor = colormodel.RGB(14, 2, 167) cunittest2.assert_floats_equal(21.0, a3.components_to_num(coeffs, testcolor)) # make sure a3.components_to_num doesn't change its color-argument object cunittest2.assert_equals(colormodel.RGB(14, 2, 167), testcolor) print "passed test of components_to_num" ################### ## testing apply_matrix ## ## you do not need to add any other test cases for apply_matrix ## ################### # test against IDENTITY matrix cunittest2.assert_equals(colormodel.RGB(255, 0, 0), a3.apply_matrix(IDENTITY, colormodel.RED)) cunittest2.assert_equals(colormodel.RGB(255, 175, 175), a3.apply_matrix(IDENTITY, colormodel.PINK)) # test against CYCLE matrix testcolor = colormodel.RGB(0, 100, 200) cunittest2.assert_equals(colormodel.RGB(100, 200, 0), a3.apply_matrix(CYCLE, testcolor)) # make sure testcolor wasn't changed (spec says new color is returned) cunittest2.assert_equals(colormodel.RGB(0, 100, 200), testcolor) # test against PROTANOPIA matrix # # The fact that the transformed versions of red and green are # much more similar than their original values ((255, 0, 0) and (0, 255, 0), # respectively is precisely the phenomenon of red-green colorblindness. # cunittest2.assert_equals(colormodel.RGB(145, 142, 0), a3.apply_matrix(PROTANOPIA, colormodel.RED)) cunittest2.assert_equals(colormodel.RGB(220, 220, 175), a3.apply_matrix(PROTANOPIA, colormodel.PINK)) cunittest2.assert_equals(colormodel.RGB(110, 113, 62), a3.apply_matrix(PROTANOPIA, colormodel.GREEN)) cunittest2.assert_equals(colormodel.RGB(0, 0, 193), a3.apply_matrix(PROTANOPIA, colormodel.BLUE)) cunittest2.assert_equals(colormodel.RGB(255, 255, 62), a3.apply_matrix(PROTANOPIA, colormodel.YELLOW)) cunittest2.assert_equals(colormodel.RGB(0, 0, 0), a3.apply_matrix(PROTANOPIA, colormodel.BLACK)) # test that the original color objects weren't changed # (specs say that a *new* object is returned) cunittest2.assert_equals(colormodel.RGB(255, 0, 0), colormodel.RED) cunittest2.assert_equals(colormodel.RGB(255, 175, 175), colormodel.PINK) cunittest2.assert_equals(colormodel.RGB(0, 255, 0), colormodel.GREEN) cunittest2.assert_equals(colormodel.RGB(0, 0, 255), colormodel.BLUE) cunittest2.assert_equals(colormodel.RGB(255, 255, 0), colormodel.YELLOW) print "passed test of apply_matrix" # Application Code if __name__ == "__main__": test_complement() test_helpers() test_list_applications() print "All test cases for A3 passed"