/lib_pypy/testcapi_long.h

https://bitbucket.org/dac_io/pypy · C++ Header · 182 lines · 130 code · 23 blank · 29 comment · 46 complexity · ad64219ee122030570766d1dd9b25e73 MD5 · raw file

  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
  33. : (unsigned TYPENAME)(-(TYPENAME)base);
  34. /* For 0 & 3, subtract 1.
  35. * For 1 & 4, leave alone.
  36. * For 2 & 5, add 1.
  37. */
  38. uin += (unsigned TYPENAME)(TYPENAME)(j % 3 - 1);
  39. pyresult = F_U_TO_PY(uin);
  40. if (pyresult == NULL)
  41. return error(
  42. "unsigned unexpected null result");
  43. uout = F_PY_TO_U(pyresult);
  44. if (uout == (unsigned TYPENAME)-1 && PyErr_Occurred())
  45. return error(
  46. "unsigned unexpected -1 result");
  47. if (uout != uin)
  48. return error(
  49. "unsigned output != input");
  50. UNBIND(pyresult);
  51. in = (TYPENAME)uin;
  52. pyresult = F_S_TO_PY(in);
  53. if (pyresult == NULL)
  54. return error(
  55. "signed unexpected null result");
  56. out = F_PY_TO_S(pyresult);
  57. if (out == (TYPENAME)-1 && PyErr_Occurred())
  58. return error(
  59. "signed unexpected -1 result");
  60. if (out != in)
  61. return error(
  62. "signed output != input");
  63. UNBIND(pyresult);
  64. }
  65. }
  66. /* Overflow tests. The loop above ensured that all limit cases that
  67. * should not overflow don't overflow, so all we need to do here is
  68. * provoke one-over-the-limit cases (not exhaustive, but sharp).
  69. */
  70. {
  71. PyObject *one, *x, *y;
  72. TYPENAME out;
  73. unsigned TYPENAME uout;
  74. one = PyLong_FromLong(1);
  75. if (one == NULL)
  76. return error(
  77. "unexpected NULL from PyLong_FromLong");
  78. /* Unsigned complains about -1? */
  79. x = PyNumber_Negative(one);
  80. if (x == NULL)
  81. return error(
  82. "unexpected NULL from PyNumber_Negative");
  83. uout = F_PY_TO_U(x);
  84. if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
  85. return error(
  86. "PyLong_AsUnsignedXXX(-1) didn't complain");
  87. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  88. return error(
  89. "PyLong_AsUnsignedXXX(-1) raised "
  90. "something other than OverflowError");
  91. PyErr_Clear();
  92. UNBIND(x);
  93. /* Unsigned complains about 2**NBITS? */
  94. y = PyLong_FromLong((long)NBITS);
  95. if (y == NULL)
  96. return error(
  97. "unexpected NULL from PyLong_FromLong");
  98. x = PyNumber_Lshift(one, y); /* 1L << NBITS, == 2**NBITS */
  99. UNBIND(y);
  100. if (x == NULL)
  101. return error(
  102. "unexpected NULL from PyNumber_Lshift");
  103. uout = F_PY_TO_U(x);
  104. if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
  105. return error(
  106. "PyLong_AsUnsignedXXX(2**NBITS) didn't "
  107. "complain");
  108. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  109. return error(
  110. "PyLong_AsUnsignedXXX(2**NBITS) raised "
  111. "something other than OverflowError");
  112. PyErr_Clear();
  113. /* Signed complains about 2**(NBITS-1)?
  114. x still has 2**NBITS. */
  115. y = PyNumber_Rshift(x, one); /* 2**(NBITS-1) */
  116. UNBIND(x);
  117. if (y == NULL)
  118. return error(
  119. "unexpected NULL from PyNumber_Rshift");
  120. out = F_PY_TO_S(y);
  121. if (out != (TYPENAME)-1 || !PyErr_Occurred())
  122. return error(
  123. "PyLong_AsXXX(2**(NBITS-1)) didn't "
  124. "complain");
  125. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  126. return error(
  127. "PyLong_AsXXX(2**(NBITS-1)) raised "
  128. "something other than OverflowError");
  129. PyErr_Clear();
  130. /* Signed complains about -2**(NBITS-1)-1?;
  131. y still has 2**(NBITS-1). */
  132. x = PyNumber_Negative(y); /* -(2**(NBITS-1)) */
  133. UNBIND(y);
  134. if (x == NULL)
  135. return error(
  136. "unexpected NULL from PyNumber_Negative");
  137. y = PyNumber_Subtract(x, one); /* -(2**(NBITS-1))-1 */
  138. UNBIND(x);
  139. if (y == NULL)
  140. return error(
  141. "unexpected NULL from PyNumber_Subtract");
  142. out = F_PY_TO_S(y);
  143. if (out != (TYPENAME)-1 || !PyErr_Occurred())
  144. return error(
  145. "PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
  146. "complain");
  147. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  148. return error(
  149. "PyLong_AsXXX(-2**(NBITS-1)-1) raised "
  150. "something other than OverflowError");
  151. PyErr_Clear();
  152. UNBIND(y);
  153. Py_XDECREF(x);
  154. Py_XDECREF(y);
  155. Py_DECREF(one);
  156. }
  157. Py_INCREF(Py_None);
  158. return Py_None;
  159. }