[Python-Dev] cpython (merge 3.2 -> default): MERGE: Better test for Issue #15402: Add a __sizeof__ method to struct.Struct

martin at v.loewis.de martin at v.loewis.de
Mon Jul 23 23:44:47 CEST 2012


Zitat von Serhiy Storchaka <storchaka at gmail.com>:

> On 23.07.12 22:46, Michael Foord wrote:
>>> I am now prepared a set of 14 __sizeof__ patches (should it be one  
>>> issue or 14 individual issues in bugtracker?), and I feel a great  
>>> desire not to write tests at all.
>>
>> Without tests how can you have any confidence the patches are  
>> correct (or will continue to be correct)?
>
> Tests may not provide this. If we add a new dynamically allocated  
> data or change the size of the old and forgot to reflect this in  
> __sizeof__, then non modified tests not notice this. __sizeof__  
> returns 42. What is 42?

42 is most likely not the right answer, as the size should be a
multiple of four.

The point of writing tests for the sizeof code is that you detect
your own mistakes *in writing the test case*. The value of the
test case is not so much that it provides detection of regressions,
but that you notice the bug once you write the test case.

You may argue that then there is no value in committing the test
case, but there is also no harm in doing so.

In addition, it *may* catch regressions: if somebody changes
the layout of an object, the corresponding test most likely
fails. Maybe the fix is trivial and just in the test, but maybe
the layout changed fundamentally, and the author forgot to change
sizeof along with the structure change.

You wouldn't have to write 14 patches if implementing sizeof
correctly was easy.

Please see the existing extensive tests for inspiration on how
to add new ones.

Regards,
Martin




More information about the Python-Dev mailing list