-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_api.cpp
More file actions
431 lines (367 loc) · 12.3 KB
/
test_api.cpp
File metadata and controls
431 lines (367 loc) · 12.3 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
#include <vector>
#include <filesystem>
#include <gtest/gtest.h>
#include <fmt/format.h>
#include "ustore/ustore.hpp"
using namespace unum::ustore;
using namespace unum;
static char const* path() {
char* path = std::getenv("USTORE_TEST_PATH");
if (path)
return std::strlen(path) ? path : nullptr;
#if defined(USTORE_FLIGHT_CLIENT)
return nullptr;
#elif defined(USTORE_TEST_PATH)
return USTORE_TEST_PATH;
#else
return nullptr;
#endif
}
static std::string config() {
auto dir = path();
if (!dir)
return {};
return fmt::format(R"({{"version": "1.0", "directory": "{}"}})", dir);
}
TEST(db, validation) {
database_t db;
EXPECT_TRUE(db.open(config().c_str()));
blobs_collection_t collection = db.main();
blobs_collection_t named_collection = *db.find_or_create("col");
transaction_t txn = *db.transact();
std::vector<ustore_key_t> keys {34, 35, 36};
std::vector<std::uint64_t> vals {34, 35, 36};
ustore_length_t val_len = sizeof(std::uint64_t);
std::vector<ustore_length_t> offs {0, val_len, val_len * 2};
auto vals_begin = reinterpret_cast<ustore_bytes_ptr_t>(vals.data());
constexpr ustore_length_t count = 3;
contents_arg_t values {
.offsets_begin = {offs.data(), sizeof(ustore_length_t)},
.lengths_begin = {&val_len, 0},
.contents_begin = {&vals_begin, 0},
.count = count,
};
using value_extractor_t = contents_arg_extractor_gt<std::remove_reference_t<contents_arg_t>>;
auto contents = value_extractor_t {}.contents(values);
auto offsets = value_extractor_t {}.offsets(values);
auto lengths = value_extractor_t {}.lengths(values);
status_t status;
std::vector<ustore_options_t> options {ustore_options_default_k, ustore_option_write_flush_k};
ustore_write_t write_options {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = collection.member_ptr(),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
for (auto& option : options) {
write_options.options = option;
ustore_write(&write_options);
EXPECT_TRUE(status);
}
if (!db.supports_named_collections()) {
ustore_collection_t collections[count] = {1, 2, 3};
ustore_write_t write {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = &collections[0],
.collections_stride = sizeof(ustore_collection_t),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
ustore_write(&write);
EXPECT_FALSE(status);
status.release_error();
ustore_collection_t collections_only_default[count] = {0, 0, 0};
ustore_write_t write_rel {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = &collections_only_default[0],
.collections_stride = sizeof(ustore_collection_t),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
ustore_write(&write_rel);
EXPECT_TRUE(status);
}
ustore_collection_t* null_collection = nullptr;
ustore_write_t write_null_coll {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = null_collection,
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
ustore_write(&write_null_coll);
EXPECT_TRUE(status);
ustore_write_t write_named {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = named_collection.member_ptr(),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
// Named Collection
ustore_write(&write_named);
if (db.supports_named_collections())
EXPECT_TRUE(status);
else {
EXPECT_FALSE(status);
status.release_error();
}
ustore_write_t write {
.db = db,
.error = status.member_ptr(),
.transaction = txn,
.arena = collection.member_arena(),
.tasks_count = count,
.collections = collection.member_ptr(),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
ustore_write(&write);
if (db.supports_transactions())
EXPECT_TRUE(status);
else {
EXPECT_FALSE(status);
status.release_error();
}
// Transaction With Flush
write.options = ustore_option_write_flush_k;
ustore_write(&write);
EXPECT_FALSE(status);
status.release_error();
// Count = 0, Keys!= nullptr
write.transaction = nullptr;
write.tasks_count = 0;
write.collections_stride = 0;
write.options = ustore_options_default_k;
ustore_write(&write);
EXPECT_FALSE(status);
status.release_error();
// Count > 0; Keys == nullptr
ustore_write_t write_null_keys {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = collection.member_ptr(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
ustore_write(&write_null_keys);
EXPECT_FALSE(status);
status.release_error();
// Wrong Write Options
std::vector<ustore_options_t> wrong_write_options {
ustore_option_transaction_dont_watch_k,
};
ustore_write_t write_wrong_options {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = collection.member_ptr(),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = offsets.get(),
.offsets_stride = offsets.stride(),
.lengths = lengths.get(),
.lengths_stride = lengths.stride(),
.values = contents.get(),
.values_stride = contents.stride(),
};
for (auto& option : wrong_write_options) {
write_wrong_options.options = option;
ustore_write(&write_wrong_options);
EXPECT_FALSE(status);
status.release_error();
}
ustore_length_t* found_offsets = nullptr;
ustore_length_t* found_lengths = nullptr;
ustore_bytes_ptr_t found_values = nullptr;
ustore_read_t read_no_txn {
.db = db,
.error = status.member_ptr(),
.arena = collection.member_arena(),
.tasks_count = count,
.collections = collection.member_ptr(),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = &found_offsets,
.lengths = &found_lengths,
.values = &found_values,
};
ustore_read(&read_no_txn);
EXPECT_TRUE(status);
ustore_read_t read {
.db = db,
.error = status.member_ptr(),
.transaction = txn,
.arena = collection.member_arena(),
.options = ustore_option_transaction_dont_watch_k,
.tasks_count = count,
.collections = collection.member_ptr(),
.keys = keys.data(),
.keys_stride = sizeof(ustore_key_t),
.offsets = &found_offsets,
.lengths = &found_lengths,
.values = &found_values,
};
ustore_read(&read);
EXPECT_TRUE(status);
// Wrong Read Options
std::vector<ustore_options_t> wrong_read_options {
ustore_option_write_flush_k,
ustore_option_transaction_dont_watch_k,
};
for (auto& option : wrong_read_options) {
read_no_txn.options = option;
ustore_read(&read_no_txn);
EXPECT_FALSE(status);
status.release_error();
}
// Transaction
ustore_transaction_t ustore_txn = nullptr;
ustore_transaction_init_t txn_init {
.db = db,
.error = status.member_ptr(),
.transaction = &ustore_txn,
};
ustore_transaction_init(&txn_init);
EXPECT_TRUE(status);
txn_init.transaction = nullptr;
ustore_transaction_init(&txn_init);
EXPECT_FALSE(status);
status.release_error();
// Wrong Transaction Begin Options
std::vector<ustore_options_t> wrong_txn_begin_options {
ustore_option_write_flush_k,
ustore_option_dont_discard_memory_k,
};
txn_init.transaction = &ustore_txn;
for (auto& option : wrong_txn_begin_options) {
txn_init.options = option;
ustore_transaction_init(&txn_init);
EXPECT_FALSE(status);
status.release_error();
}
// Wrong Transaction Commit Options
std::vector<ustore_options_t> wrong_txn_commit_options {
ustore_option_dont_discard_memory_k,
};
ustore_transaction_commit_t txn_commit {
.db = db,
.error = status.member_ptr(),
.transaction = txn,
};
for (auto& option : wrong_txn_commit_options) {
txn_commit.options = option;
ustore_transaction_commit(&txn_commit);
EXPECT_FALSE(status);
status.release_error();
}
// Scans
ustore_key_t* found_keys = nullptr;
ustore_length_t* found_counts = nullptr;
ustore_scan_t scan {
.db = db,
.error = status.member_ptr(),
.transaction = txn,
.arena = collection.member_arena(),
.collections = collection.member_ptr(),
.start_keys = keys.data(),
.count_limits = &count,
.offsets = &found_offsets,
.counts = &found_counts,
.keys = &found_keys,
};
ustore_scan(&scan);
EXPECT_TRUE(status);
// Count > 0, Keys = nullptr
ustore_scan_t scan_no_keys {
.db = db,
.error = status.member_ptr(),
.transaction = txn,
.arena = collection.member_arena(),
.collections = collection.member_ptr(),
.count_limits = &count,
.offsets = &found_offsets,
.counts = &found_counts,
.keys = &found_keys,
};
ustore_scan(&scan_no_keys);
EXPECT_FALSE(status);
status.release_error();
// Limits == nullptr
ustore_scan_t scan_no_limits {
.db = db,
.error = status.member_ptr(),
.transaction = txn,
.arena = collection.member_arena(),
.collections = collection.member_ptr(),
.start_keys = keys.data(),
.offsets = &found_offsets,
.counts = &found_counts,
.keys = &found_keys,
};
ustore_scan(&scan_no_limits);
EXPECT_FALSE(status);
status.release_error();
}
int main(int argc, char** argv) {
std::filesystem::create_directory(path());
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}