PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/test-suite/d/operator_overload_runme.1.d

#
D | 89 lines | 62 code | 12 blank | 15 comment | 20 complexity | 9fe784d08220ee1016873b99cbc9720c MD5 | raw file
 1module operator_overload_runme;
 2
 3import operator_overload.Op;
 4
 5void main() {
 6  // Invoke the C++ sanity check first.
 7  Op.sanity_check();
 8
 9  auto a = new Op();
10  auto b = new Op(5);
11  auto c = b;
12  auto d = new Op(2);
13  auto dd = d;
14  
15  // test equality
16  assert(a != b);
17  assert(b == c);
18  assert(a != d);
19  assert(d == dd);
20  
21  // test <
22  assert(a < b);
23  assert(a <= b);
24  assert(b <= c);
25  assert(b >= c);
26  assert(b > d);
27  assert(b >= d);
28  
29  // test +=
30  auto e = new Op(3);
31  e += d;
32  assert(e == b);
33  e -= c;
34  assert(e == a);
35  e = new Op(1);
36  e *= b;
37  assert(e == c);
38  e /= d;
39  assert(e == d);
40  e %= c;
41  assert(e == d);
42  
43  // test +
44  auto f = new Op(1);
45  auto g = new Op(1);
46  assert(f + g == new Op(2));
47  assert(f - g == new Op(0));
48  assert(f * g == new Op(1));
49  assert(f / g == new Op(1));
50  assert(f % g == new Op(0));
51  
52  // test unary operators
53  assert(-a == a);
54  assert(-b == new Op(-5));
55  // The unary ! operator is not overloadable in D1.
56  
57  // test []
58  auto h = new Op(3);
59  assert(h[0]==3);
60  assert(h[1]==0);
61  // Generation of opIndexAssign is not supported yet.
62  
63  // test ()
64  auto i = new Op(3);
65  assert(i()==3);
66  assert(i(1)==4);
67  assert(i(1,2)==6);
68  
69  // test ++ and --
70  auto j = new Op(100);
71  int original = j.i;
72  {
73    Op newOp = j++;
74    int newInt = original++;
75    assert(j.i == original);
76    assert(newOp.i == newInt);
77  }
78  {
79    Op newOp = j--;
80    int newInt = original--;
81    assert(j.i == original);
82    assert(newOp.i == newInt);
83  }
84  // Prefix increment/decrement operators are lowered to (foo +=/-= 1) in D1,
85  // but the test case does not define an integer overload for operator +=
86  // respectively -=.
87  
88  // Implicit casting is not overloadable in D1.
89}