| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * C Extension module to test Python interpreter C APIs. | 2 * C Extension module to test Python interpreter C APIs. |
| 3 * | 3 * |
| 4 * The 'test_*' functions exported by this module are run as part of the | 4 * The 'test_*' functions exported by this module are run as part of the |
| 5 * standard Python regression test, via Lib/test/test_capi.py. | 5 * standard Python regression test, via Lib/test/test_capi.py. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Python.h" | 8 #include "Python.h" |
| 9 #include <float.h> | 9 #include <float.h> |
| 10 #include "structmember.h" | 10 #include "structmember.h" |
| (...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 772 return NULL; | 772 return NULL; |
| 773 Py_RETURN_NONE; | 773 Py_RETURN_NONE; |
| 774 } | 774 } |
| 775 | 775 |
| 776 /* Tests for the PyMemoTable object in cPickle.c */ | 776 /* Tests for the PyMemoTable object in cPickle.c */ |
| 777 | 777 |
| 778 static PyObject * | 778 static PyObject * |
| 779 test_memotable(PyObject *self) | 779 test_memotable(PyObject *self) |
| 780 { | 780 { |
| 781 PyMemoTable *memo, *copy; | 781 PyMemoTable *memo, *copy; |
| 782 PyObject *five, *three; | |
| 782 long i; | 783 long i; |
| 783 | 784 |
| 784 #define ASSERT_EQ(a, b, msg) if ((a) != (b)) { \ | 785 #define ASSERT_EQ(a, b, msg) if ((a) != (b)) { \ |
| 785 return raiseTestError("test_memotable", #a " != " #b ": " msg); } | 786 return raiseTestError("test_memotable", #a " != " #b ": " msg); } |
| 786 | 787 |
| 787 memo = PyMemoTable_New(); | 788 memo = PyMemoTable_New(); |
| 788 ASSERT_EQ(PyMemoTable_Size(memo), 0, "Non-zero empty size"); | 789 ASSERT_EQ(PyMemoTable_Size(memo), 0, "Non-zero empty size"); |
| 789 » ASSERT_EQ(PyMemoTable_Get(memo, (void *)5), NULL, "Got invalid entry"); | 790 » five = PyInt_FromLong(5); |
| 791 » assert(five && "Failed to get 5"); | |
| 792 » ASSERT_EQ(PyMemoTable_Get(memo, five), NULL, "Got invalid entry"); | |
| 790 | 793 |
| 791 » PyMemoTable_Set(memo, (void *)5, 7); | 794 » PyMemoTable_Set(memo, five, 7); |
| 792 ASSERT_EQ(PyMemoTable_Size(memo), 1, "Bad memo size"); | 795 ASSERT_EQ(PyMemoTable_Size(memo), 1, "Bad memo size"); |
| 793 » ASSERT_EQ(*PyMemoTable_Get(memo, (void *)5), 7, "Failed to get entry"); | 796 » ASSERT_EQ(*PyMemoTable_Get(memo, five), 7, "Failed to get entry"); |
| 794 | 797 |
| 795 /* Update an existing entry */ | 798 /* Update an existing entry */ |
| 796 » PyMemoTable_Set(memo, (void *)5, 9); | 799 » PyMemoTable_Set(memo, five, 9); |
| 797 ASSERT_EQ(PyMemoTable_Size(memo), 1, "Bad memo size"); | 800 ASSERT_EQ(PyMemoTable_Size(memo), 1, "Bad memo size"); |
| 798 » ASSERT_EQ(*PyMemoTable_Get(memo, (void *)5), 9, "Failed to get entry"); | 801 » ASSERT_EQ(*PyMemoTable_Get(memo, five), 9, "Failed to get entry"); |
| 799 | |
| 800 » /* Force a hash collision */ | |
| 801 » PyMemoTable_Set(memo, (void *)21, 11); | |
| 802 » ASSERT_EQ(PyMemoTable_Size(memo), 2, "Bad memo size"); | |
| 803 » ASSERT_EQ(*PyMemoTable_Get(memo, (void *)5), 9, "Failed to get entry"); | |
| 804 » ASSERT_EQ(*PyMemoTable_Get(memo, (void *)21), 11, | |
| 805 » » "Failed to get entry"); | |
| 806 | 802 |
| 807 /* Clear the memo */ | 803 /* Clear the memo */ |
| 808 PyMemoTable_Clear(memo); | 804 PyMemoTable_Clear(memo); |
| 809 ASSERT_EQ(PyMemoTable_Size(memo), 0, "Non-zero empty size"); | 805 ASSERT_EQ(PyMemoTable_Size(memo), 0, "Non-zero empty size"); |
| 810 » ASSERT_EQ(PyMemoTable_Get(memo, (void *)5), NULL, "Got invalid entry"); | 806 » ASSERT_EQ(PyMemoTable_Get(memo, five), NULL, "Got invalid entry"); |
| 811 » ASSERT_EQ(PyMemoTable_Get(memo, (void *)21), NULL, | 807 » Py_DECREF(five); |
| 812 » » "Got invalid entry"); | |
| 813 | 808 |
| 814 » /* Insert enough entries to trigger a resize. */ | 809 » /* Insert enough entries to trigger a resize. These tests rely on the |
| 815 » for (i = 1; i < 12; i++) | 810 » fact that Python caches small ints so that we end up with the same |
| 816 » » PyMemoTable_Set(memo, (void *)i, i); | 811 » object in each of the two loops below. */ |
| 812 » for (i = 1; i < 12; i++) { | |
| 813 » » PyObject *obj = PyInt_FromLong(i); | |
| 814 » » assert(obj && "Failed to create an int"); | |
| 815 » » PyMemoTable_Set(memo, obj, i); | |
| 816 » » Py_DECREF(obj); | |
| 817 » } | |
| 817 ASSERT_EQ(PyMemoTable_Size(memo), 11, "Bad memo size"); | 818 ASSERT_EQ(PyMemoTable_Size(memo), 11, "Bad memo size"); |
| 818 ASSERT_EQ(memo->mt_allocated, 32, "Allocated less/more than expected"); | 819 ASSERT_EQ(memo->mt_allocated, 32, "Allocated less/more than expected"); |
| 819 » for (i = 1; i < 12; i++) | 820 » for (i = 1; i < 12; i++) { |
| 820 » » ASSERT_EQ(*PyMemoTable_Get(memo, (void *)i), i, "Wrong value"); | 821 » » PyObject *obj = PyInt_FromLong(i); |
| 822 » » assert(obj && "Failed to create an int"); | |
| 823 » » ASSERT_EQ(*PyMemoTable_Get(memo, obj), i, "Wrong value"); | |
| 824 » » Py_DECREF(obj); | |
| 825 » } | |
| 821 | 826 |
| 822 /* Copy the memo, and verify that the copy is independent. */ | 827 /* Copy the memo, and verify that the copy is independent. */ |
| 823 copy = PyMemoTable_Copy(memo); | 828 copy = PyMemoTable_Copy(memo); |
| 824 » PyMemoTable_Set(copy, (void *)3, 777); | 829 » three = PyInt_FromLong(3); |
| 825 » ASSERT_EQ(*PyMemoTable_Get(memo, (void *)3), 3, "Wrong value"); | 830 » assert(three && "Failed to create an int"); |
| 826 » ASSERT_EQ(*PyMemoTable_Get(copy, (void *)3), 777, "Wrong value"); | 831 » PyMemoTable_Set(copy, three, 777); |
| 832 » ASSERT_EQ(*PyMemoTable_Get(memo, three), 3, "Wrong value"); | |
| 833 » ASSERT_EQ(*PyMemoTable_Get(copy, three), 777, "Wrong value"); | |
| 834 » Py_DECREF(three); /* The memo table takes its own reference. */ | |
|
Jeffrey Yasskin
2009/07/03 01:32:59
This comment seems unnecessary, since incref'ing o
| |
| 827 | 835 |
| 828 PyMemoTable_Del(memo); | 836 PyMemoTable_Del(memo); |
| 829 PyMemoTable_Del(copy); | 837 PyMemoTable_Del(copy); |
| 830 Py_RETURN_NONE; | 838 Py_RETURN_NONE; |
| 831 } | 839 } |
| 832 | 840 |
| 833 /* End of tests for the PyMemoTable object in cPickle.c */ | 841 /* End of tests for the PyMemoTable object in cPickle.c */ |
| 834 | 842 |
| 835 static PyMethodDef TestMethods[] = { | 843 static PyMethodDef TestMethods[] = { |
| 836 {"raise_exception", raise_exception, METH_VARARGS}, | 844 {"raise_exception", raise_exception, METH_VARARGS}, |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1047 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); | 1055 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 1048 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLON G_MAX)); | 1056 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLON G_MAX)); |
| 1049 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX )); | 1057 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX )); |
| 1050 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN )); | 1058 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN )); |
| 1051 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_ Head))); | 1059 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_ Head))); |
| 1052 | 1060 |
| 1053 TestError = PyErr_NewException("_testcapi.error", NULL, NULL); | 1061 TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 1054 Py_INCREF(TestError); | 1062 Py_INCREF(TestError); |
| 1055 PyModule_AddObject(m, "error", TestError); | 1063 PyModule_AddObject(m, "error", TestError); |
| 1056 } | 1064 } |
| OLD | NEW |