operators.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. '''
  5. >>> from operators_ext import *
  6. Check __nonzero__ support
  7. >>> assert X(2)
  8. >>> assert not X(0)
  9. ----
  10. >>> x = X(42)
  11. >>> x.value()
  12. 42
  13. >>> y = x - X(5)
  14. >>> y.value()
  15. 37
  16. >>> y = x - 4
  17. >>> y.value()
  18. 38
  19. >>> y = 3 - x
  20. >>> y.value()
  21. -39
  22. >>> (-y).value()
  23. 39
  24. >>> (x + y).value()
  25. 3
  26. >>> abs(y).value()
  27. 39
  28. >>> x < 10
  29. 0
  30. >>> x < 43
  31. 1
  32. >>> 10 < x
  33. 1
  34. >>> 43 < x
  35. 0
  36. >>> x < y
  37. 0
  38. >>> y < x
  39. 1
  40. ------
  41. >>> x > 10
  42. 1
  43. >>> x > 43
  44. 0
  45. >>> 10 > x
  46. 0
  47. >>> 43 > x
  48. 1
  49. >>> x > y
  50. 1
  51. >>> y > x
  52. 0
  53. >>> y = x - 5
  54. >>> x -= y
  55. >>> x.value()
  56. 5
  57. >>> str(x)
  58. '5'
  59. >>> z = Z(10)
  60. >>> int(z)
  61. 10
  62. >>> float(z)
  63. 10.0
  64. >>> complex(z)
  65. (10+0j)
  66. >>> pow(2,x)
  67. 32
  68. >>> pow(x,2).value()
  69. 25
  70. >>> pow(X(2),x).value()
  71. 32
  72. '''
  73. def run(args = None):
  74. import sys
  75. import doctest
  76. if args is not None:
  77. sys.argv = args
  78. return doctest.testmod(sys.modules.get(__name__))
  79. if __name__ == '__main__':
  80. print("running...")
  81. import sys
  82. status = run()[0]
  83. if (status == 0): print("Done.")
  84. sys.exit(status)