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