classification
Title: add more unit tests for built-in int()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: a.kasyanov, asvetlov, chris.jerdonek, ezio.melotti, pitrou, python-dev
Priority: normal Keywords: easy, needs review, patch

Created on 2012-09-25 13:28 by chris.jerdonek, last changed 2012-12-27 20:54 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
issue-16045-1-default.patch chris.jerdonek, 2012-09-28 23:51 review
issue-16045-2-27.patch chris.jerdonek, 2012-09-29 20:54
Messages (14)
msg171270 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-25 13:28
The built-in function int() does not seem to have any basic unit tests (e.g. in test_builtin).  It would be good to add some.

Some cases (including edge cases) for possible inclusion:

int()
int(base='foo')  # no exception; returns 0
int(x=5)
int(x=5, base=10)  # raises TypeError
int(5.8)  # test truncation towards zero
int(-5.8)  # ditto
int('5.8')  # raises ValueError

Both positional and keyword argument combinations should be tested.
msg171272 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-09-25 13:36
Well, have you looked at test_int?
msg171273 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-25 13:42
Thanks for the pointer.  That should do it. :)  Searching for "test_int(" completely missed test_int.py.
msg171274 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-25 13:48
[Reopening] Actually, there may still be value in this.  Not all of the edge cases I mentioned are covered (e.g. calling using keyword arguments).
msg171275 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-25 13:54
We should also add a code-comment pointer in test_builtin to test_int (where test_int() would be located).
msg171293 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-09-25 15:46
Before adding tests (and possibly documentation) for edge cases like

> int(base='foo')  # no exception; returns 0

I'd take a look at what other implementations do.  If they do something different we might still decide to keep the tests and mark them as cpython-specific, but I would refrain to document them.
msg171299 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-25 17:01
Good thought.  Here is one data point:

$ pypy
Python 2.7.2 (341e1e3821fff77db3bb5cdb7a4851626298c44e, Jun 09 2012, 14:24:11)
[PyPy 1.9.0] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> int()
0
>>>> int(x='10', base=8)
8
>>>> int(x=5, base=10) 
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: int() can't convert non-string with explicit base
>>>> int(base=6)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: int() can't convert non-string with explicit base
>>>> int(base='foo')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: expected integer, got str object

So it looks like "no x with given base" is where behavior differs.
msg171368 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-27 07:57
From http://bugs.python.org/review/16036/ for issue 16036:

> Somewhere should be exposed that x must be str, bytes, bytearray or a subclass.

We can add tests for this, too (if not already there).
msg171550 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-28 23:51
Attaching first version of patch.  Interestingly, not all of these pass when I tried applying to 2.7.  For example,

>>> int(base='foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required

So at least one modification will be needed there.
msg171595 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-29 20:54
Adding patch modified for 2.7.  Here are the differences I found from 3.3.  Unlike 3.3--

(1) 2.7 does not accept bytearrays for x (though it accepts str/bytes).
(2) 2.7 raises a TypeError if passed a string base without providing x.

Is it acceptable to add tests like this to the 2.7 branch?

This information also helps in preparing the 2.7 documentation patch for issue 16036 and ensuring that it is correct.
msg177969 - (view) Author: Anton Kasyanov (a.kasyanov) * Date: 2012-12-23 10:16
looks good to me
msg177973 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-12-23 10:52
New changeset c502a2dc0345 by Andrew Svetlov in branch '2.7':
Issue #16045: add more unit tests for built-in int()
http://hg.python.org/cpython/rev/c502a2dc0345

New changeset a90d7003966e by Andrew Svetlov in branch '3.3':
Issue #16045: add more unit tests for built-in int()
http://hg.python.org/cpython/rev/a90d7003966e

New changeset ec7146b18274 by Andrew Svetlov in branch 'default':
Issue #16045: add more unit tests for built-in int()
http://hg.python.org/cpython/rev/ec7146b18274
msg177974 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-12-23 10:53
Committed. Thanks.
msg178328 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-12-27 20:54
New changeset eb1734e579f7 by Chris Jerdonek in branch '2.7':
Issue #16790: add some of the recent issue #16045 int tests to test_long.
http://hg.python.org/cpython/rev/eb1734e579f7
History
Date User Action Args
2012-12-27 20:54:38python-devsetmessages: + msg178328
2012-12-23 10:53:22asvetlovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2012-12-23 10:53:04asvetlovsetmessages: + msg177974
2012-12-23 10:52:10python-devsetnosy: + python-dev
messages: + msg177973
2012-12-23 10:16:49a.kasyanovsetnosy: + a.kasyanov, asvetlov
messages: + msg177969
2012-09-29 20:54:48chris.jerdoneksetfiles: + issue-16045-2-27.patch

messages: + msg171595
2012-09-28 23:51:58chris.jerdoneksetfiles: + issue-16045-1-default.patch
versions: + Python 2.7, Python 3.2
messages: + msg171550

keywords: + needs review, patch
stage: test needed -> patch review
2012-09-27 07:57:11chris.jerdoneksetmessages: + msg171368
2012-09-25 17:01:30chris.jerdoneksetmessages: + msg171299
2012-09-25 15:46:20ezio.melottisetmessages: + msg171293
2012-09-25 13:54:47chris.jerdoneksetmessages: + msg171275
2012-09-25 13:48:06chris.jerdoneksetstatus: closed -> open
title: create some unit tests for built-in int() -> add more unit tests for built-in int()
messages: + msg171274

resolution: not a bug -> (no value)
stage: resolved -> test needed
2012-09-25 13:42:57chris.jerdoneksetstatus: open -> closed
resolution: not a bug
messages: + msg171273

stage: resolved
2012-09-25 13:36:17pitrousetnosy: + pitrou
messages: + msg171272
2012-09-25 13:28:41chris.jerdonekcreate