xref: /aosp_15_r20/system/chre/util/tests/heap_test.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2017 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 "chre/util/heap.h"
18 #include "gtest/gtest.h"
19 
20 #include <algorithm>
21 #include <array>
22 
23 using chre::DynamicVector;
24 using chre::FixedSizeVector;
25 
TEST(HeapDeathTest,PushHeapWhenEmpty)26 TEST(HeapDeathTest, PushHeapWhenEmpty) {
27   DynamicVector<int> v;
28   std::less<int> comp;
29   EXPECT_DEATH(chre::push_heap(v, comp), "");
30 }
31 
TEST(HeapDeathTest,PopHeapWhenEmpty)32 TEST(HeapDeathTest, PopHeapWhenEmpty) {
33   DynamicVector<int> v;
34   std::less<int> comp;
35   EXPECT_DEATH(chre::pop_heap(v, comp), "");
36 }
37 
TEST(HeapTest,NestedPushPopHeap)38 TEST(HeapTest, NestedPushPopHeap) {
39   DynamicVector<int> v;
40   std::less<int> comp;
41 
42   // generate random test data
43   const size_t MaxSize = 1000;
44   std::array<int, MaxSize> array;
45   std::array<int, MaxSize> array_sorted;
46   std::srand(0xcafe);
47   for (size_t i = 0; i < MaxSize; ++i) {
48     array[i] = std::rand();
49     array_sorted[i] = array[i];
50   }
51 
52   // make sure the popped data is in the right order of various array sizes.
53   for (size_t s = 1; s < MaxSize + 1; ++s) {
54     for (size_t i = 0; i < s; ++i) {
55       v.push_back(array[i]);
56       chre::push_heap(v, comp);
57     }
58 
59     std::sort(array_sorted.begin(), array_sorted.begin() + s,
60               std::greater<int>());
61 
62     for (size_t i = 0; i < s; ++i) {
63       chre::pop_heap(v, comp);
64       EXPECT_EQ(array_sorted[i], v[v.size() - 1]);
65       v.erase(v.size() - 1);
66     }
67   }
68 }
69 
TEST(HeapDeathTest,RemoveHeapWithInvalidIndex)70 TEST(HeapDeathTest, RemoveHeapWithInvalidIndex) {
71   DynamicVector<int> v;
72   std::less<int> comp;
73 
74   v.push_back(0);
75   chre::push_heap(v, comp);
76   EXPECT_DEATH(chre::remove_heap(v, 1, comp), "");
77 }
78 
TEST(HeapTest,NestedRemoveHeap)79 TEST(HeapTest, NestedRemoveHeap) {
80   DynamicVector<int> v;
81   std::less<int> comp;
82 
83   // generate random test data
84   const size_t MaxSize = 1000;
85   std::array<int, MaxSize> array;
86   std::array<int, MaxSize> array_sorted;
87   std::srand(0xcafe);
88   for (size_t i = 0; i < MaxSize; ++i) {
89     array[i] = std::rand();
90     array_sorted[i] = array[i];
91   }
92 
93   for (size_t s = 1; s < MaxSize + 1; ++s) {
94     for (size_t i = 0; i < s; ++i) {
95       v.push_back(array[i]);
96       chre::push_heap(v, comp);
97     }
98 
99     std::sort(array_sorted.begin(), array_sorted.begin() + s,
100               std::greater<int>());
101 
102     // randomly remove one
103     chre::remove_heap(v, std::rand() % s, comp);
104     int data = v[v.size() - 1];
105     v.erase(v.size() - 1);
106 
107     for (size_t i = 0; i < s; ++i) {
108       if (array_sorted[i] == data) {
109         continue;
110       }
111 
112       chre::pop_heap(v, comp);
113       EXPECT_EQ(array_sorted[i], v[v.size() - 1]);
114       v.erase(v.size() - 1);
115     }
116   }
117 }
118 
TEST(HeapTest,FixedSizeVectorMinHeap)119 TEST(HeapTest, FixedSizeVectorMinHeap) {
120   FixedSizeVector<int, 16> v;
121 
122   for (size_t i = 0; i < 5; ++i) {
123     v.push_back(i);
124     chre::push_heap(v, std::greater<int>());
125   }
126 
127   for (size_t i = 0; i < 5; ++i) {
128     chre::pop_heap(v, std::greater<int>());
129     EXPECT_EQ(i, v[v.size() - 1]);
130     v.erase(v.size() - 1);
131   }
132 }
133