-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfuzz.cpp
More file actions
260 lines (204 loc) · 9.8 KB
/
fuzz.cpp
File metadata and controls
260 lines (204 loc) · 9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
MIT License
Copyright (c) 2025 Alex Emirov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <fuzztest/fuzztest.h>
#include <gtest/gtest.h>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include "flatbush.h"
// =============================================================================
// Helper functions and utilities
// =============================================================================
template <typename ArrayType>
flatbush::Flatbush<ArrayType> createIndex(uint32_t iNumItems, uint16_t iNodeSize) {
flatbush::FlatbushBuilder<ArrayType> wBuilder(iNumItems, iNodeSize);
auto wSize = static_cast<size_t>(iNumItems);
for (size_t wIdx = 0; wIdx < wSize; ++wIdx) {
auto coord = static_cast<ArrayType>(wIdx);
wBuilder.add({coord, coord, coord, coord});
}
auto wIndex = wBuilder.finish();
return wIndex;
}
std::vector<size_t> calculateNumNodesPerLevel(uint32_t iNumItems, uint32_t iNodeSize) {
size_t wCount = iNumItems;
size_t wNumNodes = iNumItems;
std::vector<size_t> wLevelBounds{wNumNodes};
do {
wCount = (wCount + iNodeSize - 1) / iNodeSize;
wNumNodes += wCount;
wLevelBounds.push_back(wNumNodes);
} while (wCount > 1);
return wLevelBounds;
}
template <typename ArrayType>
flatbush::Flatbush<ArrayType> createSearchIndex() {
flatbush::FlatbushBuilder<ArrayType> wBuilder;
wBuilder.add({static_cast<ArrayType>(42),
static_cast<ArrayType>(0),
static_cast<ArrayType>(42),
static_cast<ArrayType>(0)});
auto wIndex = wBuilder.finish();
return wIndex;
}
// =============================================================================
// FUZZ_TEST: FuzzFrom - Tests deserialization from binary data
// =============================================================================
template <typename ArrayType>
void FuzzFromTemplate(const std::string& data) {
const uint8_t* iData = flatbush::detail::bit_cast<const uint8_t*>(data.data());
size_t iSize = data.size();
if (iSize < flatbush::gHeaderByteSize) return;
if (iData[0] != flatbush::gValidityFlag) return;
if ((iData[1] >> 4) != flatbush::gVersion) return;
if ((iData[1] & 0x0f) != flatbush::detail::arrayTypeIndex<ArrayType>()) return;
// Use memcpy to safely read unaligned data
uint16_t wNodeSize;
std::memcpy(&wNodeSize, &iData[2], sizeof(uint16_t));
if (wNodeSize < 2) return;
uint32_t wNumItems;
std::memcpy(&wNumItems, &iData[4], sizeof(uint32_t));
const auto& wLevelBounds = calculateNumNodesPerLevel(wNumItems, wNodeSize);
const auto wNumNodes = wLevelBounds.empty() ? wNumItems : wLevelBounds.back();
const auto wIndicesByteSize =
wNumNodes * ((wNumNodes > flatbush::gMaxNumNodes) ? sizeof(uint32_t) : sizeof(uint16_t));
const auto wNodesByteSize = wNumNodes * sizeof(flatbush::Box<ArrayType>);
const auto wSize = flatbush::gHeaderByteSize + wNodesByteSize + wIndicesByteSize;
if (wSize != iSize) return;
auto wIndex = flatbush::FlatbushBuilder<ArrayType>::from(iData, iSize);
ASSERT_EQ(wIndex.data().size(), iSize);
ASSERT_EQ(wIndex.nodeSize(), wNodeSize);
ASSERT_EQ(wIndex.numItems(), wNumItems);
ASSERT_EQ(wIndex.indexSize(), wNumNodes);
}
void FuzzFromInt8(const std::string& data) { FuzzFromTemplate<int8_t>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromInt8);
void FuzzFromUInt8(const std::string& data) { FuzzFromTemplate<uint8_t>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromUInt8);
void FuzzFromInt16(const std::string& data) { FuzzFromTemplate<int16_t>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromInt16);
void FuzzFromUInt16(const std::string& data) { FuzzFromTemplate<uint16_t>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromUInt16);
void FuzzFromInt32(const std::string& data) { FuzzFromTemplate<int32_t>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromInt32);
void FuzzFromUInt32(const std::string& data) { FuzzFromTemplate<uint32_t>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromUInt32);
void FuzzFromFloat(const std::string& data) { FuzzFromTemplate<float>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromFloat);
void FuzzFromDouble(const std::string& data) { FuzzFromTemplate<double>(data); }
FUZZ_TEST(FlatbushFuzzTest, FuzzFromDouble);
// =============================================================================
// FUZZ_TEST: FuzzSearch - Tests spatial search functionality
// =============================================================================
template <typename ArrayType>
void FuzzSearchTemplate(ArrayType minX, ArrayType minY, ArrayType maxX, ArrayType maxY) {
auto wIndex = createSearchIndex<ArrayType>();
auto wResult = wIndex.search({minX, minY, maxX, maxY});
if (minX <= static_cast<ArrayType>(42) && maxX >= static_cast<ArrayType>(42) &&
minY <= static_cast<ArrayType>(0) && maxY >= static_cast<ArrayType>(0)) {
ASSERT_EQ(wResult.size(), 1);
} else {
ASSERT_EQ(wResult.size(), 0);
}
}
void FuzzSearchInt8(int8_t minX, int8_t minY, int8_t maxX, int8_t maxY) {
FuzzSearchTemplate<int8_t>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchInt8);
void FuzzSearchUInt8(uint8_t minX, uint8_t minY, uint8_t maxX, uint8_t maxY) {
FuzzSearchTemplate<uint8_t>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchUInt8);
void FuzzSearchInt16(int16_t minX, int16_t minY, int16_t maxX, int16_t maxY) {
FuzzSearchTemplate<int16_t>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchInt16);
void FuzzSearchUInt16(uint16_t minX, uint16_t minY, uint16_t maxX, uint16_t maxY) {
FuzzSearchTemplate<uint16_t>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchUInt16);
void FuzzSearchInt32(int32_t minX, int32_t minY, int32_t maxX, int32_t maxY) {
FuzzSearchTemplate<int32_t>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchInt32);
void FuzzSearchUInt32(uint32_t minX, uint32_t minY, uint32_t maxX, uint32_t maxY) {
FuzzSearchTemplate<uint32_t>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchUInt32);
void FuzzSearchFloat(float minX, float minY, float maxX, float maxY) {
FuzzSearchTemplate<float>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchFloat);
void FuzzSearchDouble(double minX, double minY, double maxX, double maxY) {
FuzzSearchTemplate<double>(minX, minY, maxX, maxY);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzSearchDouble);
// =============================================================================
// FUZZ_TEST: FuzzNeighbors - Tests nearest neighbor search functionality
// =============================================================================
template <typename ArrayType>
void FuzzNeighborsTemplate(ArrayType iX, ArrayType iY, size_t iMaxResults, double iMaxDistance) {
auto wIndex = createSearchIndex<ArrayType>();
const auto wX = static_cast<double>(iX);
const auto wY = static_cast<double>(iY);
const auto wMaxDistSquared = iMaxDistance * iMaxDistance;
const flatbush::Point<ArrayType> wPoint{iX, iY};
const auto wResult = wIndex.neighbors(wPoint, iMaxResults, iMaxDistance);
const auto wDistance = std::pow(wX - 42, 2.0) + std::pow(wY, 2.0);
if (iMaxResults > 0 && iMaxDistance >= 0.0 && std::isnormal(wMaxDistSquared) &&
wDistance <= wMaxDistSquared) {
ASSERT_EQ(wResult.size(), 1);
} else {
ASSERT_EQ(wResult.size(), 0);
}
}
void FuzzNeighborsInt8(int8_t iX, int8_t iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<int8_t>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsInt8);
void FuzzNeighborsUInt8(uint8_t iX, uint8_t iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<uint8_t>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsUInt8);
void FuzzNeighborsInt16(int16_t iX, int16_t iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<int16_t>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsInt16);
void FuzzNeighborsUInt16(uint16_t iX, uint16_t iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<uint16_t>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsUInt16);
void FuzzNeighborsInt32(int32_t iX, int32_t iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<int32_t>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsInt32);
void FuzzNeighborsUInt32(uint32_t iX, uint32_t iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<uint32_t>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsUInt32);
void FuzzNeighborsFloat(float iX, float iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<float>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsFloat);
void FuzzNeighborsDouble(double iX, double iY, size_t iMaxResults, double iMaxDistance) {
FuzzNeighborsTemplate<double>(iX, iY, iMaxResults, iMaxDistance);
}
FUZZ_TEST(FlatbushFuzzTest, FuzzNeighborsDouble);