/Modules/testcapi_long.h

http://unladen-swallow.googlecode.com/ · C++ Header · 166 lines · 114 code · 23 blank · 29 comment · 42 complexity · 57e98f08856881640538798c7db66604 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. PyErr_Clear();
  88. UNBIND(x);
  89. /* Unsigned complains about 2**NBITS? */
  90. y = PyLong_FromLong((long)NBITS);
  91. if (y == NULL)
  92. return error(
  93. "unexpected NULL from PyLong_FromLong");
  94. x = PyNumber_Lshift(one, y); /* 1L << NBITS, == 2**NBITS */
  95. UNBIND(y);
  96. if (x == NULL)
  97. return error(
  98. "unexpected NULL from PyNumber_Lshift");
  99. uout = F_PY_TO_U(x);
  100. if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
  101. return error(
  102. "PyLong_AsUnsignedXXX(2**NBITS) didn't "
  103. "complain");
  104. PyErr_Clear();
  105. /* Signed complains about 2**(NBITS-1)?
  106. x still has 2**NBITS. */
  107. y = PyNumber_Rshift(x, one); /* 2**(NBITS-1) */
  108. UNBIND(x);
  109. if (y == NULL)
  110. return error(
  111. "unexpected NULL from PyNumber_Rshift");
  112. out = F_PY_TO_S(y);
  113. if (out != (TYPENAME)-1 || !PyErr_Occurred())
  114. return error(
  115. "PyLong_AsXXX(2**(NBITS-1)) didn't "
  116. "complain");
  117. PyErr_Clear();
  118. /* Signed complains about -2**(NBITS-1)-1?;
  119. y still has 2**(NBITS-1). */
  120. x = PyNumber_Negative(y); /* -(2**(NBITS-1)) */
  121. UNBIND(y);
  122. if (x == NULL)
  123. return error(
  124. "unexpected NULL from PyNumber_Negative");
  125. y = PyNumber_Subtract(x, one); /* -(2**(NBITS-1))-1 */
  126. UNBIND(x);
  127. if (y == NULL)
  128. return error(
  129. "unexpected NULL from PyNumber_Subtract");
  130. out = F_PY_TO_S(y);
  131. if (out != (TYPENAME)-1 || !PyErr_Occurred())
  132. return error(
  133. "PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
  134. "complain");
  135. PyErr_Clear();
  136. UNBIND(y);
  137. Py_XDECREF(x);
  138. Py_XDECREF(y);
  139. Py_DECREF(one);
  140. }
  141. Py_INCREF(Py_None);
  142. return Py_None;
  143. }