[Python-checkins] python/dist/src/Modules _testcapimodule.c,1.17,1.18

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Tue, 28 Jan 2003 12:38:18 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv8113/python/Modules

Modified Files:
	_testcapimodule.c 
Log Message:
Added new private API function _PyLong_NumBits.  This will be used at the
start for the C implemention of new pickle LONG1 and LONG4 opcodes (the
linear-time way to pickle a long is to call _PyLong_AsByteArray, but
the caller has no idea how big an array to allocate, and correct
calculation is a bit subtle).


Index: _testcapimodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_testcapimodule.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** _testcapimodule.c	23 Jul 2002 06:31:14 -0000	1.17
--- _testcapimodule.c	28 Jan 2003 20:37:43 -0000	1.18
***************
*** 37,41 ****
  {
  	char buf[1024];
! 	PyOS_snprintf(buf, sizeof(buf), 
  		"%.200s #define == %d but sizeof(%.200s) == %d",
  		fatname, expected, typename, got);
--- 37,41 ----
  {
  	char buf[1024];
! 	PyOS_snprintf(buf, sizeof(buf),
  		"%.200s #define == %d but sizeof(%.200s) == %d",
  		fatname, expected, typename, got);
***************
*** 327,331 ****
          	return raiseTestError("test_u_code",
  			"u# code returned wrong values for u'test'");
! 	
  	Py_DECREF(tuple);
  	Py_INCREF(Py_None);
--- 327,331 ----
          	return raiseTestError("test_u_code",
  			"u# code returned wrong values for u'test'");
! 
  	Py_DECREF(tuple);
  	Py_INCREF(Py_None);
***************
*** 335,338 ****
--- 335,374 ----
  #endif
  
+ /* Simple test of _PyLong_NumBits. */
+ static PyObject *
+ test_long_numbits(PyObject *self)
+ {
+ 	struct pair {
+ 		long input;
+ 		size_t output;
+ 	} testcases[] = {{0, 1},
+ 			 {1L, 2},
+ 			 {-1L, 2},
+ 			 {2L, 3},
+ 			 {-2L, 3},
+ 			 {3L, 3},
+ 			 {-3L, 3},
+ 			 {4L, 4},
+ 			 {-4L, 4},
+ 			 {0x7fffL, 16},		/* one Python long digit */
+ 			 {-0x7fffL, 16},
+ 			 {0xfffffffL, 29},
+ 			 {-0xfffffffL, 29}};
+ 	int i;
+ 
+ 	for (i = 0; i < sizeof(testcases) / sizeof(struct pair); ++i) {
+ 		long input = testcases[i].input;
+ 		PyObject *plong = PyLong_FromLong(input);
+ 		size_t nbits = _PyLong_NumBits(plong);
+ 
+ 		Py_DECREF(plong);
+ 		if (nbits != testcases[i].output)
+ 			return raiseTestError("test_long_numbits",
+ 					      "wrong result");
+ 	}
+ 	Py_INCREF(Py_None);
+ 	return Py_None;
+ }
+ 
  static PyObject *
  raise_exception(PyObject *self, PyObject *args)
***************
*** 367,370 ****
--- 403,407 ----
  	{"test_dict_iteration",	(PyCFunction)test_dict_iteration,METH_NOARGS},
  	{"test_long_api",	(PyCFunction)test_long_api,	 METH_NOARGS},
+ 	{"test_long_numbits",	(PyCFunction)test_long_numbits,	 METH_NOARGS},
  #ifdef HAVE_LONG_LONG
  	{"test_longlong_api",	(PyCFunction)test_longlong_api,	 METH_NOARGS},