summaryrefslogtreecommitdiff
path: root/tests/src/CoreMangLib/cti/system/string/stringcopyto.cs
blob: b99dac16c32ba8806fabd943cf167e4840180ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
using System;
using System.Collections.Generic;
using System.Text;
using TestLibrary;


public class StringCopyTo
{
    private int c_MINI_STRING_LENGTH = 8;
    private int c_MAX_STRING_LENGTH = 256;
    public static int Main()
    {
        StringCopyTo sct = new StringCopyTo();
        TestLibrary.TestFramework.BeginTestCase("StringCopyTo");

        if (sct.RunTests())
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("PASS");
            return 100;
        }
        else
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("FAIL");
            return 0;
        }
    }

    public bool RunTests()
    {
        bool retVal = true;
        TestLibrary.TestFramework.LogInformation("[Postive]");
        retVal = PosTest1() && retVal;
        retVal = PosTest2() && retVal;
        retVal = PosTest3() && retVal;
        retVal = PosTest4() && retVal;
        retVal = PosTest5() && retVal;
        TestLibrary.TestFramework.LogInformation("[Negative]");
        retVal = NegTest1() && retVal;
        retVal = NegTest2() && retVal;
        retVal = NegTest3() && retVal;
        retVal = NegTest4() && retVal;
        retVal = NegTest5() && retVal;
        retVal = NegTest6() && retVal;
        retVal = NegTest7() && retVal;

        return retVal;
    }
    #region PositiveTesting
    public bool PosTest1()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;

        TestLibrary.TestFramework.BeginScenario("PosTest1:empty string excute CopyTo empty char array dst");

        try
        {
            strA = "";
            srcIndex = 0;
            dst = new char[] {};
            dstIndex  = 0;
            count = 0;
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            if(dst.Length != 0)
            {
                TestLibrary.TestFramework.LogError("001", "empty string excute CopyTo empty char array dst ExpectResult the dst is empty char array,ActualResult is " + dst + ")");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool PosTest2()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;

        TestLibrary.TestFramework.BeginScenario("PosTest2:In CoptTo method this string length equels to the char array length and copy all string to char array");

        try
        {
            string strBasic1 = TestLibrary.Generator.GetString(-55, false, c_MAX_STRING_LENGTH, c_MAX_STRING_LENGTH);
            string strBasic2 = TestLibrary.Generator.GetString(-55, false, c_MAX_STRING_LENGTH, c_MAX_STRING_LENGTH);
            strA = strBasic1;//"abc";
            srcIndex = 0;
            dst = strBasic2.ToCharArray();
            dstIndex = 0;
            count = strA.Length;
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            string dst2 = new string(dst);
            if (dst2.ToString () != strA.ToString())
            {
                TestLibrary.TestFramework.LogError("003", "In CoptTo method this string length equels to the char array length and copy all string to char array ExpectResult dst's element is replaced by strA's symbols ,ActualResult is " + dst2 + ")");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("004", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool PosTest3()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("PosTest3:In CoptTo method this string length equels to the char array length but copy count is 0");

        try
        {
            string strBasic1 = TestLibrary.Generator.GetString(-55, false, c_MAX_STRING_LENGTH, c_MAX_STRING_LENGTH);
            string strBasic2 = TestLibrary.Generator.GetString(-55, false, c_MAX_STRING_LENGTH, c_MAX_STRING_LENGTH);
            strA = strBasic1;
            srcIndex = 0;
            dst = strBasic2.ToCharArray();
            dstIndex = rand.Next(0, dst.Length + 1);
            count = 0;
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            string dst2 = new string(dst);
            if (dst2.ToString() != strBasic2.ToString())
            {
                TestLibrary.TestFramework.LogError("005", "In CoptTo method this string length equels to the char array length but copy count is 0 ExpectResult dst's elements do not change ,ActualResult is " + dst2 + ")");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool PosTest4()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("PosTest4:In CoptTo method this string length equels to the char array length and copy part of string to char arry");

        try
        {
            string strBasic1 = TestLibrary.Generator.GetString(-55, false, c_MAX_STRING_LENGTH, c_MAX_STRING_LENGTH);
            string strBasic2 = TestLibrary.Generator.GetString(-55, false, c_MAX_STRING_LENGTH, c_MAX_STRING_LENGTH);
            strA = strBasic1;
            srcIndex = 0;
            dst = strBasic2.ToCharArray();
            count = rand.Next(1,strA.Length);
            dstIndex = rand.Next(0, dst.Length - count);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            string dst2 = new string(dst);
            string dst3 = strBasic2.Substring(0,dstIndex) + strA.Substring(0,count) + strBasic2.Substring(dstIndex + count,dst.Length -dstIndex-count);
            if (dst2.ToString() !=dst3.ToString())
            {
                TestLibrary.TestFramework.LogError("007", "In CoptTo method this string length equels to the char array length and copy part of string to char array ExpectResult is "+dst3+",ActualResult is " + dst2 + ")");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("008", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool PosTest5()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("PosTest5:In CoptTo method copy part of string to the char array");

        try
        {
            string strBasic1 = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            string strBasic2 = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            strA = strBasic1;
            srcIndex = rand.Next(0,strA.Length+1);
            dst = strBasic2.ToCharArray();
            count = rand.Next(0,strA.Length+1 - srcIndex);
            if (dst.Length >= count)
            {
                dstIndex = rand.Next(0, dst.Length - count);
                strA.CopyTo(srcIndex, dst, dstIndex, count);
                string dst2 = new string(dst);
                string dst3 = strBasic2.Substring(0, dstIndex) + strA.Substring(srcIndex, count) + strBasic2.Substring(dstIndex + count, dst.Length - dstIndex - count);
                if (dst2.ToString() != dst3.ToString())
                {
                    TestLibrary.TestFramework.LogError("009", "In CoptTo method copy part of string to the char array ExpectResult is " + dst3 + ",ActualResult is " + dst2 + ")");
                    retVal = false;
                }
            }          
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("010", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }
    #endregion

    #region NegativeTesting
    public bool NegTest1()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("NegTest1:In CopyTo method destination is null");
        
        try
        {
            strA = TestLibrary.Generator.GetString(-55, false,c_MINI_STRING_LENGTH,c_MAX_STRING_LENGTH);
            srcIndex = TestLibrary.Generator.GetInt32(-55);
            dst = null;
            dstIndex = TestLibrary.Generator.GetInt32(-55);
            count = TestLibrary.Generator.GetInt32(-55);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            retVal = false;
        }
        catch(ArgumentNullException)
        {
        }
        catch(Exception e)
        {
            TestLibrary.TestFramework.LogError("N001","Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool NegTest2()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("NegTest2:In CopyTo method copy count is less than 0");
        
        try
        {
            strA = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            srcIndex = rand.Next(0, strA.Length + 1);
            string strB = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            dst = strB.ToCharArray();
            dstIndex = rand.Next(0, dst.Length + 1);
            count = rand.Next(1, strA.Length + 1 - srcIndex) * (-1);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("N002", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool NegTest3()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("NegTest3:In CopyTo method sourceIndex is less than 0");
        
        try
        {
            strA = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            srcIndex = rand.Next(1, strA.Length + 1) * (-1);
            string strB = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            dst = strB.ToCharArray();
            dstIndex = rand.Next(0, dst.Length + 1);
            count = rand.Next(0, strA.Length + 1 - srcIndex);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("N003", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool NegTest4()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("NegTest4:In CopyTo method copy count is greater source leng substract sourceIndex");

        try
        {
            strA = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            srcIndex = rand.Next(0, strA.Length + 1);
            string strB = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            dst = strB.ToCharArray();
            dstIndex = rand.Next(0, dst.Length + 1);
            count = rand.Next(strA.Length - srcIndex + 1, strA.Length);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("N004", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool NegTest5()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);
        
        TestLibrary.TestFramework.BeginScenario("NegTest5: In CopyTo method source Index is greater source length");
        
        try
        {
            strA = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            srcIndex = rand.Next(strA.Length + 1, strA.Length + 10);
            string strB = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            dst = strB.ToCharArray();
            dstIndex = rand.Next(0, dst.Length + 1);
            count = rand.Next(0,strA.Length +1);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("N005", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool NegTest6()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("NegTest6: In CopyTo method destination Index is less than 0");

        try
        {
            strA = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            srcIndex = rand.Next(0, strA.Length + 1);
            string strB = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            dst = strB.ToCharArray();
            dstIndex = rand.Next(1, dst.Length + 1) * (-1);
            count = rand.Next(0,strA.Length + 1 - srcIndex);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("N006", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }

    public bool NegTest7()
    {
        bool retVal = true;
        string strA;
        int srcIndex;
        char[] dst;
        int dstIndex;
        int count;
        Random rand = new Random(-55);

        TestLibrary.TestFramework.BeginScenario("NegTest7: In CopyTo method destination Index is greater destination length substract count");

        try
        {
            strA = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            srcIndex = rand.Next(0, strA.Length + 1);
            string strB = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            dst = strB.ToCharArray();
            count = rand.Next(0, strA.Length + 1 - srcIndex);
            dstIndex = rand.Next(dst.Length - count + 1, dst.Length + 1);
            strA.CopyTo(srcIndex, dst, dstIndex, count);
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("N007", "Unexpected exception:" + e);
            retVal = false;
        }
        return retVal;
    }
    #endregion

}