Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
1843 lines (1550 loc) · 80 KB
/
Copy pathtest.cpp
File metadata and controls
1843 lines (1550 loc) · 80 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <cmath>
#include <limits>
#include <numeric>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "flatbush.h"
using ::testing::HasSubstr;
using ::testing::Test;
using ::testing::ThrowsMessage;
using ::testing::Types;
struct ThrowingNumber {
ThrowingNumber(double) {}
};
struct MoveOnlyFilter {
MoveOnlyFilter() = default;
MoveOnlyFilter(const MoveOnlyFilter&) = delete;
MoveOnlyFilter& operator=(const MoveOnlyFilter&) = delete;
MoveOnlyFilter(MoveOnlyFilter&&) = default;
MoveOnlyFilter& operator=(MoveOnlyFilter&&) = default;
bool operator()(size_t iValue, const flatbush::Box<double>&) const { return iValue % 2UL == 0UL; }
};
struct MoveOnlyDistance {
MoveOnlyDistance() = default;
MoveOnlyDistance(const MoveOnlyDistance&) = delete;
MoveOnlyDistance& operator=(const MoveOnlyDistance&) = delete;
MoveOnlyDistance(MoveOnlyDistance&&) = default;
MoveOnlyDistance& operator=(MoveOnlyDistance&&) = default;
double operator()(const flatbush::Point<double>& iPoint, const flatbush::Box<double>& iBox) const {
return flatbush::detail::computeDistanceSquared(iPoint, iBox);
}
};
#if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
constexpr bool canMutateSpanAtCompileTime() {
int wValues[] = { 1, 2 };
flatbush::span<int> wView(wValues, 2);
wView[0] = 3;
*wView.begin() = 4;
*(wView.end() - 1) = 5;
return wView[0] == 4 && wView[1] == 5;
}
constexpr bool canUpdateBoundsAtCompileTime() {
flatbush::Box<int> wBounds { 2, 3, 4, 5 };
flatbush::detail::updateBounds(wBounds, flatbush::Box<int> { 1, 4, 6, 7 });
return wBounds.mMinX == 1 && wBounds.mMinY == 3 && wBounds.mMaxX == 6 && wBounds.mMaxY == 7;
}
constexpr bool canCalculateDataSizeAtCompileTime() {
size_t wDataSize = 0;
return flatbush::detail::tryCalculateDataSize<uint8_t>(2, 16, wDataSize) && wDataSize == 26;
}
static_assert(canMutateSpanAtCompileTime(), "Span mutation must be constant-evaluable");
static_assert(canUpdateBoundsAtCompileTime(), "Bounds updates must be constant-evaluable");
static_assert(flatbush::detail::Interleave(3) == 5, "Bit interleaving must be constant-evaluable");
static_assert(flatbush::detail::HilbertXYToIndex(0, 0) == 0, "Hilbert mapping must be constant-evaluable");
static_assert(flatbush::detail::axisDistance(5, 0, 3) == 2.0, "Axis distance must be constant-evaluable");
static_assert(flatbush::detail::computeDistanceSquared(flatbush::Point<int64_t> { 3, 4 },
flatbush::Box<int64_t> { 0, 0, 1, 1 }) == 13.0,
"Squared distance must be constant-evaluable");
static_assert(canCalculateDataSizeAtCompileTime(), "Serialized size calculation must be constant-evaluable");
#endif
static_assert(noexcept(flatbush::detail::Interleave(3)), "Bit interleaving cannot throw");
static_assert(noexcept(flatbush::detail::HilbertXYToIndex(0, 0)), "Hilbert mapping cannot throw");
static_assert(noexcept(flatbush::detail::arrayTypeIndex<int8_t>()) &&
noexcept(flatbush::detail::arrayTypeIndex<uint8_t>()) &&
noexcept(flatbush::detail::arrayTypeIndex<int16_t>()) &&
noexcept(flatbush::detail::arrayTypeIndex<uint16_t>()) &&
noexcept(flatbush::detail::arrayTypeIndex<int32_t>()) &&
noexcept(flatbush::detail::arrayTypeIndex<uint32_t>()) &&
noexcept(flatbush::detail::arrayTypeIndex<float>()) &&
noexcept(flatbush::detail::arrayTypeIndex<double>()) &&
noexcept(flatbush::detail::arrayTypeIndex<void>()),
"Array type lookup cannot throw");
static_assert(noexcept(flatbush::detail::isArrayTypeCompatible<uint8_t>(2)),
"Array type compatibility checks cannot throw");
static_assert(noexcept(flatbush::detail::arrayTypeName(0)), "Array type naming cannot throw");
static_assert(
!noexcept(std::declval<flatbush::FlatbushBuilder<double>&>().add(std::declval<const flatbush::Box<double>&>())),
"Builder insertion can allocate");
static_assert(
!noexcept(std::declval<flatbush::FlatbushBuilder<double>&>().add(std::declval<const flatbush::Point<double>&>())),
"Point insertion can allocate");
static_assert(
!noexcept(std::declval<const flatbush::Flatbush<double>&>().search(std::declval<const flatbush::Box<double>&>())),
"Search can allocate and invoke callbacks");
static_assert(!noexcept(std::declval<const flatbush::Flatbush<double>&>().neighbors(
std::declval<const flatbush::Point<double>&>())),
"Neighbor search can allocate and invoke callbacks");
static_assert(!noexcept(static_cast<flatbush::Box<ThrowingNumber>>(std::declval<const flatbush::Box<double>&>())),
"Box coordinate conversion can invoke user code");
static_assert(!noexcept(static_cast<flatbush::Point<ThrowingNumber>>(std::declval<const flatbush::Point<double>&>())),
"Point coordinate conversion can invoke user code");
static_assert(std::is_same<decltype(std::declval<const flatbush::Flatbush<double>&>().bounds()),
const flatbush::Box<double>&>::value,
"Bounds must be exposed as a read-only reference");
static_assert(noexcept(std::declval<const flatbush::Flatbush<double>&>().bounds()),
"Reading cached bounds cannot fail");
static_assert(flatbush::detail::acceptAllFilter(0, flatbush::Box<int> { 0, 0, 0, 0 }),
"Accept-all filter must be constant-evaluable");
static constexpr std::array<double, 400> gData {
8, 62, 11, 66, 57, 17, 57, 19, 76, 26, 79, 29, 36, 56, 38, 56, 92, 77, 96, 80, 87, 70, 90, 74, 43, 41, 47, 43, 0,
58, 2, 62, 76, 86, 80, 89, 27, 13, 27, 15, 71, 63, 75, 67, 25, 2, 27, 2, 87, 6, 88, 6, 22, 90, 23, 93, 22, 89,
22, 93, 57, 11, 61, 13, 61, 55, 63, 56, 17, 85, 21, 87, 33, 43, 37, 43, 6, 1, 7, 3, 80, 87, 80, 87, 23, 50, 26,
52, 58, 89, 58, 89, 12, 30, 15, 34, 32, 58, 36, 61, 41, 84, 44, 87, 44, 18, 44, 19, 13, 63, 15, 67, 52, 70, 54, 74,
57, 59, 58, 59, 17, 90, 20, 92, 48, 53, 52, 56, 92, 68, 92, 72, 26, 52, 30, 52, 56, 23, 57, 26, 88, 48, 88, 48, 66,
13, 67, 15, 7, 82, 8, 86, 46, 68, 50, 68, 37, 33, 38, 36, 6, 15, 8, 18, 85, 36, 89, 38, 82, 45, 84, 48, 12, 2,
16, 3, 26, 15, 26, 16, 55, 23, 59, 26, 76, 37, 79, 39, 86, 74, 90, 77, 16, 75, 18, 78, 44, 18, 45, 21, 52, 67, 54,
71, 59, 78, 62, 78, 24, 5, 24, 8, 64, 80, 64, 83, 66, 55, 70, 55, 0, 17, 2, 19, 15, 71, 18, 74, 87, 57, 87, 59,
6, 34, 7, 37, 34, 30, 37, 32, 51, 19, 53, 19, 72, 51, 73, 55, 29, 45, 30, 45, 94, 94, 96, 95, 7, 22, 11, 24, 86,
45, 87, 48, 33, 62, 34, 65, 18, 10, 21, 14, 64, 66, 67, 67, 64, 25, 65, 28, 27, 4, 31, 6, 84, 4, 85, 5, 48, 80,
50, 81, 1, 61, 3, 61, 71, 89, 74, 92, 40, 42, 43, 43, 27, 64, 28, 66, 46, 26, 50, 26, 53, 83, 57, 87, 14, 75, 15,
79, 31, 45, 34, 45, 89, 84, 92, 88, 84, 51, 85, 53, 67, 87, 67, 89, 39, 26, 43, 27, 47, 61, 47, 63, 23, 49, 25, 53,
12, 3, 14, 5, 16, 50, 19, 53, 63, 80, 64, 84, 22, 63, 22, 64, 26, 66, 29, 66, 2, 15, 3, 15, 74, 77, 77, 79, 64,
11, 68, 11, 38, 4, 39, 8, 83, 73, 87, 77, 85, 52, 89, 56, 74, 60, 76, 63, 62, 66, 65, 67
};
static const std::array<uint8_t, 518> gFlatbush {
251, 56, 16, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 64, 0, 0, 0, 0, 0, 0, 79, 64, 0, 0, 0, 0, 0, 0, 38, 64,
0, 0, 0, 0, 0, 128, 80, 64, 0, 0, 0, 0, 0, 128, 76, 64, 0, 0, 0, 0, 0, 0, 49, 64, 0, 0, 0, 0, 0, 128, 76, 64,
0, 0, 0, 0, 0, 0, 51, 64, 0, 0, 0, 0, 0, 0, 83, 64, 0, 0, 0, 0, 0, 0, 58, 64, 0, 0, 0, 0, 0, 192, 83, 64,
0, 0, 0, 0, 0, 0, 61, 64, 0, 0, 0, 0, 0, 0, 66, 64, 0, 0, 0, 0, 0, 0, 76, 64, 0, 0, 0, 0, 0, 0, 67, 64,
0, 0, 0, 0, 0, 0, 76, 64, 0, 0, 0, 0, 0, 0, 87, 64, 0, 0, 0, 0, 0, 64, 83, 64, 0, 0, 0, 0, 0, 0, 88, 64,
0, 0, 0, 0, 0, 0, 84, 64, 0, 0, 0, 0, 0, 192, 85, 64, 0, 0, 0, 0, 0, 128, 81, 64, 0, 0, 0, 0, 0, 128, 86, 64,
0, 0, 0, 0, 0, 128, 82, 64, 0, 0, 0, 0, 0, 128, 69, 64, 0, 0, 0, 0, 0, 128, 68, 64, 0, 0, 0, 0, 0, 128, 71, 64,
0, 0, 0, 0, 0, 128, 69, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 64, 0, 0, 0, 0, 0, 0, 0, 64,
0, 0, 0, 0, 0, 0, 79, 64, 0, 0, 0, 0, 0, 0, 83, 64, 0, 0, 0, 0, 0, 128, 85, 64, 0, 0, 0, 0, 0, 0, 84, 64,
0, 0, 0, 0, 0, 64, 86, 64, 0, 0, 0, 0, 0, 0, 59, 64, 0, 0, 0, 0, 0, 0, 42, 64, 0, 0, 0, 0, 0, 0, 59, 64,
0, 0, 0, 0, 0, 0, 46, 64, 0, 0, 0, 0, 0, 192, 81, 64, 0, 0, 0, 0, 0, 128, 79, 64, 0, 0, 0, 0, 0, 192, 82, 64,
0, 0, 0, 0, 0, 192, 80, 64, 0, 0, 0, 0, 0, 0, 57, 64, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 59, 64,
0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 192, 85, 64, 0, 0, 0, 0, 0, 0, 24, 64, 0, 0, 0, 0, 0, 0, 86, 64,
0, 0, 0, 0, 0, 0, 24, 64, 0, 0, 0, 0, 0, 0, 54, 64, 0, 0, 0, 0, 0, 128, 86, 64, 0, 0, 0, 0, 0, 0, 55, 64,
0, 0, 0, 0, 0, 64, 87, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 88, 64,
0, 0, 0, 0, 0, 64, 87, 64, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0,
12, 0, 13, 0, 0, 0
};
flatbush::Flatbush<double> createIndex() {
auto wNumItems = gData.size() / 4;
flatbush::FlatbushBuilder<double> wBuilder(wNumItems);
for (size_t wIdx = 0; wIdx < gData.size(); wIdx += 4) {
wBuilder.add({ gData[wIdx], gData[wIdx + 1], gData[wIdx + 2], gData[wIdx + 3] });
}
auto wIndex = wBuilder.finish();
EXPECT_EQ(wIndex.numItems(), wNumItems);
EXPECT_EQ(wIndex.nodeSize(), flatbush::gDefaultNodeSize);
return wIndex;
}
flatbush::Flatbush<double> createSmallIndex(uint32_t iNumItems, uint16_t iNodeSize) {
flatbush::FlatbushBuilder<double> wBuilder(iNumItems, iNodeSize);
auto wSize = static_cast<size_t>(iNumItems) * 4;
for (size_t wIdx = 0; wIdx < wSize; wIdx += 4) {
wBuilder.add({ gData[wIdx], gData[wIdx + 1], gData[wIdx + 2], gData[wIdx + 3] });
}
auto wIndex = wBuilder.finish();
EXPECT_EQ(wIndex.numItems(), iNumItems);
EXPECT_EQ(wIndex.nodeSize(), iNodeSize);
return wIndex;
}
TEST(FlatbushBaselineTest, RoundTripClampedNodeSizes) {
// Baseline's 65536, 65537 and -1 are not representable by the uint16_t node size
for (const auto wNodeSize : { uint16_t { 16 }, uint16_t { 2 }, uint16_t { 65535 }, uint16_t { 0 }, uint16_t { 1 } }) {
flatbush::FlatbushBuilder<double> wBuilder(3, wNodeSize);
wBuilder.add(flatbush::Point<double> { 10, 0 });
wBuilder.add(flatbush::Point<double> { 100, 0 });
wBuilder.add(flatbush::Point<double> { 0, 0 });
const auto wIndex = wBuilder.finish();
EXPECT_EQ(wIndex.nodeSize(), std::max(wNodeSize, flatbush::gMinNodeSize));
const auto wRestored = flatbush::FlatbushBuilder<double>::from(wIndex.data().data(), wIndex.data().size());
EXPECT_EQ(wRestored.nodeSize(), wIndex.nodeSize());
EXPECT_EQ(wRestored.search({ 0, 0, 0, 0 }), wIndex.search({ 0, 0, 0, 0 }));
auto wIds = wRestored.search({ -1, -1, 101, 1 });
std::sort(wIds.begin(), wIds.end());
EXPECT_EQ(wIds, (std::vector<size_t> { 0UL, 1UL, 2UL }));
EXPECT_EQ(wRestored.neighbors({ 0, 0 }, 1), std::vector<size_t> { 2UL });
}
}
TEST(FlatbushBaselineTest, IndexBunchOfRectangles) {
auto wIndex = createIndex();
ASSERT_EQ(wIndex.indexSize() * 4 + wIndex.indexSize(), 540);
const auto& wBounds = wIndex.bounds();
EXPECT_EQ(wBounds.mMinX, 0);
EXPECT_EQ(wBounds.mMinY, 1);
EXPECT_EQ(wBounds.mMaxX, 96);
EXPECT_EQ(wBounds.mMaxY, 95);
auto wData = wIndex.data();
auto wBoxes = flatbush::detail::bit_cast<const double*>(&wData[flatbush::gHeaderByteSize]);
size_t wBoxLen = wIndex.indexSize() * 4;
EXPECT_EQ(wBoxes[wBoxLen - 4], 0);
EXPECT_EQ(wBoxes[wBoxLen - 3], 1);
EXPECT_EQ(wBoxes[wBoxLen - 2], 96);
EXPECT_EQ(wBoxes[wBoxLen - 1], 95);
auto wIndices = flatbush::detail::bit_cast<const flatbush::NarrowIndexType*>(&wBoxes[wBoxLen]);
EXPECT_EQ(wIndices[wIndex.indexSize() - 1], 400);
}
TEST(FlatbushBaselineTest, SkipSortingLessThanNodeSizeRectangles) {
uint32_t wNumItems = 14;
uint16_t wNodeSize = 16;
auto wIndex = createSmallIndex(wNumItems, wNodeSize);
// compute expected root box extents
auto wRootMinX = std::numeric_limits<double>::max();
auto wRootMinY = std::numeric_limits<double>::max();
auto wRootMaxX = std::numeric_limits<double>::lowest();
auto wRootMaxY = std::numeric_limits<double>::lowest();
auto wSize = static_cast<size_t>(wNumItems);
for (size_t wIdx = 0; wIdx < wSize * 4; wIdx += 4) {
if (gData[wIdx] < wRootMinX) wRootMinX = gData[wIdx];
if (gData[wIdx + 1] < wRootMinY) wRootMinY = gData[wIdx + 1];
if (gData[wIdx + 2] > wRootMaxX) wRootMaxX = gData[wIdx + 2];
if (gData[wIdx + 3] > wRootMaxY) wRootMaxY = gData[wIdx + 3];
}
auto wData = wIndex.data();
auto wBoxes = flatbush::detail::bit_cast<const double*>(&wData[flatbush::gHeaderByteSize]);
size_t wBoxLen = wIndex.indexSize() * 4;
ASSERT_EQ(wBoxLen, (wSize + 1) * 4);
auto wIndices = flatbush::detail::bit_cast<const flatbush::NarrowIndexType*>(&wBoxes[wBoxLen]);
// sort should be skipped, ordered progressing indices expected
for (size_t wIdx = 0; wIdx < wSize; ++wIdx) {
EXPECT_EQ(wIndices[wIdx], wIdx);
}
EXPECT_EQ(wIndices[wSize], 0);
EXPECT_EQ(wBoxes[wBoxLen - 4], wRootMinX);
EXPECT_EQ(wBoxes[wBoxLen - 3], wRootMinY);
EXPECT_EQ(wBoxes[wBoxLen - 2], wRootMaxX);
EXPECT_EQ(wBoxes[wBoxLen - 1], wRootMaxY);
}
TEST(FlatbushBaselineTest, PerformBoxSearch) {
auto wIndex = createIndex();
flatbush::Box<double> box { 40, 40, 60, 60 };
auto wIds = wIndex.search(box);
std::vector<double> wExpected = { 57, 59, 58, 59, 48, 53, 52, 56, 40, 42, 43, 43, 43, 41, 47, 43 };
std::vector<double> wResults;
for (const auto wId : wIds) {
ASSERT_LT(wId, gData.size() / 4);
wResults.push_back(gData[4 * wId]);
wResults.push_back(gData[4 * wId + 1]);
wResults.push_back(gData[4 * wId + 2]);
wResults.push_back(gData[4 * wId + 3]);
}
std::sort(wExpected.begin(), wExpected.end());
std::sort(wResults.begin(), wResults.end());
EXPECT_EQ(wResults, wExpected);
}
TEST(FlatbushBaselineTest, ReconstructIndexFromArrayBuffer) {
auto wIndex = createIndex();
auto wIndexBuffer = wIndex.data();
auto wIndex2 = flatbush::FlatbushBuilder<double>::from(wIndexBuffer.data(), wIndexBuffer.size());
auto wIndex2Buffer = wIndex2.data();
EXPECT_EQ(wIndex2.numItems(), wIndex.numItems());
EXPECT_EQ(wIndex2.nodeSize(), wIndex.nodeSize());
EXPECT_EQ(wIndex2.indexSize(), wIndex.indexSize());
ASSERT_EQ(wIndex2Buffer.size(), wIndexBuffer.size());
EXPECT_TRUE(std::equal(wIndexBuffer.begin(), wIndexBuffer.end(), wIndex2Buffer.begin()));
EXPECT_EQ(wIndex2.search({ 40, 40, 60, 60 }), wIndex.search({ 40, 40, 60, 60 }));
EXPECT_EQ(wIndex2.neighbors({ 50, 50 }, 3), wIndex.neighbors({ 50, 50 }, 3));
}
TEST(FlatbushBaselineTest, FromViewRejectsMisalignedOffset) {
const auto wIndex = createIndex();
const size_t wByteOffset = alignof(double) > alignof(uint32_t) ? 12UL : 13UL;
auto wBuffer = std::vector<uint8_t>(wIndex.data().size() + wByteOffset);
std::memcpy(wBuffer.data() + wByteOffset, wIndex.data().data(), wIndex.data().size());
const auto wBytes = flatbush::span<const uint8_t> { wBuffer.data() + wByteOffset, wIndex.data().size() };
EXPECT_THROW({ static_cast<void>(flatbush::FlatbushBuilder<double>::fromView(wBytes)); }, std::invalid_argument);
}
TEST(FlatbushBaselineTest, ReconstructIndexFromBufferOffset) {
const auto wIndex = createIndex();
const size_t wByteOffset = 16UL;
auto wBuffer = std::vector<uint8_t>(wIndex.data().size() + wByteOffset);
std::memcpy(wBuffer.data() + wByteOffset, wIndex.data().data(), wIndex.data().size());
const auto wBytes = flatbush::span<const uint8_t> { wBuffer.data() + wByteOffset, wIndex.data().size() };
const auto wIndex2 = flatbush::FlatbushBuilder<double>::fromView(wBytes);
EXPECT_EQ(wIndex2.numItems(), wIndex.numItems());
EXPECT_EQ(wIndex2.nodeSize(), wIndex.nodeSize());
EXPECT_EQ(wIndex2.indexSize(), wIndex.indexSize());
ASSERT_EQ(wIndex2.data().size(), wIndex.data().size());
EXPECT_TRUE(std::equal(wIndex.data().begin(), wIndex.data().end(), wIndex2.data().begin()));
EXPECT_EQ(wIndex2.search({ 40, 40, 60, 60 }), wIndex.search({ 40, 40, 60, 60 }));
EXPECT_EQ(wIndex2.neighbors({ 50, 50 }, 3), wIndex.neighbors({ 50, 50 }, 3));
EXPECT_TRUE(wIndex2.isView());
EXPECT_EQ(wIndex2.data().data(), wBuffer.data() + wByteOffset);
}
TEST(FlatbushBaselineTest, AddPointWithoutMaxCoordinates) {
flatbush::FlatbushBuilder<double> wBuilder(1);
wBuilder.add(flatbush::Point<double> { 10, 10 });
const auto wIndex = wBuilder.finish();
EXPECT_EQ(wIndex.search({ 0, 0, 20, 20 }), std::vector<size_t> { 0UL });
}
TEST(FlatbushBaselineTest, FinishRejectsEmptyIndex) {
EXPECT_THROW(
{
flatbush::FlatbushBuilder<double> wBuilder(gData.size() / 4);
static_cast<void>(wBuilder.finish());
},
std::invalid_argument);
}
TEST(FlatbushBaselineTest, DoesNotFreezeOnZeroNumItems) {
EXPECT_THROW(
{
flatbush::FlatbushBuilder<double> wBuilder(0);
static_cast<void>(wBuilder.finish());
},
std::invalid_argument);
}
TEST(FlatbushBaselineTest, PerformNeighborsQuery) {
auto wIndex = createIndex();
auto wIds = wIndex.neighbors({ 50, 50 }, 3);
std::vector<size_t> wExpected = { 31, 6, 75 };
std::sort(wExpected.begin(), wExpected.end());
std::sort(wIds.begin(), wIds.end());
EXPECT_EQ(wIds, wExpected);
}
TEST(FlatbushBaselineTest, NeighborsQueryMaxDistance) {
auto wIndex = createIndex();
auto wIds = wIndex.neighbors({ 50, 50 }, flatbush::gMaxResults, 12);
std::vector<size_t> wExpected = { 6, 29, 31, 75, 85 };
std::sort(wExpected.begin(), wExpected.end());
std::sort(wIds.begin(), wIds.end());
EXPECT_EQ(wIds, wExpected);
}
TEST(FlatbushBaselineTest, NeighborsQueryFilterFunc) {
auto wIndex = createIndex();
auto wIds = wIndex.neighbors({ 50, 50 }, 6, flatbush::gMaxDistance, [](size_t iValue, const flatbush::Box<double>&) {
return iValue % 2 == 0;
});
std::vector<size_t> wExpected = { 6, 16, 18, 24, 54, 80 };
std::sort(wExpected.begin(), wExpected.end());
std::sort(wIds.begin(), wIds.end());
EXPECT_EQ(wIds, wExpected);
}
TEST(FlatbushBaselineTest, NeighborsQueryAllItems) {
auto wIndex = createIndex();
auto wIds = wIndex.neighbors({ 50, 50 });
EXPECT_EQ(wIds.size(), gData.size() / 4);
}
TEST(FlatbushBaselineTest, ReturnIndexOfNewlyAddedRectangle) {
const size_t wCount = 5;
flatbush::FlatbushBuilder<double> wBuilder(wCount);
std::vector<size_t> wIds;
for (size_t wIdx = 0; wIdx < wCount; ++wIdx) {
wIds.push_back(wBuilder.add({ gData[wIdx], gData[wIdx + 1], gData[wIdx + 2], gData[wIdx + 3] }));
}
EXPECT_EQ(wIds, (std::vector<size_t> { 0, 1, 2, 3, 4 }));
}
TEST(FlatbushBaselineTest, QuickSortImbalancedDataset) {
const uint32_t wNumItems = 15000;
flatbush::FlatbushBuilder<double> wBuilder(2 * wNumItems);
const auto linspace = [](double wStart, double wStop, uint32_t wNum) {
const auto wStep = (wStop - wStart) / (wNum - 1);
std::vector<double> wItems(wNum);
for (uint32_t wIndex = 0; wIndex < wNum; ++wIndex) {
wItems[wIndex] = wStart + wStep * static_cast<double>(wIndex);
}
return wItems;
};
const auto wItems = linspace(0, 1000, wNumItems);
const auto wItems2 = linspace(0, 1000, wNumItems);
for (const auto wItem : wItems) {
wBuilder.add({ wItem, 0, wItem, 0 });
}
for (const auto wItem : wItems2) {
wBuilder.add({ wItem, 0, wItem, 0 });
}
const auto wIndex = wBuilder.finish();
EXPECT_NO_THROW(static_cast<void>(wIndex.search({ -100, -1, 15000, 1 })));
}
TEST(FlatbushBaselineTest, QuickSortWorksOnDuplicates) {
uint32_t wNumItems = 55000 + 5500 + 7700;
flatbush::FlatbushBuilder<double> wBuilder(wNumItems);
auto wX = 0.0;
for (uint32_t wCount = 0; wCount < 55000; ++wCount, ++wX) {
wBuilder.add({ wX, 3.0, wX, 3.0 });
}
for (uint32_t wCount = 0; wCount < 5500; ++wCount, ++wX) {
wBuilder.add({ wX, 4.0, wX, 4.0 });
}
for (uint32_t wCount = 0; wCount < 7700; ++wCount, ++wX) {
wBuilder.add({ wX, 5.0, wX, 5.0 });
}
const auto wIndex = wBuilder.finish();
const auto wIds = wIndex.search({ 0.5, -1, 6.5, 1 });
EXPECT_EQ(wIds.size(), 0);
const auto wIds2 = wIndex.search({ 55000, 4.0, 55000, 4.0 });
EXPECT_EQ(wIds2.size(), 1);
}
TEST(FlatbushTest, SkipSortingEqualHilbertValues) {
static constexpr auto kNumItems = 64U;
flatbush::FlatbushBuilder<double> wBuilder(kNumItems);
for (auto wIdx = 0U; wIdx < kNumItems; ++wIdx) {
wBuilder.add({ 42.0, 42.0, 42.0, 42.0 });
}
const auto wIndex = wBuilder.finish();
const auto wData = wIndex.data();
const auto wBoxes = flatbush::detail::bit_cast<const flatbush::Box<double>*>(&wData[flatbush::gHeaderByteSize]);
const auto wIndices = flatbush::detail::bit_cast<const flatbush::NarrowIndexType*>(wBoxes + wIndex.indexSize());
for (auto wIdx = 0U; wIdx < kNumItems; ++wIdx) {
EXPECT_EQ(wIndices[wIdx], wIdx);
}
}
TEST(FlatbushTest, SkipSortingIncreasingHilbertValues) {
static constexpr auto kNumItems = 64U;
auto wBoxes = std::vector<flatbush::Box<double>> {};
auto wOrder = std::vector<size_t> {};
wBoxes.reserve(kNumItems);
wOrder.reserve(kNumItems);
for (auto wIdx = 0U; wIdx < kNumItems; ++wIdx) {
const auto wX = static_cast<double>(wIdx);
const auto wY = static_cast<double>((wIdx * 17U) % kNumItems);
wBoxes.push_back({ wX, wY, wX, wY });
wOrder.push_back(wIdx);
}
const auto wHilbertValues = flatbush::detail::computeHilbertValues(wBoxes.size(),
{ 0.0,
0.0,
static_cast<double>(kNumItems - 1U),
static_cast<double>(kNumItems - 1U) },
flatbush::span<flatbush::Box<double>> {
wBoxes.data(), wBoxes.size() });
std::sort(wOrder.begin(), wOrder.end(), [&wHilbertValues](size_t iLeft, size_t iRight) {
return wHilbertValues[iLeft] < wHilbertValues[iRight];
});
ASSERT_LT(wHilbertValues[wOrder.front()], wHilbertValues[wOrder.back()]);
flatbush::FlatbushBuilder<double> wBuilder(kNumItems);
for (const auto wIdx : wOrder) {
wBuilder.add(wBoxes[wIdx]);
}
const auto wIndex = wBuilder.finish();
const auto wData = wIndex.data();
const auto wPackedBoxes = flatbush::detail::bit_cast<const flatbush::Box<double>*>(&wData[flatbush::gHeaderByteSize]);
const auto wIndices = flatbush::detail::bit_cast<const flatbush::NarrowIndexType*>(wPackedBoxes + wIndex.indexSize());
for (auto wIdx = 0U; wIdx < kNumItems; ++wIdx) {
EXPECT_EQ(wIndices[wIdx], wIdx);
}
}
TEST(FlatbushTest, NeighborsDistanceCallbackIsInvoked) {
auto wIndex = createIndex();
size_t wCalls = 0;
auto wIds = wIndex.neighbors({ 50, 50 },
3,
flatbush::gMaxDistance,
flatbush::detail::acceptAllFilter<double>,
[&wCalls](const flatbush::Point<double>& iPoint, const flatbush::Box<double>& iBox) {
++wCalls;
return flatbush::detail::computeDistanceSquared(iPoint, iBox);
});
EXPECT_EQ(wIds.size(), 3UL);
EXPECT_GT(wCalls, 0UL);
// Pruning means the traversal stops well short of measuring every node
EXPECT_LT(wCalls, wIndex.indexSize());
}
TEST(FlatbushTest, NeighborsWithHaversineDistance) {
const auto wHaversineMeters = [](const flatbush::Point<double>& iPoint, const flatbush::Box<double>& iBox) {
if (iBox.mMinX != iBox.mMaxX || iBox.mMinY != iBox.mMaxY) {
return 0.0;
}
const auto wRadiansPerDegree = std::acos(-1.0) / 180.0;
const auto wEarthRadiusMeters = 6378160.187;
const auto wLatitude = iPoint.mY * wRadiansPerDegree;
const auto wOtherLatitude = iBox.mMinY * wRadiansPerDegree;
const auto wSinHalfLatitude = std::sin((wOtherLatitude - wLatitude) * 0.5);
const auto wSinHalfLongitude = std::sin((iBox.mMinX - iPoint.mX) * wRadiansPerDegree * 0.5);
const auto wHaversine = wSinHalfLatitude * wSinHalfLatitude +
std::cos(wLatitude) * std::cos(wOtherLatitude) * wSinHalfLongitude * wSinHalfLongitude;
return 2.0 * wEarthRadiusMeters * std::asin(std::sqrt(std::min(1.0, std::max(0.0, wHaversine))));
};
flatbush::FlatbushBuilder<double> wBuilder(3, 2);
wBuilder.add(flatbush::Point<double> { -179.5, 80.0 });
wBuilder.add(flatbush::Point<double> { -177.5, 80.0 });
wBuilder.add(flatbush::Point<double> { 170.0, 80.0 });
const auto wIndex = wBuilder.finish();
const flatbush::Point<double> wQuery { 179.5, 80.0 };
EXPECT_EQ(wIndex.neighbors(wQuery, 1, flatbush::gMaxDistance), (std::vector<size_t> { 2UL }));
EXPECT_EQ(wIndex.neighbors(wQuery, 1, flatbush::gMaxDistance, {}, wHaversineMeters), (std::vector<size_t> { 0UL }));
EXPECT_EQ(wIndex.neighbors(wQuery, 3, flatbush::gMaxDistance, {}, wHaversineMeters),
(std::vector<size_t> { 0UL, 1UL, 2UL }));
EXPECT_EQ(wIndex.neighbors(wQuery, 3, 40000.0, {}, wHaversineMeters), (std::vector<size_t> { 0UL }));
}
TEST(FlatbushTest, QueryCallbackDefaults) {
flatbush::FlatbushBuilder<double> wBuilder(3, 2);
wBuilder.add(flatbush::Point<double> { 3.0, 4.0 });
wBuilder.add(flatbush::Point<double> { 6.0, 8.0 });
wBuilder.add(flatbush::Point<double> { 1.0, 1.0 });
const auto wIndex = wBuilder.finish();
const auto& wFilter = flatbush::detail::acceptAllFilter<double>;
const auto wSearchIds = wIndex.search(wIndex.bounds());
ASSERT_EQ(wSearchIds.size(), wIndex.numItems());
EXPECT_EQ(wIndex.search(wIndex.bounds(), wFilter, 1), (std::vector<size_t> { wSearchIds.front() }));
EXPECT_TRUE(wIndex.search(wIndex.bounds(), wFilter, 0).empty());
const flatbush::Point<double> wPoint { 0.0, 0.0 };
const auto wFarFilter = +[](size_t iId, const flatbush::Box<double>&) noexcept {
return iId == 1UL;
};
const auto wLinearDistance = [](const flatbush::Point<double>& iPoint, const flatbush::Box<double>& iBox) noexcept {
return std::sqrt(flatbush::detail::computeDistanceSquared(iPoint, iBox));
};
const std::vector<size_t> wWithinRadius { 2UL, 0UL };
EXPECT_EQ(wIndex.neighbors(wPoint, 3, 6), wWithinRadius);
EXPECT_EQ(wIndex.neighbors(wPoint, 3, 6, wFilter, wLinearDistance), wWithinRadius);
EXPECT_EQ(wIndex.neighbors(wPoint, 1, flatbush::gMaxDistance, wFarFilter), (std::vector<size_t> { 1UL }));
}
TEST(FlatbushTest, QueryCallbacksCanBeMoveOnly) {
auto wIndex = createIndex();
const auto wSearchIds = wIndex.search({ 0.0, 0.0, 100.0, 100.0 }, MoveOnlyFilter {});
const auto wNeighborIds = wIndex.neighbors({ 50.0, 50.0 },
6,
flatbush::gMaxDistance,
MoveOnlyFilter {},
MoveOnlyDistance {});
EXPECT_EQ(wSearchIds.size(), wIndex.numItems() / 2UL);
for (const auto wId : wSearchIds) {
EXPECT_EQ(wId % 2UL, 0UL);
}
std::vector<size_t> wExpected { 6, 16, 18, 24, 54, 80 };
auto wSortedNeighborIds = wNeighborIds;
std::sort(wExpected.begin(), wExpected.end());
std::sort(wSortedNeighborIds.begin(), wSortedNeighborIds.end());
EXPECT_EQ(wSortedNeighborIds, wExpected);
}
TEST(FlatbushTest, SearchFilterExceptionsPropagate) {
auto wIndex = createIndex();
EXPECT_THAT(
[&] {
static_cast<void>(wIndex.search({ 0.0, 0.0, 100.0, 100.0 }, [](size_t, const flatbush::Box<double>&) -> bool {
throw std::runtime_error("filter failure");
}));
},
ThrowsMessage<std::runtime_error>("filter failure"));
EXPECT_EQ(wIndex.search({ 0.0, 0.0, 100.0, 100.0 }).size(), wIndex.numItems());
}
TEST(FlatbushTest, NeighborsFilterExceptionsPropagate) {
auto wIndex = createIndex();
EXPECT_THAT(
[&] {
static_cast<void>(wIndex.neighbors({ 50.0, 50.0 },
3,
flatbush::gMaxDistance,
[](size_t, const flatbush::Box<double>&) -> bool {
throw std::runtime_error("filter failure");
}));
},
ThrowsMessage<std::runtime_error>("filter failure"));
EXPECT_EQ(wIndex.neighbors({ 50.0, 50.0 }, 3).size(), 3UL);
}
TEST(FlatbushTest, NeighborsDistanceExceptionsPropagate) {
auto wIndex = createIndex();
size_t wCalls = 0UL;
EXPECT_THAT(
[&] {
static_cast<void>(wIndex.neighbors({ 50.0, 50.0 },
3,
flatbush::gMaxDistance,
flatbush::detail::acceptAllFilter<double>,
[&wCalls](const flatbush::Point<double>& iPoint,
const flatbush::Box<double>& iBox) -> double {
if (++wCalls > 1UL) throw std::runtime_error("distance failure");
return flatbush::detail::computeDistanceSquared(iPoint, iBox);
}));
},
ThrowsMessage<std::runtime_error>("distance failure"));
EXPECT_GT(wCalls, 1UL);
EXPECT_EQ(wIndex.neighbors({ 50.0, 50.0 }, 3).size(), 3UL);
}
TEST(FlatbushTest, AddPointAsZeroAreaBox) {
flatbush::FlatbushBuilder<double> wBuilder(1);
const flatbush::Point<double> wPoint { 10.0, 20.0 };
EXPECT_EQ(wBuilder.add(wPoint), 0UL);
const auto wIndex = wBuilder.finish();
EXPECT_EQ(wIndex.search({ 10.0, 20.0, 10.0, 20.0 }), std::vector<size_t> { 0UL });
EXPECT_TRUE(wIndex.search({ 10.0, 21.0, 10.0, 21.0 }).empty());
}
TEST(FlatbushTest, FinishConsumesBuilderItems) {
flatbush::FlatbushBuilder<int32_t> wBuilder(1);
EXPECT_EQ(wBuilder.add({ 1, 1, 1, 1 }), 0UL);
const auto wFirst = wBuilder.finish();
EXPECT_EQ(wBuilder.add({ 2, 2, 2, 2 }), 0UL);
const auto wSecond = wBuilder.finish();
EXPECT_EQ(wFirst.numItems(), 1UL);
EXPECT_EQ(wSecond.numItems(), 1UL);
EXPECT_EQ(wFirst.search({ 1, 1, 1, 1 }), std::vector<size_t> { 0UL });
EXPECT_EQ(wSecond.search({ 2, 2, 2, 2 }), std::vector<size_t> { 0UL });
}
TEST(FlatbushTest, ClearDiscardsBuilderItems) {
flatbush::FlatbushBuilder<int32_t> wBuilder(2);
wBuilder.add({ 1, 1, 1, 1 });
wBuilder.clear();
EXPECT_EQ(wBuilder.add({ 2, 2, 2, 2 }), 0UL);
const auto wIndex = wBuilder.finish();
EXPECT_TRUE(wIndex.search({ 1, 1, 1, 1 }).empty());
EXPECT_EQ(wIndex.search({ 2, 2, 2, 2 }), std::vector<size_t> { 0UL });
}
TEST(FlatbushTest, SearchQueryFilterFunc) {
auto wIndex = createIndex();
auto wIds = wIndex.search({ 40, 40, 60, 60 }, [](size_t iValue, const flatbush::Box<double>&) {
return iValue % 2 == 0;
});
EXPECT_EQ(wIds, std::vector<size_t> { 6UL });
}
class FlatbushSearchContainedRangesTest : public ::testing::TestWithParam<std::tuple<size_t, bool>> {};
TEST_P(FlatbushSearchContainedRangesTest, PreserveOrderAndLimits) {
const auto wNumItems = std::get<0>(GetParam());
const auto wFullBounds = std::get<1>(GetParam());
flatbush::FlatbushBuilder<int32_t> wBuilder(wNumItems, 16);
for (size_t wId = 0UL; wId < wNumItems; ++wId) {
wBuilder.add(flatbush::Point<int32_t> { static_cast<int32_t>(wNumItems - wId), static_cast<int32_t>(wId % 31UL) });
}
const auto wIndex = wBuilder.finish();
const auto wQuery = wFullBounds ? wIndex.bounds()
: flatbush::Box<int32_t> {
1, 0, static_cast<int32_t>(std::max<size_t>(1UL, wNumItems / 2UL)), 31
};
ASSERT_EQ(wIndex.search(wIndex.bounds()).size(), wNumItems);
const auto wAllIds = wIndex.search(wQuery, flatbush::detail::acceptAllFilter<int32_t>);
EXPECT_EQ(wIndex.search(wQuery), wAllIds);
const std::array<size_t, 20> wLimits { 0UL, 1UL, 2UL, 7UL, 8UL,
9UL, 15UL, 16UL, 17UL, 18UL,
63UL, 64UL, 65UL, 100UL, 256UL,
32768UL, 32769UL, 65537UL, 65538UL, flatbush::gMaxResults };
for (const auto wLimit : wLimits) {
SCOPED_TRACE(wLimit);
auto wExpected = wAllIds;
wExpected.resize(std::min(wLimit, wExpected.size()));
EXPECT_EQ(wIndex.search(wQuery, {}, wLimit), wExpected);
size_t wCalls = 0UL;
const auto wFilter = [&wCalls](size_t, const flatbush::Box<int32_t>&) {
++wCalls;
return true;
};
EXPECT_EQ(wIndex.search(wQuery, wFilter, wLimit), wExpected);
EXPECT_EQ(wCalls, wExpected.size());
}
}
INSTANTIATE_TEST_SUITE_P(NumItemsAndQuery,
FlatbushSearchContainedRangesTest,
::testing::Combine(::testing::Values(1UL, 17UL, 65537UL), ::testing::Values(true, false)));
class FlatbushFilteredSearchTest : public Test {
protected:
const flatbush::Box<int32_t> mQuery { 1, 0, 3, 0 };
const flatbush::Flatbush<int32_t> mIndex = [] {
flatbush::FlatbushBuilder<int32_t> wBuilder(5);
for (int32_t wCoord = 0; wCoord < 5; ++wCoord) {
wBuilder.add(flatbush::Point<int32_t> { wCoord, 0 });
}
return wBuilder.finish();
}();
};
TEST_F(FlatbushFilteredSearchTest, MatchesQueryBounds) {
EXPECT_EQ(mIndex.search(mQuery), (std::vector<size_t> { 1UL, 2UL, 3UL }));
}
TEST_F(FlatbushFilteredSearchTest, LimitCountsAcceptedItems) {
size_t wCalls = 0UL;
const auto wFilter = [&wCalls](size_t iId, const flatbush::Box<int32_t>&) {
++wCalls;
return iId % 2UL == 0UL;
};
EXPECT_EQ(mIndex.search(mQuery, wFilter, 1), std::vector<size_t> { 2UL });
EXPECT_EQ(wCalls, 2UL);
}
TEST_F(FlatbushFilteredSearchTest, ContainedLimitCountsAcceptedItems) {
size_t wCalls = 0UL;
const auto wFilter = [&wCalls](size_t iId, const flatbush::Box<int32_t>&) {
++wCalls;
return iId % 2UL != 0UL;
};
EXPECT_EQ(mIndex.search(mIndex.bounds(), wFilter, 2), (std::vector<size_t> { 1UL, 3UL }));
EXPECT_EQ(wCalls, 4UL);
}
TEST_F(FlatbushFilteredSearchTest, RejectAllVisitsEveryMatch) {
size_t wCalls = 0UL;
const auto wFilter = [&wCalls](size_t, const flatbush::Box<int32_t>&) {
++wCalls;
return false;
};
EXPECT_TRUE(mIndex.search(mQuery, wFilter, 1).empty());
EXPECT_EQ(wCalls, 3UL);
}
TEST(FlatbushTest, ReconstructIndexFromJSArrayBuffer) {
auto wIndex = flatbush::FlatbushBuilder<double>::from(gFlatbush.data(), gFlatbush.size());
auto wIndexBuffer = wIndex.data();
ASSERT_EQ(wIndexBuffer.size(), gFlatbush.size());
EXPECT_TRUE(std::equal(wIndexBuffer.begin(), wIndexBuffer.end(), gFlatbush.begin()));
}
TEST(FlatbushTest, BuildMatchesJSArrayBuffer) {
const auto wIndex = createSmallIndex(14, 16);
auto wData = wIndex.data();
ASSERT_EQ(wData.size(), gFlatbush.size());
EXPECT_TRUE(std::equal(wData.begin(), wData.end(), gFlatbush.begin()));
}
TEST(FlatbushTest, ReconstructIndexFromJSUint8ClampedArray) {
flatbush::FlatbushBuilder<uint8_t> wBuilder(2);
wBuilder.add({ 10, 20, 10, 20 });
wBuilder.add({ 30, 40, 30, 40 });
const auto wIndex = wBuilder.finish();
auto wData = std::vector<uint8_t> { wIndex.data().begin(), wIndex.data().end() };
EXPECT_EQ(wData[1] & 0x0fU, 1U);
wData[1] = static_cast<uint8_t>((flatbush::gVersion << 4U) | 2U);
const auto wClampedIndex = flatbush::FlatbushBuilder<uint8_t>::from(wData.data(), wData.size());
EXPECT_EQ(wClampedIndex.search({ 0, 0, 255, 255 }), wIndex.search({ 0, 0, 255, 255 }));
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<int8_t>::from(wData.data(), wData.size()));
},
ThrowsMessage<std::invalid_argument>("Expected type is uint8_t, but got template type int8_t"));
}
TEST(FlatbushTest, FromNull) {
EXPECT_THAT(
[] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(nullptr, flatbush::gHeaderByteSize));
},
ThrowsMessage<std::invalid_argument>("Data is incomplete or missing."));
}
TEST(FlatbushTest, FromWrongMagic) {
auto wData = gFlatbush;
wData[0] = 0xf1;
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(wData.data(), wData.size()));
},
ThrowsMessage<std::invalid_argument>("Data does not appear to be in a Flatbush format."));
}
TEST(FlatbushTest, FromWrongVersion) {
auto wData = gFlatbush;
wData[1] ^= 0x10U;
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(wData.data(), wData.size()));
},
ThrowsMessage<std::invalid_argument>("Got v2 data when expected v3."));
}
TEST(FlatbushTest, FromWrongEncodedType) {
EXPECT_THAT(
[] {
static_cast<void>(flatbush::FlatbushBuilder<int>::from(gFlatbush.data(), gFlatbush.size()));
},
ThrowsMessage<std::invalid_argument>("Expected type is double, but got template type int32_t"));
}
TEST(FlatbushTest, FromInvalidHeaderSize) {
EXPECT_THAT(
[] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(gFlatbush.data(), flatbush::gHeaderByteSize - 1UL));
},
ThrowsMessage<std::invalid_argument>("Data buffer size must be at least 8 bytes."));
}
TEST(FlatbushTest, FromInvalidNodeSize) {
auto wData = gFlatbush;
std::memset(wData.data() + 2, 0, sizeof(uint16_t));
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(wData.data(), wData.size()));
},
ThrowsMessage<std::invalid_argument>("Node size cannot be < 2."));
}
TEST(FlatbushTest, FromInvalidNumItems) {
auto wData = gFlatbush;
const uint32_t wNumItems = 13U;
std::memcpy(wData.data() + 4, &wNumItems, sizeof(wNumItems));
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(wData.data(), wData.size()));
},
ThrowsMessage<std::invalid_argument>(HasSubstr("Num items dictates a total size of ")));
}
TEST(FlatbushTest, FromZeroNumItems) {
auto wData = gFlatbush;
std::memset(wData.data() + 4, 0, sizeof(uint32_t));
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(wData.data(), wData.size()));
},
ThrowsMessage<std::invalid_argument>("Num items cannot be 0."));
}
TEST(FlatbushTest, FromOversizedBuffer) {
auto wIndex = createIndex();
auto wVector = std::vector<uint8_t> { wIndex.data().begin(), wIndex.data().end() };
wVector.resize(wVector.size() + 512, 0xAB);
const auto wSizeMismatch = ThrowsMessage<std::invalid_argument>(HasSubstr("Num items dictates a total size of "));
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(wVector.data(), wVector.size()));
},
wSizeMismatch);
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<double>::from(std::move(wVector)));
},
wSizeMismatch);
}
TEST(FlatbushTest, FromInvalidInternalNodeIndex) {
static constexpr auto kNumItems = 5U;
flatbush::FlatbushBuilder<uint32_t> wBuilder(kNumItems, 2U);
for (uint32_t wIdx = 0U; wIdx < kNumItems; ++wIdx) {
wBuilder.add({ wIdx, wIdx, wIdx, wIdx });
}
auto wIndex = wBuilder.finish();
const auto wData = std::vector<uint8_t> { wIndex.data().begin(), wIndex.data().end() };
const auto wIndicesOffset = flatbush::gHeaderByteSize + wIndex.indexSize() * sizeof(flatbush::Box<uint32_t>);
for (auto wPosition = wIndex.numItems(); wPosition < wIndex.indexSize(); ++wPosition) {
auto wCorruptData = wData;
flatbush::NarrowIndexType wIndexValue;
std::memcpy(&wIndexValue,
wCorruptData.data() + wIndicesOffset + wPosition * sizeof(wIndexValue),
sizeof(wIndexValue));
wIndexValue = static_cast<flatbush::NarrowIndexType>(wIndexValue ^ 4U);
std::memcpy(wCorruptData.data() + wIndicesOffset + wPosition * sizeof(wIndexValue),
&wIndexValue,
sizeof(wIndexValue));
EXPECT_THAT(
[&] {
static_cast<void>(flatbush::FlatbushBuilder<uint32_t>::from(wCorruptData.data(), wCorruptData.size()));
},
ThrowsMessage<std::invalid_argument>("Data contains an invalid internal node index."));
}
}
TEST(FlatbushTest, FromInvalidWideInternalNodeIndex) {
static constexpr auto kNumItems = 20000U;
flatbush::FlatbushBuilder<uint32_t> wBuilder(kNumItems);
for (uint32_t wIdx = 0U; wIdx < kNumItems; ++wIdx) {
wBuilder.add({ wIdx, wIdx, wIdx, wIdx });
}
auto wIndex = wBuilder.finish();
auto wData = std::vector<uint8_t> { wIndex.data().begin(), wIndex.data().end() };
const auto wRootPosition = wIndex.indexSize() - 1UL;
const auto wIndicesOffset = flatbush::gHeaderByteSize + wIndex.indexSize() * sizeof(flatbush::Box<uint32_t>);