1//
2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// Official repository: https://github.com/boostorg/beast
8//
9
10// Test that header file is self-contained.
11#include <boost/beast/core/static_string.hpp>
12
13#include <boost/beast/_experimental/unit_test/suite.hpp>
14
15namespace boost {
16namespace beast {
17
18class static_string_test : public beast::unit_test::suite
19{
20public:
21 void
22 testConstruct()
23 {
24 {
25 static_string<1> s;
26 BEAST_EXPECT(s.empty());
27 BEAST_EXPECT(s.size() == 0);
28 BEAST_EXPECT(s == "");
29 BEAST_EXPECT(*s.end() == 0);
30 }
31 {
32 static_string<4> s1(3, 'x');
33 BEAST_EXPECT(! s1.empty());
34 BEAST_EXPECT(s1.size() == 3);
35 BEAST_EXPECT(s1 == "xxx");
36 BEAST_EXPECT(*s1.end() == 0);
37 try
38 {
39 static_string<2> s2(3, 'x');
40 fail(reason: "", __FILE__, __LINE__);
41 }
42 catch(std::length_error const&)
43 {
44 pass();
45 }
46 }
47 {
48 static_string<5> s1("12345");
49 BEAST_EXPECT(*s1.end() == 0);
50 static_string<3> s2(s1, 2);
51 BEAST_EXPECT(s2 == "345");
52 BEAST_EXPECT(*s2.end() == 0);
53 static_string<0> s3(s1, 5);
54 BEAST_EXPECT(s3.empty());
55 BEAST_EXPECT(s3.front() == 0);
56 BEAST_EXPECT(*s3.end() == 0);
57 }
58 {
59 static_string<5> s1("12345");
60 static_string<2> s2(s1, 1, 2);
61 BEAST_EXPECT(s2 == "23");
62 BEAST_EXPECT(*s2.end() == 0);
63 static_string<0> s3(s1, 5, 1);
64 BEAST_EXPECT(s3.empty());
65 BEAST_EXPECT(s3.front() == 0);
66 BEAST_EXPECT(*s3.end() == 0);
67 try
68 {
69 static_string<5> s4(s1, 6);
70 fail(reason: "", __FILE__, __LINE__);
71 }
72 catch(std::out_of_range const&)
73 {
74 pass();
75 }
76 }
77 {
78 static_string<5> s1("UVXYZ", 3);
79 BEAST_EXPECT(s1 == "UVX");
80 BEAST_EXPECT(*s1.end() == 0);
81 static_string<5> s2("X\0""Y\0""Z", 3);
82 BEAST_EXPECT(std::memcmp(
83 s2.data(), "X\0""Y", 3) == 0);
84 BEAST_EXPECT(*s2.end() == 0);
85 }
86 {
87 static_string<5> s1("12345");
88 static_string<3> s2(
89 s1.begin() + 1, s1.begin() + 3);
90 BEAST_EXPECT(s2 == "23");
91 BEAST_EXPECT(*s2.end() == 0);
92 }
93 {
94 static_string<5> s1("12345");
95 static_string<5> s2(s1);
96 BEAST_EXPECT(s2 == "12345");
97 BEAST_EXPECT(*s2.end() == 0);
98 static_string<6> s3(s1);
99 BEAST_EXPECT(s3 == "12345");
100 BEAST_EXPECT(*s3.end() == 0);
101 try
102 {
103 static_string<4> s4(s1);
104 fail(reason: "", __FILE__, __LINE__);
105 }
106 catch(std::length_error const&)
107 {
108 pass();
109 }
110 }
111 {
112 static_string<3> s1({'1', '2', '3'});
113 BEAST_EXPECT(s1 == "123");
114 BEAST_EXPECT(*s1.end() == 0);
115 BEAST_EXPECT(
116 static_string<0>({}) == static_string<0>());
117 try
118 {
119 static_string<2> s2({'1', '2', '3'});
120 fail(reason: "", __FILE__, __LINE__);
121 }
122 catch(std::length_error const&)
123 {
124 pass();
125 }
126 }
127 {
128 static_string<3> s1(
129 string_view("123"));
130 BEAST_EXPECT(s1 == "123");
131 BEAST_EXPECT(*s1.end() == 0);
132 try
133 {
134 static_string<2> s2(
135 string_view("123"));
136 fail(reason: "", __FILE__, __LINE__);
137 }
138 catch(std::length_error const&)
139 {
140 pass();
141 }
142 }
143 {
144 static_string<5> s1(
145 std::string("12345"), 2, 2);
146 BEAST_EXPECT(s1 == "34");
147 BEAST_EXPECT(*s1.end() == 0);
148 try
149 {
150 static_string<2> s2(
151 std::string("12345"), 1, 3);
152 fail(reason: "", __FILE__, __LINE__);
153 }
154 catch(std::length_error const&)
155 {
156 pass();
157 }
158 }
159 }
160
161 void
162 testAssign()
163 {
164 {
165 static_string<3> s1("123");
166 static_string<3> s2;
167 s2 = s1;
168 BEAST_EXPECT(s2 == "123");
169 BEAST_EXPECT(*s2.end() == 0);
170 }
171 {
172 static_string<3> s1("123");
173 static_string<5> s2;
174 s2 = s1;
175 BEAST_EXPECT(s2 == "123");
176 BEAST_EXPECT(*s2.end() == 0);
177 try
178 {
179 static_string<1> s3;
180 s3 = s1;
181 fail(reason: "", __FILE__, __LINE__);
182 }
183 catch(std::length_error const&)
184 {
185 pass();
186 }
187 }
188 {
189 static_string<3> s1;
190 s1 = "123";
191 BEAST_EXPECT(s1 == "123");
192 BEAST_EXPECT(*s1.end() == 0);
193 try
194 {
195 static_string<1> s2;
196 s2 = "123";
197 fail(reason: "", __FILE__, __LINE__);
198 }
199 catch(std::length_error const&)
200 {
201 pass();
202 }
203 }
204 {
205 static_string<1> s1;
206 s1 = 'x';
207 BEAST_EXPECT(s1 == "x");
208 BEAST_EXPECT(*s1.end() == 0);
209 try
210 {
211 static_string<0> s2;
212 s2 = 'x';
213 fail(reason: "", __FILE__, __LINE__);
214 }
215 catch(std::length_error const&)
216 {
217 pass();
218 }
219 }
220 {
221 static_string<3> s1;
222 s1 = {'1', '2', '3'};
223 BEAST_EXPECT(s1 == "123");
224 BEAST_EXPECT(*s1.end() == 0);
225 try
226 {
227 static_string<1> s2;
228 s2 = {'1', '2', '3'};
229 fail(reason: "", __FILE__, __LINE__);
230 }
231 catch(std::length_error const&)
232 {
233 pass();
234 }
235 }
236 {
237 static_string<3> s1;
238 s1 = string_view("123");
239 BEAST_EXPECT(s1 == "123");
240 BEAST_EXPECT(*s1.end() == 0);
241 try
242 {
243 static_string<1> s2;
244 s2 = string_view("123");
245 fail(reason: "", __FILE__, __LINE__);
246 }
247 catch(std::length_error const&)
248 {
249 pass();
250 }
251 }
252
253 {
254 static_string<4> s1;
255 s1.assign(count: 3, ch: 'x');
256 BEAST_EXPECT(s1 == "xxx");
257 BEAST_EXPECT(*s1.end() == 0);
258 try
259 {
260 static_string<2> s2;
261 s2.assign(count: 3, ch: 'x');
262 fail(reason: "", __FILE__, __LINE__);
263 }
264 catch(std::length_error const&)
265 {
266 pass();
267 }
268 }
269 {
270 static_string<5> s1("12345");
271 BEAST_EXPECT(*s1.end() == 0);
272 static_string<5> s2;
273 s2.assign(s: s1);
274 BEAST_EXPECT(s2 == "12345");
275 BEAST_EXPECT(*s2.end() == 0);
276 }
277 {
278 static_string<5> s1("12345");
279 BEAST_EXPECT(*s1.end() == 0);
280 static_string<7> s2;
281 s2.assign(s: s1);
282 BEAST_EXPECT(s2 == "12345");
283 BEAST_EXPECT(*s2.end() == 0);
284 try
285 {
286 static_string<3> s3;
287 s3.assign(s: s1);
288 fail(reason: "", __FILE__, __LINE__);
289 }
290 catch(std::length_error const&)
291 {
292 pass();
293 }
294 }
295 {
296 static_string<5> s1("12345");
297 static_string<5> s2;
298 s2.assign(s: s1, pos: 1);
299 BEAST_EXPECT(s2 == "2345");
300 BEAST_EXPECT(*s2.end() == 0);
301 s2.assign(s: s1, pos: 1, count: 2);
302 BEAST_EXPECT(s2 == "23");
303 BEAST_EXPECT(*s2.end() == 0);
304 s2.assign(s: s1, pos: 1, count: 100);
305 BEAST_EXPECT(s2 == "2345");
306 BEAST_EXPECT(*s2.end() == 0);
307 try
308 {
309 s2.assign(s: s1, pos: 6);
310 fail(reason: "", __FILE__, __LINE__);
311 }
312 catch(std::out_of_range const&)
313 {
314 pass();
315 }
316 try
317 {
318 static_string<3> s3;
319 s3.assign(s: s1, pos: 1);
320 fail(reason: "", __FILE__, __LINE__);
321 }
322 catch(std::length_error const&)
323 {
324 pass();
325 }
326 }
327 {
328 static_string<5> s1;
329 s1.assign(s: "12");
330 BEAST_EXPECT(s1 == "12");
331 BEAST_EXPECT(*s1.end() == 0);
332 s1.assign(s: "12345");
333 BEAST_EXPECT(s1 == "12345");
334 BEAST_EXPECT(*s1.end() == 0);
335 }
336 {
337 static_string<5> s1;
338 s1.assign(s: "12345", count: 3);
339 BEAST_EXPECT(s1 == "123");
340 BEAST_EXPECT(*s1.end() == 0);
341 }
342 {
343 static_string<5> s1("12345");
344 static_string<3> s2;
345 s2.assign(first: s1.begin(), last: s1.begin() + 2);
346 BEAST_EXPECT(s2 == "12");
347 BEAST_EXPECT(*s2.end() == 0);
348 try
349 {
350 s2.assign(first: s1.begin(), last: s1.end());
351 fail(reason: "", __FILE__, __LINE__);
352 }
353 catch(std::length_error const&)
354 {
355 pass();
356 }
357 }
358 {
359 static_string<5> s1;
360 s1.assign(ilist: {'1', '2', '3'});
361 BEAST_EXPECT(s1 == "123");
362 BEAST_EXPECT(*s1.end() == 0);
363 try
364 {
365 static_string<1> s2;
366 s2.assign(ilist: {'1', '2', '3'});
367 fail(reason: "", __FILE__, __LINE__);
368 }
369 catch(std::length_error const&)
370 {
371 pass();
372 }
373 }
374 {
375 static_string<5> s1;
376 s1.assign(t: string_view("123"));
377 BEAST_EXPECT(s1 == "123");
378 BEAST_EXPECT(*s1.end() == 0);
379 s1.assign(t: string_view("12345"));
380 BEAST_EXPECT(s1 == "12345");
381 BEAST_EXPECT(*s1.end() == 0);
382 try
383 {
384 s1.assign(t: string_view("1234567"));
385 fail(reason: "", __FILE__, __LINE__);
386 }
387 catch(std::length_error const&)
388 {
389 pass();
390 }
391 }
392 {
393 static_string<5> s1;
394 s1.assign(t: std::string("12345"), pos: 2, count: 2);
395 BEAST_EXPECT(s1 == "34");
396 BEAST_EXPECT(*s1.end() == 0);
397 s1.assign(t: std::string("12345"), pos: 3);
398 BEAST_EXPECT(s1 == "45");
399 BEAST_EXPECT(*s1.end() == 0);
400 try
401 {
402 static_string<2> s2;
403 s2.assign(
404 t: std::string("12345"), pos: 1, count: 3);
405 fail(reason: "", __FILE__, __LINE__);
406 }
407 catch(std::length_error const&)
408 {
409 pass();
410 }
411 }
412 }
413
414 void
415 testAccess()
416 {
417 {
418 static_string<5> s("12345");
419 BEAST_EXPECT(s.at(1) == '2');
420 BEAST_EXPECT(s.at(4) == '5');
421 try
422 {
423 BEAST_EXPECT(s.at(5) == 0);
424 fail(reason: "", __FILE__, __LINE__);
425 }
426 catch(std::out_of_range const&)
427 {
428 pass();
429 }
430 }
431 {
432 static_string<5> const s("12345");
433 BEAST_EXPECT(s.at(1) == '2');
434 BEAST_EXPECT(s.at(4) == '5');
435 try
436 {
437 BEAST_EXPECT(s.at(5) == 0);
438 fail(reason: "", __FILE__, __LINE__);
439 }
440 catch(std::out_of_range const&)
441 {
442 pass();
443 }
444 }
445 {
446 static_string<5> s("12345");
447 BEAST_EXPECT(s[1] == '2');
448 BEAST_EXPECT(s[4] == '5');
449 s[1] = '_';
450 BEAST_EXPECT(s == "1_345");
451 }
452 {
453 static_string<5> const s("12345");
454 BEAST_EXPECT(s[1] == '2');
455 BEAST_EXPECT(s[4] == '5');
456 BEAST_EXPECT(s[5] == 0);
457 }
458 {
459 static_string<3> s("123");
460 BEAST_EXPECT(s.front() == '1');
461 BEAST_EXPECT(s.back() == '3');
462 s.front() = '_';
463 BEAST_EXPECT(s == "_23");
464 s.back() = '_';
465 BEAST_EXPECT(s == "_2_");
466 }
467 {
468 static_string<3> const s("123");
469 BEAST_EXPECT(s.front() == '1');
470 BEAST_EXPECT(s.back() == '3');
471 }
472 {
473 static_string<3> s("123");
474 BEAST_EXPECT(std::memcmp(
475 s.data(), "123", 3) == 0);
476 }
477 {
478 static_string<3> const s("123");
479 BEAST_EXPECT(std::memcmp(
480 s.data(), "123", 3) == 0);
481 }
482 {
483 static_string<3> s("123");
484 BEAST_EXPECT(std::memcmp(
485 s.c_str(), "123\0", 4) == 0);
486 }
487 {
488 static_string<3> s("123");
489 string_view sv = s;
490 BEAST_EXPECT(static_string<5>(sv) == "123");
491 }
492 }
493
494 void
495 testIterators()
496 {
497 {
498 static_string<3> s;
499 BEAST_EXPECT(std::distance(
500 s.begin(), s.end()) == 0);
501 BEAST_EXPECT(std::distance(
502 s.rbegin(), s.rend()) == 0);
503 s = "123";
504 BEAST_EXPECT(std::distance(
505 s.begin(), s.end()) == 3);
506 BEAST_EXPECT(std::distance(
507 s.rbegin(), s.rend()) == 3);
508 }
509 {
510 static_string<3> const s("123");
511 BEAST_EXPECT(std::distance(
512 s.begin(), s.end()) == 3);
513 BEAST_EXPECT(std::distance(
514 s.cbegin(), s.cend()) == 3);
515 BEAST_EXPECT(std::distance(
516 s.rbegin(), s.rend()) == 3);
517 BEAST_EXPECT(std::distance(
518 s.crbegin(), s.crend()) == 3);
519 }
520 }
521
522 void
523 testCapacity()
524 {
525 static_string<3> s;
526 BEAST_EXPECT(s.empty());
527 BEAST_EXPECT(s.size() == 0);
528 BEAST_EXPECT(s.length() == 0);
529 BEAST_EXPECT(s.max_size() == 3);
530 BEAST_EXPECT(s.capacity() == 3);
531 s = "123";
532 BEAST_EXPECT(! s.empty());
533 BEAST_EXPECT(s.size() == 3);
534 BEAST_EXPECT(s.length() == 3);
535 s.reserve(n: 0);
536 s.reserve(n: 3);
537 try
538 {
539 s.reserve(n: 4);
540 fail(reason: "", __FILE__, __LINE__);
541 }
542 catch(std::length_error const&)
543 {
544 pass();
545 }
546 s.shrink_to_fit();
547 BEAST_EXPECT(! s.empty());
548 BEAST_EXPECT(s.size() == 3);
549 BEAST_EXPECT(s.length() == 3);
550 BEAST_EXPECT(*s.end() == 0);
551 }
552
553 void
554 testOperations()
555 {
556 //
557 // clear
558 //
559
560 {
561 static_string<3> s("123");
562 s.clear();
563 BEAST_EXPECT(s.empty());
564 BEAST_EXPECT(*s.end() == 0);
565 }
566
567 //
568 // insert
569 //
570
571 {
572 // Using 7 as the size causes a miscompile in MSVC14.2 x64 Release
573 static_string<8> s1("12345");
574 s1.insert(index: 2, count: 2, ch: '_');
575 BEAST_EXPECT(s1 == "12__345");
576 BEAST_EXPECT(*s1.end() == 0);
577 try
578 {
579 static_string<6> s2("12345");
580 s2.insert(index: 2, count: 2, ch: '_');
581 fail(reason: "", __FILE__, __LINE__);
582 }
583 catch(std::length_error const&)
584 {
585 pass();
586 }
587 try
588 {
589 static_string<6> s2("12345");
590 s2.insert(index: 6, count: 2, ch: '_');
591 fail(reason: "", __FILE__, __LINE__);
592 }
593 catch(std::out_of_range const&)
594 {
595 pass();
596 }
597 }
598 {
599 static_string<7> s1("12345");
600 s1.insert(index: 2, s: "__");
601 BEAST_EXPECT(s1 == "12__345");
602 BEAST_EXPECT(*s1.end() == 0);
603 try
604 {
605 static_string<6> s2("12345");
606 s2.insert(index: 2, s: "__");
607 fail(reason: "", __FILE__, __LINE__);
608 }
609 catch(std::length_error const&)
610 {
611 pass();
612 }
613 try
614 {
615 static_string<6> s2("12345");
616 s2.insert(index: 6, s: "__");
617 fail(reason: "", __FILE__, __LINE__);
618 }
619 catch(std::out_of_range const&)
620 {
621 pass();
622 }
623 }
624 {
625 static_string<7> s1("12345");
626 s1.insert(index: 2, s: "TUV", count: 2);
627 BEAST_EXPECT(s1 == "12TU345");
628 BEAST_EXPECT(*s1.end() == 0);
629 try
630 {
631 static_string<6> s2("12345");
632 s2.insert(index: 2, s: "TUV", count: 2);
633 fail(reason: "", __FILE__, __LINE__);
634 }
635 catch(std::length_error const&)
636 {
637 pass();
638 }
639 try
640 {
641 static_string<6> s2("12345");
642 s2.insert(index: 6, s: "TUV", count: 2);
643 fail(reason: "", __FILE__, __LINE__);
644 }
645 catch(std::out_of_range const&)
646 {
647 pass();
648 }
649 }
650 {
651 static_string<7> s1("12345");
652 s1.insert(index: 2, str: static_string<3>("TU"));
653 BEAST_EXPECT(s1 == "12TU345");
654 BEAST_EXPECT(*s1.end() == 0);
655 try
656 {
657 static_string<6> s2("12345");
658 s2.insert(index: 2, str: static_string<3>("TUV"));
659 fail(reason: "", __FILE__, __LINE__);
660 }
661 catch(std::length_error const&)
662 {
663 pass();
664 }
665 try
666 {
667 static_string<6> s2("12345");
668 s2.insert(index: 6, str: static_string<3>("TUV"));
669 fail(reason: "", __FILE__, __LINE__);
670 }
671 catch(std::out_of_range const&)
672 {
673 pass();
674 }
675 }
676 {
677 static_string<7> s1("12345");
678 s1.insert(index: 2, str: static_string<3>("TUV"), index_str: 1);
679 BEAST_EXPECT(s1 == "12UV345");
680 BEAST_EXPECT(*s1.end() == 0);
681 s1 = "12345";
682 s1.insert(index: 2, str: static_string<3>("TUV"), index_str: 1, count: 1);
683 BEAST_EXPECT(s1 == "12U345");
684 BEAST_EXPECT(*s1.end() == 0);
685 try
686 {
687 static_string<6> s2("12345");
688 s2.insert(index: 2, str: static_string<3>("TUV"), index_str: 1, count: 2);
689 fail(reason: "", __FILE__, __LINE__);
690 }
691 catch(std::length_error const&)
692 {
693 pass();
694 }
695 try
696 {
697 static_string<6> s2("12345");
698 s2.insert(index: 6, str: static_string<3>("TUV"), index_str: 1, count: 2);
699 fail(reason: "", __FILE__, __LINE__);
700 }
701 catch(std::out_of_range const&)
702 {
703 pass();
704 }
705 }
706 {
707 static_string<4> s1("123");
708 s1.insert(pos: s1.begin() + 1, ch: '_');
709 BEAST_EXPECT(s1 == "1_23");
710 BEAST_EXPECT(*s1.end() == 0);
711 try
712 {
713 static_string<3> s2("123");
714 s2.insert(pos: s2.begin() + 1, ch: '_');
715 fail(reason: "", __FILE__, __LINE__);
716 }
717 catch(std::length_error const&)
718 {
719 pass();
720 }
721 }
722 {
723 static_string<4> s1("12");
724 s1.insert(pos: s1.begin() + 1, count: 2, ch: '_');
725 BEAST_EXPECT(s1 == "1__2");
726 BEAST_EXPECT(*s1.end() == 0);
727 try
728 {
729 static_string<4> s2("123");
730 s2.insert(pos: s2.begin() + 1, count: 2, ch: ' ');
731 fail(reason: "", __FILE__, __LINE__);
732 }
733 catch(std::length_error const&)
734 {
735 pass();
736 }
737 }
738 {
739 static_string<3> s1("123");
740 static_string<5> s2("UV");
741 s2.insert(pos: s2.begin() + 1, first: s1.begin(), last: s1.end());
742 BEAST_EXPECT(s2 == "U123V");
743 BEAST_EXPECT(*s2.end() == 0);
744 try
745 {
746 static_string<4> s3("UV");
747 s3.insert(pos: s3.begin() + 1, first: s1.begin(), last: s1.end());
748 fail(reason: "", __FILE__, __LINE__);
749 }
750 catch(std::length_error const&)
751 {
752 pass();
753 }
754 }
755 {
756 static_string<5> s1("123");
757 s1.insert(index: 1, t: string_view("UV"));
758 BEAST_EXPECT(s1 == "1UV23");
759 BEAST_EXPECT(*s1.end() == 0);
760 try
761 {
762 static_string<4> s2("123");
763 s2.insert(index: 1, t: string_view("UV"));
764 fail(reason: "", __FILE__, __LINE__);
765 }
766 catch(std::length_error const&)
767 {
768 pass();
769 }
770 try
771 {
772 static_string<5> s2("123");
773 s2.insert(index: 5, t: string_view("UV"));
774 fail(reason: "", __FILE__, __LINE__);
775 }
776 catch(std::out_of_range const&)
777 {
778 pass();
779 }
780 }
781 {
782 static_string<5> s1("123");
783 s1.insert(index: 1, t: std::string("UV"));
784 BEAST_EXPECT(s1 == "1UV23");
785 BEAST_EXPECT(*s1.end() == 0);
786 try
787 {
788 s1.insert(index: 1, t: std::string("UV"));
789 fail(reason: "", __FILE__, __LINE__);
790 }
791 catch(std::length_error const&)
792 {
793 pass();
794 }
795 }
796 {
797 static_string<6> s1("123");
798 s1.insert(index: 1, t: std::string("UVX"), index_str: 1);
799 BEAST_EXPECT(s1 == "1VX23");
800 BEAST_EXPECT(*s1.end() == 0);
801 s1.insert(index: 4, t: std::string("PQR"), index_str: 1, count: 1);
802 BEAST_EXPECT(s1 == "1VX2Q3");
803 BEAST_EXPECT(*s1.end() == 0);
804 try
805 {
806 s1.insert(index: 4, t: std::string("PQR"), index_str: 1, count: 1);
807 fail(reason: "", __FILE__, __LINE__);
808 }
809 catch(std::length_error const&)
810 {
811 pass();
812 }
813 }
814
815 //
816 // erase
817 //
818
819 {
820 static_string<9> s1("123456789");
821 BEAST_EXPECT(s1.erase(1, 1) == "13456789");
822 BEAST_EXPECT(s1 == "13456789");
823 BEAST_EXPECT(*s1.end() == 0);
824 BEAST_EXPECT(s1.erase(5) == "13456");
825 BEAST_EXPECT(s1 == "13456");
826 BEAST_EXPECT(*s1.end() == 0);
827 try
828 {
829 s1.erase(index: 7);
830 fail(reason: "", __FILE__, __LINE__);
831 }
832 catch(std::out_of_range const&)
833 {
834 pass();
835 }
836 }
837 {
838 static_string<9> s1("123456789");
839 BEAST_EXPECT(*s1.erase(s1.begin() + 5) == '7');
840 BEAST_EXPECT(s1 == "12345789");
841 BEAST_EXPECT(*s1.end() == 0);
842 }
843 {
844 static_string<9> s1("123456789");
845 BEAST_EXPECT(*s1.erase(
846 s1.begin() + 5, s1.begin() + 7) == '8');
847 BEAST_EXPECT(s1 == "1234589");
848 BEAST_EXPECT(*s1.end() == 0);
849 }
850
851 //
852 // push_back
853 //
854
855 {
856 static_string<3> s1("12");
857 s1.push_back(ch: '3');
858 BEAST_EXPECT(s1 == "123");
859 try
860 {
861 s1.push_back(ch: '4');
862 fail(reason: "", __FILE__, __LINE__);
863 }
864 catch(std::length_error const&)
865 {
866 pass();
867 }
868 static_string<0> s2;
869 try
870 {
871 s2.push_back(ch: '_');
872 fail(reason: "", __FILE__, __LINE__);
873 }
874 catch(std::length_error const&)
875 {
876 pass();
877 }
878 }
879
880 //
881 // pop_back
882 //
883
884 {
885 static_string<3> s1("123");
886 s1.pop_back();
887 BEAST_EXPECT(s1 == "12");
888 BEAST_EXPECT(*s1.end() == 0);
889 s1.pop_back();
890 BEAST_EXPECT(s1 == "1");
891 BEAST_EXPECT(*s1.end() == 0);
892 s1.pop_back();
893 BEAST_EXPECT(s1.empty());
894 BEAST_EXPECT(*s1.end() == 0);
895 }
896
897 //
898 // append
899 //
900
901 {
902 static_string<3> s1("1");
903 s1.append(count: 2, ch: '_');
904 BEAST_EXPECT(s1 == "1__");
905 BEAST_EXPECT(*s1.end() == 0);
906 try
907 {
908 static_string<2> s2("1");
909 s2.append(count: 2, ch: '_');
910 fail(reason: "", __FILE__, __LINE__);
911 }
912 catch(std::length_error const&)
913 {
914 pass();
915 }
916 }
917 {
918 static_string<2> s1("__");
919 static_string<3> s2("1");
920 s2.append(s: s1);
921 BEAST_EXPECT(s2 == "1__");
922 BEAST_EXPECT(*s2.end() == 0);
923 try
924 {
925 static_string<2> s3("1");
926 s3.append(s: s1);
927 fail(reason: "", __FILE__, __LINE__);
928 }
929 catch(std::length_error const&)
930 {
931 pass();
932 }
933 }
934 {
935 static_string<3> s1("XYZ");
936 static_string<4> s2("12");
937 s2.append(s: s1, pos: 1);
938 BEAST_EXPECT(s2 == "12YZ");
939 BEAST_EXPECT(*s2.end() == 0);
940 static_string<3> s3("12");
941 s3.append(s: s1, pos: 1, count: 1);
942 BEAST_EXPECT(s3 == "12Y");
943 BEAST_EXPECT(*s3.end() == 0);
944 {
945 static_string<3> s4("12");
946 s4.append(s: s1, pos: 3);
947 BEAST_EXPECT(s4 == "12");
948 }
949 try
950 {
951 static_string<3> s4("12");
952 s4.append(s: s1, pos: 4);
953 fail(reason: "", __FILE__, __LINE__);
954 }
955 catch(std::out_of_range const&)
956 {
957 pass();
958 }
959 try
960 {
961 static_string<3> s4("12");
962 s4.append(s: s1, pos: 1);
963 fail(reason: "", __FILE__, __LINE__);
964 }
965 catch(std::length_error const&)
966 {
967 pass();
968 }
969 }
970 {
971 static_string<4> s1("12");
972 s1.append(s: "XYZ", count: 2);
973 BEAST_EXPECT(s1 == "12XY");
974 BEAST_EXPECT(*s1.end() == 0);
975 try
976 {
977 static_string<3> s3("12");
978 s3.append(s: "XYZ", count: 2);
979 fail(reason: "", __FILE__, __LINE__);
980 }
981 catch(std::length_error const&)
982 {
983 pass();
984 }
985 }
986 {
987 static_string<5> s1("12");
988 s1.append(s: "XYZ");
989 BEAST_EXPECT(s1 == "12XYZ");
990 BEAST_EXPECT(*s1.end() == 0);
991 try
992 {
993 static_string<4> s2("12");
994 s2.append(s: "XYZ");
995 fail(reason: "", __FILE__, __LINE__);
996 }
997 catch(std::length_error const&)
998 {
999 pass();
1000 }
1001 }
1002 {
1003 static_string<3> s1("XYZ");
1004 static_string<5> s2("12");
1005 s2.append(first: s1.begin(), last: s1.end());
1006 BEAST_EXPECT(s2 == "12XYZ");
1007 BEAST_EXPECT(*s2.end() == 0);
1008 try
1009 {
1010 static_string<4> s3("12");
1011 s3.append(first: s1.begin(), last: s1.end());
1012 fail(reason: "", __FILE__, __LINE__);
1013 }
1014 catch(std::length_error const&)
1015 {
1016 pass();
1017 }
1018 }
1019 {
1020 static_string<5> s1("123");
1021 s1.append(ilist: {'X', 'Y'});
1022 BEAST_EXPECT(s1 == "123XY");
1023 BEAST_EXPECT(*s1.end() == 0);
1024 try
1025 {
1026 static_string<4> s2("123");
1027 s2.append(ilist: {'X', 'Y'});
1028 fail(reason: "", __FILE__, __LINE__);
1029 }
1030 catch(std::length_error const&)
1031 {
1032 pass();
1033 }
1034 }
1035 {
1036 string_view s1("XYZ");
1037 static_string<5> s2("12");
1038 s2.append(t: s1);
1039 BEAST_EXPECT(s2 == "12XYZ");
1040 BEAST_EXPECT(*s2.end() == 0);
1041 try
1042 {
1043 static_string<4> s3("12");
1044 s3.append(t: s1);
1045 fail(reason: "", __FILE__, __LINE__);
1046 }
1047 catch(std::length_error const&)
1048 {
1049 pass();
1050 }
1051 }
1052 {
1053 static_string<6> s1("123");
1054 s1.append(t: std::string("UVX"), pos: 1);
1055 BEAST_EXPECT(s1 == "123VX");
1056 BEAST_EXPECT(*s1.end() == 0);
1057 s1.append(t: std::string("PQR"), pos: 1, count: 1);
1058 BEAST_EXPECT(s1 == "123VXQ");
1059 BEAST_EXPECT(*s1.end() == 0);
1060 try
1061 {
1062 static_string<3> s2("123");
1063 s2.append(t: std::string("PQR"), pos: 1, count: 1);
1064 fail(reason: "", __FILE__, __LINE__);
1065 }
1066 catch(std::length_error const&)
1067 {
1068 pass();
1069 }
1070 }
1071
1072 //
1073 // operator+=
1074 //
1075
1076 {
1077 static_string<2> s1("__");
1078 static_string<3> s2("1");
1079 s2 += s1;
1080 BEAST_EXPECT(s2 == "1__");
1081 BEAST_EXPECT(*s2.end() == 0);
1082 try
1083 {
1084 static_string<2> s3("1");
1085 s3 += s1;
1086 fail(reason: "", __FILE__, __LINE__);
1087 }
1088 catch(std::length_error const&)
1089 {
1090 pass();
1091 }
1092 }
1093 {
1094 static_string<3> s1("12");
1095 s1 += '3';
1096 BEAST_EXPECT(s1 == "123");
1097 try
1098 {
1099 s1 += '4';
1100 fail(reason: "", __FILE__, __LINE__);
1101 }
1102 catch(std::length_error const&)
1103 {
1104 pass();
1105 }
1106 }
1107 {
1108 static_string<4> s1("12");
1109 s1 += "34";
1110 BEAST_EXPECT(s1 == "1234");
1111 try
1112 {
1113 s1 += "5";
1114 fail(reason: "", __FILE__, __LINE__);
1115 }
1116 catch(std::length_error const&)
1117 {
1118 pass();
1119 }
1120 }
1121 {
1122 static_string<4> s1("12");
1123 s1 += {'3', '4'};
1124 BEAST_EXPECT(s1 == "1234");
1125 try
1126 {
1127 s1 += {'5'};
1128 fail(reason: "", __FILE__, __LINE__);
1129 }
1130 catch(std::length_error const&)
1131 {
1132 pass();
1133 }
1134 }
1135 {
1136 string_view s1("34");
1137 static_string<4> s2("12");
1138 s2 += s1;
1139 BEAST_EXPECT(s2 == "1234");
1140 try
1141 {
1142 s2 += s1;
1143 fail(reason: "", __FILE__, __LINE__);
1144 }
1145 catch(std::length_error const&)
1146 {
1147 pass();
1148 }
1149 }
1150 }
1151
1152 void
1153 testCompare()
1154 {
1155 using str1 = static_string<1>;
1156 using str2 = static_string<2>;
1157 {
1158 str1 s1;
1159 str2 s2;
1160 s1 = "1";
1161 s2 = "22";
1162 BEAST_EXPECT(s1.compare(s2) < 0);
1163 BEAST_EXPECT(s2.compare(s1) > 0);
1164 BEAST_EXPECT(s1 < "10");
1165 BEAST_EXPECT(s2 > "1");
1166 BEAST_EXPECT("10" > s1);
1167 BEAST_EXPECT("1" < s2);
1168 BEAST_EXPECT(s1 < "20");
1169 BEAST_EXPECT(s2 > "1");
1170 BEAST_EXPECT(s2 > "2");
1171 }
1172 {
1173 str2 s1("x");
1174 str2 s2("x");
1175 BEAST_EXPECT(s1 == s2);
1176 BEAST_EXPECT(s1 <= s2);
1177 BEAST_EXPECT(s1 >= s2);
1178 BEAST_EXPECT(! (s1 < s2));
1179 BEAST_EXPECT(! (s1 > s2));
1180 BEAST_EXPECT(! (s1 != s2));
1181 }
1182 {
1183 str1 s1("x");
1184 str2 s2("x");
1185 BEAST_EXPECT(s1 == s2);
1186 BEAST_EXPECT(s1 <= s2);
1187 BEAST_EXPECT(s1 >= s2);
1188 BEAST_EXPECT(! (s1 < s2));
1189 BEAST_EXPECT(! (s1 > s2));
1190 BEAST_EXPECT(! (s1 != s2));
1191 }
1192 {
1193 str2 s("x");
1194 BEAST_EXPECT(s == "x");
1195 BEAST_EXPECT(s <= "x");
1196 BEAST_EXPECT(s >= "x");
1197 BEAST_EXPECT(! (s < "x"));
1198 BEAST_EXPECT(! (s > "x"));
1199 BEAST_EXPECT(! (s != "x"));
1200 BEAST_EXPECT("x" == s);
1201 BEAST_EXPECT("x" <= s);
1202 BEAST_EXPECT("x" >= s);
1203 BEAST_EXPECT(! ("x" < s));
1204 BEAST_EXPECT(! ("x" > s));
1205 BEAST_EXPECT(! ("x" != s));
1206 }
1207 {
1208 str2 s("x");
1209 BEAST_EXPECT(s <= "y");
1210 BEAST_EXPECT(s < "y");
1211 BEAST_EXPECT(s != "y");
1212 BEAST_EXPECT(! (s == "y"));
1213 BEAST_EXPECT(! (s >= "y"));
1214 BEAST_EXPECT(! (s > "x"));
1215 BEAST_EXPECT("y" >= s);
1216 BEAST_EXPECT("y" > s);
1217 BEAST_EXPECT("y" != s);
1218 BEAST_EXPECT(! ("y" == s));
1219 BEAST_EXPECT(! ("y" <= s));
1220 BEAST_EXPECT(! ("y" < s));
1221 }
1222 {
1223 str1 s1("x");
1224 str2 s2("y");
1225 BEAST_EXPECT(s1 <= s2);
1226 BEAST_EXPECT(s1 < s2);
1227 BEAST_EXPECT(s1 != s2);
1228 BEAST_EXPECT(! (s1 == s2));
1229 BEAST_EXPECT(! (s1 >= s2));
1230 BEAST_EXPECT(! (s1 > s2));
1231 }
1232 {
1233 str1 s1("x");
1234 str2 s2("xx");
1235 BEAST_EXPECT(s1 < s2);
1236 BEAST_EXPECT(s2 > s1);
1237 }
1238 {
1239 str1 s1("x");
1240 str2 s2("yy");
1241 BEAST_EXPECT(s1 < s2);
1242 BEAST_EXPECT(s2 > s1);
1243 }
1244 }
1245
1246 void
1247 testSwap()
1248 {
1249 {
1250 static_string<3> s1("123");
1251 static_string<3> s2("XYZ");
1252 swap(lhs&: s1, rhs&: s2);
1253 BEAST_EXPECT(s1 == "XYZ");
1254 BEAST_EXPECT(*s1.end() == 0);
1255 BEAST_EXPECT(s2 == "123");
1256 BEAST_EXPECT(*s2.end() == 0);
1257 static_string<3> s3("UV");
1258 swap(lhs&: s2, rhs&: s3);
1259 BEAST_EXPECT(s2 == "UV");
1260 BEAST_EXPECT(*s2.end() == 0);
1261 BEAST_EXPECT(s3 == "123");
1262 BEAST_EXPECT(*s3.end() == 0);
1263 }
1264 {
1265 static_string<5> s1("123");
1266 static_string<7> s2("XYZ");
1267 swap(lhs&: s1, rhs&: s2);
1268 BEAST_EXPECT(s1 == "XYZ");
1269 BEAST_EXPECT(*s1.end() == 0);
1270 BEAST_EXPECT(s2 == "123");
1271 BEAST_EXPECT(*s2.end() == 0);
1272 static_string<3> s3("UV");
1273 swap(lhs&: s2, rhs&: s3);
1274 BEAST_EXPECT(s2 == "UV");
1275 BEAST_EXPECT(*s2.end() == 0);
1276 BEAST_EXPECT(s3 == "123");
1277 BEAST_EXPECT(*s3.end() == 0);
1278 try
1279 {
1280 static_string<5> s4("12345");
1281 static_string<3> s5("XYZ");
1282 swap(lhs&: s4, rhs&: s5);
1283 fail(reason: "", __FILE__, __LINE__);
1284 }
1285 catch(std::length_error const&)
1286 {
1287 pass();
1288 }
1289 try
1290 {
1291 static_string<3> s4("XYZ");
1292 static_string<5> s5("12345");
1293 swap(lhs&: s4, rhs&: s5);
1294 fail(reason: "", __FILE__, __LINE__);
1295 }
1296 catch(std::length_error const&)
1297 {
1298 pass();
1299 }
1300 }
1301 }
1302
1303 void
1304 testGeneral()
1305 {
1306 using str1 = static_string<1>;
1307 using str2 = static_string<2>;
1308 {
1309 str1 s1;
1310 BEAST_EXPECT(s1 == "");
1311 BEAST_EXPECT(s1.empty());
1312 BEAST_EXPECT(s1.size() == 0);
1313 BEAST_EXPECT(s1.max_size() == 1);
1314 BEAST_EXPECT(s1.capacity() == 1);
1315 BEAST_EXPECT(s1.begin() == s1.end());
1316 BEAST_EXPECT(s1.cbegin() == s1.cend());
1317 BEAST_EXPECT(s1.rbegin() == s1.rend());
1318 BEAST_EXPECT(s1.crbegin() == s1.crend());
1319 try
1320 {
1321 BEAST_EXPECT(s1.at(0) == 0);
1322 fail();
1323 }
1324 catch(std::exception const&)
1325 {
1326 pass();
1327 }
1328 BEAST_EXPECT(s1.data()[0] == 0);
1329 BEAST_EXPECT(*s1.c_str() == 0);
1330 BEAST_EXPECT(std::distance(s1.begin(), s1.end()) == 0);
1331 BEAST_EXPECT(std::distance(s1.cbegin(), s1.cend()) == 0);
1332 BEAST_EXPECT(std::distance(s1.rbegin(), s1.rend()) == 0);
1333 BEAST_EXPECT(std::distance(s1.crbegin(), s1.crend()) == 0);
1334 BEAST_EXPECT(s1.compare(s1) == 0);
1335 }
1336 {
1337 str1 const s1;
1338 BEAST_EXPECT(s1 == "");
1339 BEAST_EXPECT(s1.empty());
1340 BEAST_EXPECT(s1.size() == 0);
1341 BEAST_EXPECT(s1.max_size() == 1);
1342 BEAST_EXPECT(s1.capacity() == 1);
1343 BEAST_EXPECT(s1.begin() == s1.end());
1344 BEAST_EXPECT(s1.cbegin() == s1.cend());
1345 BEAST_EXPECT(s1.rbegin() == s1.rend());
1346 BEAST_EXPECT(s1.crbegin() == s1.crend());
1347 try
1348 {
1349 BEAST_EXPECT(s1.at(0) == 0);
1350 fail();
1351 }
1352 catch(std::exception const&)
1353 {
1354 pass();
1355 }
1356 BEAST_EXPECT(s1.data()[0] == 0);
1357 BEAST_EXPECT(*s1.c_str() == 0);
1358 BEAST_EXPECT(std::distance(s1.begin(), s1.end()) == 0);
1359 BEAST_EXPECT(std::distance(s1.cbegin(), s1.cend()) == 0);
1360 BEAST_EXPECT(std::distance(s1.rbegin(), s1.rend()) == 0);
1361 BEAST_EXPECT(std::distance(s1.crbegin(), s1.crend()) == 0);
1362 BEAST_EXPECT(s1.compare(s1) == 0);
1363 }
1364 {
1365 str1 s1;
1366 str1 s2("x");
1367 BEAST_EXPECT(s2 == "x");
1368 BEAST_EXPECT(s2[0] == 'x');
1369 BEAST_EXPECT(s2.at(0) == 'x');
1370 BEAST_EXPECT(s2.front() == 'x');
1371 BEAST_EXPECT(s2.back() == 'x');
1372 str1 const s3(s2);
1373 BEAST_EXPECT(s3 == "x");
1374 BEAST_EXPECT(s3[0] == 'x');
1375 BEAST_EXPECT(s3.at(0) == 'x');
1376 BEAST_EXPECT(s3.front() == 'x');
1377 BEAST_EXPECT(s3.back() == 'x');
1378 s2 = "y";
1379 BEAST_EXPECT(s2 == "y");
1380 BEAST_EXPECT(s3 == "x");
1381 s1 = s2;
1382 BEAST_EXPECT(s1 == "y");
1383 s1.clear();
1384 BEAST_EXPECT(s1.empty());
1385 BEAST_EXPECT(s1.size() == 0);
1386 }
1387 {
1388 str2 s1("x");
1389 str1 s2(s1);
1390 BEAST_EXPECT(s2 == "x");
1391 str1 s3;
1392 s3 = s2;
1393 BEAST_EXPECT(s3 == "x");
1394 s1 = "xy";
1395 BEAST_EXPECT(s1.size() == 2);
1396 BEAST_EXPECT(s1[0] == 'x');
1397 BEAST_EXPECT(s1[1] == 'y');
1398 BEAST_EXPECT(s1.at(0) == 'x');
1399 BEAST_EXPECT(s1.at(1) == 'y');
1400 BEAST_EXPECT(s1.front() == 'x');
1401 BEAST_EXPECT(s1.back() == 'y');
1402 auto const s4 = s1;
1403 BEAST_EXPECT(s4[0] == 'x');
1404 BEAST_EXPECT(s4[1] == 'y');
1405 BEAST_EXPECT(s4.at(0) == 'x');
1406 BEAST_EXPECT(s4.at(1) == 'y');
1407 BEAST_EXPECT(s4.front() == 'x');
1408 BEAST_EXPECT(s4.back() == 'y');
1409 try
1410 {
1411 s3 = s1;
1412 fail();
1413 }
1414 catch(std::exception const&)
1415 {
1416 pass();
1417 }
1418 try
1419 {
1420 str1 s5(s1);
1421 fail();
1422 }
1423 catch(std::exception const&)
1424 {
1425 pass();
1426 }
1427 }
1428 {
1429 str1 s1("x");
1430 str2 s2;
1431 s2 = s1;
1432 try
1433 {
1434 s1.resize(n: 2);
1435 fail();
1436 }
1437 catch(std::length_error const&)
1438 {
1439 pass();
1440 }
1441 }
1442 pass();
1443 }
1444
1445 void
1446 testToStaticString()
1447 {
1448 BEAST_EXPECT(to_static_string<long>(0) == "0");
1449 BEAST_EXPECT(to_static_string<long>(1) == "1");
1450 BEAST_EXPECT(to_static_string<long>(0xffff) == "65535");
1451 BEAST_EXPECT(to_static_string<long>(0x10000) == "65536");
1452 BEAST_EXPECT(to_static_string<long long>(0xffffffff) == "4294967295");
1453
1454 BEAST_EXPECT(to_static_string<long>(-1) == "-1");
1455 BEAST_EXPECT(to_static_string<long>(-65535) == "-65535");
1456 BEAST_EXPECT(to_static_string<long>(-65536) == "-65536");
1457 BEAST_EXPECT(to_static_string<long long>(-4294967295ll) == "-4294967295");
1458
1459 BEAST_EXPECT(to_static_string<unsigned long>(0) == "0");
1460 BEAST_EXPECT(to_static_string<unsigned long>(1) == "1");
1461 BEAST_EXPECT(to_static_string<unsigned long>(0xffff) == "65535");
1462 BEAST_EXPECT(to_static_string<unsigned long>(0x10000) == "65536");
1463 BEAST_EXPECT(to_static_string<unsigned long>(0xffffffff) == "4294967295");
1464 }
1465
1466 void
1467 run() override
1468 {
1469 testConstruct();
1470 testAssign();
1471 testAccess();
1472 testIterators();
1473 testCapacity();
1474 testOperations();
1475 testCompare();
1476 testSwap();
1477
1478 testGeneral();
1479 testToStaticString();
1480 }
1481};
1482
1483BEAST_DEFINE_TESTSUITE(beast,core,static_string);
1484
1485} // beast
1486} // boost
1487

source code of boost/libs/beast/test/beast/core/static_string.cpp