@@ -22,35 +22,14 @@ using namespace std;
22
22
23
23
namespace dfly {
24
24
25
- class BPTreeSetTest : public ::testing::Test {
26
- using Node = detail::BPTreeNode<uint64_t >;
27
-
28
- protected:
29
- static constexpr size_t kNumElems = 7000 ;
30
-
31
- BPTreeSetTest () : mi_alloc_(mi_heap_get_backing()), bptree_(&mi_alloc_) {
32
- }
33
- static void SetUpTestSuite () {
34
- }
25
+ namespace {
35
26
36
- void FillTree (unsigned factor = 1 ) {
37
- for (unsigned i = 0 ; i < kNumElems ; ++i) {
38
- bptree_.Insert (i * factor);
39
- }
40
- }
41
-
42
- bool Validate ();
43
-
44
- static bool Validate (const Node* node, uint64_t ubound);
45
-
46
- MiMemoryResource mi_alloc_;
47
- BPTree<uint64_t > bptree_;
48
- mt19937 generator_{1 };
49
- };
27
+ template <typename Node, typename Policy>
28
+ bool ValidateNode (const Node* node, typename Node::KeyT ubound) {
29
+ typename Policy::KeyCompareTo cmp;
50
30
51
- bool BPTreeSetTest::Validate (const Node* node, uint64_t ubound) {
52
31
for (unsigned i = 1 ; i < node->NumItems (); ++i) {
53
- if (node->Key (i - 1 ) >= node->Key (i))
32
+ if (cmp ( node->Key (i - 1 ), node->Key (i)) > - 1 )
54
33
return false ;
55
34
}
56
35
@@ -71,25 +50,72 @@ bool BPTreeSetTest::Validate(const Node* node, uint64_t ubound) {
71
50
}
72
51
}
73
52
74
- return node->Key (node->NumItems () - 1 ) < ubound;
53
+ return cmp ( node->Key (node->NumItems () - 1 ), ubound) == - 1 ;
75
54
}
76
55
56
+ struct ZsetPolicy {
57
+ struct KeyT {
58
+ double d;
59
+ sds s;
60
+ };
61
+
62
+ struct KeyCompareTo {
63
+ int operator ()(const KeyT& left, const KeyT& right) {
64
+ if (left.d < right.d )
65
+ return -1 ;
66
+ if (left.d > right.d )
67
+ return 1 ;
68
+
69
+ // Note that sdscmp can return values outside of [-1, 1] range.
70
+ return sdscmp (left.s , right.s );
71
+ }
72
+ };
73
+ };
74
+
75
+ using SDSTree = BPTree<ZsetPolicy::KeyT, ZsetPolicy>;
76
+
77
+ } // namespace
78
+
79
+ class BPTreeSetTest : public ::testing::Test {
80
+ using Node = detail::BPTreeNode<uint64_t >;
81
+
82
+ protected:
83
+ static constexpr size_t kNumElems = 7000 ;
84
+
85
+ BPTreeSetTest () : mi_alloc_(mi_heap_get_backing()), bptree_(&mi_alloc_) {
86
+ }
87
+ static void SetUpTestSuite () {
88
+ }
89
+
90
+ void FillTree (unsigned factor = 1 ) {
91
+ for (unsigned i = 0 ; i < kNumElems ; ++i) {
92
+ bptree_.Insert (i * factor);
93
+ }
94
+ }
95
+
96
+ bool Validate ();
97
+
98
+ MiMemoryResource mi_alloc_;
99
+ BPTree<uint64_t > bptree_;
100
+ mt19937 generator_{1 };
101
+ };
102
+
77
103
bool BPTreeSetTest::Validate () {
78
104
auto * root = bptree_.DEBUG_root ();
79
105
if (!root)
80
106
return true ;
81
107
82
108
// node, upper bound
83
- std:: vector<pair<Node*, uint64_t >> stack;
109
+ vector<pair<const Node*, uint64_t >> stack;
84
110
85
111
stack.emplace_back (root, UINT64_MAX);
86
112
87
113
while (!stack.empty ()) {
88
- Node* node = stack.back ().first ;
114
+ const Node* node = stack.back ().first ;
89
115
uint64_t ubound = stack.back ().second ;
90
116
stack.pop_back ();
91
117
92
- if (!Validate (node, ubound))
118
+ if (!ValidateNode<Node, BPTreePolicy< uint64_t >> (node, ubound))
93
119
return false ;
94
120
95
121
if (!node->IsLeaf ()) {
@@ -353,25 +379,24 @@ TEST_F(BPTreeSetTest, MemoryUsage) {
353
379
LOG (INFO) << " btree after: " << mi_alloc.used () << " bytes" ;
354
380
}
355
381
356
- struct ZsetPolicy {
357
- struct KeyT {
358
- double d;
359
- sds s;
360
- };
382
+ TEST_F (BPTreeSetTest, InsertSDS) {
383
+ vector<ZsetPolicy::KeyT> vals;
384
+ for (unsigned i = 0 ; i < 256 ; ++i) {
385
+ sds s = sdsempty ();
361
386
362
- struct KeyCompareTo {
363
- int operator ()(const KeyT& left, const KeyT& right) {
364
- if (left.d < right.d )
365
- return -1 ;
366
- if (left.d > right.d )
367
- return 1 ;
387
+ s = sdscatfmt (s, " a%u" , i);
388
+ vals.emplace_back (ZsetPolicy::KeyT{.d = 1000 , .s = s});
389
+ }
368
390
369
- return sdscmp (left. s , right. s );
370
- }
371
- } ;
372
- };
391
+ SDSTree tree (&mi_alloc_ );
392
+ for ( size_t i = 0 ; i < vals. size (); ++i) {
393
+ ASSERT_TRUE (tree. Insert (vals[i])) ;
394
+ }
373
395
374
- using SDSTree = BPTree<ZsetPolicy::KeyT, ZsetPolicy>;
396
+ for (auto v : vals) {
397
+ sdsfree (v.s );
398
+ }
399
+ }
375
400
376
401
static string RandomString (mt19937& rand, unsigned len) {
377
402
const string_view alpanum = " 1234567890abcdefghijklmnopqrstuvwxyz" ;
0 commit comments