summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Directed/StrAccess/straccess4.cs
blob: 0974034ad11ba0dd5548d3c63214c229e1266eb6 (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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

//random length and random content string
//IndexOutOfRangeException

using System;

internal struct VT
{
    public String str;
}

internal class CL
{
    public String str;
}

internal class StrAccess1
{
    public static String str1;
    public static Random rand = new Random();

    private static int randomUnicodeLetterOrDigit()
    {
        int c = (char)rand.Next((int)Char.MinValue, (int)Char.MaxValue);
        while (!Char.IsLetterOrDigit((char)c))
            c = rand.Next((int)Char.MinValue, (int)Char.MaxValue);
        return c;
    }

    private static string randomUnicodeString(int len)
    {
        string str = "";
        while (len-- >= 0)
            str += "\\u" + randomUnicodeLetterOrDigit().ToString("X4");
        return str;
    }

    public static int Main(string[] args)
    {
        bool passed;

        string teststr = "";
        int len = 0;

        if (args.Length != 0)
        {
            teststr = args[0];
            len = teststr.Length;
        }
        else
        {
            //construct random string with random length
            len = rand.Next(50);
            teststr = randomUnicodeString(len);
        }

        Console.WriteLine("Test string is {0}", teststr);

        String str2 = "";
        CL cl1 = new CL();
        VT vt1;

        str1 = str2 = cl1.str = vt1.str = teststr;

        String[] str1darr = new String[len];
        for (int j = 0; j < len; j++)
            str1darr[j] = Convert.ToString(teststr[j]);

        char b0, b1, b2, b3, b4;
        try
        {
            passed = false;
            b0 = cl1.str[len];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b1 = str1[len];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b2 = str2[len];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b3 = vt1.str[len];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b4 = Convert.ToChar(str1darr[len]);
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }

        try
        {
            passed = false;
            b0 = cl1.str[-1];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b1 = str1[-1];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b2 = str2[-1];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b3 = vt1.str[-1];
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }
        try
        {
            passed = false;
            b4 = Convert.ToChar(str1darr[-1]);
        }
        catch (IndexOutOfRangeException)
        {
            passed = true;
        }

        int i;
        while (len != 0)
        {
            i = rand.Next(0, len);
            b0 = cl1.str[i];
            b1 = str1[i];
            b2 = str2[i];
            b3 = vt1.str[i];
            b4 = Convert.ToChar(str1darr[i]);
            if ((b0 != b1) || (b1 != b2) || (b2 != b3) || (b3 != b4))
                passed = false;
            len /= 2;
        }

        Console.WriteLine();
        if (!passed)
        {
            Console.WriteLine("FAILED");
            Console.WriteLine("Use the following command to repro:");
            Console.WriteLine("straccess3.exe {0}", teststr);
            return 1;
        }
        else
        {
            Console.WriteLine("PASSED");
            return 100;
        }
    }
}