1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #include <ut/ut.h>
26 #include "../../micxx/string.h"
27 #include "../../common/common.h"
28 #include <base/strings.h>
29
30 using namespace mi;
31
32 namespace tt {
33 static void setUp4()
34 {
35 char* p = (char*) malloc(90);
36 free(p);
37 }
38 }
39
40 static void setUp()
41 {
42 tt::setUp4();
43 mike 1.1 }
44
45 static void cleanup()
46 {
47 }
48
49
50 static void TestCtor()
51 {
52 String s1(MI_T("string1"));
53
54 UT_ASSERT( Zcmp(s1.Str(), MI_T("string1")) == 0);
55
56 // reset value
57 s1 = 0;
58 UT_ASSERT( Zcmp(s1.Str(), MI_T("string1")) != 0);
59 UT_ASSERT( Zcmp(s1.Str(), MI_T("")) == 0);
60 }
61
62 static void TestCharWithSizeCtor()
63 {
64 mike 1.1 String s1(MI_T("string1"), 3);
65
66 UT_ASSERT( Zcmp(s1.Str(), MI_T("str")) == 0);
67 UT_ASSERT(s1.GetSize() == 3);
68 UT_ASSERT(s1 == MI_T("str"));
69 }
70
71 static void TestCharWithZeroSizeCtor()
72 {
73 String s1(MI_T("string1"), 0);
74
75 UT_ASSERT( Zcmp(s1.Str(), MI_T("")) == 0);
76 UT_ASSERT(s1.GetSize() == 0);
77 UT_ASSERT(s1 == MI_T(""));
78 }
79
80 static void TestNullCharWithZeroSizeCtor()
81 {
82 String s1(0, 0);
83
84 UT_ASSERT( Zcmp(s1.Str(), MI_T("")) == 0);
85 mike 1.1 UT_ASSERT(s1.GetSize() == 0);
86 UT_ASSERT(s1 == MI_T(""));
87 }
88
89 static void TestCopyCtor()
90 {
91 String s1(MI_T("str1"));
92 String s2 = s1;
93 String s3(s1);
94 String s4; s4 = s1;
95
96 // all strings has to be identical and point to the same memory block
97 UT_ASSERT( Zcmp(s1.Str(), MI_T("str1")) == 0);
98 UT_ASSERT( s1 == s2 );
99 UT_ASSERT( s1 == s3 );
100 UT_ASSERT( s1 == s4 );
101
102 UT_ASSERT( s1.Str() == s2.Str() );
103 UT_ASSERT( s1.Str() == s3.Str() );
104 UT_ASSERT( s1.Str() == s4.Str() );
105
106 mike 1.1 }
107
108 static void TestAssignmentToSelf()
109 {
110 String s1;
111
112 s1 = s1;
113 UT_ASSERT( s1 == MI_T("") );
114
115 s1 = MI_T("*");
116 s1 = s1;
117 UT_ASSERT( s1 == MI_T("*") );
118
119 }
120
121 static void TestRefCounting()
122 {
123 String s1(MI_T("str1"));
124 String s2 = s1;
125 String s3(s1);
126 String s4; s4 = s1;
127 mike 1.1
128 // assign the same string again - should re-allocate string, but keep contetn the same
129 s1 = MI_T("str1");
130
131 UT_ASSERT( s1 == s2 );
132 UT_ASSERT( s1 == s3 );
133 UT_ASSERT( s1 == s4 );
134
135 UT_ASSERT(s1.Str() != s2.Str());
136 UT_ASSERT(s2.Str() == s3.Str());
137 UT_ASSERT(s2.Str() == s4.Str());
138 }
139
140 static void TestComparison()
141 {
142 // compare == / !=
143 String s1 = MI_T("s1");
144
145 // compare to self
146 UT_ASSERT( s1 == s1 );
147 // even null
148 mike 1.1 String s_null;
149
150 UT_ASSERT( s_null == s_null );
151
152 String s2 = MI_T("s_2");
153
154 UT_ASSERT( s1 != s2 );
155
156 s2 = MI_T("s1");
157
158 UT_ASSERT(s1 == s2 );
159
160
161 UT_ASSERT( s1 == MI_T("s1") );
162 UT_ASSERT( s1 != MI_T("something else") );
163 }
164
165 static void TestComparisonOfEmptyStrings()
166 {
167 String s_null, s_empty = MI_T(""), s_not_empty = MI_T("123");
168
169 mike 1.1 // compare to self
170 UT_ASSERT( s_null == s_null );
171 UT_ASSERT( s_empty == s_empty );
172 UT_ASSERT( s_not_empty == s_not_empty );
173
174 // compare null
175 UT_ASSERT( s_null == s_empty );
176 UT_ASSERT( s_null != s_not_empty );
177
178 // compare empty
179 UT_ASSERT( s_empty != s_not_empty );
180
181 // compare to char*
182 UT_ASSERT( s_null == MI_T("") );
183 UT_ASSERT( s_empty == MI_T("") );
184 UT_ASSERT( s_not_empty == MI_T("123") );
185
186 }
187
188 static void TestGetSize()
189 {
190 mike 1.1 String s;
191
192 UT_ASSERT( s.GetSize() == 0 );
193
194 s = MI_T("123");
195
196 UT_ASSERT( s.GetSize() == 3 );
197 }
198
199 static void TestConcatString()
200 {
201 String s;
202
203 UT_ASSERT( s == MI_T("") );
204
205 s += MI_T("1");
206 UT_ASSERT( s == MI_T("1") );
207 UT_ASSERT( s.GetSize() == 1 );
208
209 s += String(MI_T("4"));
210 UT_ASSERT( s == MI_T("14") );
211 mike 1.1 UT_ASSERT( s.GetSize() == 2 );
212 }
213
214 static void TestConcatStringRefCounter()
215 {
216 // check that ref-counter works properly with += operator
217
218 String source1 = MI_T("source");
219 String source2 = MI_T("*");
220
221 String target = source2;
222
223 // they point to the same buffer
224 UT_ASSERT( target == source2 );
225 UT_ASSERT( target.Str() == source2.Str() );
226
227 const MI_Char* source2_p = source2.Str();
228
229 target += source1;
230 UT_ASSERT( target == MI_T("*source") );
231 UT_ASSERT( target.Str() != source2.Str() );
232 mike 1.1 UT_ASSERT( source2_p == source2.Str() );
233 UT_ASSERT( source2 == MI_T("*") );
234 }
235
236 static void TestConcatStringEmptyString()
237 {
238 String s1,s2;
239
240 s1 += String(MI_T("1"));
241 UT_ASSERT(s1 == MI_T("1"));
242 s1 += String(MI_T("234567890123456"));
243 UT_ASSERT(s1 == MI_T("1234567890123456"));
244 UT_ASSERT(s1.GetSize() == 16);
245 s1 += String();
246 UT_ASSERT(s1 == MI_T("1234567890123456"));
247 UT_ASSERT(s1.GetSize() == 16);
248
249 s2 += MI_T("1");
250 UT_ASSERT(s2 == MI_T("1"));
251 s2 += MI_T("234567890123456");
252 UT_ASSERT(s2 == MI_T("1234567890123456"));
253 mike 1.1 UT_ASSERT(s2.GetSize() == 16);
254 s2 += MI_T("");
255 UT_ASSERT(s2 == MI_T("1234567890123456"));
256 UT_ASSERT(s2.GetSize() == 16);
257
258 const MI_Char* null_str = 0;
259 s2 += null_str;
260 UT_ASSERT(s2 == MI_T("1234567890123456"));
261 UT_ASSERT(s2.GetSize() == 16);
262 }
263
264 static void RunTests()
265 {
266 UT_TEST(TestCtor);
267 UT_TEST(TestCopyCtor);
268 UT_TEST(TestCharWithSizeCtor);
269 UT_TEST(TestCharWithZeroSizeCtor);
270 UT_TEST(TestNullCharWithZeroSizeCtor);
271 UT_TEST(TestAssignmentToSelf);
272 UT_TEST(TestRefCounting);
273 UT_TEST(TestComparison);
274 mike 1.1 UT_TEST(TestComparisonOfEmptyStrings);
275 UT_TEST(TestGetSize);
276 UT_TEST(TestConcatString);
277 UT_TEST(TestConcatStringRefCounter);
278 UT_TEST(TestConcatStringEmptyString);
279 }
280
281 UT_ENTRY_POINT(RunTests);
|