PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. // The unary ! operator is not overloadable in D1.
  48. // test []
  49. auto h = new Op(3);
  50. assert(h[0]==3);
  51. assert(h[1]==0);
  52. // Generation of opIndexAssign is not supported yet.
  53. // test ()
  54. auto i = new Op(3);
  55. assert(i()==3);
  56. assert(i(1)==4);
  57. assert(i(1,2)==6);
  58. // test ++ and --
  59. auto j = new Op(100);
  60. int original = j.i;
  61. {
  62. Op newOp = j++;
  63. int newInt = original++;
  64. assert(j.i == original);
  65. assert(newOp.i == newInt);
  66. }
  67. {
  68. Op newOp = j--;
  69. int newInt = original--;
  70. assert(j.i == original);
  71. assert(newOp.i == newInt);
  72. }
  73. // Prefix increment/decrement operators are lowered to (foo +=/-= 1) in D1,
  74. // but the test case does not define an integer overload for operator +=
  75. // respectively -=.
  76. // Implicit casting is not overloadable in D1.
  77. }