/branches/gsoc2009-ashishs99/Examples/test-suite/d/operator_overload_runme.2.d

# · D · 91 lines · 62 code · 13 blank · 16 comment · 20 complexity · d60f1073e8f127a4f58fd1aea7d7d452 MD5 · raw file

  1. module operator_overload_runme;
  2. import operator_overload.Op;
  3. void main() {
  4. // Invoke the C++ sanity check first.
  5. Op.sanity_check();
  6. auto a = new Op();
  7. auto b = new Op(5);
  8. auto c = b;
  9. auto d = new Op(2);
  10. auto dd = d;
  11. // test equality
  12. assert(a != b);
  13. assert(b == c);
  14. assert(a != d);
  15. assert(d == dd);
  16. // test <
  17. assert(a < b);
  18. assert(a <= b);
  19. assert(b <= c);
  20. assert(b >= c);
  21. assert(b > d);
  22. assert(b >= d);
  23. // test +=
  24. auto e = new Op(3);
  25. e += d;
  26. assert(e == b);
  27. e -= c;
  28. assert(e == a);
  29. e = new Op(1);
  30. e *= b;
  31. assert(e == c);
  32. e /= d;
  33. assert(e == d);
  34. e %= c;
  35. assert(e == d);
  36. // test +
  37. auto f = new Op(1);
  38. auto g = new Op(1);
  39. assert(f + g == new Op(2));
  40. assert(f - g == new Op(0));
  41. assert(f * g == new Op(1));
  42. assert(f / g == new Op(1));
  43. assert(f % g == new Op(0));
  44. // test unary operators
  45. assert(-a == a);
  46. assert(-b == new Op(-5));
  47. // Unfortunaly, there is no way to override conversion to boolean for
  48. // classes in D, opCast!("bool") is only used for structs.
  49. // test []
  50. auto h = new Op(3);
  51. assert(h[0]==3);
  52. assert(h[1]==0);
  53. // Generation of opIndexAssign is not supported yet.
  54. // test ()
  55. auto i = new Op(3);
  56. assert(i()==3);
  57. assert(i(1)==4);
  58. assert(i(1,2)==6);
  59. // test ++ and --
  60. auto j = new Op(100);
  61. int original = j.i;
  62. // The prefix increment/decrement operators are not directly overloadable in
  63. // D2, and because the proxy classes are reference types, the lowering
  64. // yields the same value as the postfix operators.
  65. {
  66. Op newOp = ++j;
  67. int newInt = ++original;
  68. assert(j.i == original);
  69. assert(newOp.i == newInt);
  70. }
  71. {
  72. Op newOp = --j;
  73. int newInt = --original;
  74. assert(j.i == original);
  75. assert(newOp.i == newInt);
  76. }
  77. // Implicit casting via alias this is not supported yet.
  78. }