1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 module tanya.container.tests..string;
5 
6 import tanya.container.string;
7 import tanya.test.assertion;
8 
9 @nogc nothrow pure @safe unittest
10 {
11     auto s = String(0, 'K');
12     assert(s.length == 0);
13 }
14 
15 // Allocates enough space for 3-byte character.
16 @nogc pure @safe unittest
17 {
18     String s;
19     s.insertBack('\u8100');
20 }
21 
22 @nogc pure @safe unittest
23 {
24     assertThrown!UTFException(() => String(1, cast(dchar) 0xd900));
25     assertThrown!UTFException(() => String(1, cast(wchar) 0xd900));
26 }
27 
28 @nogc nothrow pure @safe unittest
29 {
30     auto s1 = String("Buttercup");
31     auto s2 = String("Cap");
32     s2[] = s1[6 .. $];
33     assert(s2 == "cup");
34 }
35 
36 @nogc nothrow pure @safe unittest
37 {
38     auto s1 = String("Wow");
39     s1[] = 'a';
40     assert(s1 == "aaa");
41 }
42 
43 @nogc nothrow pure @safe unittest
44 {
45     auto s1 = String("ö");
46     s1[] = "oe";
47     assert(s1 == "oe");
48 }
49 
50 // Postblit works
51 @nogc nothrow pure @safe unittest
52 {
53     void internFunc(String arg)
54     {
55     }
56     void middleFunc(S...)(S args)
57     {
58         foreach (arg; args)
59         {
60             internFunc(arg);
61         }
62     }
63     void topFunc(String args)
64     {
65         middleFunc(args);
66     }
67     topFunc(String("asdf"));
68 }
69 
70 // Const range produces mutable ranges
71 @nogc pure @safe unittest
72 {
73     auto s = const String("И снизу лед, и сверху - маюсь между.");
74     {
75         const constRange = s[];
76 
77         auto fromConstRange = constRange[];
78         fromConstRange.popFront();
79         assert(fromConstRange.front == s[1]);
80 
81         fromConstRange = constRange[0 .. $];
82         fromConstRange.popFront();
83         assert(fromConstRange.front == s[1]);
84 
85         assert(constRange.get() is s.get());
86     }
87     {
88         const constRange = s.byCodePoint();
89 
90         auto fromConstRange = constRange[];
91         fromConstRange.popFront();
92         assert(fromConstRange.front == ' ');
93     }
94 }
95 
96 // Can pop multibyte characters
97 @nogc pure @safe unittest
98 {
99     auto s = String("\U00024B62\U00002260");
100     auto range = s.byCodePoint();
101 
102     range.popFront();
103     assert(!range.empty);
104 
105     range.popFront();
106     assert(range.empty);
107 
108     range = s.byCodePoint();
109     range.popFront();
110     s[$ - 3] = 0xf0;
111     assertThrown!UTFException(&(range.popFront));
112 }
113 
114 // Inserts own char range correctly
115 @nogc nothrow pure @safe unittest
116 {
117     auto s1 = String(`ü`);
118     String s2;
119     s2.insertBack(s1[]);
120     assert(s1 == s2);
121 }