mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-03 12:24:26 +02:00
feat: basic_fixed_string
implementation and testing improved
This commit is contained in:
@@ -24,7 +24,9 @@ cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
find_package(Catch2 3 REQUIRED)
|
||||
|
||||
add_executable(unit_tests_runtime distribution_test.cpp fmt_test.cpp math_test.cpp atomic_test.cpp)
|
||||
add_executable(
|
||||
unit_tests_runtime distribution_test.cpp fixed_string_test.cpp fmt_test.cpp math_test.cpp atomic_test.cpp
|
||||
)
|
||||
if(${projectPrefix}BUILD_CXX_MODULES)
|
||||
target_compile_definitions(unit_tests_runtime PUBLIC ${projectPrefix}MODULES)
|
||||
endif()
|
||||
|
67
test/runtime/fixed_string_test.cpp
Normal file
67
test/runtime/fixed_string_test.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/matchers/catch_matchers_exception.hpp>
|
||||
#include <mp-units/compat_macros.h>
|
||||
#include <string_view>
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ext/fixed_string.h>
|
||||
#endif
|
||||
|
||||
using namespace mp_units;
|
||||
|
||||
TEST_CASE("fixed_string::at", "[fixed_string]")
|
||||
{
|
||||
basic_fixed_string txt = "abc";
|
||||
SECTION("in range")
|
||||
{
|
||||
CHECK(txt.at(0) == 'a');
|
||||
CHECK(txt.at(1) == 'b');
|
||||
CHECK(txt.at(2) == 'c');
|
||||
}
|
||||
SECTION("out of range")
|
||||
{
|
||||
REQUIRE_THROWS_MATCHES(txt.at(3), std::out_of_range, Catch::Matchers::Message("basic_fixed_string::at"));
|
||||
REQUIRE_THROWS_MATCHES(txt.at(1024), std::out_of_range, Catch::Matchers::Message("basic_fixed_string::at"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("fixed_string text output", "[fixed_string][ostream][fmt]")
|
||||
{
|
||||
basic_fixed_string txt = "units";
|
||||
SECTION("iostream")
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << txt;
|
||||
CHECK(os.str() == "units");
|
||||
}
|
||||
SECTION("fmt") { CHECK(MP_UNITS_STD_FMT::format("{}", txt) == "units"); }
|
||||
}
|
||||
|
||||
TEST_CASE("fixed_string hash", "[fixed_string][hash]")
|
||||
{
|
||||
basic_fixed_string txt = "units";
|
||||
CHECK(std::hash<fixed_string<5>>{}(txt) == std::hash<std::string_view>{}("units"));
|
||||
}
|
@@ -34,7 +34,6 @@
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ext/fixed_string.h>
|
||||
#include <mp-units/format.h>
|
||||
#include <mp-units/ostream.h> // IWYU pragma: keep
|
||||
#include <mp-units/systems/cgs.h>
|
||||
@@ -51,18 +50,6 @@ inline constexpr bool mp_units::is_vector<T> = true;
|
||||
using namespace mp_units;
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
TEST_CASE("fixed_string", "[text][ostream][fmt]")
|
||||
{
|
||||
basic_fixed_string txt = "units";
|
||||
SECTION("iostream")
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << txt;
|
||||
CHECK(os.str() == "units");
|
||||
}
|
||||
SECTION("fmt") { CHECK(MP_UNITS_STD_FMT::format("{}", txt) == "units"); }
|
||||
}
|
||||
|
||||
TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
|
||||
{
|
||||
std::ostringstream os;
|
||||
|
@@ -27,36 +27,152 @@ using namespace mp_units;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::array array = {'a', 'b', 'c'};
|
||||
|
||||
auto from_string = [] {
|
||||
std::string txt = "abc";
|
||||
return fixed_string<3>(std::from_range, txt);
|
||||
};
|
||||
|
||||
auto from_string_iter = [] {
|
||||
std::string txt = "abc";
|
||||
return fixed_string<3>(txt.begin(), txt.end());
|
||||
};
|
||||
|
||||
constexpr fixed_string<0> txt0;
|
||||
constexpr basic_fixed_string txt1('a');
|
||||
constexpr basic_fixed_string txt2('a', 'b', 'c');
|
||||
constexpr basic_fixed_string txt3 = "abc";
|
||||
constexpr fixed_string<3> txt4(array.begin(), array.end());
|
||||
constexpr basic_fixed_string txt5(std::from_range, array);
|
||||
constexpr basic_fixed_string txt6(from_string());
|
||||
constexpr basic_fixed_string txt7(from_string_iter());
|
||||
|
||||
constexpr fixed_string<3> txt8(txt2.begin(), txt2.end());
|
||||
constexpr fixed_string<3> txt9(txt2.rbegin(), txt2.rend());
|
||||
|
||||
static_assert(txt0.size() == 0);
|
||||
static_assert(txt1.size() == 1);
|
||||
static_assert(txt2.size() == 3);
|
||||
static_assert(txt3.size() == 3);
|
||||
static_assert(txt4.size() == 3);
|
||||
static_assert(txt5.size() == 3);
|
||||
static_assert(txt6.size() == 3);
|
||||
static_assert(txt7.size() == 3);
|
||||
static_assert(txt8.size() == 3);
|
||||
static_assert(txt9.size() == 3);
|
||||
|
||||
static_assert(txt0.length() == 0);
|
||||
static_assert(txt1.length() == 1);
|
||||
static_assert(txt2.length() == 3);
|
||||
|
||||
static_assert(txt0.max_size() == 0);
|
||||
static_assert(txt1.max_size() == 1);
|
||||
static_assert(txt2.max_size() == 3);
|
||||
|
||||
static_assert(txt0.empty() == true);
|
||||
static_assert(txt1.empty() == false);
|
||||
static_assert(txt2.empty() == false);
|
||||
static_assert(txt3.empty() == false);
|
||||
static_assert(txt4.empty() == false);
|
||||
static_assert(txt5.empty() == false);
|
||||
static_assert(txt6.empty() == false);
|
||||
static_assert(txt7.empty() == false);
|
||||
static_assert(txt8.empty() == false);
|
||||
static_assert(txt9.empty() == false);
|
||||
|
||||
static_assert(txt1[0] == 'a');
|
||||
static_assert(txt2[0] == 'a');
|
||||
static_assert(txt2[1] == 'b');
|
||||
static_assert(txt2[2] == 'c');
|
||||
static_assert(txt9[0] == 'c');
|
||||
static_assert(txt9[1] == 'b');
|
||||
static_assert(txt9[2] == 'a');
|
||||
|
||||
static_assert(txt1.at(0) == 'a');
|
||||
static_assert(txt2.at(0) == 'a');
|
||||
static_assert(txt2.at(1) == 'b');
|
||||
static_assert(txt2.at(2) == 'c');
|
||||
static_assert(txt9.at(0) == 'c');
|
||||
static_assert(txt9.at(1) == 'b');
|
||||
static_assert(txt9.at(2) == 'a');
|
||||
|
||||
static_assert(txt1.front() == 'a');
|
||||
static_assert(txt1.back() == 'a');
|
||||
static_assert(txt2.front() == 'a');
|
||||
static_assert(txt2.back() == 'c');
|
||||
static_assert(txt5.front() == 'a');
|
||||
static_assert(txt5.back() == 'c');
|
||||
static_assert(txt6.front() == 'a');
|
||||
static_assert(txt6.back() == 'c');
|
||||
static_assert(txt7.front() == 'a');
|
||||
static_assert(txt7.back() == 'c');
|
||||
static_assert(txt8.front() == 'a');
|
||||
static_assert(txt8.back() == 'c');
|
||||
static_assert(txt9.front() == 'c');
|
||||
static_assert(txt9.back() == 'a');
|
||||
|
||||
static_assert(std::string_view(txt0.data()) == "");
|
||||
static_assert(std::string_view(txt0.c_str()) == "");
|
||||
static_assert(std::string_view(txt1.data()) == "a");
|
||||
static_assert(std::string_view(txt1.c_str()) == "a");
|
||||
static_assert(std::string_view(txt2.data()) == "abc");
|
||||
static_assert(std::string_view(txt2.c_str()) == "abc");
|
||||
|
||||
static_assert(txt0 == "");
|
||||
static_assert("a" == txt1);
|
||||
static_assert(txt2 == "abc");
|
||||
static_assert(txt3 == "abc");
|
||||
static_assert(txt4 == "abc");
|
||||
static_assert(txt5 == "abc");
|
||||
static_assert(txt6 == "abc");
|
||||
static_assert(txt7 == "abc");
|
||||
static_assert(txt8 == "abc");
|
||||
static_assert(txt9 == "cba");
|
||||
|
||||
static_assert(txt1 == basic_fixed_string("a"));
|
||||
static_assert(txt1 != basic_fixed_string("b"));
|
||||
static_assert(txt1 != basic_fixed_string("aa"));
|
||||
static_assert(txt1 < basic_fixed_string("b"));
|
||||
static_assert(txt1 < basic_fixed_string("aa"));
|
||||
static_assert(txt1 + basic_fixed_string('b') == basic_fixed_string("ab"));
|
||||
static_assert(basic_fixed_string('b') + txt1 == basic_fixed_string("ba"));
|
||||
static_assert(txt1 + basic_fixed_string("bc") == basic_fixed_string("abc"));
|
||||
static_assert(basic_fixed_string("bc") + txt1 == basic_fixed_string("bca"));
|
||||
static_assert(txt1 == "a");
|
||||
static_assert(txt1 != "b");
|
||||
static_assert(txt1 != "aa");
|
||||
static_assert(txt1 < "b");
|
||||
static_assert(txt1 < "aa");
|
||||
|
||||
static_assert(txt1 + basic_fixed_string('b') == "ab");
|
||||
static_assert(basic_fixed_string('b') + txt1 == "ba");
|
||||
static_assert(txt1 + basic_fixed_string("bc") == "abc");
|
||||
static_assert(basic_fixed_string("bc") + txt1 == "bca");
|
||||
static_assert(txt1 + 'b' == "ab");
|
||||
static_assert('b' + txt1 == "ba");
|
||||
static_assert(txt1 + "bc" == "abc");
|
||||
static_assert("bc" + txt1 == "bca");
|
||||
|
||||
constexpr basic_fixed_string txt2("abc");
|
||||
static_assert(txt2.size() == 3);
|
||||
static_assert(txt2[0] == 'a');
|
||||
static_assert(txt2[1] == 'b');
|
||||
static_assert(txt2[2] == 'c');
|
||||
static_assert(txt2 == basic_fixed_string("abc"));
|
||||
static_assert(txt2 != basic_fixed_string("cba"));
|
||||
static_assert(txt2 != basic_fixed_string("abcd"));
|
||||
static_assert(txt2 < basic_fixed_string("b"));
|
||||
static_assert(txt2 > basic_fixed_string("aa"));
|
||||
static_assert(txt2 + basic_fixed_string('d') == basic_fixed_string("abcd"));
|
||||
static_assert(basic_fixed_string('d') + txt2 == basic_fixed_string("dabc"));
|
||||
static_assert(txt2 + basic_fixed_string("def") == basic_fixed_string("abcdef"));
|
||||
static_assert(basic_fixed_string("def") + txt2 == basic_fixed_string("defabc"));
|
||||
static_assert(txt2 == "abc");
|
||||
static_assert(txt2 != "cba");
|
||||
static_assert(txt2 != "abcd");
|
||||
static_assert(txt2 < "b");
|
||||
static_assert(txt2 > "aa");
|
||||
|
||||
#ifndef MP_UNITS_COMP_GCC
|
||||
static_assert(std::string_view(basic_fixed_string("abcd")).find('c') == 2);
|
||||
#endif
|
||||
static_assert(txt2 + basic_fixed_string('d') == "abcd");
|
||||
static_assert(basic_fixed_string('d') + txt2 == "dabc");
|
||||
static_assert(txt2 + basic_fixed_string("def") == "abcdef");
|
||||
static_assert(basic_fixed_string("def") + txt2 == "defabc");
|
||||
static_assert(txt2 + 'd' == "abcd");
|
||||
static_assert('d' + txt2 == "dabc");
|
||||
static_assert(txt2 + "def" == "abcdef");
|
||||
static_assert("def" + txt2 == "defabc");
|
||||
|
||||
static_assert(std::string_view(txt2) == "abc");
|
||||
static_assert(txt2.view() == "abc");
|
||||
static_assert(std::string_view(txt2).find('b') == 1);
|
||||
static_assert(txt2.view().find('b') == 1);
|
||||
|
||||
} // namespace
|
||||
|
Reference in New Issue
Block a user