summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Methodical/refany/format.cs
blob: 7b4c90cacc377062d69f0edd06d7a72ed9e5e865 (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
// 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;

namespace JitTest
{
    internal enum Mood
    {
        good,
        bad,
        worse
    }

    internal class TestClass
    {
        private static String Format(TypedReference format, TypedReference _ref)
        {
            int length = __refvalue(format, String).Length;
            char[] chars = __refvalue(format, String).ToCharArray();

            String result = "";
            int arg = 0;
            for (int I = 0; I < length; I++)
            {
                if (chars[I] != '%')
                    result += chars[I];
                else
                {
                    I++;
                    if (I >= length)
                        throw new Exception();

                    bool FALSE = false;
                    bool TRUE = true;
                    TypedReference bLong = __makeref(FALSE);
                    if (chars[I] == 'l')
                    {
                        bLong = __makeref(TRUE);
                        I++;
                        if (I >= length)
                            throw new Exception();
                    }

                    if (arg++ == 1)
                        throw new Exception();

                    switch (chars[I])
                    {
                        case 'b':
                            if (__refvalue(bLong, bool))
                                throw new Exception();
                            if (__reftype(_ref) != typeof(bool))
                                throw new Exception();
                            if (__refvalue(_ref, bool))
                                result += "true";
                            else
                                result += "false";
                            break;

                        case 'd':
                            if (__refvalue(bLong, bool))
                            {
                                if (__reftype(_ref) != typeof(long))
                                    throw new Exception();
                                result += __refvalue(_ref, long).ToString();
                            }
                            else
                            {
                                if (__reftype(_ref) != typeof(int))
                                    throw new Exception();
                                result += __refvalue(_ref, int).ToString();
                            }
                            break;

                        case 'u':
                            if (__refvalue(bLong, bool))
                            {
                                if (__reftype(_ref) != typeof(UInt64))
                                    throw new Exception();
                                result += __refvalue(_ref, ulong).ToString();
                            }
                            else
                            {
                                if (__reftype(_ref) != typeof(uint))
                                    throw new Exception();
                                result += __refvalue(_ref, uint).ToString();
                            }
                            break;

                        case 'f':
                            if (__refvalue(bLong, bool))
                            {
                                if (__reftype(_ref) != typeof(double))
                                    throw new Exception();
                                result += __refvalue(_ref, double).ToString();
                            }
                            else
                            {
                                if (__reftype(_ref) != typeof(float))
                                    throw new Exception();
                                result += __refvalue(_ref, float).ToString();
                            }
                            break;

                        case 's':
                            if (__refvalue(bLong, bool))
                                throw new Exception();
                            if (__reftype(_ref) != typeof(String))
                                throw new Exception();
                            result += __refvalue(_ref, String) != null ? __refvalue(_ref, String) : "(null)";
                            break;

                        case 't':
                            if (__refvalue(bLong, bool))
                                throw new Exception();
                            if (__reftype(_ref) != typeof(DateTime))
                                throw new Exception();
                            result += __refvalue(_ref, DateTime).ToString();
                            break;

                        case 'p':
                            if (__refvalue(bLong, bool))
                                throw new Exception();
                            if (__reftype(_ref) != typeof(PlatformID))
                                throw new Exception();
                            result += __refvalue(_ref, PlatformID).ToString();
                            break;

                        case 'e':
                            if (__refvalue(bLong, bool))
                                throw new Exception();
                            if (__reftype(_ref) != typeof(Mood))
                                throw new Exception();
                            switch (__refvalue(_ref, Mood))
                            {
                                case Mood.good:
                                    result += "good";
                                    break;
                                case Mood.bad:
                                    result += "bad";
                                    break;
                                case Mood.worse:
                                    result += "worse";
                                    break;
                                default:
                                    throw new Exception();
                            }
                            break;

                        default:
                            throw new Exception();
                    }
                }
            }
            return result;
        }

        private static void Test(String format, TypedReference arg, String result)
        {
            String s = Format(__makeref(format), arg);
            if (s != result)
            {
                throw new Exception();
            }
        }

        private static void TestLocals()
        {
            int d = 10;
            uint u = 11u;
            long l = 12;
            ulong ul = 13u;
            float f = 14.0f;
            double dbl = 15.0d;
            bool b = true;
            DateTime t = new DateTime(100, 10, 1);
            PlatformID pid = PlatformID.Win32NT;
            Mood mood = Mood.good;
            Test("{%d}", __makeref(d), "{10}");
            Test("{%u}", __makeref(u), "{11}");
            Test("{%ld}", __makeref(l), "{12}");
            Test("{%lu}", __makeref(ul), "{13}");
            Test("{%f}", __makeref(f), "{14}");
            Test("{%lf}", __makeref(dbl), "{15}");
            Test("{%b}", __makeref(b), "{true}");
            Test("{%t}", __makeref(t), "{" + t.ToString() + "}");
            Test("{%p}", __makeref(pid), "{Win32NT}");
            Test("{%e}", __makeref(mood), "{good}");
        }

        private int _m_d = 20;
        private static uint s_m_u = 21u;
        private long _m_l = 22;
        private static ulong s_m_ul = 23u;
        private float _m_f = 24.0f;
        private double _m_dbl = 25.0d;
        private bool _m_b = false;
        private static DateTime s_m_t = new DateTime(100, 10, 1);
        private PlatformID _m_pid = PlatformID.Win32NT;
        private Mood _m_mood = Mood.good;

        private void TestFields()
        {
            Test("{%d}", __makeref(_m_d), "{20}");
            Test("{%u}", __makeref(s_m_u), "{21}");
            Test("{%ld}", __makeref(_m_l), "{22}");
            Test("{%lu}", __makeref(s_m_ul), "{23}");
            Test("{%f}", __makeref(_m_f), "{24}");
            Test("{%lf}", __makeref(_m_dbl), "{25}");
            Test("{%b}", __makeref(_m_b), "{false}");
            Test("{%t}", __makeref(s_m_t), "{" + s_m_t.ToString() + "}");
            Test("{%p}", __makeref(_m_pid), "{Win32NT}");
            Test("{%e}", __makeref(_m_mood), "{good}");
        }

        private static void DoTestArgSlots(ref int d, ref uint u, ref long l,
            ref ulong ul, ref float f, ref double dbl, ref bool b,
            ref DateTime t, ref PlatformID pid)
        {
            Test("{%d}", __makeref(d), "{20}");
            Test("{%u}", __makeref(u), "{21}");
            Test("{%ld}", __makeref(l), "{22}");
            Test("{%lu}", __makeref(ul), "{23}");
            Test("{%f}", __makeref(f), "{24}");
            Test("{%lf}", __makeref(dbl), "{25}");
            Test("{%b}", __makeref(b), "{false}");
            Test("{%t}", __makeref(t), "{" + t.ToString() + "}");
            Test("{%p}", __makeref(pid), "{2}");
        }

        private static void TestArgSlots()
        {
            int d = 20;
            uint u = 21u;
            long l = 22;
            ulong ul = 23u;
            float f = 24.0f;
            double dbl = 25.0d;
            bool b = false;
            DateTime t = new DateTime(100, 10, 1);
            PlatformID pid = PlatformID.Win32NT;
            DoTestArgSlots(ref d, ref u, ref l, ref ul, ref f, ref dbl, ref b, ref t, ref pid);
        }

        private static void TestArrayElem()
        {
            int[] d = new int[] { 10 };
            uint[] u = new uint[] { 11u };
            long[] l = new long[] { 12 };
            ulong[] ul = new ulong[] { 13u };
            float[] f = new float[] { 14.0f };
            double[] dbl = new double[] { 15.0d };
            bool[] b = new bool[] { true };
            DateTime[] t = new DateTime[200];
            t[1] = new DateTime(100, 10, 1);
            PlatformID[] pid = new PlatformID[] { PlatformID.Win32NT };
            Mood[] mood = new Mood[] { Mood.good };
            Test("{%d}", __makeref(d[0]), "{10}");
            Test("{%u}", __makeref(u[0]), "{11}");
            Test("{%ld}", __makeref(l[0]), "{12}");
            Test("{%lu}", __makeref(ul[0]), "{13}");
            Test("{%f}", __makeref(f[0]), "{14}");
            Test("{%lf}", __makeref(dbl[0]), "{15}");
            Test("{%b}", __makeref(b[0]), "{true}");
            Test("{%t}", __makeref(t[1]), "{" + t[1].ToString() + "}");
            Test("{%p}", __makeref(pid[0]), "{Win32NT}");
            Test("{%e}", __makeref(mood[0]), "{good}");
        }

        private static int Main()
        {
            TestLocals();
            new TestClass().TestFields();
            TestArrayElem();
            return 100;
        }
    }
}