|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | +#include <catch2/catch_test_macros.hpp> |
| 4 | +#include <vortex.h> |
| 5 | + |
| 6 | +using namespace std::string_view_literals; |
| 7 | +using namespace std::string_literals; |
| 8 | + |
| 9 | +TEST_CASE("Struct builder", "[struct]") { |
| 10 | + vx_struct_fields_builder *builder = vx_struct_fields_builder_new(); |
| 11 | + |
| 12 | + constexpr auto col1 = "col1"sv; |
| 13 | + const vx_string *col1_name = vx_string_new(col1.data(), col1.size()); |
| 14 | + const vx_dtype *col1_dtype = vx_dtype_new_primitive(PTYPE_U8, false); |
| 15 | + vx_struct_fields_builder_add_field(builder, col1_name, col1_dtype); |
| 16 | + |
| 17 | + constexpr auto col2 = "col2"sv; |
| 18 | + const vx_string *col2_name = vx_string_new(col2.data(), col2.size()); |
| 19 | + const vx_dtype *col2_dtype = vx_dtype_new_binary(true); |
| 20 | + vx_struct_fields_builder_add_field(builder, col2_name, col2_dtype); |
| 21 | + |
| 22 | + SECTION("Struct builder free") { |
| 23 | + vx_struct_fields_builder_free(builder); |
| 24 | + } |
| 25 | + |
| 26 | + SECTION("Struct builder finalize") { |
| 27 | + vx_struct_fields *fields = vx_struct_fields_builder_finalize(builder); |
| 28 | + |
| 29 | + SECTION("struct fields free") { |
| 30 | + vx_struct_fields_free(fields); |
| 31 | + } |
| 32 | + |
| 33 | + SECTION("struct fields finalize") { |
| 34 | + const vx_dtype *dtype = vx_dtype_new_struct(fields, false); |
| 35 | + vx_dtype_free(dtype); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +constexpr size_t STRUCT_LEN = 10; |
| 41 | +TEST_CASE("Creating structs", "[struct]") { |
| 42 | + vx_struct_fields_builder *builder = vx_struct_fields_builder_new(); |
| 43 | + REQUIRE(builder != nullptr); |
| 44 | + |
| 45 | + for (size_t i = 0; i < STRUCT_LEN; ++i) { |
| 46 | + const std::string target_name = "name"s + std::to_string(i); |
| 47 | + const vx_string *name = vx_string_new(target_name.data(), target_name.size()); |
| 48 | + const vx_dtype *dtype = i % 2 ? vx_dtype_new_binary(false) : vx_dtype_new_primitive(PTYPE_F32, true); |
| 49 | + vx_struct_fields_builder_add_field(builder, name, dtype); |
| 50 | + } |
| 51 | + vx_struct_fields *fields = vx_struct_fields_builder_finalize(builder); |
| 52 | + REQUIRE(fields != nullptr); |
| 53 | + |
| 54 | + const size_t len = vx_struct_fields_nfields(fields); |
| 55 | + CHECK(len == STRUCT_LEN); |
| 56 | + for (size_t i = 0; i < len; ++i) { |
| 57 | + // borrowed |
| 58 | + const vx_string *name = vx_struct_fields_field_name(fields, i); |
| 59 | + // owned TODO(myrrc): that's weird API |
| 60 | + const vx_dtype *dtype = vx_struct_fields_field_dtype(fields, i); |
| 61 | + |
| 62 | + std::string_view name_view {vx_string_ptr(name), vx_string_len(name)}; |
| 63 | + std::string target_name = "name"s + std::to_string(i); |
| 64 | + |
| 65 | + CHECK(name_view == target_name); |
| 66 | + |
| 67 | + if (i % 2) { |
| 68 | + CHECK_FALSE(vx_dtype_is_nullable(dtype)); |
| 69 | + CHECK(vx_dtype_get_variant(dtype) == DTYPE_BINARY); |
| 70 | + } else { |
| 71 | + CHECK(vx_dtype_is_nullable(dtype)); |
| 72 | + CHECK(vx_dtype_get_variant(dtype) == DTYPE_PRIMITIVE); |
| 73 | + } |
| 74 | + |
| 75 | + vx_dtype_free(dtype); |
| 76 | + } |
| 77 | + |
| 78 | + vx_struct_fields_free(fields); |
| 79 | +} |
0 commit comments