Lolly 1.4.28
Loading...
Searching...
No Matches
string_u16_test.cpp
Go to the documentation of this file.
1/** \file lolly_string_test.cpp
2 * \copyright GPLv3
3 * \details Test for string
4 * \author jingkaimori
5 * \date 2024
6 */
7
8#include "a_lolly_test.hpp"
10#include <doctest/doctest.h>
11
12using namespace lolly::data;
13
14TEST_CASE ("equality of string") {
15 CHECK_EQ (string_u16 () == string_u16 (), true);
16
17 CHECK_EQ (string_u16 (u"abc") == u"abc", true);
18 CHECK_EQ (u"abc" == string_u16 (u"abc"), true);
19
20 CHECK_EQ (string_u16 (u"abc") == u"", false);
21 CHECK_EQ (u"" == string_u16 (u"abc"), false);
22
23 CHECK_EQ (string_u16 (u"abc") != u"abc", false);
24 CHECK_EQ (u"abc" != string_u16 (u"abc"), false);
25
26 CHECK_EQ (string_u16 (u"abc") != u"", true);
27 CHECK_EQ (u"" != string_u16 (u"abc"), true);
28
29 CHECK_EQ (string_u16 (u"abc") == string_u16 (u"abc"), true);
30 CHECK_EQ (string_u16 (u"abc") == string_u16 (), false);
31 CHECK_EQ (string_u16 (u"abc") != string_u16 (u"abc"), false);
32 CHECK_EQ (string_u16 (u"abc") != string_u16 (), true);
33}
34
35TEST_CASE ("compare string") {
36 CHECK (string_u16 (u"ab") < string_u16 (u"b"));
37 CHECK (!(string_u16 (u"b") < string_u16 (u"ab")));
38 CHECK (string_u16 () < string_u16 (u"0"));
39 CHECK (!(string_u16 (u"0") < string_u16 ()));
40 CHECK (string_u16 (u"a") <= string_u16 (u"a"));
41 CHECK (!(string_u16 (u"ab") <= string_u16 (u"a")));
42 CHECK (string_u16 (u"ab") <= string_u16 (u"b"));
43 CHECK (!(string_u16 (u"b") <= string_u16 (u"ab")));
44 CHECK (string_u16 () <= string_u16 ());
45 CHECK (string_u16 () <= string_u16 (u"0"));
46 CHECK (!(string_u16 (u"0") <= string_u16 ()));
47}
48
49TEST_CASE ("test slice") {
50 auto abcde= string_u16 (u"abcde");
51 CHECK_EQ (abcde (0, 0) == string_u16 (), true);
52 CHECK_EQ (abcde (0, 1) == string_u16 (u"a"), true);
53 CHECK_EQ (abcde (1, 3) (0, 1) == string_u16 (u"b"), true);
54 CHECK_EQ (abcde (0, 10) == string_u16 (u"abcde"), true);
55 CHECK_EQ (abcde (-1, 1) == string_u16 (u"a"), true);
56 CHECK_EQ (abcde (3, 2) == string_u16 (), true);
57 CHECK_EQ (abcde (3, -2) == string_u16 (), true);
58 CHECK_EQ (abcde (10, 11) == string_u16 (), true);
59 CHECK_EQ (abcde (-3, -2) == string_u16 (), true);
60
61 // string_view == string
62 CHECK_EQ (string_u16 (u"a") == abcde (0, 1), true);
63}
64
65TEST_CASE ("test concat") {
66 CHECK_EQ (string_u16 (u"abc") * u"de" == string_u16 (u"abcde"), true);
67 CHECK_EQ (string_u16 (u"abc") * string_u16 (u"de") == string_u16 (u"abcde"),
68 true);
69 CHECK_EQ (string_u16 (u"abc") * string_u16 (u"Hello") (0, 2) ==
70 string_u16 (u"abcHe"),
71 true);
72}
73
74/******************************************************************************
75 * Modifications
76 ******************************************************************************/
77
78TEST_CASE ("test append") {
79 auto str= string_u16 ();
80 str << u'x';
81 CHECK (str == u"x");
82 str << string_u16 (u"yz");
83 CHECK (str == u"xyz");
84}
85
86TEST_CASE ("test reserve along with append") {
87
88 SUBCASE ("reserved more space") {
89 auto str= string_u16 ();
90 str->reserve (6);
91 str << u'x';
92 CHECK_EQ (str == u"x", true);
93 str << string_u16 (u"yz");
94 CHECK_EQ (str == u"xyz", true);
95 str << string_u16 (u": larger than reserved space");
96 CHECK_EQ (str == u"xyz: larger than reserved space", true);
97 }
98 SUBCASE ("reserved the same space") {
99 auto str= string_u16 (u"abc");
100 str->reserve (3);
101 CHECK_EQ (str == u"abc", true);
102 }
103 SUBCASE ("reserved less space should take no effect") {
104 auto str= string_u16 (u"abc");
105 str->reserve (2);
106 CHECK_EQ (str == u"abc", true);
107 }
108}
109
110TEST_CASE ("test expand_or_shrink_by along with sub index") {
111
112 SUBCASE ("expand") {
113 auto str = string_u16 (u"abc");
114 int previous_n= str->expand_or_shrink_by (1);
115 CHECK_EQ (previous_n, 3);
116 str[3]= u'd';
117 CHECK_EQ (str == u"abcd", true);
118 }
119 SUBCASE ("shrink") {
120 auto str = string_u16 (u"abc");
121 int previous_n= str->expand_or_shrink_by (-1);
122 CHECK_EQ (previous_n, 3);
123 CHECK_EQ (str == u"ab", true);
124 }
125 SUBCASE ("delta 0 takes no effect") {
126 auto str = string_u16 (u"abc");
127 int previous_n= str->expand_or_shrink_by (0);
128 CHECK_EQ (previous_n, 3);
129 CHECK_EQ (str == u"abc", true);
130 }
131}
132
133TEST_CASE ("test resize") {
134
135 SUBCASE ("expand") {
136 auto str= string_u16 (u"abc");
137 str->resize (4);
138 str[3]= u'd';
139 CHECK_EQ (str == u"abcd", true);
140 }
141 SUBCASE ("shrink") {
142 auto str= string_u16 (u"abc");
143 str->resize (2);
144 CHECK_EQ (str == u"ab", true);
145 }
146 SUBCASE ("delta 0 takes no effect") {
147 auto str= string_u16 (u"abc");
148 str->resize (3);
149 CHECK_EQ (str == u"abc", true);
150 }
151}
152
153TEST_CASE ("test copy") {
154 string_u16 str1= u"str1";
156 CHECK (str1 == str2);
157 CHECK (str1.buffer () != str2.buffer ());
158
159 string_u16 str3= copy (str1 (0, 3));
160 CHECK (str3 == u"str");
161}
162
163TEST_CASE ("test iteration") {
164 string_u16 str1 = u"s\0tr1";
165 int expectedIndex= 0;
166 for (const auto element : str1) {
169 }
171}
The list class represents a linked list.
Definition list.hpp:48
int N(lolly_tree< T > t)
lolly_tree< T > copy(lolly_tree< T > t)
TEST_CASE("test for operator+= and advance()")