2010-02-19 Rodrigo Kumpera <rkumpera@novell.com>
[mcs.git] / tests / test-7.cs
blob8d27a6dec75a797514cddef3046d6492ac945db6
1 using System;
3 namespace Mine {
5 public class MyBoolean {
6 public static implicit operator bool (MyBoolean x)
8 return true;
12 public class MyTrueFalse {
13 public static bool operator true (MyTrueFalse i)
15 return true;
18 public static bool operator false (MyTrueFalse i)
20 return false;
24 public class Blah {
26 public int i;
28 public static int Main ()
30 Blah k, l;
32 k = new Blah (2) + new Blah (3);
33 if (k.i != 5)
34 return 1;
36 k = ~ new Blah (5);
37 if (k.i != -6)
38 return 1;
41 k = + new Blah (4);
42 if (k.i != 4)
43 return 1;
45 k = - new Blah (21);
46 if (k.i != -21)
47 return 1;
49 k = new Blah (22) - new Blah (21);
50 if (k.i != 1)
51 return 1;
53 if (!k)
54 Console.WriteLine ("! returned true");
56 int number = k;
57 if (number != 1)
58 return 1;
60 k++;
61 ++k;
63 if (k)
64 Console.WriteLine ("k is definitely true");
66 k = new Blah (30);
68 double f = (double) k;
70 if (f != 30.0)
71 return 1;
73 int i = new Blah (5) * new Blah (10);
75 if (i != 50)
76 return 1;
78 k = new Blah (50);
79 l = new Blah (10);
81 i = k / l;
83 if (i != 5)
84 return 1;
86 i = k % l;
88 if (i != 0)
89 return 1;
91 MyBoolean myb = new MyBoolean ();
93 if (!myb)
94 return 10;
97 // Tests the conditional operator invoking operator true
98 MyTrueFalse mf = new MyTrueFalse ();
99 int x = mf ? 1 : 2;
100 if (x != 1)
101 return 11;
103 Console.WriteLine ("Test passed");
104 return 0;
107 public Blah (int v)
109 i = v;
112 public static Blah operator + (Blah i, Blah j)
114 Blah b = new Blah (i.i + j.i);
115 Console.WriteLine ("Overload binary + operator");
116 return b;
119 public static Blah operator + (Blah i)
121 Console.WriteLine ("Overload unary + operator");
122 return new Blah (i.i);
125 public static Blah operator - (Blah i)
127 Console.WriteLine ("Overloaded unary - operator");
128 return new Blah (- i.i);
131 public static Blah operator - (Blah i, Blah j)
133 Blah b = new Blah (i.i - j.i);
134 Console.WriteLine ("Overloaded binary - operator");
135 return b;
138 public static int operator * (Blah i, Blah j)
140 Console.WriteLine ("Overloaded binary * operator");
141 return i.i * j.i;
144 public static int operator / (Blah i, Blah j)
146 Console.WriteLine ("Overloaded binary / operator");
147 return i.i / j.i;
150 public static int operator % (Blah i, Blah j)
152 Console.WriteLine ("Overloaded binary % operator");
153 return i.i % j.i;
156 public static Blah operator ~ (Blah i)
158 Console.WriteLine ("Overloaded ~ operator");
159 return new Blah (~i.i);
162 public static bool operator ! (Blah i)
164 Console.WriteLine ("Overloaded ! operator");
165 return (i.i == 1);
168 public static Blah operator ++ (Blah i)
170 Blah b = new Blah (i.i + 1);
171 Console.WriteLine ("Incrementing i");
172 return b;
175 public static Blah operator -- (Blah i)
177 Blah b = new Blah (i.i - 1);
178 Console.WriteLine ("Decrementing i");
179 return b;
182 public static bool operator true (Blah i)
184 Console.WriteLine ("Overloaded true operator");
185 return (i.i == 3);
188 public static bool operator false (Blah i)
190 Console.WriteLine ("Overloaded false operator");
191 return (i.i != 1);
194 public static implicit operator int (Blah i)
196 Console.WriteLine ("Converting implicitly from Blah->int");
197 return i.i;
200 public static explicit operator double (Blah i)
202 Console.WriteLine ("Converting explicitly from Blah->double");
203 return (double) i.i;