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