Debian Codegraph @

mfbt/tests/TestBinarySearch.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "mozilla/Assertions.h"
8 #include "mozilla/BinarySearch.h"
9 #include "mozilla/Vector.h"
10
11 #include <cstdlib>
12
13 using mozilla::BinarySearch;
14 using mozilla::BinarySearchIf;
15 using mozilla::Vector;
16
17 #define A(a) MOZ_RELEASE_ASSERT(a)
18
19 struct Person {
20 int mAge;
21 int mId;
22 Person(int aAge, int aId) : mAge(aAge), mId(aId) {}
23 };
24
25 struct GetAge {
26 Vector<Person>& mV;
27 explicit GetAge(Vector<Person>& aV) : mV(aV) {}
28 int operator[](size_t index) const { return mV[index].mAge; }
29 };
30
31 struct RangeFinder {
32 const int mLower, mUpper;
33 RangeFinder(int lower, int upper) : mLower(lower), mUpper(upper) {}
34 int operator()(int val) const {
35 if (val >= mUpper) return -1;
36 if (val < mLower) return 1;
37 return 0;
38 }
39 };
40
41 static void TestBinarySearch() {
42 size_t m;
43
44 Vector<int> v1;
45 MOZ_RELEASE_ASSERT(v1.append(2));
46 MOZ_RELEASE_ASSERT(v1.append(4));
47 MOZ_RELEASE_ASSERT(v1.append(6));
48 MOZ_RELEASE_ASSERT(v1.append(8));
49
50 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 0, v1.length(), 1, &m) && m == 0);
51 MOZ_RELEASE_ASSERT(BinarySearch(v1, 0, v1.length(), 2, &m) && m == 0);
52 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 0, v1.length(), 3, &m) && m == 1);
53 MOZ_RELEASE_ASSERT(BinarySearch(v1, 0, v1.length(), 4, &m) && m == 1);
54 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 0, v1.length(), 5, &m) && m == 2);
55 MOZ_RELEASE_ASSERT(BinarySearch(v1, 0, v1.length(), 6, &m) && m == 2);
56 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 0, v1.length(), 7, &m) && m == 3);
57 MOZ_RELEASE_ASSERT(BinarySearch(v1, 0, v1.length(), 8, &m) && m == 3);
58 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 0, v1.length(), 9, &m) && m == 4);
59
60 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 1, 3, 1, &m) && m == 1);
61 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 1, 3, 2, &m) && m == 1);
62 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 1, 3, 3, &m) && m == 1);
63 MOZ_RELEASE_ASSERT(BinarySearch(v1, 1, 3, 4, &m) && m == 1);
64 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 1, 3, 5, &m) && m == 2);
65 MOZ_RELEASE_ASSERT(BinarySearch(v1, 1, 3, 6, &m) && m == 2);
66 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 1, 3, 7, &m) && m == 3);
67 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 1, 3, 8, &m) && m == 3);
68 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 1, 3, 9, &m) && m == 3);
69
70 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 0, 0, 0, &m) && m == 0);
71 MOZ_RELEASE_ASSERT(!BinarySearch(v1, 0, 0, 9, &m) && m == 0);
72
73 Vector<int> v2;
74 MOZ_RELEASE_ASSERT(!BinarySearch(v2, 0, 0, 0, &m) && m == 0);
75 MOZ_RELEASE_ASSERT(!BinarySearch(v2, 0, 0, 9, &m) && m == 0);
76
77 Vector<Person> v3;
78 MOZ_RELEASE_ASSERT(v3.append(Person(2, 42)));
79 MOZ_RELEASE_ASSERT(v3.append(Person(4, 13)));
80 MOZ_RELEASE_ASSERT(v3.append(Person(6, 360)));
81
82 A(!BinarySearch(GetAge(v3), 0, v3.length(), 1, &m) && m == 0);
83 A(BinarySearch(GetAge(v3), 0, v3.length(), 2, &m) && m == 0);
84 A(!BinarySearch(GetAge(v3), 0, v3.length(), 3, &m) && m == 1);
85 A(BinarySearch(GetAge(v3), 0, v3.length(), 4, &m) && m == 1);
86 A(!BinarySearch(GetAge(v3), 0, v3.length(), 5, &m) && m == 2);
87 A(BinarySearch(GetAge(v3), 0, v3.length(), 6, &m) && m == 2);
88 A(!BinarySearch(GetAge(v3), 0, v3.length(), 7, &m) && m == 3);
89 }
90
91 static void TestBinarySearchIf() {
92 const int v1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
93 const size_t len = std::size(v1);
94 size_t m;
95
96 A(BinarySearchIf(v1, 0, len, RangeFinder(2, 3), &m) && m == 2);
97 A(!BinarySearchIf(v1, 0, len, RangeFinder(-5, -2), &m) && m == 0);
98 A(BinarySearchIf(v1, 0, len, RangeFinder(3, 5), &m) && m >= 3 && m < 5);
99 A(!BinarySearchIf(v1, 0, len, RangeFinder(10, 12), &m) && m == 10);
100 }
101
102 static void TestEqualRange() {
103 struct CompareN {
104 int mVal;
105 explicit CompareN(int n) : mVal(n) {}
106 int operator()(int aVal) const { return mVal - aVal; }
107 };
108
109 constexpr int kMaxNumber = 100;
110 constexpr int kMaxRepeat = 2;
111
112 Vector<int> sortedArray;
113 MOZ_RELEASE_ASSERT(sortedArray.reserve(kMaxNumber * kMaxRepeat));
114
115 // Make a sorted array by appending the loop counter [0, kMaxRepeat] times
116 // in each iteration. The array will be something like:
117 // [0, 0, 1, 1, 2, 2, 8, 9, ..., kMaxNumber]
118 for (int i = 0; i <= kMaxNumber; ++i) {
119 int repeat = rand() % (kMaxRepeat + 1);
120 for (int j = 0; j < repeat; ++j) {
121 MOZ_RELEASE_ASSERT(sortedArray.emplaceBack(i));
122 }
123 }
124
125 for (int i = -1; i < kMaxNumber + 1; ++i) {
126 auto bounds = EqualRange(sortedArray, 0, sortedArray.length(), CompareN(i));
127
128 MOZ_RELEASE_ASSERT(bounds.first <= sortedArray.length());
129 MOZ_RELEASE_ASSERT(bounds.second <= sortedArray.length());
130 MOZ_RELEASE_ASSERT(bounds.first <= bounds.second);
131
132 if (bounds.first == 0) {
133 MOZ_RELEASE_ASSERT(sortedArray[0] >= i);
134 } else if (bounds.first == sortedArray.length()) {
135 MOZ_RELEASE_ASSERT(sortedArray[sortedArray.length() - 1] < i);
136 } else {
137 MOZ_RELEASE_ASSERT(sortedArray[bounds.first - 1] < i);
138 MOZ_RELEASE_ASSERT(sortedArray[bounds.first] >= i);
139 }
140
141 if (bounds.second == 0) {
142 MOZ_RELEASE_ASSERT(sortedArray[0] > i);
143 } else if (bounds.second == sortedArray.length()) {
144 MOZ_RELEASE_ASSERT(sortedArray[sortedArray.length() - 1] <= i);
145 } else {
146 MOZ_RELEASE_ASSERT(sortedArray[bounds.second - 1] <= i);
147 MOZ_RELEASE_ASSERT(sortedArray[bounds.second] > i);
148 }
149 }
150 }
151
152 int main() {
153 TestBinarySearch();
154 TestBinarySearchIf();
155 TestEqualRange();
156 return 0;
157 }