1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2016 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "template_test.h"
26
27 #include <cstring>
28 #include <iostream>
29 #include <sstream>
30
31 #include <CUnit/CUnit.h>
32
33 #include "template.h"
34
35 namespace nghttp2 {
36
test_template_immutable_string(void)37 void test_template_immutable_string(void) {
38 ImmutableString null;
39
40 CU_ASSERT("" == null);
41 CU_ASSERT(0 == null.size());
42 CU_ASSERT(null.empty());
43
44 ImmutableString from_cstr("alpha");
45
46 CU_ASSERT(0 == strcmp("alpha", from_cstr.c_str()));
47 CU_ASSERT(5 == from_cstr.size());
48 CU_ASSERT(!from_cstr.empty());
49 CU_ASSERT("alpha" == from_cstr);
50 CU_ASSERT(from_cstr == "alpha");
51 CU_ASSERT(std::string("alpha") == from_cstr);
52 CU_ASSERT(from_cstr == std::string("alpha"));
53
54 // copy constructor
55 ImmutableString src("charlie");
56 ImmutableString copy = src;
57
58 CU_ASSERT("charlie" == copy);
59 CU_ASSERT(7 == copy.size());
60
61 // copy assignment
62 ImmutableString copy2;
63 copy2 = src;
64
65 CU_ASSERT("charlie" == copy2);
66 CU_ASSERT(7 == copy2.size());
67
68 // move constructor
69 ImmutableString move = std::move(copy);
70
71 CU_ASSERT("charlie" == move);
72 CU_ASSERT(7 == move.size());
73 CU_ASSERT("" == copy);
74 CU_ASSERT(0 == copy.size());
75
76 // move assignment
77 move = std::move(from_cstr);
78
79 CU_ASSERT("alpha" == move);
80 CU_ASSERT(5 == move.size());
81 CU_ASSERT("" == from_cstr);
82 CU_ASSERT(0 == from_cstr.size());
83
84 // from string literal
85 auto from_lit = StringRef::from_lit("bravo");
86
87 CU_ASSERT("bravo" == from_lit);
88 CU_ASSERT(5 == from_lit.size());
89
90 // equality
91 ImmutableString eq("delta");
92
93 CU_ASSERT("delta1" != eq);
94 CU_ASSERT("delt" != eq);
95 CU_ASSERT(eq != "delta1");
96 CU_ASSERT(eq != "delt");
97
98 // operator[]
99 ImmutableString br_op("foxtrot");
100
101 CU_ASSERT('f' == br_op[0]);
102 CU_ASSERT('o' == br_op[1]);
103 CU_ASSERT('t' == br_op[6]);
104 CU_ASSERT('\0' == br_op[7]);
105
106 // operator==(const ImmutableString &, const ImmutableString &)
107 {
108 ImmutableString a("foo");
109 ImmutableString b("foo");
110 ImmutableString c("fo");
111
112 CU_ASSERT(a == b);
113 CU_ASSERT(a != c);
114 CU_ASSERT(c != b);
115 }
116
117 // operator<<
118 {
119 ImmutableString a("foo");
120 std::stringstream ss;
121 ss << a;
122
123 CU_ASSERT("foo" == ss.str());
124 }
125
126 // operator +=(std::string &, const ImmutableString &)
127 {
128 std::string a = "alpha";
129 a += ImmutableString("bravo");
130
131 CU_ASSERT("alphabravo" == a);
132 }
133 }
134
test_template_string_ref(void)135 void test_template_string_ref(void) {
136 StringRef empty;
137
138 CU_ASSERT("" == empty);
139 CU_ASSERT(0 == empty.size());
140
141 // from std::string
142 std::string alpha = "alpha";
143
144 StringRef ref(alpha);
145
146 CU_ASSERT("alpha" == ref);
147 CU_ASSERT(ref == "alpha");
148 CU_ASSERT(alpha == ref);
149 CU_ASSERT(ref == alpha);
150 CU_ASSERT(5 == ref.size());
151
152 // from string literal
153 auto from_lit = StringRef::from_lit("alpha");
154
155 CU_ASSERT("alpha" == from_lit);
156 CU_ASSERT(5 == from_lit.size());
157
158 // from ImmutableString
159 auto im = ImmutableString::from_lit("bravo");
160
161 StringRef imref(im);
162
163 CU_ASSERT("bravo" == imref);
164 CU_ASSERT(5 == imref.size());
165
166 // from C-string
167 StringRef cstrref("charlie");
168
169 CU_ASSERT("charlie" == cstrref);
170 CU_ASSERT(7 == cstrref.size());
171
172 // from C-string and its length
173 StringRef cstrnref("delta", 5);
174
175 CU_ASSERT("delta" == cstrnref);
176 CU_ASSERT(5 == cstrnref.size());
177
178 // operator[]
179 StringRef br_op("foxtrot");
180
181 CU_ASSERT('f' == br_op[0]);
182 CU_ASSERT('o' == br_op[1]);
183 CU_ASSERT('t' == br_op[6]);
184 CU_ASSERT('\0' == br_op[7]);
185
186 // operator<<
187 {
188 StringRef a("foo");
189 std::stringstream ss;
190 ss << a;
191
192 CU_ASSERT("foo" == ss.str());
193 }
194
195 // operator +=(std::string &, const StringRef &)
196 {
197 std::string a = "alpha";
198 a += StringRef("bravo");
199
200 CU_ASSERT("alphabravo" == a);
201 }
202 }
203
204 } // namespace nghttp2
205