openMSX
static_string_view_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
3
4TEST_CASE("static_string_view")
5{
6 // construct from string-literal
7 static_string_view v1("bla"); // ok, this compiles
8 std::string_view sv1 = v1;
9 CHECK(sv1 == "bla");
10
11 //const char* cp = "foobar";
12 //static_string_view v2(cp); // ok, does NOT compile
13
14 //std::string s = "qux";
15 //static_string_view v3(s); // ok, does NOT compile
16
17 //char buf[10];
18 //static_string_view v4(buf); // ok, does NOT compile
19
20 // This compiles, but ideally it shouldn't :(
21 //const char cBuf[3] = { 'f', 'o', 'o' };
22 //static_string_view v5(cBuf); // but it does trigger an assert at runtime
23
24 // This compiles, but ideally it shouldn't :(
25 const char cBuf0[4] = { 'f', 'o', 'o', '\0' };
26 static_string_view v6(cBuf0); // and also no assert
27 std::string_view sv6 = v6;
28 CHECK(sv6 == "foo");
29
30 // Escape hatch:
31 // Not checked by the compiler, it's the programmers responsibility to
32 // ensure that the argument passed to the constructor outlives the
33 // resulting static_string_view object.
34 std::string s7 = "qux";
36 std::string_view sv7 = v7;
37 CHECK(sv7 == "qux");
38
39 std::string s8 = "baz";
40 auto [storage8, v8] = make_string_storage(s8);
41 std::string_view sv8 = v8;
42 CHECK(sv8 == "baz");
43 s8 = "changed"; // changing the original string
44 CHECK(sv8 == "baz"); // does not change the copy
45
46}
static_string_view
CHECK(m3==m3)
auto make_string_storage(std::string_view sv)
Take a string_view, make a copy of it, and return a pair of.
TEST_CASE("static_string_view")