/trunk/Examples/test-suite/python/python_richcompare_runme.py

# · Python · 100 lines · 58 code · 32 blank · 10 comment · 23 complexity · bec5a33d918ff1f2c70bcfb4585a61cc MD5 · raw file

  1. import python_richcompare
  2. base1 = python_richcompare.BaseClass(1)
  3. base2 = python_richcompare.BaseClass(2)
  4. base3 = python_richcompare.BaseClass(3)
  5. a1 = python_richcompare.SubClassA(1)
  6. a2 = python_richcompare.SubClassA(2)
  7. a3 = python_richcompare.SubClassA(3)
  8. b1 = python_richcompare.SubClassB(1)
  9. b2 = python_richcompare.SubClassB(2)
  10. b3 = python_richcompare.SubClassB(3)
  11. # Check == and != within a single type
  12. #-------------------------------------------------------------------------------
  13. if not (base1 == base1) :
  14. raise RuntimeError("Object not == to itself")
  15. if not (base1 == python_richcompare.BaseClass(1)) :
  16. raise RuntimeError("Object not == to an equivalent object")
  17. if (base1 == base2) :
  18. raise RuntimeError("Comparing non-equivalent objects of the same type, == returned True")
  19. if (base1 != base1) :
  20. raise RuntimeError("Object is != itself")
  21. if (base1 != python_richcompare.BaseClass(1)) :
  22. raise RuntimeError("Object is != an equivalent object")
  23. if not (base1 != base2) :
  24. raise RuntimeError("Comparing non-equivalent objects of the same type, != returned False")
  25. # Check redefined operator== in SubClassA
  26. #-------------------------------------------------------------------------------
  27. if (a2 == base2) :
  28. raise RuntimeError("Redefined operator== in SubClassA failed")
  29. if (a2 == b2) :
  30. raise RuntimeError("Redefined operator== in SubClassA failed")
  31. if not (a1 == a2) :
  32. raise RuntimeError("Redefined operator== in SubClassA failed")
  33. # Check up-casting of subclasses
  34. #-------------------------------------------------------------------------------
  35. if (base2 != a2) :
  36. raise RuntimeError("Comparing equivalent base and subclass instances, != returned True")
  37. if (a2 == base2) :
  38. raise RuntimeError("Comparing non-equivalent base and subclass instances, == returned True")
  39. if (a1 == b1) :
  40. raise RuntimeError("Comparing equivalent instances of different subclasses, == returned True")
  41. if (b1 == a1) :
  42. raise RuntimeError("Comparing equivalent instances of different subclasses, == returned True")
  43. # Check inequalities
  44. #-------------------------------------------------------------------------------
  45. if (a2 > b2) :
  46. raise RuntimeError("operator> failed")
  47. if (a2 < b2) :
  48. raise RuntimeError("operator< failed")
  49. if not (a2 >= b2) :
  50. raise RuntimeError("operator>= failed")
  51. if not (a2 <= b2) :
  52. raise RuntimeError("operator<= failed")
  53. # Check inequalities used for ordering
  54. #-------------------------------------------------------------------------------
  55. x = sorted([a2, a3, a1])
  56. if not (x[0] is a1) :
  57. raise RuntimeError("Ordering failed")
  58. if not (x[1] is a2) :
  59. raise RuntimeError("Ordering failed")
  60. if not (x[2] is a3) :
  61. raise RuntimeError("Ordering failed")
  62. x = sorted([base2, a3, b1])
  63. if not (x[0] is b1) :
  64. raise RuntimeError("Ordering failed")
  65. if not (x[1] is base2) :
  66. raise RuntimeError("Ordering failed")
  67. if not (x[2] is a3) :
  68. raise RuntimeError("Ordering failed")