-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathEnumTest.cs
441 lines (390 loc) · 11.9 KB
/
EnumTest.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using LitJson;
namespace TestCases
{
public class EnumTest
{
public enum TestEnum5
{
Enum1,
Enum2,
Enum4 = 0x12345789,
}
public enum TestEnum : long
{
Enum1,
Enum2,
Enum4 = 0x123456789,
}
enum TestEnum2 : ulong
{
Enum1,
Enum2,
Enum3234 = 0x123456789,
}
enum TestEnum3 : byte
{
Enum1bbb,
Enum2bbb,
}
enum TestEnum4 : ushort
{
零 = 0,
One = 1,
佰 = 100
}
enum TestEnumUint : uint
{
Zero = 0,
UOne = 1,
Max = uint.MaxValue
}
enum TestEnumInt:int
{
Min = int.MinValue,
Max = int.MaxValue
}
enum TestEnumSByte : sbyte
{
Min = sbyte.MinValue,
Max = sbyte.MaxValue
}
enum TestEnumFlag
{
Feature1 = 1,
Feature2 = 2,
Feature3 = 4,
Feature4 = 8,
}
enum TestEnumEmpty
{
}
static TestEnum b = TestEnum.Enum2;
public static string Test01()
{
TestEnum a = TestEnum.Enum4;
Console.WriteLine("a=" + a);
return a.ToString();
}
public static string Test02()
{
TestEnum a = (TestEnum)1;
Console.WriteLine("a=" + a);
return a.ToString();
}
public static bool Test03()
{
switch (b)
{
case TestEnum.Enum1:
return false;
case TestEnum.Enum2:
return true;
default:
return false;
}
}
public static string Test04()
{
return Test04Sub(TestEnum.Enum4);
}
static string Test04Sub(TestEnum a)
{
return a.ToString();
}
public static void Test05()
{
TestEnum a = TestEnum.Enum4;
TestEnum2 b = (TestEnum2)a;
Console.WriteLine("b=" + b);
TestEnum3 c = (TestEnum3)EnumTest.b;
Console.WriteLine("c=" + c);
}
public static string Test06()
{
System.IO.FileMode a = System.IO.FileMode.Create;
Console.WriteLine("a=" + a);
return a.ToString();
}
public static void Test08()
{
object o = TestEnum.Enum4;
Console.WriteLine((TestEnum)o == TestEnum.Enum4);
}
public static void Test09()
{
Dictionary<TestEnum, int> dic = new Dictionary<TestEnum, int>();
dic[TestEnum.Enum2] = 123;
int res;
if (dic.TryGetValue(TestEnum.Enum2, out res))
{
Console.WriteLine(res);
}
}
public static void Test10()
{
object e = TestEnum3.Enum2bbb;
byte b = (byte)e; //InvalidCastException
Console.WriteLine(b);
}
public static void Test11()
{
//Enum defined in ILRuntime
var enumInIL = TestEnum.Enum4;
var valueDirectly = $"{enumInIL}";
var valueToString = enumInIL.ToString();
if (valueDirectly.Equals(valueToString) == false)
{
throw new Exception($"Different string value: {valueDirectly} vs. {valueToString}");
}
//Enum defined in native code
var enumInNative = JsonType.Int;
valueDirectly = $"{enumInNative}";
valueToString = enumInNative.ToString();
var enumObj = (Object)enumInNative;
Console.WriteLine(enumObj.GetType().FullName);
if (valueDirectly.Equals(valueToString) == false)
{
throw new Exception($"Different string value: {valueDirectly} vs. {valueToString}");
}
}
public static void Test12()
{
var arr = Enum.GetValues(typeof(TestEnum));
foreach (var i in arr)
{
Console.WriteLine(i.ToString());
}
}
public static void Test13()
{
var emmm = TestEnum2.Enum3234;
Console.WriteLine(emmm.ToString());
var one = TestEnum4.One;
Console.WriteLine(one);
var hundred = TestEnum4.佰;
Console.WriteLine(hundred);
Console.WriteLine(hundred.ToString());
var UOne = TestEnumUint.Max;
Console.WriteLine(UOne);
Console.WriteLine(TestEnumSByte.Max);
}
public static void Test14()
{
var arr = Enum.GetNames(typeof(TestEnum));
foreach (var i in arr)
{
Console.WriteLine(i);
}
}
public static void Test16()
{
TestEnum[] eTests = (TestEnum[])System.Enum.GetValues(typeof(TestEnum));
foreach (var item in eTests)
{
Console.WriteLine(item);
}
}
public static void Test17()
{
/*Dictionary<TestEnum, int> dic = new Dictionary<TestEnum, int>();
dic[TestEnum.Enum2] = 123;
int res;
if (dic.TryGetValue(TestEnum.Enum2, out res))
{
Console.WriteLine(res);
}*/
//下面这个用例跑不过
Dictionary<string, TestEnum> dic2 = new Dictionary<string, TestEnum>();
dic2["abc"] = TestEnum.Enum1;
TestEnum e;
if (dic2.TryGetValue("abc", out e))
{
Console.WriteLine(e);
}
}
public static void Test18()
{
var a = TestEnum.Enum2;
Test18Sub(0x123456789, out a);
switch(a)
{
case TestEnum.Enum1:
Console.WriteLine(a + "1");
break;
case TestEnum.Enum2:
Console.WriteLine(a + "1");
break;
case TestEnum.Enum4:
Console.WriteLine(a + "2");
break;
}
Console.WriteLine(a);
}
static void Test18Sub(ulong val, out TestEnum e)
{
e = (TestEnum)val;
}
public static void Test19()
{
Console.WriteLine(Enum.GetValues(typeof(TestEnumEmpty)).Length);
}
public static void Test20()
{
TestEnumFlag flag = TestEnumFlag.Feature1 | TestEnumFlag.Feature3;
var res = flag.HasFlag(TestEnumFlag.Feature3);
Console.WriteLine(res);
if (!res)
throw new Exception();
res = flag.HasFlag(TestEnumFlag.Feature2);
Console.WriteLine(res);
if (res)
throw new Exception();
}
public static void Test21()
{
bool res = false;
object obj = Enum.ToObject(typeof(TestEnum5), 0x12345789);
Console.WriteLine(obj);
TestEnum5 b = (TestEnum5)obj;
switch (b)
{
case TestEnum5.Enum1:
res = false;
break;
case TestEnum5.Enum2:
res = false;
break;
case TestEnum5.Enum4:
res = true;
break;
default:
res = false;
break;
}
if (!res)
throw new Exception();
}
public static void Test22()
{
int res = 0;
TestEnum5 b = TestEnum5.Enum2;
res = b.CompareTo(TestEnum5.Enum4);
if (res >= 0)
throw new Exception();
}
public static void Test30()
{
var test = new TestClass1();
for (int i = 0; i < 10; ++i)
test.Test();
}
public static void Test31()
{
var test = new TestClass1();
for (int i = 0; i < 10; ++i)
test.Test1();
}
public static void Test32()
{
var test = new TestClass1();
for (int i = 0; i < 10; ++i)
test.Test2();
}
public static void Test33()
{
var test = new TestClass1();
for (int i = 0; i < 10; ++i)
test.Test3();
}
private class TestClass1
{
private TestClass2 TestValue = new TestClass2();
public void Test()
{
for (int i = 0; i < 10; ++i)
{
TestValue.Test1();
TestValue.Test2();
TestValue.Test3();
}
}
public void Test1()
{
for (int i = 0; i < 10; ++i)
TestValue.Test1();
}
public void Test2()
{
for (int i = 0; i < 10; ++i)
TestValue.Test2();
}
public void Test3()
{
for (int i = 0; i < 10; ++i)
TestValue.Test3();
}
}
private class TestClass2
{
private object _testValue = TestEnumFlag.Feature3;
public void Test1()
{
if (_testValue is TestEnumFlag.Feature3)
return;
throw new System.Exception();
}
public void Test2()
{
if (_testValue.Equals(TestEnumFlag.Feature3))
return;
throw new System.Exception();
}
public void Test3()
{
if (object.Equals(_testValue, TestEnumFlag.Feature3))
return;
throw new System.Exception();
}
}
class SystemType
{
public int value = 10;
}
class TestConstructor
{
int[] ArrayTest = null;
TestEnum[] ArrayTest2 = new TestEnum[9];
TestEnum[] ArrayTest3 = null;
SystemType ReferenceTest1 = null;
public void ArrayLengthTest()
{
if (ArrayTest == null)
ArrayTest = new int[5];
Console.WriteLine(string.Format("Int array type {0}", ArrayTest.GetType().Name));
Console.WriteLine(string.Format("Int array length {0}", ArrayTest.Length));
if (ArrayTest2 == null)
ArrayTest2 = new TestEnum[10];
Console.WriteLine(string.Format("Enum array type {0}", ArrayTest2.GetType().Name));
Console.WriteLine(string.Format("Enum array length {0}", ArrayTest2.Length));
if (ReferenceTest1 == null)
ReferenceTest1 = new SystemType();
Console.WriteLine(string.Format("SystemType type {0}", ReferenceTest1.GetType().Name));
Console.WriteLine(string.Format("SystemType va {0}", ReferenceTest1.value));
if (ArrayTest3 == null)
ArrayTest3 = new TestEnum[15];
Console.WriteLine(string.Format("Enum array type {0}", ArrayTest3.GetType().Name));
Console.WriteLine(string.Format("Enum array length {0}", ArrayTest3.Length));
}
}
public static void Test15()
{
TestConstructor test = new TestConstructor();
test.ArrayLengthTest();
}
}
}