OXYGENHUB
◆ main openmsx / src / unittest / static_string_view_test.cc
1.4 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "catch.hpp"
2
#include "static_string_view.hh"
3
4
TEST_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";
35
	static_string_view v7(static_string_view::lifetime_ok_tag{}, s7);
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
}