1 /*
2 * Copyright (C) 2014 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 "androidfw/ByteBucketArray.h"
18
19 #include "gtest/gtest.h"
20
21 namespace android {
22
TEST(ByteBucketArrayTest,TestSparseInsertion)23 TEST(ByteBucketArrayTest, TestSparseInsertion) {
24 ByteBucketArray<int> bba;
25 ASSERT_TRUE(bba.set(0, 1));
26 ASSERT_TRUE(bba.set(10, 2));
27 ASSERT_TRUE(bba.set(26, 3));
28 ASSERT_TRUE(bba.set(129, 4));
29 ASSERT_TRUE(bba.set(234, 5));
30
31 for (size_t i = 0; i < bba.size(); i++) {
32 switch (i) {
33 case 0:
34 EXPECT_EQ(1, bba[i]);
35 break;
36 case 10:
37 EXPECT_EQ(2, bba[i]);
38 break;
39 case 26:
40 EXPECT_EQ(3, bba[i]);
41 break;
42 case 129:
43 EXPECT_EQ(4, bba[i]);
44 break;
45 case 234:
46 EXPECT_EQ(5, bba[i]);
47 break;
48 default:
49 EXPECT_EQ(0, bba[i]);
50 break;
51 }
52 }
53 }
54
TEST(ByteBucketArrayTest,TestForEach)55 TEST(ByteBucketArrayTest, TestForEach) {
56 ByteBucketArray<int> bba;
57 ASSERT_TRUE(bba.set(0, 1));
58 ASSERT_TRUE(bba.set(10, 2));
59 ASSERT_TRUE(bba.set(26, 3));
60 ASSERT_TRUE(bba.set(129, 4));
61 ASSERT_TRUE(bba.set(234, 5));
62
63 int count = 0;
64 bba.forEachItem([&count](auto i, auto val) {
65 ++count;
66 switch (i) {
67 case 0:
68 EXPECT_EQ(1, val);
69 break;
70 case 10:
71 EXPECT_EQ(2, val);
72 break;
73 case 26:
74 EXPECT_EQ(3, val);
75 break;
76 case 129:
77 EXPECT_EQ(4, val);
78 break;
79 case 234:
80 EXPECT_EQ(5, val);
81 break;
82 default:
83 EXPECT_EQ(0, val);
84 break;
85 }
86 });
87 ASSERT_EQ(4 * 16, count);
88 }
89
TEST(ByteBucketArrayTest,TestTrimBuckets)90 TEST(ByteBucketArrayTest, TestTrimBuckets) {
91 ByteBucketArray<int> bba;
92 ASSERT_TRUE(bba.set(0, 1));
93 ASSERT_TRUE(bba.set(255, 2));
94 {
95 bba.trimBuckets([](auto val) { return val < 2; });
96 int count = 0;
97 bba.forEachItem([&count](auto, auto) { ++count; });
98 ASSERT_EQ(1 * 16, count);
99 }
100 {
101 bba.trimBuckets([](auto val) { return val < 3; });
102 int count = 0;
103 bba.forEachItem([&count](auto, auto) { ++count; });
104 ASSERT_EQ(0, count);
105 }
106 }
107
108 } // namespace android
109