1 /**
2 * Copyright (c) 2021-2024 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 #include "libpandabase/utils/utils.h"
20
21 #include <gtest/gtest.h>
22
23 namespace ark::verifier::test {
24
25 enum class Numbers { ONE, TWO, THREE, FOUR, FIVE };
26
TEST_F(VerifierTest,saturated_enum)27 TEST_F(VerifierTest, saturated_enum)
28 {
29 SaturatedEnum<Numbers, Numbers::FOUR, Numbers::THREE, Numbers::TWO, Numbers::ONE> sEnum;
30 EXPECT_EQ(sEnum, Numbers::ONE);
31
32 #ifndef NDEBUG
33 EXPECT_DEATH(sEnum[Numbers::FIVE], "");
34 #endif
35
36 EXPECT_FALSE(sEnum[Numbers::TWO]);
37 EXPECT_TRUE(sEnum[Numbers::ONE]);
38
39 sEnum |= Numbers::THREE;
40 EXPECT_EQ(sEnum, Numbers::THREE);
41 EXPECT_FALSE(sEnum[Numbers::FOUR]);
42 EXPECT_TRUE(sEnum[Numbers::TWO]);
43
44 int i = 0;
45 sEnum.EnumerateValues([&i](Numbers) {
46 i++;
47 return true;
48 });
49 EXPECT_EQ(i, 3_I);
50
51 i = 0;
52 sEnum.EnumerateValues([&i](Numbers en) {
53 i++;
54 return en != Numbers::TWO;
55 });
56 EXPECT_EQ(i, 2_I);
57 }
58
59 } // namespace ark::verifier::test