1*760c253cSXin Li# Copyright 2013 The ChromiumOS Authors 2*760c253cSXin Li# Use of this source code is governed by a BSD-style license that can be 3*760c253cSXin Li# found in the LICENSE file. 4*760c253cSXin Li"""Unit tests for the classes in module 'flags'. 5*760c253cSXin Li 6*760c253cSXin LiPart of the Chrome build flags optimization. 7*760c253cSXin Li""" 8*760c253cSXin Li 9*760c253cSXin Li__author__ = "[email protected] (Yuheng Long)" 10*760c253cSXin Li 11*760c253cSXin Liimport random 12*760c253cSXin Liimport sys 13*760c253cSXin Liimport unittest 14*760c253cSXin Li 15*760c253cSXin Lifrom flags import Flag 16*760c253cSXin Lifrom flags import FlagSet 17*760c253cSXin Li 18*760c253cSXin Li 19*760c253cSXin Li# The number of tests to test. 20*760c253cSXin LiNUM_TESTS = 20 21*760c253cSXin Li 22*760c253cSXin Li 23*760c253cSXin Liclass FlagTest(unittest.TestCase): 24*760c253cSXin Li """This class tests the Flag class.""" 25*760c253cSXin Li 26*760c253cSXin Li def testInit(self): 27*760c253cSXin Li """The value generated should fall within start and end of the spec. 28*760c253cSXin Li 29*760c253cSXin Li If the value is not specified, the value generated should fall within start 30*760c253cSXin Li and end of the spec. 31*760c253cSXin Li """ 32*760c253cSXin Li 33*760c253cSXin Li for _ in range(NUM_TESTS): 34*760c253cSXin Li start = random.randint(1, sys.maxint - 1) 35*760c253cSXin Li end = random.randint(start + 1, sys.maxint) 36*760c253cSXin Li 37*760c253cSXin Li spec = "flag=[%s-%s]" % (start, end) 38*760c253cSXin Li 39*760c253cSXin Li test_flag = Flag(spec) 40*760c253cSXin Li 41*760c253cSXin Li value = test_flag.GetValue() 42*760c253cSXin Li 43*760c253cSXin Li # If the value is not specified when the flag is constructed, a random 44*760c253cSXin Li # value is chosen. This value should fall within start and end of the 45*760c253cSXin Li # spec. 46*760c253cSXin Li assert start <= value and value < end 47*760c253cSXin Li 48*760c253cSXin Li def testEqual(self): 49*760c253cSXin Li """Test the equal operator (==) of the flag. 50*760c253cSXin Li 51*760c253cSXin Li Two flags are equal if and only if their spec and value are equal. 52*760c253cSXin Li """ 53*760c253cSXin Li 54*760c253cSXin Li tests = range(NUM_TESTS) 55*760c253cSXin Li 56*760c253cSXin Li # Two tasks having the same spec and value should be equivalent. 57*760c253cSXin Li for test in tests: 58*760c253cSXin Li assert Flag(str(test), test) == Flag(str(test), test) 59*760c253cSXin Li 60*760c253cSXin Li # Two tasks having different flag set should be different. 61*760c253cSXin Li for test in tests: 62*760c253cSXin Li flag = Flag(str(test), test) 63*760c253cSXin Li other_flag_sets = [other for other in tests if test != other] 64*760c253cSXin Li for other_test in other_flag_sets: 65*760c253cSXin Li assert flag != Flag(str(other_test), other_test) 66*760c253cSXin Li 67*760c253cSXin Li def testFormattedForUse(self): 68*760c253cSXin Li """Test the FormattedForUse method of the flag. 69*760c253cSXin Li 70*760c253cSXin Li The FormattedForUse replaces the string within the [] with the actual value. 71*760c253cSXin Li """ 72*760c253cSXin Li 73*760c253cSXin Li for _ in range(NUM_TESTS): 74*760c253cSXin Li start = random.randint(1, sys.maxint - 1) 75*760c253cSXin Li end = random.randint(start + 1, sys.maxint) 76*760c253cSXin Li value = random.randint(start, end - 1) 77*760c253cSXin Li 78*760c253cSXin Li spec = "flag=[%s-%s]" % (start, end) 79*760c253cSXin Li 80*760c253cSXin Li test_flag = Flag(spec, value) 81*760c253cSXin Li 82*760c253cSXin Li # For numeric flag, the FormattedForUse replaces the string within the [] 83*760c253cSXin Li # with the actual value. 84*760c253cSXin Li test_value = test_flag.FormattedForUse() 85*760c253cSXin Li actual_value = "flag=%s" % value 86*760c253cSXin Li 87*760c253cSXin Li assert test_value == actual_value 88*760c253cSXin Li 89*760c253cSXin Li for _ in range(NUM_TESTS): 90*760c253cSXin Li value = random.randint(1, sys.maxint - 1) 91*760c253cSXin Li 92*760c253cSXin Li test_flag = Flag("flag", value) 93*760c253cSXin Li 94*760c253cSXin Li # For boolean flag, the FormattedForUse returns the spec. 95*760c253cSXin Li test_value = test_flag.FormattedForUse() 96*760c253cSXin Li actual_value = "flag" 97*760c253cSXin Li assert test_value == actual_value 98*760c253cSXin Li 99*760c253cSXin Li 100*760c253cSXin Liclass FlagSetTest(unittest.TestCase): 101*760c253cSXin Li """This class test the FlagSet class.""" 102*760c253cSXin Li 103*760c253cSXin Li def testEqual(self): 104*760c253cSXin Li """Test the equal method of the Class FlagSet. 105*760c253cSXin Li 106*760c253cSXin Li Two FlagSet instances are equal if all their flags are equal. 107*760c253cSXin Li """ 108*760c253cSXin Li 109*760c253cSXin Li flag_names = range(NUM_TESTS) 110*760c253cSXin Li 111*760c253cSXin Li # Two flag sets having the same flags should be equivalent. 112*760c253cSXin Li for flag_name in flag_names: 113*760c253cSXin Li spec = "%s" % flag_name 114*760c253cSXin Li 115*760c253cSXin Li assert FlagSet([Flag(spec)]) == FlagSet([Flag(spec)]) 116*760c253cSXin Li 117*760c253cSXin Li # Two flag sets having different flags should be different. 118*760c253cSXin Li for flag_name in flag_names: 119*760c253cSXin Li spec = "%s" % flag_name 120*760c253cSXin Li flag_set = FlagSet([Flag(spec)]) 121*760c253cSXin Li other_flag_sets = [ 122*760c253cSXin Li other for other in flag_names if flag_name != other 123*760c253cSXin Li ] 124*760c253cSXin Li for other_name in other_flag_sets: 125*760c253cSXin Li other_spec = "%s" % other_name 126*760c253cSXin Li assert flag_set != FlagSet([Flag(other_spec)]) 127*760c253cSXin Li 128*760c253cSXin Li def testGetItem(self): 129*760c253cSXin Li """Test the get item method of the Class FlagSet. 130*760c253cSXin Li 131*760c253cSXin Li The flag set is also indexed by the specs. The flag set should return the 132*760c253cSXin Li appropriate flag given the spec. 133*760c253cSXin Li """ 134*760c253cSXin Li 135*760c253cSXin Li tests = range(NUM_TESTS) 136*760c253cSXin Li 137*760c253cSXin Li specs = [str(spec) for spec in tests] 138*760c253cSXin Li flag_array = [Flag(spec) for spec in specs] 139*760c253cSXin Li 140*760c253cSXin Li flag_set = FlagSet(flag_array) 141*760c253cSXin Li 142*760c253cSXin Li # Created a dictionary of spec and flag, the flag set should return the flag 143*760c253cSXin Li # the same as this dictionary. 144*760c253cSXin Li spec_flag = dict(zip(specs, flag_array)) 145*760c253cSXin Li 146*760c253cSXin Li for spec in spec_flag: 147*760c253cSXin Li assert flag_set[spec] == spec_flag[spec] 148*760c253cSXin Li 149*760c253cSXin Li def testContain(self): 150*760c253cSXin Li """Test the contain method of the Class FlagSet. 151*760c253cSXin Li 152*760c253cSXin Li The flag set is also indexed by the specs. The flag set should return true 153*760c253cSXin Li for spec if it contains a flag containing spec. 154*760c253cSXin Li """ 155*760c253cSXin Li 156*760c253cSXin Li true_tests = range(NUM_TESTS) 157*760c253cSXin Li false_tests = range(NUM_TESTS, NUM_TESTS * 2) 158*760c253cSXin Li 159*760c253cSXin Li specs = [str(spec) for spec in true_tests] 160*760c253cSXin Li 161*760c253cSXin Li flag_set = FlagSet([Flag(spec) for spec in specs]) 162*760c253cSXin Li 163*760c253cSXin Li for spec in specs: 164*760c253cSXin Li assert spec in flag_set 165*760c253cSXin Li 166*760c253cSXin Li for spec in false_tests: 167*760c253cSXin Li assert spec not in flag_set 168*760c253cSXin Li 169*760c253cSXin Li def testFormattedForUse(self): 170*760c253cSXin Li """Test the FormattedForUse method of the Class FlagSet. 171*760c253cSXin Li 172*760c253cSXin Li The output should be a sorted list of strings. 173*760c253cSXin Li """ 174*760c253cSXin Li 175*760c253cSXin Li flag_names = range(NUM_TESTS) 176*760c253cSXin Li flag_names.reverse() 177*760c253cSXin Li flags = [] 178*760c253cSXin Li result = [] 179*760c253cSXin Li 180*760c253cSXin Li # Construct the flag set. 181*760c253cSXin Li for flag_name in flag_names: 182*760c253cSXin Li spec = "%s" % flag_name 183*760c253cSXin Li flags.append(Flag(spec)) 184*760c253cSXin Li result.append(spec) 185*760c253cSXin Li 186*760c253cSXin Li flag_set = FlagSet(flags) 187*760c253cSXin Li 188*760c253cSXin Li # The results string should be sorted. 189*760c253cSXin Li assert sorted(result) == flag_set.FormattedForUse() 190*760c253cSXin Li 191*760c253cSXin Li 192*760c253cSXin Liif __name__ == "__main__": 193*760c253cSXin Li unittest.main() 194