summaryrefslogtreecommitdiff
path: root/tests/src/CoreMangLib/cti/system/text/stringbuilder/stringbuilderremove.cs
blob: 24348dd6f4f188909d46eaf422714c36d7fc8b86 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;

/// <summary>
/// System.Text.StringBuilder.Remove
/// </summary>
public class StringBuilderRemove
{
    private const int c_MIN_STRING_LEN = 8;
    private const int c_MAX_STRING_LEN = 256;

    public static int Main()
    {
        StringBuilderRemove test = new StringBuilderRemove();

        TestLibrary.TestFramework.BeginTestCase("for Method:System.Text.StringBuilder.Remove(indexStart,length)");

        if (test.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("[Positive]");
        retVal = PosTest1() && retVal;
        retVal = PosTest2() && retVal;
        retVal = PosTest3() && retVal;
        retVal = PosTest4() && retVal;


        TestLibrary.TestFramework.LogInformation("[Negative]");
        retVal = NegTest1() && retVal;
        retVal = NegTest2() && retVal;
        retVal = NegTest3() && retVal;
        retVal = NegTest4() && retVal;
       
        return retVal;
    }

    
    public bool PosTest1()
    {
        bool    retVal      = true;

        string  oldString       = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LEN, c_MAX_STRING_LEN);
        int     startIndex      = TestLibrary.Generator.GetInt32(-55) % Math.Max(1,oldString.Length);
        int     removedLength   = TestLibrary.Generator.GetInt32(-55) % Math.Max(1,(oldString.Length-startIndex-1));
        string  newString        = oldString.Remove(startIndex, removedLength);

        TestLibrary.TestFramework.BeginScenario("PosTest1: Verify Remove subString form a StringBuilder ...");

        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);
        System.Text.StringBuilder removedStringBuilder = new System.Text.StringBuilder(newString);

        try
        {
           
            stringBuilder.Remove(startIndex,removedLength);

            int compareResult = string.CompareOrdinal(stringBuilder.ToString(), removedStringBuilder.ToString());

            if (compareResult != 0)
            {
                TestLibrary.TestFramework.LogError("001", "StringBuilder can't corrently remove");
                retVal = false;
            }
            
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }

    public bool PosTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest2:Verify StringBuilder Remove  itself ");

        string  oldString       = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LEN, c_MAX_STRING_LEN);
        int     startIndex      = 0;
        int     removedLength   = oldString.Length;

        try
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);
            stringBuilder.Remove(startIndex, removedLength);
            if (stringBuilder.Length != 0)
            {
                TestLibrary.TestFramework.LogError("003", "StringBuilder can't corrently remove itself");
                retVal = false;
            }
        }
        catch(Exception e)
        {
            TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }

    public bool PosTest3()
    {
        bool retVal = true;

        string  oldString       = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LEN, c_MAX_STRING_LEN);
        int     startIndex      = 0;
        int     removedLength   = TestLibrary.Generator.GetInt32(-55) % (oldString.Length - startIndex);
        string  newString       = oldString.Remove(startIndex, removedLength);


        TestLibrary.TestFramework.BeginScenario("PosTest3: Verify StringBuilder Remove form posization of  0 index ...");

        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);
        System.Text.StringBuilder removedStringBuilder = new System.Text.StringBuilder(newString);

        try
        {
            
            stringBuilder.Remove(startIndex, removedLength);

            int compareResult = string.CompareOrdinal(stringBuilder.ToString(), removedStringBuilder.ToString());

            if (compareResult != 0)
            {
                TestLibrary.TestFramework.LogError("005", "StringBuilder can't corrently remove from posization of 0 index");
                retVal = false;
            }

        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }

    public bool PosTest4()
    {
        bool retVal = true;
        string  oldString       = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LEN, c_MAX_STRING_LEN);
        int     startIndex      = TestLibrary.Generator.GetInt32(-55) % oldString.Length;
        int     removedLength   = 0;
        string  newString       = oldString.Remove(startIndex, removedLength);

        TestLibrary.TestFramework.BeginScenario("PosTest4: Verify StringBuilder Remove 0 length ...");

        try
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);
            System.Text.StringBuilder removedStringBuilder = new System.Text.StringBuilder(newString);
            stringBuilder.Remove(startIndex, removedLength);

            if (stringBuilder.ToString() != removedStringBuilder.ToString())
            {
                TestLibrary.TestFramework.LogError("007", "StringBuilder can't corrently Remove 0 length");
                retVal = false;
            }

        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("008", "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }
   
    public bool NegTest1()
    {
        bool retVal = true;

        const string c_TEST_DESC    = "NegTest1: StingBuilder length is 0 and length of removed larger than 0";
        const string c_TEST_ID      = "N001";


        
        string oldString = TestLibrary.Generator.GetString(-55, false, 0, 0);
        int startIndex = 0;
        int removedLength = 0;

        while (removedLength == 0)
        {
            removedLength = TestLibrary.Generator.GetInt32(-55);
        }

        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            stringBuilder.Remove(startIndex, removedLength);
            TestLibrary.TestFramework.LogError("009" + " TestId-" + c_TEST_ID, "ArgumentOutOfRangeException is not thrown as expected." );
            retVal = false;
        }
        catch (ArgumentException)
        { }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("010" + " TestId-" + c_TEST_ID, "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }

    public bool NegTest2()
    {
        bool retVal = true;

        const string c_TEST_DESC = "NegTest2: StingBuilder length is 0 and started index  larger than 0";
        const string c_TEST_ID = "N002";

        
        int startIndex      = 0;
        int removedLength   = 0;
        while (startIndex == 0)
        {
            startIndex = TestLibrary.Generator.GetInt32(-55);
        }
        string oldString = TestLibrary.Generator.GetString(-55, false, 0, 0);
        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);


        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            stringBuilder.Remove(startIndex, removedLength);
            TestLibrary.TestFramework.LogError("011" + " TestId-" + c_TEST_ID, "ArgumentOutOfRangeException is not thrown as expected.");
            retVal = false;
        }
        catch (ArgumentException)
        { }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("012" + " TestId-" + c_TEST_ID, "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }

    public bool NegTest3()
    {
        bool retVal = true;

        const string c_TEST_DESC = "NegTest3: length of Removed is  larger than length of StringBuilder ";
        const string c_TEST_ID = "N003";


        string oldString = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LEN, c_MAX_STRING_LEN);
        
        int startIndex = TestLibrary.Generator.GetInt32(-55);
        int removedLength = TestLibrary.Generator.GetInt32(-55);


        while (startIndex <= oldString.Length )
        {
            startIndex = TestLibrary.Generator.GetInt32(-55);
        }

        
        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);


        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            stringBuilder.Remove(startIndex, removedLength);
            TestLibrary.TestFramework.LogError("013" + " TestId-" + c_TEST_ID, "ArgumentOutOfRangeException is not thrown as expected.");
            retVal = false;
        }
        catch (ArgumentException)
        { }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("014" + " TestId-" + c_TEST_ID, "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }

    public bool NegTest4()
    {
        bool retVal = true;

        const string c_TEST_DESC = "NegTest4: Sum of length of Removed and index of started is  larger than length of StringBuilder ";
        const string c_TEST_ID = "N004";


        string oldString = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LEN, c_MAX_STRING_LEN);

        int removedLength = TestLibrary.Generator.GetInt32(-55);
        int startIndex = TestLibrary.Generator.GetInt32(-55) % (oldString.Length-removedLength);

        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(oldString);

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            stringBuilder.Remove(startIndex, removedLength);
            TestLibrary.TestFramework.LogError("015" + " TestId-" + c_TEST_ID, "ArgumentOutOfRangeException is not thrown as expected.");
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        { }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("016" + " TestId-" + c_TEST_ID, "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }
    
}