-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathstruct.cpp
More file actions
79 lines (63 loc) · 2.82 KB
/
struct.cpp
File metadata and controls
79 lines (63 loc) · 2.82 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
#include <catch2/catch_test_macros.hpp>
#include <vortex.h>
using namespace std::string_view_literals;
using namespace std::string_literals;
TEST_CASE("Struct builder", "[struct]") {
vx_struct_fields_builder *builder = vx_struct_fields_builder_new();
constexpr auto col1 = "col1"sv;
const vx_string *col1_name = vx_string_new(col1.data(), col1.size());
const vx_dtype *col1_dtype = vx_dtype_new_primitive(PTYPE_U8, false);
vx_struct_fields_builder_add_field(builder, col1_name, col1_dtype);
constexpr auto col2 = "col2"sv;
const vx_string *col2_name = vx_string_new(col2.data(), col2.size());
const vx_dtype *col2_dtype = vx_dtype_new_binary(true);
vx_struct_fields_builder_add_field(builder, col2_name, col2_dtype);
SECTION("Struct builder free") {
vx_struct_fields_builder_free(builder);
}
SECTION("Struct builder finalize") {
vx_struct_fields *fields = vx_struct_fields_builder_finalize(builder);
SECTION("struct fields free") {
vx_struct_fields_free(fields);
}
SECTION("struct fields finalize") {
const vx_dtype *dtype = vx_dtype_new_struct(fields, false);
vx_dtype_free(dtype);
}
}
}
constexpr size_t STRUCT_LEN = 10;
TEST_CASE("Creating structs", "[struct]") {
vx_struct_fields_builder *builder = vx_struct_fields_builder_new();
REQUIRE(builder != nullptr);
for (size_t i = 0; i < STRUCT_LEN; ++i) {
const std::string target_name = "name"s + std::to_string(i);
const vx_string *name = vx_string_new(target_name.data(), target_name.size());
const vx_dtype *dtype = i % 2 ? vx_dtype_new_binary(false) : vx_dtype_new_primitive(PTYPE_F32, true);
vx_struct_fields_builder_add_field(builder, name, dtype);
}
vx_struct_fields *fields = vx_struct_fields_builder_finalize(builder);
REQUIRE(fields != nullptr);
const size_t len = vx_struct_fields_nfields(fields);
CHECK(len == STRUCT_LEN);
for (size_t i = 0; i < len; ++i) {
// borrowed
const vx_string *name = vx_struct_fields_field_name(fields, i);
// owned TODO(myrrc): that's weird API
const vx_dtype *dtype = vx_struct_fields_field_dtype(fields, i);
std::string_view name_view {vx_string_ptr(name), vx_string_len(name)};
std::string target_name = "name"s + std::to_string(i);
CHECK(name_view == target_name);
if (i % 2) {
CHECK_FALSE(vx_dtype_is_nullable(dtype));
CHECK(vx_dtype_get_variant(dtype) == DTYPE_BINARY);
} else {
CHECK(vx_dtype_is_nullable(dtype));
CHECK(vx_dtype_get_variant(dtype) == DTYPE_PRIMITIVE);
}
vx_dtype_free(dtype);
}
vx_struct_fields_free(fields);
}