Skip to content

Commit 41d4f96

Browse files
committed
[UTest] Add tests for ArrayIndex
1 parent 54a525c commit 41d4f96

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

unittest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ set(
5151

5252
# storage
5353
storage/ColumnStoreTest.cpp
54+
storage/IndexTest.cpp
5455
storage/PaxStoreTest.cpp
5556
storage/RowStoreTest.cpp
5657
storage/StoreTest.cpp

unittest/storage/IndexTest.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "catch2/catch.hpp"
2+
3+
#include <mutable/catalog/Catalog.hpp>
4+
#include <mutable/mutable.hpp>
5+
#include <mutable/storage/Index.hpp>
6+
#include <mutable/util/concepts.hpp>
7+
#include <mutable/util/Diagnostic.hpp>
8+
#include "storage/PaxStore.hpp"
9+
10+
11+
using namespace m;
12+
using namespace m::idx;
13+
14+
15+
TEST_CASE("ArrayIndex::finalized()", "[core][storage][index]")
16+
{
17+
/* Create empty index. */
18+
ArrayIndex<int64_t> idx;
19+
REQUIRE_FALSE(idx.finalized());
20+
21+
/* Querying not allowerd, not finalized yet. */
22+
REQUIRE_THROWS(idx.lower_bound(13));
23+
REQUIRE_THROWS(idx.upper_bound(13));
24+
25+
/* Adding another key/value-pair should invalidate index. */
26+
idx.add(19, 4);
27+
REQUIRE_FALSE(idx.finalized());
28+
idx.finalize();
29+
REQUIRE(idx.finalized());
30+
31+
/* Index is finalized, should not throw exception. */
32+
REQUIRE_NOTHROW(idx.lower_bound(13));
33+
REQUIRE_NOTHROW(idx.upper_bound(13));
34+
}
35+
36+
TEMPLATE_TEST_CASE("ArrayIndex::add() with Numeric types", "[core][storage][index]",
37+
int8_t, int16_t, int32_t, int64_t, float, double)
38+
{
39+
/* Create empty index. */
40+
ArrayIndex<TestType> idx;
41+
42+
/* Add keys/value-pairs to index. */
43+
std::vector<TestType> keys = { 0, 42, 15 };
44+
std::size_t i = 0;
45+
for (auto key : keys)
46+
idx.add(key, i++);
47+
48+
/* Finalize index. */
49+
REQUIRE_FALSE(idx.finalized());
50+
idx.finalize();
51+
52+
/* Check contents of index. */
53+
i = 0;
54+
for (auto key : keys) {
55+
auto it = idx.lower_bound(key);
56+
REQUIRE(it->first == key);
57+
REQUIRE(it->second == i);
58+
i++;
59+
}
60+
61+
/* Check sortedness of iterator. */
62+
for (auto it = idx.begin() + 1; it != idx.end(); ++it)
63+
REQUIRE((it - 1)->first <= it->first);
64+
65+
}
66+
67+
TEMPLATE_TEST_CASE("ArrayIndex::bulkload() with Numeric types", "[core][storage][index]",
68+
int8_t, int16_t, int32_t, int64_t, float, double)
69+
{
70+
Catalog::Clear();
71+
Diagnostic diag(false, std::cout, std::cerr);
72+
73+
/* Create and use a DB. */
74+
Catalog &C = Catalog::Get();
75+
const char *db_name = "db";
76+
auto &DB = C.add_database(db_name);
77+
C.set_database_in_use(DB);
78+
auto &table = DB.add_table(C.pool("t"));
79+
80+
/* Create a table with a single attribute. */
81+
table.push_back(C.pool("val"), []() {
82+
if constexpr(integral<TestType>)
83+
return Type::Get_Integer(Type::TY_Vector, sizeof(TestType));
84+
else if constexpr(std::same_as<TestType, float>)
85+
return Type::Get_Float(Type::TY_Vector);
86+
else // double
87+
return Type::Get_Double(Type::TY_Vector);
88+
}());
89+
table.layout(C.data_layout());
90+
table.store(C.create_store(table));
91+
92+
/* Build and execute insert statement. */
93+
std::vector<TestType> keys = { 0, 42, 15 };
94+
std::ostringstream oss2;
95+
oss2 << "INSERT INTO t VALUES ";
96+
for (auto key : keys)
97+
oss2 << "(" << +key << "), ";
98+
oss2 << " (NULL);";
99+
100+
auto insert_stmt = statement_from_string(diag, oss2.str());
101+
execute_statement(diag, *insert_stmt);
102+
103+
/* Create empty index. */
104+
ArrayIndex<TestType> idx;
105+
106+
/* Bulkload index from table. */
107+
idx.bulkload(table, table.schema());
108+
REQUIRE(idx.finalized()); // bulkloading should automatically finalize index.
109+
110+
/* Check contents of index. */
111+
std::size_t i = 0;
112+
for (auto key : keys) {
113+
auto it = idx.lower_bound(key);
114+
REQUIRE(it->first == key);
115+
REQUIRE(it->second == i);
116+
i++;
117+
}
118+
119+
/* Index should not contain NULL. */
120+
REQUIRE(idx.num_entries() == keys.size());
121+
}

0 commit comments

Comments
 (0)