-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathTest03.cs
302 lines (259 loc) · 8.43 KB
/
Test03.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ILRuntime.Runtime;
namespace TestCases
{
class Test03
{
[ILRuntimeJIT(ILRuntimeJITFlags.NoJIT)]
public static void Test_01()
{
Console.WriteLine(Equals2(new int[] { 1 }, 1)); //NotSupportedException: Not supported opcode Readonly
Console.WriteLine(Equals(1, new object())); //InvalidCastException: Cannot cast from source type to destination type.
Console.WriteLine(IndexOf(new byte[] { 0, 1, 2, 3 }, (byte)1)); //NullReferenceException: Object reference not set to an instance of an object
}
static bool Equals2<T>(T[] a, T b)
{
return a[0].Equals(b);
}
static bool Equals<T>(T a, object b)
{
return a.Equals(b);
}
static int IndexOf<T>(T[] array, T value)
{
return System.Array.IndexOf(array, value);
}
public static object Run()
{
List<int> list1 = new List<int>();//c#Light 不支持模板,所以这里要注意一下
//List<int> 可以 List < int > 有空格不可以
list1.Add(1);
list1.Add(2);
list1.Add(3);
List<List<int>> list2 = new List<List<int>>();
list2.Add(list1);
List<List<List<int>>> list3 = new List<List<List<int>>>();
list3.Add(list2);
return list3;
}
public static object Run2()
{
Dictionary<string, int> test1 = new Dictionary<string, int>();
test1["aa"] = 100;
test1["bb"] = 200;
Console.WriteLine("Test dic start...");
Console.WriteLine("aa=" + test1["aa"]);
Console.WriteLine("bb=" + test1["bb"]);
Dictionary<string, DicTest> test2 = new Dictionary<string, DicTest>();
DicTest a = new DicTest();
a.Value = 100;
test2["aa"] = a;
a = new DicTest();
a.Value = 200;
test2["bb"] = a;
Console.WriteLine("Test dic2 start...");
Console.WriteLine("aa=" + test2["aa"].Value);
Console.WriteLine("bb=" + test2["bb"].Value);
Dictionary<DicTest, int> test3 = new Dictionary<DicTest, int>();
a = new DicTest();
a.Value = 100;
DicTest b = new DicTest();
b.Value = 200;
test3[a] = 100;
test3[b] = 200;
Console.WriteLine("Test dic3 start...");
Console.WriteLine("aa=" + test3[a]);
Console.WriteLine("bb=" + test3[b]);
return test3;
}
public static object Run3()
{
int[] arr = new int[] { 1, 2, 3, 4, 5 };
int index = Array.IndexOf(arr, 3);
List<int[]>[] arr2 = new List<int[]>[10];
List<int[]> e = new List<int[]>();
e.Add(arr);
Console.WriteLine("e.Contains:" + e.Contains<int[]>(arr));
arr2[3] = e;
index = Array.IndexOf(arr2, e);
return index;
}
class DicTest
{
public int Value { get; set; }
}
public static void Test04()
{
Test04Sub((byte)1); //wrong
Test04Sub2(new object[] { (byte)1 }); //wrong
Test04Sub3((byte)1); //right
}
static void Test04Sub(params object[] o)
{
Test04Sub3(o[0]);
}
static void Test04Sub2(object[] o)
{
Test04Sub3(o[0]);
}
static void Test04Sub3(object o)
{
List<object> l = new List<object>() { o };
Console.WriteLine("contains int:" + l.Contains(1));
Console.WriteLine("contains byte:" + l.Contains((byte)1));
}
public static void Test05()
{
IsBombOrHulu(new byte[] { 1, 1, 3, 3, 5, 6, 7, 8, 9 }, false, false);
}
const int StarCard = 0x41;
public static bool IsBombOrHulu(byte[] sortedCards, bool crazy, bool hasStarCard)
{
var groupList =
(from p in sortedCards
where p != StarCard
group p by p
into g
select new { Card = GetCardNum(g.Key), Count = g.Count() }).OrderBy(p => p.Count).ToArray();
foreach(var i in groupList)
{
Console.WriteLine($"{i.Card},{i.Count}");
}
return true;
}
public static int GetCardNum(byte card)
{
return card & 0x0F;
}
public static void Test06()
{
int initial = 2;
string res = ILRuntimeTest.TestFramework.TestClass3.getString(ref initial, 2);
Console.WriteLine(res);
}
class ClsWithManyParams
{
public int a;
public int b;
public int c;
public int d;
public int e;
public int f;
public int g;
public ClsWithManyParams(int a, int b, int c, int d, int e, int f, int g)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
}
}
public static void Test07()
{
ClsWithManyParams cls = new ClsWithManyParams(1, 2, 3, 4, 5, 6, 7);
if (cls.a != 1)
throw new Exception();
if (cls.b != 2)
throw new Exception();
if (cls.c != 3)
throw new Exception();
if (cls.d != 4)
throw new Exception();
if (cls.e != 5)
throw new Exception();
if (cls.f != 6)
throw new Exception();
if (cls.g != 7)
throw new Exception();
}
public static void TestUsingForeach()
{
var cls = new TestUsingCls();
var cls2 = new TestUsingCls();
int[] arr = new int[10];
int res = 0;
using (cls)
{
using (cls2)
{
foreach (var i in arr)
{
res += i;
}
}
}
if (!cls.Disposed || !cls2.Disposed)
{
throw new Exception();
}
}
public static void TestUsingNested() // 在外部 dll 中调用此过程
{
int len = 8;
using (var o = new SampleDisposableClass<int>(len))
{
using (var p = new SampleDisposableClass<float>(len))
{
for (int i = 0; i < len; i++)
{
o[i] = i;
p[i] = o[i] * 0.5f;
}
Console.WriteLine(p[len - 1]);
}
}
}
class TestUsingCls : IDisposable
{
public bool Disposed { get; set; }
public void Dispose()
{
Disposed = true;
}
}
public class SampleDisposableClass<T> : IDisposable
{
readonly T[] arr;
public SampleDisposableClass(int length = 8)
{
arr = new T[length];
}
public T this[int index]
{
get => arr[index];
set => arr[index] = value;
}
private bool disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
Console.WriteLine($"Dispose({disposing}) -> {typeof(T)}");
if (disposing)
{
// pass
}
disposedValue = true;
}
}
~SampleDisposableClass()
{
Dispose(disposing: false);
#if UNITY_EDITOR || DEVELOPMENT_BUILD
Debug.Log($"Finalizer -> {typeof(T)}");
#endif
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
}