• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libpandabase/utils/utils.h"
17 #include "util/enum_tag.h"
18 #include "util/int_tag.h"
19 #include "util/tagged_index.h"
20 #include "util/tests/verifier_test.h"
21 
22 #include <gtest/gtest.h>
23 
24 #include <unordered_set>
25 
26 namespace ark::verifier::test {
27 
TEST_F(VerifierTest,Tagged_index)28 TEST_F(VerifierTest, Tagged_index)
29 {
30     enum class Tag { TAG0, TAG1, TAG2 };
31     using TagType1 = TagForEnum<Tag, Tag::TAG0, Tag::TAG1, Tag::TAG2>;
32     // NOLINTNEXTLINE(readability-magic-numbers)
33     using TagType2 = TagForInt<int, 5_I, 10_I>;
34     TaggedIndex<TagType1, TagType2, size_t> tagind1 {};
35     EXPECT_FALSE(tagind1.IsValid());
36     tagind1.SetTag<0>(Tag::TAG1);
37     tagind1.SetTag<1>(7_I);
38     tagind1.SetInt(8_I);
39     ASSERT_TRUE(tagind1.IsValid());
40     EXPECT_EQ(tagind1.GetTag<0>(), Tag::TAG1);
41     EXPECT_EQ(tagind1.GetTag<1>(), 7_I);
42     EXPECT_EQ(tagind1, 8_I);
43     tagind1.Invalidate();
44     EXPECT_FALSE(tagind1.IsValid());
45 
46     TaggedIndex<TagType2, TagType1> tagind2 {};
47     EXPECT_FALSE(tagind2.IsValid());
48     tagind2.SetTag<1>(Tag::TAG1);
49     tagind2.SetTag<0>(7_I);
50     tagind2.SetInt(8_I);
51     ASSERT_TRUE(tagind2.IsValid());
52     EXPECT_EQ(tagind2.GetTag<1>(), Tag::TAG1);
53     EXPECT_EQ(tagind2.GetTag<0>(), 7_I);
54     EXPECT_EQ(tagind2, 8_I);
55     tagind2.Invalidate();
56     EXPECT_FALSE(tagind2.IsValid());
57 }
58 
TEST_F(VerifierTest,Tagged_index_in_container)59 TEST_F(VerifierTest, Tagged_index_in_container)
60 {
61     enum class Tag { TAG0, TAG1, TAG2 };
62     using TagType1 = TagForEnum<Tag, Tag::TAG0, Tag::TAG1, Tag::TAG2>;
63     // NOLINTNEXTLINE(readability-magic-numbers)
64     using TagType2 = TagForInt<int, 5_I, 10_I>;
65     using TI = TaggedIndex<TagType1, TagType2, size_t>;
66     TI tagind1 {};
67     std::unordered_set<TI> iSet {};
68     tagind1.SetTag<0>(Tag::TAG1);
69     tagind1.SetTag<1>(7_I);
70     tagind1.SetInt(8_I);
71     iSet.insert(tagind1);
72     tagind1.SetTag<0>(Tag::TAG2);
73     // NOLINTNEXTLINE(readability-magic-numbers)
74     tagind1.SetTag<1>(9_I);
75     tagind1.SetInt(3_I);
76     iSet.insert(tagind1);
77     tagind1.SetTag<0>(Tag::TAG1);
78     tagind1.SetTag<1>(7_I);
79     tagind1.SetInt(8_I);
80     EXPECT_EQ(iSet.count(tagind1), 1);
81     tagind1.SetTag<0>(Tag::TAG2);
82     // NOLINTNEXTLINE(readability-magic-numbers)
83     tagind1.SetTag<1>(9_I);
84     tagind1.SetInt(3_I);
85     EXPECT_EQ(iSet.count(tagind1), 1);
86     tagind1.SetInt(4_I);
87     EXPECT_EQ(iSet.count(tagind1), 0);
88 }
89 
90 }  // namespace ark::verifier::test
91