gem5  v21.1.0.2
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 
36 using namespace gem5;
37 
39 TEST(FlagsTest, ConstructorZero)
40 {
41  const Flags<uint32_t> flags;
42  ASSERT_EQ(uint32_t(0), uint32_t(flags));
43 }
44 
46 TEST(FlagsTest, ConstructorSingle)
47 {
48  const uint32_t value = (1 << 3);
49  const Flags<uint32_t> flags(value);
50  ASSERT_EQ(value, uint32_t(flags));
51 }
52 
54 TEST(FlagsTest, ConstructorMulti)
55 {
56  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
57  const Flags<uint32_t> flags(value);
58  ASSERT_EQ(value, uint32_t(flags));
59 }
60 
62 TEST(FlagsTest, TypeAssignment)
63 {
64  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
65  Flags<uint32_t> flags;
66  flags = value;
67  ASSERT_EQ(value, uint32_t(flags));
68 }
69 
74 TEST(FlagsTest, TypeAssignmentOverwrite)
75 {
76  const uint32_t init_value = (1 << 5) | (1 << 6) ;
77  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
78  Flags<uint32_t> flags(init_value);
79  flags = value;
80  ASSERT_EQ(value, uint32_t(flags));
81 }
82 
84 TEST(FlagsTest, FlagsAssignment)
85 {
86  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
87  Flags<uint32_t> flags_a;
88  Flags<uint32_t> flags_b(value);
89  flags_a = flags_b;
90  ASSERT_EQ(uint32_t(flags_a), uint32_t(flags_b));
91 }
92 
94 TEST(FlagsTest, FlagsAssignmentOverwrite)
95 {
96  const uint32_t init_value = (1 << 5) | (1 << 6);
97  const uint32_t value = (1 << 3) | (1 << 5) | (1 << 9);
98  Flags<uint32_t> flags_a(init_value);
99  Flags<uint32_t> flags_b(value);
100  flags_a = flags_b;
101  ASSERT_EQ(uint32_t(flags_a), uint32_t(flags_b));
102 }
103 
105 TEST(FlagsTest, IsSetValue)
106 {
107  const uint32_t value_a = (1 << 3);
108  const uint32_t value_b = (1 << 5);
109  const Flags<uint32_t> flags(value_a | value_b);
110  ASSERT_TRUE(flags.isSet(value_a));
111  ASSERT_FALSE(flags.isSet(value_a << 1));
112  ASSERT_TRUE(flags.isSet(value_b));
113 }
114 
116 TEST(FlagsTest, IsSetType)
117 {
118  const uint32_t value_a = (1 << 5) | (1 << 6);
119  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
120  const uint32_t value_c = (1 << 4) | (1 << 8);
121  const Flags<uint32_t> flags(value_a);
122  ASSERT_TRUE(flags.isSet(value_b));
123  ASSERT_FALSE(flags.isSet(value_c));
124 }
125 
127 TEST(FlagsTest, AllSetMatch)
128 {
129  const uint32_t value_a = (1 << 5) | (1 << 6);
130  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
131  const Flags<uint32_t> flags(value_a);
132  ASSERT_TRUE(flags.allSet(value_a));
133  ASSERT_FALSE(flags.allSet(value_b));
134 }
135 
137 TEST(FlagsTest, NoneSetMatch)
138 {
139  const uint32_t value_a = (1 << 5) | (1 << 6);
140  const uint32_t value_b = (1 << 3) | (1 << 6);
141  const uint32_t value_c = (1 << 3) | (1 << 4) | (1 << 9);
142  const Flags<uint32_t> flags(value_a);
143  ASSERT_FALSE(flags.noneSet(value_a));
144  ASSERT_FALSE(flags.noneSet(value_b));
145  ASSERT_TRUE(flags.noneSet(value_c));
146 }
147 
149 TEST(FlagsTest, Clear)
150 {
151  const uint32_t value = (1 << 5) | (1 << 6);
152  Flags<uint32_t> flags(value);
153  flags.clear();
154  ASSERT_EQ(0, uint32_t(flags));
155 }
156 
158 TEST(FlagsTest, ClearMatch)
159 {
160  const uint32_t value_a = (1 << 5) | (1 << 6);
161  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
162  Flags<uint32_t> flags(value_a);
163  flags.clear(value_b);
164  ASSERT_FALSE(flags.isSet(value_a & value_b));
165  ASSERT_TRUE(flags.isSet(value_a ^ (value_a & value_b)));
166 }
167 
169 TEST(FlagsTest, SetOverlapping)
170 {
171  const uint32_t value_a = (1 << 5) | (1 << 6);
172  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
173  Flags<uint32_t> flags(value_a);
174  flags.set(value_b);
175  ASSERT_EQ(value_a | value_b, uint32_t(flags));
176 }
177 
182 TEST(FlagsTest, ConditionalSet)
183 {
184  const uint32_t value_a = (1 << 5) | (1 << 6);
185  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
186 
187  Flags<uint32_t> flags_true(value_a);
188  flags_true.set(value_b, true);
189  ASSERT_EQ(value_a | value_b, uint32_t(flags_true));
190 
191  Flags<uint32_t> flags_false(value_a);
192  flags_false.set(value_b, false);
193  ASSERT_EQ(value_a & ~value_b, uint32_t(flags_false));
194 }
195 
201 TEST(FlagsTest, ReplaceOverlapping)
202 {
203  const uint32_t value_a = (1 << 4) | (1 << 5) | (1 << 6);
204  const uint32_t value_b = (1 << 3) | (1 << 5) | (1 << 9);
205  const uint32_t mask = (1 << 4) | (1 << 5) | (1 << 9) | (1 << 10);
206  // (1 << 4) is set in value_a, but is not set in value_b, so it is cleared
207  // (1 << 5) is set in both values, so it remains set
208  // (1 << 9) is not set in value_a, but it is in value_b, so it is set
209  // (1 << 10) is not set in both values, so it remains not set
210  const uint32_t result = (1 << 5) | (1 << 6) | (1 << 9);
211  Flags<uint32_t> flags(value_a);
212  flags.replace(value_b, mask);
213  ASSERT_EQ(result, uint32_t(flags));
214 }
gem5::Flags::set
void set(Type mask)
Set all flag's bits matching the given mask.
Definition: flags.hh:116
gem5::Flags::clear
void clear()
Clear all flag's bits.
Definition: flags.hh:102
gem5::Flags::allSet
bool allSet(Type mask) const
Verifies whether no bits matching the given mask are set.
Definition: flags.hh:91
gem5::mask
constexpr uint64_t mask(unsigned nbits)
Generate a 64-bit mask of 'nbits' 1s, right justified.
Definition: bitfield.hh:63
gem5::Flags
Wrapper that groups a few flag bits under the same undelying container.
Definition: flags.hh:44
gem5::Flags::isSet
bool isSet(Type mask) const
Verifies whether any bit matching the given mask is set.
Definition: flags.hh:83
gem5::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:141
flags.hh
TEST
TEST(FlagsTest, ConstructorZero)
Test default zero-initialized constructor.
Definition: flags.test.cc:39
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::Flags::noneSet
bool noneSet(Type mask) const
Verifies whether no bits matching the given mask are set.
Definition: flags.hh:99

Generated on Tue Sep 21 2021 12:24:57 for gem5 by doxygen 1.8.17