summaryrefslogtreecommitdiff
path: root/Examples/test-suite/d/operator_overload_runme.2.d
blob: 2ff61cd564c473f27dc10c0195cbef5c25bd3571 (plain)
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
module operator_overload_runme;

import operator_overload.Op;

void main() {
  // Invoke the C++ sanity check first.
  Op.sanity_check();

  auto a = new Op();
  auto b = new Op(5);
  auto c = b;
  auto d = new Op(2);
  auto dd = d;
  
  // test equality
  assert(a != b);
  assert(b == c);
  assert(a != d);
  assert(d == dd);
  
  // test <
  assert(a < b);
  assert(a <= b);
  assert(b <= c);
  assert(b >= c);
  assert(b > d);
  assert(b >= d);
  
  // test +=
  auto e = new Op(3);
  e += d;
  assert(e == b);
  e -= c;
  assert(e == a);
  e = new Op(1);
  e *= b;
  assert(e == c);
  e /= d;
  assert(e == d);
  e %= c;
  assert(e == d);
  
  // test +
  auto f = new Op(1);
  auto g = new Op(1);
  assert(f + g == new Op(2));
  assert(f - g == new Op(0));
  assert(f * g == new Op(1));
  assert(f / g == new Op(1));
  assert(f % g == new Op(0));
  
  // test unary operators
  assert(-a == a);
  assert(-b == new Op(-5));
  
  // Unfortunaly, there is no way to override conversion to boolean for
  // classes in D, opCast!("bool") is only used for structs. 
  
  // test []
  auto h = new Op(3);
  assert(h[0]==3);
  assert(h[1]==0);
  // Generation of opIndexAssign is not supported yet.
  
  // test ()
  auto i = new Op(3);
  assert(i()==3);
  assert(i(1)==4);
  assert(i(1,2)==6);
  
  // test ++ and --
  auto j = new Op(100);
  int original = j.i;
  // The prefix increment/decrement operators are not directly overloadable in
  // D2, and because the proxy classes are reference types, the lowering
  // yields the same value as the postfix operators.
  {
    Op newOp = ++j;
    int newInt = ++original;
    assert(j.i == original);
    assert(newOp.i == newInt);
  }
  {
    Op newOp = --j;
    int newInt = --original;
    assert(j.i == original);
    assert(newOp.i == newInt);
  }
  
  // Implicit casting via alias this is not supported yet.
}