|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | +#include <catch2/catch_test_macros.hpp> |
| 4 | + |
| 5 | +#include "vortex.h" |
| 6 | + |
| 7 | +using namespace std::string_literals; |
| 8 | +using namespace std::string_view_literals; |
| 9 | + |
| 10 | +TEST_CASE("Session creation", "[session]") { |
| 11 | + vx_session *session = vx_session_new(); |
| 12 | + REQUIRE(session != nullptr); |
| 13 | + vx_session *session2 = vx_session_clone(session); |
| 14 | + REQUIRE(session2 != nullptr); |
| 15 | + REQUIRE(session != session2); |
| 16 | + vx_session_free(session); |
| 17 | + vx_session_free(session2); |
| 18 | +} |
| 19 | + |
| 20 | +TEST_CASE("Creating and iterating binaries", "[binary]") { |
| 21 | + for (std::string_view str : {"ololo"sv, "Широкая строка"sv, "مرحبا بالعالم"sv}) { |
| 22 | + const vx_binary *binary = vx_binary_new(str.data(), str.size()); |
| 23 | + |
| 24 | + REQUIRE(binary != nullptr); |
| 25 | + const size_t len = vx_binary_len(binary); |
| 26 | + REQUIRE(len == str.size()); |
| 27 | + |
| 28 | + const char *ptr = vx_binary_ptr(binary); |
| 29 | + REQUIRE(std::string_view {ptr, len} == str); |
| 30 | + |
| 31 | + const vx_binary *binary2 = vx_binary_clone(binary); |
| 32 | + vx_binary_free(binary); |
| 33 | + |
| 34 | + ptr = vx_binary_ptr(binary2); |
| 35 | + REQUIRE(std::string_view {ptr, len} == str); |
| 36 | + |
| 37 | + vx_binary_free(binary2); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +TEST_CASE("Creating dtypes", "[dtype]") { |
| 42 | + const vx_dtype *dtype = vx_dtype_new_null(); |
| 43 | + REQUIRE(dtype != nullptr); |
| 44 | + CHECK(vx_dtype_get_variant(dtype) == DTYPE_NULL); |
| 45 | + CHECK(vx_dtype_is_nullable(dtype)); |
| 46 | + vx_dtype_free(dtype); |
| 47 | + |
| 48 | + dtype = vx_dtype_new_decimal(5, 2, false); |
| 49 | + REQUIRE(dtype != nullptr); |
| 50 | + CHECK(vx_dtype_get_variant(dtype) == DTYPE_DECIMAL); |
| 51 | + CHECK(vx_dtype_decimal_precision(dtype) == 5); |
| 52 | + CHECK(vx_dtype_decimal_scale(dtype) == 2); |
| 53 | + CHECK_FALSE(vx_dtype_is_nullable(dtype)); |
| 54 | + |
| 55 | + CHECK(vx_dtype_struct_dtype(dtype) == nullptr); |
| 56 | + CHECK(vx_dtype_list_element(dtype) == nullptr); |
| 57 | + |
| 58 | + vx_dtype_free(dtype); |
| 59 | +} |
| 60 | + |
| 61 | +constexpr size_t STRUCT_LEN = 10; |
| 62 | +TEST_CASE("Creating structs", "[struct]") { |
| 63 | + vx_struct_fields_builder *builder = vx_struct_fields_builder_new(); |
| 64 | + REQUIRE(builder != nullptr); |
| 65 | + |
| 66 | + for (size_t i = 0; i < STRUCT_LEN; ++i) { |
| 67 | + const std::string target_name = "name"s + std::to_string(i); |
| 68 | + const vx_string *name = vx_string_new(target_name.data(), target_name.size()); |
| 69 | + const vx_dtype *dtype = i % 2 ? vx_dtype_new_binary(false) : vx_dtype_new_primitive(PTYPE_F32, true); |
| 70 | + vx_struct_fields_builder_add_field(builder, name, dtype); |
| 71 | + } |
| 72 | + const vx_struct_fields *fields = vx_struct_fields_builder_finalize(builder); |
| 73 | + REQUIRE(fields != nullptr); |
| 74 | + |
| 75 | + const size_t len = vx_struct_fields_nfields(fields); |
| 76 | + CHECK(len == STRUCT_LEN); |
| 77 | + for (size_t i = 0; i < len; ++i) { |
| 78 | + // borrowed |
| 79 | + const vx_string *name = vx_struct_fields_field_name(fields, i); |
| 80 | + // owned TODO(myrrc): that's weird API |
| 81 | + const vx_dtype *dtype = vx_struct_fields_field_dtype(fields, i); |
| 82 | + |
| 83 | + std::string_view name_view {vx_string_ptr(name), vx_string_len(name)}; |
| 84 | + std::string target_name = "name"s + std::to_string(i); |
| 85 | + |
| 86 | + CHECK(name_view == target_name); |
| 87 | + |
| 88 | + if (i % 2) { |
| 89 | + CHECK_FALSE(vx_dtype_is_nullable(dtype)); |
| 90 | + CHECK(vx_dtype_get_variant(dtype) == DTYPE_BINARY); |
| 91 | + } else { |
| 92 | + CHECK(vx_dtype_is_nullable(dtype)); |
| 93 | + CHECK(vx_dtype_get_variant(dtype) == DTYPE_PRIMITIVE); |
| 94 | + } |
| 95 | + |
| 96 | + vx_dtype_free(dtype); |
| 97 | + } |
| 98 | + |
| 99 | + vx_struct_fields_free(fields); |
| 100 | +} |
0 commit comments