From d741f5b0bd2282480dde67115603a9c2142f7f94 Mon Sep 17 00:00:00 2001 From: Alexis Placet Date: Fri, 10 Jan 2025 15:24:58 +0100 Subject: [PATCH] wip --- include/sparrow/layout/primitive_array.hpp | 4 +- include/sparrow/utils/format.hpp | 78 ------------------ test/CMakeLists.txt | 1 + test/test_fixed_width_binary_array.cpp | 18 +--- test/test_ranges.cpp | 95 ++++++++++++++++++++++ test/test_traits.cpp | 28 +++---- 6 files changed, 115 insertions(+), 109 deletions(-) create mode 100644 test/test_ranges.cpp diff --git a/include/sparrow/layout/primitive_array.hpp b/include/sparrow/layout/primitive_array.hpp index 70274ede..16b29bb3 100644 --- a/include/sparrow/layout/primitive_array.hpp +++ b/include/sparrow/layout/primitive_array.hpp @@ -258,9 +258,9 @@ namespace sparrow friend base_type::base_type::base_type; }; - /******************************************* + /********************************** * primitive_array implementation * - *******************************************/ + **********************************/ namespace detail { diff --git a/include/sparrow/utils/format.hpp b/include/sparrow/utils/format.hpp index c8056032..43edef17 100644 --- a/include/sparrow/utils/format.hpp +++ b/include/sparrow/utils/format.hpp @@ -16,13 +16,11 @@ #pragma once #include -#include #include #include #include #include #include -#include #include #include @@ -58,82 +56,6 @@ namespace std std::string m_format_string = "{:"; }; - -#if !defined(__cpp_lib_format_ranges) - template - struct formatter> - { - private: - - char delimiter = ','; - char opening = '['; - char closing = ']'; - bool compact = false; - - public: - - constexpr auto parse(std::format_parse_context& ctx) - { - auto it = ctx.begin(); - auto end = ctx.end(); - - // Parse format specifiers - while (it != end && *it != '}') - { - switch (*it) - { - case 'c': // compact mode - compact = true; - break; - case '(': // use parentheses - opening = '('; - closing = ')'; - break; - case '{': // use curly braces - opening = '{'; - closing = '}'; - break; - case ';': // use semicolon as delimiter - delimiter = ';'; - break; - default: - break; - } - ++it; - } - return it; - } - - auto format(const std::array& array, std::format_context& ctx) const - { - auto out = ctx.out(); - - // Output opening bracket - *out++ = opening; - - bool first = true; - for (const auto& elem : array) - { - if (!first) - { - *out++ = delimiter; - if (!compact) - { - *out++ = ' '; - } - } - first = false; - - // Format each element using its own formatter - out = std::format_to(out, "{}", elem); - } - - // Output closing bracket - *out++ = closing; - return out; - } - }; -#endif } namespace sparrow diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4bea7011..bf296f91 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -145,6 +145,7 @@ else() test_null_array.cpp test_nullable.cpp test_primitive_array.cpp + test_ranges.cpp test_record_batch.cpp test_repeat_container.cpp test_run_end_encoded_array.cpp diff --git a/test/test_fixed_width_binary_array.cpp b/test/test_fixed_width_binary_array.cpp index 714a9be9..e8f6542b 100644 --- a/test/test_fixed_width_binary_array.cpp +++ b/test/test_fixed_width_binary_array.cpp @@ -23,23 +23,11 @@ namespace sparrow { - template - class my_class - { - std::array data; - T my_value; - }; - - template - class my_incompatible_class - { - std::vector data; - }; - TEST_SUITE("fixed_width_binary_array") { - auto make_array = [](size_t count, size_t offset = 0 - ) -> std::pair>> + static const auto make_array = + [](size_t count, + size_t offset = 0) -> std::pair>> { std::vector> input_values; input_values.reserve(count); diff --git a/test/test_ranges.cpp b/test/test_ranges.cpp new file mode 100644 index 00000000..08820de1 --- /dev/null +++ b/test/test_ranges.cpp @@ -0,0 +1,95 @@ +// Copyright 2024 Man Group Operations Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "sparrow/utils/ranges.hpp" + +#include "doctest/doctest.h" + +namespace sparrow +{ + TEST_SUITE("ranges") + { + TEST_CASE("range_size") + { + SUBCASE("for sized_range") + { + std::vector v{1, 2, 3, 4, 5}; + CHECK_EQ(range_size(v), 5); + } + + SUBCASE("for non-sized_range") + { + std::vector v{1, 2, 3, 4, 5}; + CHECK_EQ( + range_size( + v + | std::views::filter( + [](int i) + { + return i % 2 == 0; + } + ) + ), + 2 + ); + } + + SUBCASE("for empty range") + { + std::vector v; + CHECK_EQ(range_size(v), 0); + } + } + + TEST_CASE("all_same_size") + { + SUBCASE("for std::array") + { + std::vector> v{ + {std::array{1, 2, 3}, std::array{4, 5, 6}, std::array{7, 8, 9}} + }; + CHECK(all_same_size(v)); + } + + SUBCASE("for std::vector") + { + std::vector> v{ + std::vector{1, 2, 3}, + std::vector{4, 5, 6}, + std::vector{7, 8, 9} + }; + CHECK(all_same_size(v)); + } + + SUBCASE("for std::vector with different sizes") + { + std::vector> v{ + std::vector{1, 2, 3}, + std::vector{4, 5, 6, 7}, + std::vector{8, 9} + }; + CHECK_FALSE(all_same_size(v)); + } + + SUBCASE("for empty range") + { + std::vector> v; + CHECK(all_same_size(v)); + } + } + } +} diff --git a/test/test_traits.cpp b/test/test_traits.cpp index 4feef5fe..3aad1c1e 100644 --- a/test/test_traits.cpp +++ b/test/test_traits.cpp @@ -18,20 +18,20 @@ ///////////////////////////////////////////////////////////////////////////////////////// // Opt-in support for custom C++ representations of arrow data types. -// struct MyDataType -// { -// }; - -// template <> -// struct sparrow::arrow_traits -// { -// static constexpr data_type type_id = sparrow::data_type::INT32; -// using value_type = MyDataType; -// using default_layout = sparrow::fixed_width_binary_array; -// }; - -// static_assert(sparrow::is_arrow_traits>); -// static_assert(sparrow::any_arrow_type); +struct MyDataType +{ +}; + +template <> +struct sparrow::arrow_traits +{ + static constexpr data_type type_id = sparrow::data_type::FIXED_WIDTH_BINARY; + using value_type = MyDataType; + using default_layout = sparrow::fixed_width_binary_array; +}; + +static_assert(sparrow::is_arrow_traits>); +static_assert(sparrow::any_arrow_type); ////////////////////////////////////////////////////////////////////////////////////////// // Base arrow types representations support tests and concept checking.