mirror of
https://github.com/typesense/typesense.git
synced 2025-05-23 15:23:40 +08:00
Add test cases.
This commit is contained in:
parent
d440e510c0
commit
855368e543
@ -25,7 +25,7 @@ void NumericTrie::search_range(const int32_t& low, const bool& low_inclusive,
|
||||
|
||||
uint32_t* negative_ids = nullptr;
|
||||
uint32_t negative_ids_length = 0;
|
||||
if (!(low == -1 && !low_inclusive)) {
|
||||
if (!(low == -1 && !low_inclusive)) { // No need to search for (-1, ...
|
||||
auto abs_low = std::abs(low);
|
||||
// Since we store absolute values, search_lesser would yield result for >low from negative_trie.
|
||||
negative_trie->search_lesser(low_inclusive ? abs_low : abs_low - 1, negative_ids, negative_ids_length);
|
||||
@ -33,7 +33,7 @@ void NumericTrie::search_range(const int32_t& low, const bool& low_inclusive,
|
||||
|
||||
uint32_t* positive_ids = nullptr;
|
||||
uint32_t positive_ids_length = 0;
|
||||
if (!(high == 0 && !high_inclusive)) {
|
||||
if (!(high == 0 && !high_inclusive)) { // No need to search for ..., 0)
|
||||
positive_trie->search_lesser(high_inclusive ? high : high - 1, positive_ids, positive_ids_length);
|
||||
}
|
||||
|
||||
@ -94,24 +94,18 @@ void NumericTrieNode::search_lesser(const int32_t& value, uint32_t*& ids, uint32
|
||||
}
|
||||
|
||||
void NumericTrieNode::search_lesser_helper(const int32_t& value, char& level, std::vector<NumericTrieNode*>& matches) {
|
||||
if (level > MAX_LEVEL) {
|
||||
return;
|
||||
} else if (level == MAX_LEVEL) {
|
||||
if (level == MAX_LEVEL) {
|
||||
matches.push_back(this);
|
||||
return;
|
||||
}
|
||||
|
||||
if (children == nullptr) {
|
||||
} else if (level > MAX_LEVEL || children == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto index = get_index(value, ++level);
|
||||
if (children[index] == nullptr) {
|
||||
return;
|
||||
if (children[index] != nullptr) {
|
||||
children[index]->search_lesser_helper(value, level, matches);
|
||||
}
|
||||
|
||||
children[index]->search_lesser_helper(value, level, matches);
|
||||
|
||||
while (--index >= 0) {
|
||||
if (children[index] != nullptr) {
|
||||
matches.push_back(children[index]);
|
||||
|
@ -9,8 +9,9 @@ protected:
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
TEST_F(NumericRangeTrieTest, Insert) {
|
||||
TEST_F(NumericRangeTrieTest, SearchRange) {
|
||||
auto trie = new NumericTrie();
|
||||
std::unique_ptr<NumericTrie> trie_guard(trie);
|
||||
std::vector<std::pair<int32_t, uint32_t>> pairs = {
|
||||
{-8192, 8},
|
||||
{-16384, 32},
|
||||
@ -30,12 +31,81 @@ TEST_F(NumericRangeTrieTest, Insert) {
|
||||
uint32_t ids_length = 0;
|
||||
|
||||
trie->search_range(-32768, true, 32768, true, ids, ids_length);
|
||||
std::unique_ptr<uint32_t[]> ids_guard(ids);
|
||||
|
||||
ASSERT_EQ(pairs.size(), ids_length);
|
||||
for (uint32_t i = 0; i < pairs.size(); i++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[i]);
|
||||
}
|
||||
|
||||
delete [] ids;
|
||||
delete trie;
|
||||
}
|
||||
trie->search_range(-32768, true, 32768, false, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(pairs.size() - 1, ids_length);
|
||||
for (uint32_t i = 0; i < pairs.size() - 1; i++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[i]);
|
||||
}
|
||||
|
||||
trie->search_range(-32768, true, 134217728, true, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(pairs.size(), ids_length);
|
||||
for (uint32_t i = 0; i < pairs.size(); i++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[i]);
|
||||
}
|
||||
|
||||
trie->search_range(-32768, true, 0, true, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(4, ids_length);
|
||||
for (uint32_t i = 0; i < 4; i++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[i]);
|
||||
}
|
||||
|
||||
trie->search_range(-32768, true, 0, false, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(4, ids_length);
|
||||
for (uint32_t i = 0; i < 4; i++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[i]);
|
||||
}
|
||||
|
||||
trie->search_range(-32768, false, 32768, true, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(pairs.size() - 1, ids_length);
|
||||
for (uint32_t i = 0, j = 0; i < pairs.size(); i++) {
|
||||
if (i == 3) continue; // id for -32768 would not be present
|
||||
ASSERT_EQ(pairs[i].second, ids[j++]);
|
||||
}
|
||||
|
||||
trie->search_range(-134217728, true, 32768, true, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(pairs.size(), ids_length);
|
||||
for (uint32_t i = 0; i < pairs.size(); i++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[i]);
|
||||
}
|
||||
|
||||
trie->search_range(-1, true, 32768, true, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(4, ids_length);
|
||||
for (uint32_t i = 4, j = 0; i < pairs.size(); i++, j++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[j]);
|
||||
}
|
||||
|
||||
trie->search_range(-1, false, 32768, true, ids, ids_length);
|
||||
ids_guard.reset(ids);
|
||||
|
||||
ASSERT_EQ(4, ids_length);
|
||||
for (uint32_t i = 4, j = 0; i < pairs.size(); i++, j++) {
|
||||
ASSERT_EQ(pairs[i].second, ids[j]);
|
||||
}
|
||||
|
||||
trie->search_range(-1, true, 0, true, ids, ids_length);
|
||||
ASSERT_EQ(0, ids_length);
|
||||
|
||||
trie->search_range(-1, false, 0, false, ids, ids_length);
|
||||
ASSERT_EQ(0, ids_length);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user