-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathscalar.cpp
More file actions
100 lines (83 loc) · 3.52 KB
/
Copy pathscalar.cpp
File metadata and controls
100 lines (83 loc) · 3.52 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
#include "vortex/error.hpp"
#include <catch2/catch_test_macros.hpp>
#include <string_view>
#include <vortex/scalar.hpp>
using namespace vortex;
using namespace std::string_view_literals;
namespace {
using enum vortex::PType;
TEST_CASE("Boolean scalar", "[scalar]") {
Scalar s = scalar::of(true);
REQUIRE_FALSE(s.is_null());
REQUIRE(s.dtype().variant() == DataTypeVariant::Bool);
}
TEST_CASE("Integer scalars", "[scalar]") {
REQUIRE(scalar::of<uint8_t>(42).dtype().primitive_type() == U8);
REQUIRE(scalar::of<uint16_t>(42).dtype().primitive_type() == U16);
REQUIRE(scalar::of<uint32_t>(42).dtype().primitive_type() == U32);
REQUIRE(scalar::of<uint64_t>(42).dtype().primitive_type() == U64);
REQUIRE(scalar::of<int8_t>(-1).dtype().primitive_type() == I8);
REQUIRE(scalar::of<int16_t>(-1).dtype().primitive_type() == I16);
REQUIRE(scalar::of<int32_t>(-1).dtype().primitive_type() == I32);
REQUIRE(scalar::of<int64_t>(-1).dtype().primitive_type() == I64);
}
TEST_CASE("Float scalars", "[scalar]") {
REQUIRE(scalar::of(1.5F).dtype().primitive_type() == F32);
REQUIRE(scalar::of(1.5).dtype().primitive_type() == F64);
REQUIRE(scalar::of(float16_t {0x3C00}).dtype().primitive_type() == F16);
}
TEST_CASE("Nullable scalars", "[scalar]") {
Scalar s = scalar::of<int32_t>(0, true);
REQUIRE(s.dtype().nullable());
REQUIRE_FALSE(s.is_null());
}
TEST_CASE("Null scalar", "[scalar]") {
Scalar s = scalar::null(dtype::int32(true));
REQUIRE(s.is_null());
REQUIRE(s.dtype().variant() == DataTypeVariant::Primitive);
}
TEST_CASE("UTF-8 scalar", "[scalar]") {
Scalar s = scalar::of("hello"sv);
REQUIRE_FALSE(s.is_null());
REQUIRE(s.dtype().variant() == DataTypeVariant::Utf8);
REQUIRE_FALSE(scalar::of(""sv).is_null());
s = scalar::of("Широкая строка"sv);
REQUIRE(s.dtype().variant() == DataTypeVariant::Utf8);
REQUIRE_THROWS_AS(scalar::of("\xFF\xFE"sv), VortexException);
}
TEST_CASE("Binary scalar", "[scalar]") {
const std::byte bytes[] = {std::byte {1}, std::byte {2}, std::byte {0}, std::byte {4}};
Scalar s = scalar::of(std::span<const std::byte> {bytes});
REQUIRE_FALSE(s.is_null());
REQUIRE(s.dtype().variant() == DataTypeVariant::Binary);
}
TEST_CASE("Decimal scalars", "[scalar]") {
Scalar d8 = scalar::decimal<int8_t>(56, 5, 2);
REQUIRE(d8.dtype().variant() == DataTypeVariant::Decimal);
REQUIRE(d8.dtype().decimal_precision() == 5);
REQUIRE(d8.dtype().decimal_scale() == 2);
Scalar d16 = scalar::decimal(int16_t(1234), 5, 2);
REQUIRE(d16.dtype().variant() == DataTypeVariant::Decimal);
REQUIRE(d16.dtype().decimal_precision() == 5);
REQUIRE(d16.dtype().decimal_scale() == 2);
Scalar d32 = scalar::decimal(int32_t(1234), 5, 2);
REQUIRE(d32.dtype().variant() == DataTypeVariant::Decimal);
REQUIRE(d32.dtype().decimal_precision() == 5);
REQUIRE(d32.dtype().decimal_scale() == 2);
Scalar d64 = scalar::decimal<int64_t>(99999, 12, 3);
REQUIRE(d64.dtype().variant() == DataTypeVariant::Decimal);
REQUIRE(d64.dtype().decimal_precision() == 12);
REQUIRE(d64.dtype().decimal_scale() == 3);
}
TEST_CASE("Copy scalar", "[scalar]") {
Scalar a = scalar::of<int64_t>(42);
Scalar b = a;
REQUIRE(a.dtype().primitive_type() == I64);
REQUIRE(b.dtype().primitive_type() == I64);
Scalar c = scalar::of<int32_t>(1);
c = b;
REQUIRE(c.dtype().primitive_type() == I64);
}
} // namespace