LCOV - code coverage report
Current view: top level - Modules/_testcapi - long.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 3 293 1.0 %
Date: 2023-03-20 08:15:36 Functions: 1 12 8.3 %
Branches: 1 210 0.5 %

           Branch data     Line data    Source code
       1                 :            : #include "parts.h"
       2                 :            : 
       3                 :            : 
       4                 :            : static PyObject *
       5                 :          0 : raiseTestError(const char* test_name, const char* msg)
       6                 :            : {
       7                 :          0 :     PyErr_Format(PyExc_AssertionError, "%s: %s", test_name, msg);
       8                 :          0 :     return NULL;
       9                 :            : }
      10                 :            : 
      11                 :            : /* Tests of PyLong_{As, From}{Unsigned,}Long(), and
      12                 :            :    PyLong_{As, From}{Unsigned,}LongLong().
      13                 :            : 
      14                 :            :    Note that the meat of the test is contained in testcapi_long.h.
      15                 :            :    This is revolting, but delicate code duplication is worse:  "almost
      16                 :            :    exactly the same" code is needed to test long long, but the ubiquitous
      17                 :            :    dependence on type names makes it impossible to use a parameterized
      18                 :            :    function.  A giant macro would be even worse than this.  A C++ template
      19                 :            :    would be perfect.
      20                 :            : 
      21                 :            :    The "report an error" functions are deliberately not part of the #include
      22                 :            :    file:  if the test fails, you can set a breakpoint in the appropriate
      23                 :            :    error function directly, and crawl back from there in the debugger.
      24                 :            : */
      25                 :            : 
      26                 :            : #define UNBIND(X)  Py_DECREF(X); (X) = NULL
      27                 :            : 
      28                 :            : static PyObject *
      29                 :          0 : raise_test_long_error(const char* msg)
      30                 :            : {
      31                 :          0 :     return raiseTestError("test_long_api", msg);
      32                 :            : }
      33                 :            : 
      34                 :            : #define TESTNAME        test_long_api_inner
      35                 :            : #define TYPENAME        long
      36                 :            : #define F_S_TO_PY       PyLong_FromLong
      37                 :            : #define F_PY_TO_S       PyLong_AsLong
      38                 :            : #define F_U_TO_PY       PyLong_FromUnsignedLong
      39                 :            : #define F_PY_TO_U       PyLong_AsUnsignedLong
      40                 :            : 
      41                 :            : #include "testcapi_long.h"
      42                 :            : 
      43                 :            : static PyObject *
      44                 :          0 : test_long_api(PyObject* self, PyObject *Py_UNUSED(ignored))
      45                 :            : {
      46                 :          0 :     return TESTNAME(raise_test_long_error);
      47                 :            : }
      48                 :            : 
      49                 :            : #undef TESTNAME
      50                 :            : #undef TYPENAME
      51                 :            : #undef F_S_TO_PY
      52                 :            : #undef F_PY_TO_S
      53                 :            : #undef F_U_TO_PY
      54                 :            : #undef F_PY_TO_U
      55                 :            : 
      56                 :            : static PyObject *
      57                 :          0 : raise_test_longlong_error(const char* msg)
      58                 :            : {
      59                 :          0 :     return raiseTestError("test_longlong_api", msg);
      60                 :            : }
      61                 :            : 
      62                 :            : #define TESTNAME        test_longlong_api_inner
      63                 :            : #define TYPENAME        long long
      64                 :            : #define F_S_TO_PY       PyLong_FromLongLong
      65                 :            : #define F_PY_TO_S       PyLong_AsLongLong
      66                 :            : #define F_U_TO_PY       PyLong_FromUnsignedLongLong
      67                 :            : #define F_PY_TO_U       PyLong_AsUnsignedLongLong
      68                 :            : 
      69                 :            : #include "testcapi_long.h"
      70                 :            : 
      71                 :            : static PyObject *
      72                 :          0 : test_longlong_api(PyObject* self, PyObject *args)
      73                 :            : {
      74                 :          0 :     return TESTNAME(raise_test_longlong_error);
      75                 :            : }
      76                 :            : 
      77                 :            : #undef TESTNAME
      78                 :            : #undef TYPENAME
      79                 :            : #undef F_S_TO_PY
      80                 :            : #undef F_PY_TO_S
      81                 :            : #undef F_U_TO_PY
      82                 :            : #undef F_PY_TO_U
      83                 :            : 
      84                 :            : /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
      85                 :            :    is tested by test_long_api_inner. This test will concentrate on proper
      86                 :            :    handling of overflow.
      87                 :            : */
      88                 :            : 
      89                 :            : static PyObject *
      90                 :          0 : test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
      91                 :            : {
      92                 :            :     PyObject *num, *one, *temp;
      93                 :            :     long value;
      94                 :            :     int overflow;
      95                 :            : 
      96                 :            :     /* Test that overflow is set properly for a large value. */
      97                 :            :     /* num is a number larger than LONG_MAX even on 64-bit platforms */
      98                 :          0 :     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
      99         [ #  # ]:          0 :     if (num == NULL)
     100                 :          0 :         return NULL;
     101                 :          0 :     overflow = 1234;
     102                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     103                 :          0 :     Py_DECREF(num);
     104   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     105                 :          0 :         return NULL;
     106         [ #  # ]:          0 :     if (value != -1)
     107                 :          0 :         return raiseTestError("test_long_and_overflow",
     108                 :            :             "return value was not set to -1");
     109         [ #  # ]:          0 :     if (overflow != 1)
     110                 :          0 :         return raiseTestError("test_long_and_overflow",
     111                 :            :             "overflow was not set to 1");
     112                 :            : 
     113                 :            :     /* Same again, with num = LONG_MAX + 1 */
     114                 :          0 :     num = PyLong_FromLong(LONG_MAX);
     115         [ #  # ]:          0 :     if (num == NULL)
     116                 :          0 :         return NULL;
     117                 :          0 :     one = PyLong_FromLong(1L);
     118         [ #  # ]:          0 :     if (one == NULL) {
     119                 :          0 :         Py_DECREF(num);
     120                 :          0 :         return NULL;
     121                 :            :     }
     122                 :          0 :     temp = PyNumber_Add(num, one);
     123                 :          0 :     Py_DECREF(one);
     124                 :          0 :     Py_SETREF(num, temp);
     125         [ #  # ]:          0 :     if (num == NULL)
     126                 :          0 :         return NULL;
     127                 :          0 :     overflow = 0;
     128                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     129                 :          0 :     Py_DECREF(num);
     130   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     131                 :          0 :         return NULL;
     132         [ #  # ]:          0 :     if (value != -1)
     133                 :          0 :         return raiseTestError("test_long_and_overflow",
     134                 :            :             "return value was not set to -1");
     135         [ #  # ]:          0 :     if (overflow != 1)
     136                 :          0 :         return raiseTestError("test_long_and_overflow",
     137                 :            :             "overflow was not set to 1");
     138                 :            : 
     139                 :            :     /* Test that overflow is set properly for a large negative value. */
     140                 :            :     /* num is a number smaller than LONG_MIN even on 64-bit platforms */
     141                 :          0 :     num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
     142         [ #  # ]:          0 :     if (num == NULL)
     143                 :          0 :         return NULL;
     144                 :          0 :     overflow = 1234;
     145                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     146                 :          0 :     Py_DECREF(num);
     147   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     148                 :          0 :         return NULL;
     149         [ #  # ]:          0 :     if (value != -1)
     150                 :          0 :         return raiseTestError("test_long_and_overflow",
     151                 :            :             "return value was not set to -1");
     152         [ #  # ]:          0 :     if (overflow != -1)
     153                 :          0 :         return raiseTestError("test_long_and_overflow",
     154                 :            :             "overflow was not set to -1");
     155                 :            : 
     156                 :            :     /* Same again, with num = LONG_MIN - 1 */
     157                 :          0 :     num = PyLong_FromLong(LONG_MIN);
     158         [ #  # ]:          0 :     if (num == NULL)
     159                 :          0 :         return NULL;
     160                 :          0 :     one = PyLong_FromLong(1L);
     161         [ #  # ]:          0 :     if (one == NULL) {
     162                 :          0 :         Py_DECREF(num);
     163                 :          0 :         return NULL;
     164                 :            :     }
     165                 :          0 :     temp = PyNumber_Subtract(num, one);
     166                 :          0 :     Py_DECREF(one);
     167                 :          0 :     Py_SETREF(num, temp);
     168         [ #  # ]:          0 :     if (num == NULL)
     169                 :          0 :         return NULL;
     170                 :          0 :     overflow = 0;
     171                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     172                 :          0 :     Py_DECREF(num);
     173   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     174                 :          0 :         return NULL;
     175         [ #  # ]:          0 :     if (value != -1)
     176                 :          0 :         return raiseTestError("test_long_and_overflow",
     177                 :            :             "return value was not set to -1");
     178         [ #  # ]:          0 :     if (overflow != -1)
     179                 :          0 :         return raiseTestError("test_long_and_overflow",
     180                 :            :             "overflow was not set to -1");
     181                 :            : 
     182                 :            :     /* Test that overflow is cleared properly for small values. */
     183                 :          0 :     num = PyLong_FromString("FF", NULL, 16);
     184         [ #  # ]:          0 :     if (num == NULL)
     185                 :          0 :         return NULL;
     186                 :          0 :     overflow = 1234;
     187                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     188                 :          0 :     Py_DECREF(num);
     189   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     190                 :          0 :         return NULL;
     191         [ #  # ]:          0 :     if (value != 0xFF)
     192                 :          0 :         return raiseTestError("test_long_and_overflow",
     193                 :            :             "expected return value 0xFF");
     194         [ #  # ]:          0 :     if (overflow != 0)
     195                 :          0 :         return raiseTestError("test_long_and_overflow",
     196                 :            :             "overflow was not cleared");
     197                 :            : 
     198                 :          0 :     num = PyLong_FromString("-FF", NULL, 16);
     199         [ #  # ]:          0 :     if (num == NULL)
     200                 :          0 :         return NULL;
     201                 :          0 :     overflow = 0;
     202                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     203                 :          0 :     Py_DECREF(num);
     204   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     205                 :          0 :         return NULL;
     206         [ #  # ]:          0 :     if (value != -0xFF)
     207                 :          0 :         return raiseTestError("test_long_and_overflow",
     208                 :            :             "expected return value 0xFF");
     209         [ #  # ]:          0 :     if (overflow != 0)
     210                 :          0 :         return raiseTestError("test_long_and_overflow",
     211                 :            :             "overflow was set incorrectly");
     212                 :            : 
     213                 :          0 :     num = PyLong_FromLong(LONG_MAX);
     214         [ #  # ]:          0 :     if (num == NULL)
     215                 :          0 :         return NULL;
     216                 :          0 :     overflow = 1234;
     217                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     218                 :          0 :     Py_DECREF(num);
     219   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     220                 :          0 :         return NULL;
     221         [ #  # ]:          0 :     if (value != LONG_MAX)
     222                 :          0 :         return raiseTestError("test_long_and_overflow",
     223                 :            :             "expected return value LONG_MAX");
     224         [ #  # ]:          0 :     if (overflow != 0)
     225                 :          0 :         return raiseTestError("test_long_and_overflow",
     226                 :            :             "overflow was not cleared");
     227                 :            : 
     228                 :          0 :     num = PyLong_FromLong(LONG_MIN);
     229         [ #  # ]:          0 :     if (num == NULL)
     230                 :          0 :         return NULL;
     231                 :          0 :     overflow = 0;
     232                 :          0 :     value = PyLong_AsLongAndOverflow(num, &overflow);
     233                 :          0 :     Py_DECREF(num);
     234   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     235                 :          0 :         return NULL;
     236         [ #  # ]:          0 :     if (value != LONG_MIN)
     237                 :          0 :         return raiseTestError("test_long_and_overflow",
     238                 :            :             "expected return value LONG_MIN");
     239         [ #  # ]:          0 :     if (overflow != 0)
     240                 :          0 :         return raiseTestError("test_long_and_overflow",
     241                 :            :             "overflow was not cleared");
     242                 :            : 
     243                 :          0 :     Py_RETURN_NONE;
     244                 :            : }
     245                 :            : 
     246                 :            : /* Test the PyLong_AsLongLongAndOverflow API. General conversion to
     247                 :            :    long long is tested by test_long_api_inner. This test will
     248                 :            :    concentrate on proper handling of overflow.
     249                 :            : */
     250                 :            : 
     251                 :            : static PyObject *
     252                 :          0 : test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
     253                 :            : {
     254                 :            :     PyObject *num, *one, *temp;
     255                 :            :     long long value;
     256                 :            :     int overflow;
     257                 :            : 
     258                 :            :     /* Test that overflow is set properly for a large value. */
     259                 :            :     /* num is a number larger than LLONG_MAX on a typical machine. */
     260                 :          0 :     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
     261         [ #  # ]:          0 :     if (num == NULL)
     262                 :          0 :         return NULL;
     263                 :          0 :     overflow = 1234;
     264                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     265                 :          0 :     Py_DECREF(num);
     266   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     267                 :          0 :         return NULL;
     268         [ #  # ]:          0 :     if (value != -1)
     269                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     270                 :            :             "return value was not set to -1");
     271         [ #  # ]:          0 :     if (overflow != 1)
     272                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     273                 :            :             "overflow was not set to 1");
     274                 :            : 
     275                 :            :     /* Same again, with num = LLONG_MAX + 1 */
     276                 :          0 :     num = PyLong_FromLongLong(LLONG_MAX);
     277         [ #  # ]:          0 :     if (num == NULL)
     278                 :          0 :         return NULL;
     279                 :          0 :     one = PyLong_FromLong(1L);
     280         [ #  # ]:          0 :     if (one == NULL) {
     281                 :          0 :         Py_DECREF(num);
     282                 :          0 :         return NULL;
     283                 :            :     }
     284                 :          0 :     temp = PyNumber_Add(num, one);
     285                 :          0 :     Py_DECREF(one);
     286                 :          0 :     Py_SETREF(num, temp);
     287         [ #  # ]:          0 :     if (num == NULL)
     288                 :          0 :         return NULL;
     289                 :          0 :     overflow = 0;
     290                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     291                 :          0 :     Py_DECREF(num);
     292   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     293                 :          0 :         return NULL;
     294         [ #  # ]:          0 :     if (value != -1)
     295                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     296                 :            :             "return value was not set to -1");
     297         [ #  # ]:          0 :     if (overflow != 1)
     298                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     299                 :            :             "overflow was not set to 1");
     300                 :            : 
     301                 :            :     /* Test that overflow is set properly for a large negative value. */
     302                 :            :     /* num is a number smaller than LLONG_MIN on a typical platform */
     303                 :          0 :     num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
     304         [ #  # ]:          0 :     if (num == NULL)
     305                 :          0 :         return NULL;
     306                 :          0 :     overflow = 1234;
     307                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     308                 :          0 :     Py_DECREF(num);
     309   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     310                 :          0 :         return NULL;
     311         [ #  # ]:          0 :     if (value != -1)
     312                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     313                 :            :             "return value was not set to -1");
     314         [ #  # ]:          0 :     if (overflow != -1)
     315                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     316                 :            :             "overflow was not set to -1");
     317                 :            : 
     318                 :            :     /* Same again, with num = LLONG_MIN - 1 */
     319                 :          0 :     num = PyLong_FromLongLong(LLONG_MIN);
     320         [ #  # ]:          0 :     if (num == NULL)
     321                 :          0 :         return NULL;
     322                 :          0 :     one = PyLong_FromLong(1L);
     323         [ #  # ]:          0 :     if (one == NULL) {
     324                 :          0 :         Py_DECREF(num);
     325                 :          0 :         return NULL;
     326                 :            :     }
     327                 :          0 :     temp = PyNumber_Subtract(num, one);
     328                 :          0 :     Py_DECREF(one);
     329                 :          0 :     Py_SETREF(num, temp);
     330         [ #  # ]:          0 :     if (num == NULL)
     331                 :          0 :         return NULL;
     332                 :          0 :     overflow = 0;
     333                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     334                 :          0 :     Py_DECREF(num);
     335   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     336                 :          0 :         return NULL;
     337         [ #  # ]:          0 :     if (value != -1)
     338                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     339                 :            :             "return value was not set to -1");
     340         [ #  # ]:          0 :     if (overflow != -1)
     341                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     342                 :            :             "overflow was not set to -1");
     343                 :            : 
     344                 :            :     /* Test that overflow is cleared properly for small values. */
     345                 :          0 :     num = PyLong_FromString("FF", NULL, 16);
     346         [ #  # ]:          0 :     if (num == NULL)
     347                 :          0 :         return NULL;
     348                 :          0 :     overflow = 1234;
     349                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     350                 :          0 :     Py_DECREF(num);
     351   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     352                 :          0 :         return NULL;
     353         [ #  # ]:          0 :     if (value != 0xFF)
     354                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     355                 :            :             "expected return value 0xFF");
     356         [ #  # ]:          0 :     if (overflow != 0)
     357                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     358                 :            :             "overflow was not cleared");
     359                 :            : 
     360                 :          0 :     num = PyLong_FromString("-FF", NULL, 16);
     361         [ #  # ]:          0 :     if (num == NULL)
     362                 :          0 :         return NULL;
     363                 :          0 :     overflow = 0;
     364                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     365                 :          0 :     Py_DECREF(num);
     366   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     367                 :          0 :         return NULL;
     368         [ #  # ]:          0 :     if (value != -0xFF)
     369                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     370                 :            :             "expected return value 0xFF");
     371         [ #  # ]:          0 :     if (overflow != 0)
     372                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     373                 :            :             "overflow was set incorrectly");
     374                 :            : 
     375                 :          0 :     num = PyLong_FromLongLong(LLONG_MAX);
     376         [ #  # ]:          0 :     if (num == NULL)
     377                 :          0 :         return NULL;
     378                 :          0 :     overflow = 1234;
     379                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     380                 :          0 :     Py_DECREF(num);
     381   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     382                 :          0 :         return NULL;
     383         [ #  # ]:          0 :     if (value != LLONG_MAX)
     384                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     385                 :            :             "expected return value LLONG_MAX");
     386         [ #  # ]:          0 :     if (overflow != 0)
     387                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     388                 :            :             "overflow was not cleared");
     389                 :            : 
     390                 :          0 :     num = PyLong_FromLongLong(LLONG_MIN);
     391         [ #  # ]:          0 :     if (num == NULL)
     392                 :          0 :         return NULL;
     393                 :          0 :     overflow = 0;
     394                 :          0 :     value = PyLong_AsLongLongAndOverflow(num, &overflow);
     395                 :          0 :     Py_DECREF(num);
     396   [ #  #  #  # ]:          0 :     if (value == -1 && PyErr_Occurred())
     397                 :          0 :         return NULL;
     398         [ #  # ]:          0 :     if (value != LLONG_MIN)
     399                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     400                 :            :             "expected return value LLONG_MIN");
     401         [ #  # ]:          0 :     if (overflow != 0)
     402                 :          0 :         return raiseTestError("test_long_long_and_overflow",
     403                 :            :             "overflow was not cleared");
     404                 :            : 
     405                 :          0 :     Py_RETURN_NONE;
     406                 :            : }
     407                 :            : 
     408                 :            : /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that
     409                 :            :    non-integer arguments are handled correctly. It should be extended to
     410                 :            :    test overflow handling.
     411                 :            :  */
     412                 :            : 
     413                 :            : static PyObject *
     414                 :          0 : test_long_as_size_t(PyObject *self, PyObject *Py_UNUSED(ignored))
     415                 :            : {
     416                 :            :     size_t out_u;
     417                 :            :     Py_ssize_t out_s;
     418                 :            : 
     419                 :          0 :     Py_INCREF(Py_None);
     420                 :            : 
     421                 :          0 :     out_u = PyLong_AsSize_t(Py_None);
     422   [ #  #  #  # ]:          0 :     if (out_u != (size_t)-1 || !PyErr_Occurred())
     423                 :          0 :         return raiseTestError("test_long_as_size_t",
     424                 :            :                               "PyLong_AsSize_t(None) didn't complain");
     425         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_TypeError))
     426                 :          0 :         return raiseTestError("test_long_as_size_t",
     427                 :            :                               "PyLong_AsSize_t(None) raised "
     428                 :            :                               "something other than TypeError");
     429                 :          0 :     PyErr_Clear();
     430                 :            : 
     431                 :          0 :     out_s = PyLong_AsSsize_t(Py_None);
     432   [ #  #  #  # ]:          0 :     if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred())
     433                 :          0 :         return raiseTestError("test_long_as_size_t",
     434                 :            :                               "PyLong_AsSsize_t(None) didn't complain");
     435         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_TypeError))
     436                 :          0 :         return raiseTestError("test_long_as_size_t",
     437                 :            :                               "PyLong_AsSsize_t(None) raised "
     438                 :            :                               "something other than TypeError");
     439                 :          0 :     PyErr_Clear();
     440                 :            : 
     441                 :            :     /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
     442                 :          0 :     return Py_None;
     443                 :            : }
     444                 :            : 
     445                 :            : static PyObject *
     446                 :          0 : test_long_as_unsigned_long_long_mask(PyObject *self,
     447                 :            :                                      PyObject *Py_UNUSED(ignored))
     448                 :            : {
     449                 :          0 :     unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL);
     450                 :            : 
     451   [ #  #  #  # ]:          0 :     if (res != (unsigned long long)-1 || !PyErr_Occurred()) {
     452                 :          0 :         return raiseTestError("test_long_as_unsigned_long_long_mask",
     453                 :            :                               "PyLong_AsUnsignedLongLongMask(NULL) didn't "
     454                 :            :                               "complain");
     455                 :            :     }
     456         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
     457                 :          0 :         return raiseTestError("test_long_as_unsigned_long_long_mask",
     458                 :            :                               "PyLong_AsUnsignedLongLongMask(NULL) raised "
     459                 :            :                               "something other than SystemError");
     460                 :            :     }
     461                 :          0 :     PyErr_Clear();
     462                 :          0 :     Py_RETURN_NONE;
     463                 :            : }
     464                 :            : 
     465                 :            : /* Test the PyLong_AsDouble API. At present this just tests that
     466                 :            :    non-integer arguments are handled correctly.
     467                 :            :  */
     468                 :            : 
     469                 :            : static PyObject *
     470                 :          0 : test_long_as_double(PyObject *self, PyObject *Py_UNUSED(ignored))
     471                 :            : {
     472                 :            :     double out;
     473                 :            : 
     474                 :          0 :     Py_INCREF(Py_None);
     475                 :            : 
     476                 :          0 :     out = PyLong_AsDouble(Py_None);
     477   [ #  #  #  # ]:          0 :     if (out != -1.0 || !PyErr_Occurred())
     478                 :          0 :         return raiseTestError("test_long_as_double",
     479                 :            :                               "PyLong_AsDouble(None) didn't complain");
     480         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_TypeError))
     481                 :          0 :         return raiseTestError("test_long_as_double",
     482                 :            :                               "PyLong_AsDouble(None) raised "
     483                 :            :                               "something other than TypeError");
     484                 :          0 :     PyErr_Clear();
     485                 :            : 
     486                 :            :     /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
     487                 :          0 :     return Py_None;
     488                 :            : }
     489                 :            : 
     490                 :            : /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
     491                 :            : static PyObject *
     492                 :          0 : test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored))
     493                 :            : {
     494                 :            :     struct triple {
     495                 :            :         long input;
     496                 :            :         size_t nbits;
     497                 :            :         int sign;
     498                 :          0 :     } testcases[] = {{0, 0, 0},
     499                 :            :                      {1L, 1, 1},
     500                 :            :                      {-1L, 1, -1},
     501                 :            :                      {2L, 2, 1},
     502                 :            :                      {-2L, 2, -1},
     503                 :            :                      {3L, 2, 1},
     504                 :            :                      {-3L, 2, -1},
     505                 :            :                      {4L, 3, 1},
     506                 :            :                      {-4L, 3, -1},
     507                 :            :                      {0x7fffL, 15, 1},          /* one Python int digit */
     508                 :            :              {-0x7fffL, 15, -1},
     509                 :            :              {0xffffL, 16, 1},
     510                 :            :              {-0xffffL, 16, -1},
     511                 :            :              {0xfffffffL, 28, 1},
     512                 :            :              {-0xfffffffL, 28, -1}};
     513                 :            :     size_t i;
     514                 :            : 
     515         [ #  # ]:          0 :     for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
     516                 :            :         size_t nbits;
     517                 :            :         int sign;
     518                 :            :         PyObject *plong;
     519                 :            : 
     520                 :          0 :         plong = PyLong_FromLong(testcases[i].input);
     521         [ #  # ]:          0 :         if (plong == NULL)
     522                 :          0 :             return NULL;
     523                 :          0 :         nbits = _PyLong_NumBits(plong);
     524                 :          0 :         sign = _PyLong_Sign(plong);
     525                 :            : 
     526                 :          0 :         Py_DECREF(plong);
     527         [ #  # ]:          0 :         if (nbits != testcases[i].nbits)
     528                 :          0 :             return raiseTestError("test_long_numbits",
     529                 :            :                             "wrong result for _PyLong_NumBits");
     530         [ #  # ]:          0 :         if (sign != testcases[i].sign)
     531                 :          0 :             return raiseTestError("test_long_numbits",
     532                 :            :                             "wrong result for _PyLong_Sign");
     533                 :            :     }
     534                 :          0 :     Py_RETURN_NONE;
     535                 :            : }
     536                 :            : 
     537                 :            : static PyMethodDef test_methods[] = {
     538                 :            :     {"test_long_and_overflow",  test_long_and_overflow,          METH_NOARGS},
     539                 :            :     {"test_long_api",           test_long_api,                   METH_NOARGS},
     540                 :            :     {"test_long_as_double",     test_long_as_double,             METH_NOARGS},
     541                 :            :     {"test_long_as_size_t",     test_long_as_size_t,             METH_NOARGS},
     542                 :            :     {"test_long_as_unsigned_long_long_mask", test_long_as_unsigned_long_long_mask, METH_NOARGS},
     543                 :            :     {"test_long_long_and_overflow",test_long_long_and_overflow,  METH_NOARGS},
     544                 :            :     {"test_long_numbits",       test_long_numbits,               METH_NOARGS},
     545                 :            :     {"test_longlong_api",       test_longlong_api,               METH_NOARGS},
     546                 :            :     {NULL},
     547                 :            : };
     548                 :            : 
     549                 :            : int
     550                 :          2 : _PyTestCapi_Init_Long(PyObject *mod)
     551                 :            : {
     552         [ -  + ]:          2 :     if (PyModule_AddFunctions(mod, test_methods) < 0) {
     553                 :          0 :         return -1;
     554                 :            :     }
     555                 :            : 
     556                 :          2 :     return 0;
     557                 :            : }

Generated by: LCOV version 1.14