[2019-12] [threads] Add back mono_threads_attach_tools_thread as a public API (#18074)
[mono-project.git] / mono / tests / constant-division.cs
blob1e9941c9bea6181e2a488bf93fb80af8ac096248
1 using System;
2 using System.Reflection;
3 using System.Reflection.Emit;
5 namespace Test {
6 public class T {
7 private delegate TReturn OneParameter<TReturn, TParameter0> (TParameter0 p0);
9 private static Type[] SDivisionArgs = {typeof(int)};
10 private static Type[] UDivisionArgs = {typeof(uint)};
12 public static int Main (string[] args) {
13 if (TestSDivision (2000, 10) != 0)
14 return -1;
16 if (TestUDivision (2000, 10) != 0)
17 return -1;
19 return 0;
22 private static int TestSDivision (int divisions, int invokes)
24 int i, j;
25 Random rand = new Random ();
26 for (i = 0; i < divisions; i++) {
27 int divisor = rand.Next (Int32.MinValue, Int32.MaxValue);
29 if (divisor == 0 || divisor == -1)
30 continue;
32 DynamicMethod SDivision = new DynamicMethod(
33 String.Format ("SDivision{0}", i),
34 typeof(int),
35 SDivisionArgs,
36 typeof(T).Module);
38 ILGenerator il = SDivision.GetILGenerator();
39 il.Emit(OpCodes.Ldarg_0);
40 il.Emit(OpCodes.Ldc_I4, divisor);
41 il.Emit(OpCodes.Div);
42 il.Emit(OpCodes.Ret);
44 OneParameter<int, int> invokeSDivision =
45 (OneParameter<int, int>)
46 SDivision.CreateDelegate(typeof(OneParameter<int, int>));
48 for (j = 0; j < invokes; j++) {
49 int dividend = rand.Next (Int32.MinValue, Int32.MaxValue);
50 int result, expected;
52 result = invokeSDivision (dividend);
53 expected = dividend / divisor;
55 if (result != expected) {
56 Console.WriteLine("{0} / {1} = {2} != {3})", dividend, divisor, expected, result);
57 return -1;
62 return 0;
65 private static int TestUDivision (int divisions, int invokes)
67 int i, j;
68 Random rand = new Random ();
69 for (i = 0; i < divisions; i++) {
70 uint divisor = (uint)rand.Next (Int32.MinValue, Int32.MaxValue);
72 if (divisor == 0)
73 continue;
75 DynamicMethod UDivision = new DynamicMethod(
76 String.Format ("UDivision{0}", i),
77 typeof(uint),
78 UDivisionArgs,
79 typeof(T).Module);
81 ILGenerator il = UDivision.GetILGenerator();
82 il.Emit(OpCodes.Ldarg_0);
83 il.Emit(OpCodes.Ldc_I4, divisor);
84 il.Emit(OpCodes.Div_Un);
85 il.Emit(OpCodes.Ret);
87 OneParameter<uint, uint> invokeUDivision =
88 (OneParameter<uint, uint>)
89 UDivision.CreateDelegate(typeof(OneParameter<uint, uint>));
91 for (j = 0; j < invokes; j++) {
92 uint dividend = (uint)rand.Next (Int32.MinValue, Int32.MaxValue);
93 uint result, expected;
95 result = invokeUDivision (dividend);
96 expected = dividend / divisor;
98 if (result != expected) {
99 Console.WriteLine("{0} / {1} = {2} != {3})", dividend, divisor, expected, result);
100 return -1;
104 return 0;