kokkos-execution 0.0.1
Loading...
Searching...
No Matches
test_fixed_string.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2
3#include <string>
4
6
17
18namespace Tests::Impl {
19
20using Kokkos::Execution::Impl::FixedString;
21using namespace std::string_view_literals;
22
30
32
35 static_assert(std::same_as<decltype(FixedString{""}), FixedString<1>>);
36 static_assert(std::same_as<decltype(FixedString{"a"}), FixedString<2>>);
37 static_assert(std::same_as<decltype(FixedString{"ab"}), FixedString<3>>);
38 static_assert(std::same_as<decltype(FixedString{"abc"}), FixedString<4>>);
39
40 return true;
41}
42
44
46template <size_t Size>
47consteval bool test_FixedString_layout() {
48 static_assert(sizeof(FixedString<Size>) == Size);
49 static_assert(std::is_trivially_copyable_v<FixedString<Size>>);
50 static_assert(std::is_standard_layout_v<FixedString<Size>>);
51
52 return true;
53}
54
55static_assert(test_FixedString_layout<1>());
56static_assert(test_FixedString_layout<7>());
57
60 static_assert(std::is_constructible_v<FixedString<4>, const char (&)[4]>);
61
63 static_assert(!std::is_constructible_v<FixedString<4>, const char (&)[5]>);
64
66 static_assert(!std::is_constructible_v<FixedString<4>, const char*>);
67
68 static_assert(!std::is_constructible_v<FixedString<4>, std::string_view>);
69 static_assert(!std::is_constructible_v<FixedString<4>, std::string>);
70
71 return true;
72}
73
75
78 constexpr FixedString fstr{"label"};
79 static_assert(fstr.view() == "label"sv);
80 static_assert(fstr.size() == 5);
81
82 return true;
83}
84
85static_assert(test_FixedString_from_literal());
86
89 constexpr FixedString fstr{""};
90 static_assert(fstr.size() == 0);
91 static_assert(fstr.view().empty());
92 static_assert(fstr.begin() == fstr.end());
93
94 return true;
95}
96
98
105consteval bool test_view_is_noexcept() {
106 static_assert(noexcept(FixedString{"abc"}.view()));
107
108 return true;
109}
110
111static_assert(test_view_is_noexcept());
112
114TEST(FixedStringTest, converts_to_string_view_and_string) {
115 static_assert(std::is_constructible_v<std::string_view, const FixedString<6>&>);
116
118 static_assert(std::is_constructible_v<std::string, const FixedString<6>&>);
119
120 constexpr FixedString fstr{"label"};
121
122 const std::string_view fstr_view = fstr;
123 EXPECT_EQ(fstr_view, "label"sv);
124 EXPECT_EQ(std::string(fstr), "label");
125}
126
127template <FixedString S>
128struct Tag {
129 static constexpr std::string_view value = S.view();
130};
131
134 static_assert(Tag<"abcd">::value == "abcd"sv);
135 static_assert(std::same_as<Tag<"abcd">, Tag<"abcd">>);
136 static_assert(!std::same_as<Tag<"abcd">, Tag<"abdc">>);
137
138 return true;
139}
140
142
143#if defined(__cpp_lib_is_structural)
145consteval bool test_FixedString_is_structural() {
146 static_assert(std::is_structural_v<FixedString{"abcd"}>);
147
148 return true;
149}
150
151static_assert(test_FixedString_is_structural());
152#endif
153
154} // namespace Tests::Impl
constexpr bool is_fixed_string_v
consteval bool test_FixedString_is_fixed_string()
consteval bool test_FixedString_is_usable_as_a_template_parameter()
consteval bool test_FixedString_is_constructible_from_matching_char_array_only()
consteval bool test_FixedString_from_literal()
consteval bool test_FixedString_from_empty_literal()
consteval bool test_FixedString_layout()
consteval bool test_view_is_noexcept()
consteval bool test_FixedString_deduces_capacity_from_literal()
Fixed-capacity string built from a char array.
constexpr auto size() const noexcept
constexpr auto begin() const noexcept
constexpr auto view() const noexcept(noexcept(std::string_view{data, size()})) -> decltype(std::string_view{data, size()})
constexpr auto end() const noexcept
Fixed-capacity string built from a char array.
static constexpr std::string_view value