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