Debian Codegraph @

LspCpp/third_party/utfcpp/tests/test_cpp17.cpp

raw
1 #include "../extern/ftest/ftest.h"
2 #include "utf8.h"
3 #include <string>
4 using namespace utf8;
5 using namespace std;
6
7 #if __cplusplus >= 201703L // C++ 17 or later
8
9
10 TEST(CPP17APITests, test_utf16to8)
11 {
12 u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e};
13 u16string_view utf16stringview(u16string);
14 string u = utf16to8(utf16string);
15 EXPECT_EQ (u.size(), 10);
16 }
17
18 TEST(CPP17APITests, test_utf8to16)
19 {
20 string_view utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";
21 u16string utf16result = utf8to16(utf8_with_surrogates);
22 EXPECT_EQ (utf16result.size(), 4);
23 EXPECT_EQ (utf16result[2], 0xd834);
24 EXPECT_EQ (utf16result[3], 0xdd1e);
25 }
26
27 TEST(CPP17APITests, test_utf32to8)
28 {
29 u32string utf32string = {0x448, 0x65E5, 0x10346};
30 u32string_view utf32stringview(utf32string);
31 string utf8result = utf32to8(utf32stringview);
32 EXPECT_EQ (utf8result.size(), 9);
33 }
34
35 TEST(CPP17APITests, test_utf8to32)
36 {
37 string_view twochars = "\xe6\x97\xa5\xd1\x88";
38 u32string utf32result = utf8to32(twochars);
39 EXPECT_EQ (utf32result.size(), 2);
40 }
41
42 TEST(CPP17APITests, test_find_invalid)
43 {
44 string_view utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa";
45 auto invalid = find_invalid(utf_invalid);
46 EXPECT_EQ (invalid, 5);
47 }
48
49 TEST(CPP17APITests, test_is_valid)
50 {
51 string_view utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa";
52 bool bvalid = is_valid(utf_invalid);
53 EXPECT_FALSE (bvalid);
54 string_view utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";
55 bvalid = is_valid(utf8_with_surrogates);
56 EXPECT_TRUE (bvalid);
57 }
58
59 TEST(CPP17APITests, test_replace_invalid)
60 {
61 string_view invalid_sequence = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z";
62 string replace_invalid_result = replace_invalid(invalid_sequence, '?');
63 bool bvalid = is_valid(replace_invalid_result);
64 EXPECT_TRUE (bvalid);
65 const string fixed_invalid_sequence = "a????z";
66 EXPECT_EQ(fixed_invalid_sequence, replace_invalid_result);
67 }
68
69 TEST(CPP17APITests, test_starts_with_bom)
70 {
71 string byte_order_mark = {char(0xef), char(0xbb), char(0xbf)};
72 string_view byte_order_mark_view(byte_order_mark);
73 bool bbom = starts_with_bom(byte_order_mark_view);
74 EXPECT_TRUE (bbom);
75 string_view threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88";
76 bool no_bbom = starts_with_bom(threechars);
77 EXPECT_FALSE (no_bbom);
78 }
79
80 TEST(CPP17APITests, string_class_and_literals)
81 {
82 const char* twochars = u8"ab";
83 EXPECT_TRUE (is_valid(twochars));
84 const string two_chars_string(twochars);
85 EXPECT_TRUE (is_valid(two_chars_string));
86 }
87
88 #endif // C++ 11 or later