1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "util/saturated_enum.h"
17
18 #include "util/tests/verifier_test.h"
19
20 #include <gtest/gtest.h>
21
22 namespace panda::verifier::test {
23
24 enum class Numbers { ONE, TWO, THREE, FOUR, FIVE };
25
TEST_F(VerifierTest,saturated_enum)26 TEST_F(VerifierTest, saturated_enum)
27 {
28 SaturatedEnum<Numbers, Numbers::FOUR, Numbers::THREE, Numbers::TWO, Numbers::ONE> s_enum;
29 EXPECT_EQ(s_enum, Numbers::ONE);
30
31 #ifndef NDEBUG
32 EXPECT_DEATH(s_enum[Numbers::FIVE], "");
33 #endif
34
35 EXPECT_FALSE(s_enum[Numbers::TWO]);
36 EXPECT_TRUE(s_enum[Numbers::ONE]);
37
38 s_enum |= Numbers::THREE;
39 EXPECT_EQ(s_enum, Numbers::THREE);
40 EXPECT_FALSE(s_enum[Numbers::FOUR]);
41 EXPECT_TRUE(s_enum[Numbers::TWO]);
42
43 int i = 0;
44 s_enum.EnumerateValues([&i](Numbers) {
45 i++;
46 return true;
47 });
48 EXPECT_EQ(i, 3);
49
50 i = 0;
51 s_enum.EnumerateValues([&i](Numbers en) {
52 i++;
53 if (en == Numbers::TWO)
54 return false;
55 return true;
56 });
57 EXPECT_EQ(i, 2);
58 }
59
60 } // namespace panda::verifier::test