You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.8 KiB

  1. /* Poor-man's template. Macros used:
  2. TESTNAME name of the test (like test_long_api_inner)
  3. TYPENAME the signed type (like long)
  4. F_S_TO_PY convert signed to pylong; TYPENAME -> PyObject*
  5. F_PY_TO_S convert pylong to signed; PyObject* -> TYPENAME
  6. F_U_TO_PY convert unsigned to pylong; unsigned TYPENAME -> PyObject*
  7. F_PY_TO_U convert pylong to unsigned; PyObject* -> unsigned TYPENAME
  8. */
  9. static PyObject *
  10. TESTNAME(PyObject *error(const char*))
  11. {
  12. const int NBITS = sizeof(TYPENAME) * 8;
  13. unsigned TYPENAME base;
  14. PyObject *pyresult;
  15. int i;
  16. /* Note: This test lets PyObjects leak if an error is raised. Since
  17. an error should never be raised, leaks are impossible <wink>. */
  18. /* Test native -> PyLong -> native roundtrip identity.
  19. * Generate all powers of 2, and test them and their negations,
  20. * plus the numbers +-1 off from them.
  21. */
  22. base = 1;
  23. for (i = 0;
  24. i < NBITS + 1; /* on last, base overflows to 0 */
  25. ++i, base <<= 1)
  26. {
  27. int j;
  28. for (j = 0; j < 6; ++j) {
  29. TYPENAME in, out;
  30. unsigned TYPENAME uin, uout;
  31. /* For 0, 1, 2 use base; for 3, 4, 5 use -base */
  32. uin = j < 3 ? base : 0U - base;
  33. /* For 0 & 3, subtract 1.
  34. * For 1 & 4, leave alone.
  35. * For 2 & 5, add 1.
  36. */
  37. uin += (unsigned TYPENAME)(TYPENAME)(j % 3 - 1);
  38. pyresult = F_U_TO_PY(uin);
  39. if (pyresult == NULL)
  40. return error(
  41. "unsigned unexpected null result");
  42. uout = F_PY_TO_U(pyresult);
  43. if (uout == (unsigned TYPENAME)-1 && PyErr_Occurred())
  44. return error(
  45. "unsigned unexpected -1 result");
  46. if (uout != uin)
  47. return error(
  48. "unsigned output != input");
  49. UNBIND(pyresult);
  50. in = (TYPENAME)uin;
  51. pyresult = F_S_TO_PY(in);
  52. if (pyresult == NULL)
  53. return error(
  54. "signed unexpected null result");
  55. out = F_PY_TO_S(pyresult);
  56. if (out == (TYPENAME)-1 && PyErr_Occurred())
  57. return error(
  58. "signed unexpected -1 result");
  59. if (out != in)
  60. return error(
  61. "signed output != input");
  62. UNBIND(pyresult);
  63. }
  64. }
  65. /* Overflow tests. The loop above ensured that all limit cases that
  66. * should not overflow don't overflow, so all we need to do here is
  67. * provoke one-over-the-limit cases (not exhaustive, but sharp).
  68. */
  69. {
  70. PyObject *one, *x, *y;
  71. TYPENAME out;
  72. unsigned TYPENAME uout;
  73. one = PyLong_FromLong(1);
  74. if (one == NULL)
  75. return error(
  76. "unexpected NULL from PyLong_FromLong");
  77. /* Unsigned complains about -1? */
  78. x = PyNumber_Negative(one);
  79. if (x == NULL)
  80. return error(
  81. "unexpected NULL from PyNumber_Negative");
  82. uout = F_PY_TO_U(x);
  83. if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
  84. return error(
  85. "PyLong_AsUnsignedXXX(-1) didn't complain");
  86. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  87. return error(
  88. "PyLong_AsUnsignedXXX(-1) raised "
  89. "something other than OverflowError");
  90. PyErr_Clear();
  91. UNBIND(x);
  92. /* Unsigned complains about 2**NBITS? */
  93. y = PyLong_FromLong((long)NBITS);
  94. if (y == NULL)
  95. return error(
  96. "unexpected NULL from PyLong_FromLong");
  97. x = PyNumber_Lshift(one, y); /* 1L << NBITS, == 2**NBITS */
  98. UNBIND(y);
  99. if (x == NULL)
  100. return error(
  101. "unexpected NULL from PyNumber_Lshift");
  102. uout = F_PY_TO_U(x);
  103. if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
  104. return error(
  105. "PyLong_AsUnsignedXXX(2**NBITS) didn't "
  106. "complain");
  107. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  108. return error(
  109. "PyLong_AsUnsignedXXX(2**NBITS) raised "
  110. "something other than OverflowError");
  111. PyErr_Clear();
  112. /* Signed complains about 2**(NBITS-1)?
  113. x still has 2**NBITS. */
  114. y = PyNumber_Rshift(x, one); /* 2**(NBITS-1) */
  115. UNBIND(x);
  116. if (y == NULL)
  117. return error(
  118. "unexpected NULL from PyNumber_Rshift");
  119. out = F_PY_TO_S(y);
  120. if (out != (TYPENAME)-1 || !PyErr_Occurred())
  121. return error(
  122. "PyLong_AsXXX(2**(NBITS-1)) didn't "
  123. "complain");
  124. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  125. return error(
  126. "PyLong_AsXXX(2**(NBITS-1)) raised "
  127. "something other than OverflowError");
  128. PyErr_Clear();
  129. /* Signed complains about -2**(NBITS-1)-1?;
  130. y still has 2**(NBITS-1). */
  131. x = PyNumber_Negative(y); /* -(2**(NBITS-1)) */
  132. UNBIND(y);
  133. if (x == NULL)
  134. return error(
  135. "unexpected NULL from PyNumber_Negative");
  136. y = PyNumber_Subtract(x, one); /* -(2**(NBITS-1))-1 */
  137. UNBIND(x);
  138. if (y == NULL)
  139. return error(
  140. "unexpected NULL from PyNumber_Subtract");
  141. out = F_PY_TO_S(y);
  142. if (out != (TYPENAME)-1 || !PyErr_Occurred())
  143. return error(
  144. "PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
  145. "complain");
  146. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  147. return error(
  148. "PyLong_AsXXX(-2**(NBITS-1)-1) raised "
  149. "something other than OverflowError");
  150. PyErr_Clear();
  151. UNBIND(y);
  152. Py_XDECREF(x);
  153. Py_XDECREF(y);
  154. Py_DECREF(one);
  155. }
  156. /* Test F_PY_TO_{S,U} on non-pylong input. This should raise a TypeError. */
  157. {
  158. TYPENAME out;
  159. unsigned TYPENAME uout;
  160. Py_INCREF(Py_None);
  161. out = F_PY_TO_S(Py_None);
  162. if (out != (TYPENAME)-1 || !PyErr_Occurred())
  163. return error("PyLong_AsXXX(None) didn't complain");
  164. if (!PyErr_ExceptionMatches(PyExc_TypeError))
  165. return error("PyLong_AsXXX(None) raised "
  166. "something other than TypeError");
  167. PyErr_Clear();
  168. uout = F_PY_TO_U(Py_None);
  169. if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
  170. return error("PyLong_AsXXX(None) didn't complain");
  171. if (!PyErr_ExceptionMatches(PyExc_TypeError))
  172. return error("PyLong_AsXXX(None) raised "
  173. "something other than TypeError");
  174. PyErr_Clear();
  175. Py_DECREF(Py_None);
  176. }
  177. Py_INCREF(Py_None);
  178. return Py_None;
  179. }