1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <audio_utils/linked_hash_map.h>
18
19 #include <gtest/gtest.h>
20
21 using android::audio_utils::linked_hash_map;
22
TEST(linked_hash_map,basic)23 TEST(linked_hash_map, basic) {
24 linked_hash_map<int, int> lhm;
25
26 // assignment
27 lhm[10] = 1;
28 lhm[20] = 2;
29 lhm[30] = 3;
30
31 // access by key
32 ASSERT_EQ(1, lhm[10]);
33 ASSERT_EQ(2, lhm[20]);
34 ASSERT_EQ(3, lhm[30]);
35
36 // iterates in insertion order
37 auto it = lhm.begin();
38 ASSERT_EQ(1UL, it->second);
39 ++it;
40 ASSERT_EQ(2UL, it->second);
41 ++it;
42 ASSERT_EQ(3UL, it->second);
43 ++it;
44 ASSERT_EQ(lhm.end(), it);
45
46 // correct size
47 ASSERT_EQ(3UL, lhm.size());
48
49 // invalid key search returns end().
50 it = lhm.find(22);
51 ASSERT_EQ(lhm.end(), it);
52
53 // valid key search returns proper iterator.
54 it = lhm.find(20);
55 ASSERT_EQ(20, it->first);
56 ASSERT_EQ(2, it->second);
57
58 // test deletion
59 lhm.erase(it);
60 it = lhm.find(20);
61 ASSERT_EQ(lhm.end(), it);
62
63 // correct size
64 ASSERT_EQ(2UL, lhm.size());
65
66 // iterates in order
67 it = lhm.begin();
68 ASSERT_EQ(1UL, it->second);
69 ++it;
70 ASSERT_EQ(3UL, it->second);
71 ++it;
72 ASSERT_EQ(lhm.end(), it);
73
74 // add new value
75 lhm[2] = -1;
76
77 ASSERT_EQ(-1, lhm[2]);
78
79 // iterates in order of insertion
80 it = lhm.begin();
81 ASSERT_EQ(1UL, it->second);
82 ++it;
83 ASSERT_EQ(3UL, it->second);
84 ++it;
85 ASSERT_EQ(-1UL, it->second);
86 ++it;
87 ASSERT_EQ(lhm.end(), it);
88 }
89
TEST(linked_hash_map,structural)90 TEST(linked_hash_map, structural) {
91 linked_hash_map<int, int> lhm;
92
93 // assignment
94 lhm[10] = 1;
95 lhm[20] = 2;
96 lhm[30] = 3;
97
98 // exercise default copy ctor (or move ctor)
99 auto lhm2 = lhm;
100
101 // exercise comparator
102 ASSERT_EQ(lhm, lhm2);
103
104 // access by key
105 ASSERT_EQ(1, lhm2[10]);
106 ASSERT_EQ(2, lhm2[20]);
107 ASSERT_EQ(3, lhm2[30]);
108 }
109