PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Python | 192 lines | 133 code | 55 blank | 4 comment | 48 complexity | aa0be85e6d688b315a9f8a99b2c0027f MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. from python_overload_simple_cast import *
  2. class Ai:
  3. def __init__(self,x):
  4. self.x = x
  5. def __int__(self):
  6. return self.x
  7. class Ad:
  8. def __init__(self,x):
  9. self.x = x
  10. def __float__(self):
  11. return self.x
  12. ai = Ai(4)
  13. ad = Ad(5.0)
  14. add = Ad(5.5)
  15. try:
  16. fint(add)
  17. good = 0
  18. except:
  19. good = 1
  20. if not good:
  21. raise RuntimeError, "fint(int)"
  22. if fint(ad) != "fint:int":
  23. raise RuntimeError, "fint(int)"
  24. if fdouble(ad) != "fdouble:double":
  25. raise RuntimeError, "fdouble(double)"
  26. if fint(ai) != "fint:int":
  27. raise RuntimeError, "fint(int)"
  28. if fint(5.0) != "fint:int":
  29. raise RuntimeError, "fint(int)"
  30. if fint(3) != "fint:int":
  31. raise RuntimeError, "fint(int)"
  32. if fint(3.0) != "fint:int":
  33. raise RuntimeError, "fint(int)"
  34. if fdouble(ad) != "fdouble:double":
  35. raise RuntimeError, "fdouble(double)"
  36. if fdouble(3) != "fdouble:double":
  37. raise RuntimeError, "fdouble(double)"
  38. if fdouble(3.0) != "fdouble:double":
  39. raise RuntimeError, "fdouble(double)"
  40. if fid(3,3.0) != "fid:intdouble":
  41. raise RuntimeError, "fid:intdouble"
  42. if fid(3.0,3) != "fid:doubleint":
  43. raise RuntimeError, "fid:doubleint"
  44. if fid(ad,ai) != "fid:doubleint":
  45. raise RuntimeError, "fid:doubleint"
  46. if fid(ai,ad) != "fid:intdouble":
  47. raise RuntimeError, "fid:intdouble"
  48. if foo(3) != "foo:int":
  49. raise RuntimeError, "foo(int)"
  50. if foo(3.0) != "foo:double":
  51. raise RuntimeError, "foo(double)"
  52. if foo("hello") != "foo:char *":
  53. raise RuntimeError, "foo(char *)"
  54. f = Foo()
  55. b = Bar()
  56. if foo(f) != "foo:Foo *":
  57. raise RuntimeError, "foo(Foo *)"
  58. if foo(b) != "foo:Bar *":
  59. raise RuntimeError, "foo(Bar *)"
  60. v = malloc_void(32)
  61. if foo(v) != "foo:void *":
  62. raise RuntimeError, "foo(void *)"
  63. s = Spam()
  64. if s.foo(3) != "foo:int":
  65. raise RuntimeError, "Spam::foo(int)"
  66. if s.foo(3.0) != "foo:double":
  67. raise RuntimeError, "Spam::foo(double)"
  68. if s.foo("hello") != "foo:char *":
  69. raise RuntimeError, "Spam::foo(char *)"
  70. if s.foo(f) != "foo:Foo *":
  71. raise RuntimeError, "Spam::foo(Foo *)"
  72. if s.foo(b) != "foo:Bar *":
  73. raise RuntimeError, "Spam::foo(Bar *)"
  74. if s.foo(v) != "foo:void *":
  75. raise RuntimeError, "Spam::foo(void *)"
  76. if Spam_bar(3) != "bar:int":
  77. raise RuntimeError, "Spam::bar(int)"
  78. if Spam_bar(3.0) != "bar:double":
  79. raise RuntimeError, "Spam::bar(double)"
  80. if Spam_bar("hello") != "bar:char *":
  81. raise RuntimeError, "Spam::bar(char *)"
  82. if Spam_bar(f) != "bar:Foo *":
  83. raise RuntimeError, "Spam::bar(Foo *)"
  84. if Spam_bar(b) != "bar:Bar *":
  85. raise RuntimeError, "Spam::bar(Bar *)"
  86. if Spam_bar(v) != "bar:void *":
  87. raise RuntimeError, "Spam::bar(void *)"
  88. # Test constructors
  89. s = Spam()
  90. if s.type != "none":
  91. raise RuntimeError, "Spam()"
  92. s = Spam(3)
  93. if s.type != "int":
  94. raise RuntimeError, "Spam(int)"
  95. s = Spam(3.4)
  96. if s.type != "double":
  97. raise RuntimeError, "Spam(double)"
  98. s = Spam("hello")
  99. if s.type != "char *":
  100. raise RuntimeError, "Spam(char *)"
  101. s = Spam(f)
  102. if s.type != "Foo *":
  103. raise RuntimeError, "Spam(Foo *)"
  104. s = Spam(b)
  105. if s.type != "Bar *":
  106. raise RuntimeError, "Spam(Bar *)"
  107. s = Spam(v)
  108. if s.type != "void *":
  109. raise RuntimeError, "Spam(void *)"
  110. # unsigned long long
  111. ullmax = 9223372036854775807 #0xffffffffffffffff
  112. ullmaxd = 9007199254740992.0
  113. ullmin = 0
  114. ullmind = 0.0
  115. if ull(ullmin) != ullmin:
  116. raise runtimeerror, "ull(ullmin)"
  117. if ull(ullmax) != ullmax:
  118. raise runtimeerror, "ull(ullmax)"
  119. if ull(ullmind) != ullmind:
  120. raise RuntimeError, "ull(ullmind)"
  121. if ull(ullmaxd) != ullmaxd:
  122. raise RuntimeError, "ull(ullmaxd)"
  123. # long long
  124. llmax = 9223372036854775807 #0x7fffffffffffffff
  125. llmin = -9223372036854775808
  126. # these are near the largest floats we can still convert into long long
  127. llmaxd = 9007199254740992.0
  128. llmind = -9007199254740992.0
  129. if ll(llmin) != llmin:
  130. raise runtimeerror, "ll(llmin)"
  131. if ll(llmax) != llmax:
  132. raise runtimeerror, "ll(llmax)"
  133. if ll(llmind) != llmind:
  134. raise RuntimeError, "ll(llmind)"
  135. if ll(llmaxd) != llmaxd:
  136. raise RuntimeError, "ll(llmaxd)"
  137. free_void(v)