xref: /aosp_15_r20/external/toolchain-utils/bestflags/flags_test.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1# Copyright 2013 The ChromiumOS Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Unit tests for the classes in module 'flags'.
5
6Part of the Chrome build flags optimization.
7"""
8
9__author__ = "[email protected] (Yuheng Long)"
10
11import random
12import sys
13import unittest
14
15from flags import Flag
16from flags import FlagSet
17
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 operator (==) 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            flag = Flag(str(test), test)
63            other_flag_sets = [other for other in tests if test != other]
64            for other_test in other_flag_sets:
65                assert flag != Flag(str(other_test), other_test)
66
67    def testFormattedForUse(self):
68        """Test the FormattedForUse method of the flag.
69
70        The FormattedForUse replaces the string within the [] with the actual value.
71        """
72
73        for _ in range(NUM_TESTS):
74            start = random.randint(1, sys.maxint - 1)
75            end = random.randint(start + 1, sys.maxint)
76            value = random.randint(start, end - 1)
77
78            spec = "flag=[%s-%s]" % (start, end)
79
80            test_flag = Flag(spec, value)
81
82            # For numeric flag, the FormattedForUse replaces the string within the []
83            # with the actual value.
84            test_value = test_flag.FormattedForUse()
85            actual_value = "flag=%s" % value
86
87            assert test_value == actual_value
88
89        for _ in range(NUM_TESTS):
90            value = random.randint(1, sys.maxint - 1)
91
92            test_flag = Flag("flag", value)
93
94            # For boolean flag, the FormattedForUse returns the spec.
95            test_value = test_flag.FormattedForUse()
96            actual_value = "flag"
97            assert test_value == actual_value
98
99
100class FlagSetTest(unittest.TestCase):
101    """This class test the FlagSet class."""
102
103    def testEqual(self):
104        """Test the equal method of the Class FlagSet.
105
106        Two FlagSet instances are equal if all their flags are equal.
107        """
108
109        flag_names = range(NUM_TESTS)
110
111        # Two flag sets having the same flags should be equivalent.
112        for flag_name in flag_names:
113            spec = "%s" % flag_name
114
115            assert FlagSet([Flag(spec)]) == FlagSet([Flag(spec)])
116
117        # Two flag sets having different flags should be different.
118        for flag_name in flag_names:
119            spec = "%s" % flag_name
120            flag_set = FlagSet([Flag(spec)])
121            other_flag_sets = [
122                other for other in flag_names if flag_name != other
123            ]
124            for other_name in other_flag_sets:
125                other_spec = "%s" % other_name
126                assert flag_set != FlagSet([Flag(other_spec)])
127
128    def testGetItem(self):
129        """Test the get item method of the Class FlagSet.
130
131        The flag set is also indexed by the specs. The flag set should return the
132        appropriate flag given the spec.
133        """
134
135        tests = range(NUM_TESTS)
136
137        specs = [str(spec) for spec in tests]
138        flag_array = [Flag(spec) for spec in specs]
139
140        flag_set = FlagSet(flag_array)
141
142        # Created a dictionary of spec and flag, the flag set should return the flag
143        # the same as this dictionary.
144        spec_flag = dict(zip(specs, flag_array))
145
146        for spec in spec_flag:
147            assert flag_set[spec] == spec_flag[spec]
148
149    def testContain(self):
150        """Test the contain method of the Class FlagSet.
151
152        The flag set is also indexed by the specs. The flag set should return true
153        for spec if it contains a flag containing spec.
154        """
155
156        true_tests = range(NUM_TESTS)
157        false_tests = range(NUM_TESTS, NUM_TESTS * 2)
158
159        specs = [str(spec) for spec in true_tests]
160
161        flag_set = FlagSet([Flag(spec) for spec in specs])
162
163        for spec in specs:
164            assert spec in flag_set
165
166        for spec in false_tests:
167            assert spec not in flag_set
168
169    def testFormattedForUse(self):
170        """Test the FormattedForUse method of the Class FlagSet.
171
172        The output should be a sorted list of strings.
173        """
174
175        flag_names = range(NUM_TESTS)
176        flag_names.reverse()
177        flags = []
178        result = []
179
180        # Construct the flag set.
181        for flag_name in flag_names:
182            spec = "%s" % flag_name
183            flags.append(Flag(spec))
184            result.append(spec)
185
186        flag_set = FlagSet(flags)
187
188        # The results string should be sorted.
189        assert sorted(result) == flag_set.FormattedForUse()
190
191
192if __name__ == "__main__":
193    unittest.main()
194