gem5  v21.0.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
flags.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Daniel R. Carvalho
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include <cstdint>
32 #include <limits>
33 
34 #include "base/flags.hh"
35 
37 TEST(FlagsTest, ConstructorZero)
38 {
39  const Flags<uint32_t> flags;
40  ASSERT_EQ(uint32_t(0), uint32_t(flags));
41 }
42 
44 TEST(FlagsTest, ConstructorSingle)
45 {
46  const uint32_t value = (1 << 3);
47  const Flags<uint32_t> flags(value);
48  ASSERT_EQ(value, uint32_t(flags));
49 }
50 
52 TEST(FlagsTest, ConstructorMulti)
53 {
54  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
55  const Flags<uint32_t> flags(value);
56  ASSERT_EQ(value, uint32_t(flags));
57 }
58 
60 TEST(FlagsTest, TypeAssignment)
61 {
62  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
63  Flags<uint32_t> flags;
64  flags = value;
65  ASSERT_EQ(value, uint32_t(flags));
66 }
67 
72 TEST(FlagsTest, TypeAssignmentOverwrite)
73 {
74  const uint32_t init_value = (1 << 5) | (1 << 6) ;
75  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
76  Flags<uint32_t> flags(init_value);
77  flags = value;
78  ASSERT_EQ(value, uint32_t(flags));
79 }
80 
82 TEST(FlagsTest, FlagsAssignment)
83 {
84  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
85  Flags<uint32_t> flags_a;
86  Flags<uint32_t> flags_b(value);
87  flags_a = flags_b;
88  ASSERT_EQ(uint32_t(flags_a), uint32_t(flags_b));
89 }
90 
92 TEST(FlagsTest, FlagsAssignmentOverwrite)
93 {
94  const uint32_t init_value = (1 << 5) | (1 << 6);
95  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
96  Flags<uint32_t> flags_a(init_value);
97  Flags<uint32_t> flags_b(value);
98  flags_a = flags_b;
99  ASSERT_EQ(uint32_t(flags_a), uint32_t(flags_b));
100 }
101 
103 TEST(FlagsTest, IsSetValue)
104 {
105  const uint32_t value_a = (1 << 3);
106  const uint32_t value_b = (1 << 5);
107  const Flags<uint32_t> flags(value_a | value_b);
108  ASSERT_TRUE(flags.isSet(value_a));
109  ASSERT_FALSE(flags.isSet(value_a << 1));
110  ASSERT_TRUE(flags.isSet(value_b));
111 }
112 
114 TEST(FlagsTest, IsSetType)
115 {
116  const uint32_t value_a = (1 << 5) | (1 << 6);
117  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
118  const uint32_t value_c = (1 << 4) | (1 << 8);
119  const Flags<uint32_t> flags(value_a);
120  ASSERT_TRUE(flags.isSet(value_b));
121  ASSERT_FALSE(flags.isSet(value_c));
122 }
123 
125 TEST(FlagsTest, AllSetMatch)
126 {
127  const uint32_t value_a = (1 << 5) | (1 << 6);
128  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
129  const Flags<uint32_t> flags(value_a);
130  ASSERT_TRUE(flags.allSet(value_a));
131  ASSERT_FALSE(flags.allSet(value_b));
132 }
133 
135 TEST(FlagsTest, NoneSetMatch)
136 {
137  const uint32_t value_a = (1 << 5) | (1 << 6);
138  const uint32_t value_b = (1 << 3) | (1 << 6);
139  const uint32_t value_c = (1 << 3) | (1 << 4) | (1 << 9);
140  const Flags<uint32_t> flags(value_a);
141  ASSERT_FALSE(flags.noneSet(value_a));
142  ASSERT_FALSE(flags.noneSet(value_b));
143  ASSERT_TRUE(flags.noneSet(value_c));
144 }
145 
147 TEST(FlagsTest, Clear)
148 {
149  const uint32_t value = (1 << 5) | (1 << 6);
150  Flags<uint32_t> flags(value);
151  flags.clear();
152  ASSERT_EQ(0, uint32_t(flags));
153 }
154 
156 TEST(FlagsTest, ClearMatch)
157 {
158  const uint32_t value_a = (1 << 5) | (1 << 6);
159  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
160  Flags<uint32_t> flags(value_a);
161  flags.clear(value_b);
162  ASSERT_FALSE(flags.isSet(value_a & value_b));
163  ASSERT_TRUE(flags.isSet(value_a ^ (value_a & value_b)));
164 }
165 
167 TEST(FlagsTest, SetOverlapping)
168 {
169  const uint32_t value_a = (1 << 5) | (1 << 6);
170  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
171  Flags<uint32_t> flags(value_a);
172  flags.set(value_b);
173  ASSERT_EQ(value_a | value_b, uint32_t(flags));
174 }
175 
180 TEST(FlagsTest, ConditionalSet)
181 {
182  const uint32_t value_a = (1 << 5) | (1 << 6);
183  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
184 
185  Flags<uint32_t> flags_true(value_a);
186  flags_true.set(value_b, true);
187  ASSERT_EQ(value_a | value_b, uint32_t(flags_true));
188 
189  Flags<uint32_t> flags_false(value_a);
190  flags_false.set(value_b, false);
191  ASSERT_EQ(value_a & ~value_b, uint32_t(flags_false));
192 }
193 
199 TEST(FlagsTest, ReplaceOverlapping)
200 {
201  const uint32_t value_a = (1 << 4) | (1 << 5) | (1 << 6);
202  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
203  const uint32_t mask = (1 << 4) | (1 << 5) | (1 << 9) | (1 << 10);
204  // (1 << 4) is set in value_a, but is not set in value_b, so it is cleared
205  // (1 << 5) is set in both values, so it remains set
206  // (1 << 9) is not set in value_a, but it is in value_b, so it is set
207  // (1 << 10) is not set in both values, so it remains not set
208  const uint32_t result = (1 << 5) | (1 << 6) | (1 << 9);
209  Flags<uint32_t> flags(value_a);
210  flags.replace(value_b, mask);
211  ASSERT_EQ(result, uint32_t(flags));
212 }
Flags
Wrapper that groups a few flag bits under the same undelying container.
Definition: flags.hh:41
Flags::noneSet
bool noneSet(Type mask) const
Verifies whether no bits matching the given mask are set.
Definition: flags.hh:96
Flags::clear
void clear()
Clear all flag's bits.
Definition: flags.hh:99
Flags::set
void set(Type mask)
Set all flag's bits matching the given mask.
Definition: flags.hh:113
Flags::isSet
bool isSet(Type mask) const
Verifies whether any bit matching the given mask is set.
Definition: flags.hh:80
flags.hh
TEST
TEST(FlagsTest, ConstructorZero)
Test default zero-initialized constructor.
Definition: flags.test.cc:37
Flags::allSet
bool allSet(Type mask) const
Verifies whether no bits matching the given mask are set.
Definition: flags.hh:88
Flags::replace
void replace(Type flags, Type mask)
Replace the contents of the bits matching the mask with the corresponding bits in the provided flags.
Definition: flags.hh:138
ArmISA::mask
Bitfield< 28, 24 > mask
Definition: miscregs_types.hh:711

Generated on Tue Jun 22 2021 15:28:25 for gem5 by doxygen 1.8.17