Skip to content

Commit

Permalink
Add CPP byte-type datatype checks. (#4826)
Browse files Browse the repository at this point in the history
Fix `datatype_is_byte` to appropriately validate `std::byte` Datatypes.
Also add a C++ byte-type check and add the byte case to `typeError`.

----

TYPE: IMPROVEMENT
DESC: Add CPP byte-type checks.
  • Loading branch information
bekadavis9 authored Mar 25, 2024
1 parent b754881 commit 2029308
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
11 changes: 9 additions & 2 deletions test/src/unit-cppapi-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2017-2021 TileDB Inc.
* @copyright Copyright (c) 2017-2024 TileDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,14 +33,15 @@
#include <test/support/tdb_catch.h>
#include "tiledb/sm/cpp_api/tiledb"

using namespace tiledb;

struct MyData {
int a;
float b;
double c[3];
};

TEST_CASE("C++ API: Types", "[cppapi][types]") {
using namespace tiledb;
Context ctx;

CHECK(
Expand Down Expand Up @@ -123,6 +124,12 @@ TEST_CASE("C++ API: Types", "[cppapi][types]") {
CHECK_NOTHROW(impl::type_check<std::array<char, 1>>(
TILEDB_STRING_ASCII, TILEDB_VAR_NUM));

// Validate byte datatypes
CHECK_NOTHROW(impl::type_check<std::byte>(TILEDB_BLOB));
CHECK_NOTHROW(impl::type_check<std::byte>(TILEDB_GEOM_WKB));
CHECK_NOTHROW(impl::type_check<std::byte>(TILEDB_GEOM_WKT));
CHECK_THROWS(impl::type_check<std::byte>(TILEDB_BOOL));

auto a1 = Attribute::create<int>(ctx, "a1");
auto a2 = Attribute::create<float>(ctx, "a2");
auto a3 = Attribute::create<float[5]>(ctx, "a3");
Expand Down
8 changes: 8 additions & 0 deletions test/src/unit-enum-helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ TEST_CASE(
REQUIRE_THROWS(datatype_max_integral_value(Datatype::FLOAT64));
REQUIRE_THROWS(datatype_max_integral_value(Datatype::STRING_ASCII));
}

TEST_CASE("Test datatype_is_byte", "[enums][datatype][datatype_is_byte]") {
auto datatype =
GENERATE(Datatype::BLOB, Datatype::GEOM_WKB, Datatype::GEOM_WKT);

CHECK(datatype_is_byte(datatype));
CHECK(!datatype_is_byte(Datatype::BOOL));
}
8 changes: 7 additions & 1 deletion tiledb/sm/cpp_api/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2017-2021 TileDB, Inc.
* @copyright Copyright (c) 2017-2024 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -90,6 +90,12 @@ inline void type_check(tiledb_datatype_t type, unsigned num = 0) {
"type (" +
impl::type_to_str(type) + ")");
}
} else if (tiledb_byte_type(type)) {
if (!std::is_same<T, std::byte>::value) {
throw TypeError(
"Static type does not match expected container type std::byte for "
"tiledb byte type");
}
} else if (tiledb_datetime_type(type)) {
if (!std::is_same<T, int64_t>::value) {
throw TypeError(
Expand Down
13 changes: 12 additions & 1 deletion tiledb/sm/cpp_api/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2017-2023 TileDB, Inc.
* @copyright Copyright (c) 2017-2024 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -294,6 +294,17 @@ inline bool tiledb_string_type(tiledb_datatype_t type) {
}
}

inline bool tiledb_byte_type(tiledb_datatype_t type) {
switch (type) {
case TILEDB_BLOB:
case TILEDB_GEOM_WKB:
case TILEDB_GEOM_WKT:
return true;
default:
return false;
}
}

inline bool tiledb_datetime_type(tiledb_datatype_t type) {
switch (type) {
case TILEDB_DATETIME_YEAR:
Expand Down
2 changes: 1 addition & 1 deletion tiledb/sm/enums/datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ inline bool datatype_is_time(Datatype type) {
/** Returns true if the input datatype is a std::byte type. */
inline bool datatype_is_byte(Datatype type) {
return (
type == Datatype::BOOL || type == Datatype::GEOM_WKB ||
type == Datatype::BLOB || type == Datatype::GEOM_WKB ||
type == Datatype::GEOM_WKT);
}

Expand Down

0 comments on commit 2029308

Please sign in to comment.