Skip to content

Commit

Permalink
Merge branch 'xZise-official-count' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
edschofield committed Nov 11, 2021
2 parents c66309c + c8eb497 commit 1b427ba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/future/backports/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def ceil(x):

from itertools import islice

if PY26:
# itertools.count in Py 2.6 doesn't accept a step parameter
def count(start=0, step=1):
while True:
yield start
start += step
else:
from itertools import count


if PY3:
try:
from _thread import get_ident
Expand Down Expand Up @@ -85,6 +95,10 @@ def wrapper(self):
return decorating_function


# OrderedDict Shim from Raymond Hettinger, python core dev
# http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/
# here to support version 2.6.

################################################################################
### OrderedDict
################################################################################
Expand Down
57 changes: 57 additions & 0 deletions tests/test_future/test_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
"""
Tests for the backported class:`range` class.
"""
from itertools import count as it_count

from future.backports.misc import count
from future.tests.base import unittest, skip26


class CountTest(unittest.TestCase):

"""Test the count function."""

def _test_count_func(self, func):
self.assertEqual(next(func(1)), 1)
self.assertEqual(next(func(start=1)), 1)

c = func()
self.assertEqual(next(c), 0)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 2)
c = func(1, 1)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 2)
c = func(step=1)
self.assertEqual(next(c), 0)
self.assertEqual(next(c), 1)
c = func(start=1, step=1)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 2)

c = func(-1)
self.assertEqual(next(c), -1)
self.assertEqual(next(c), 0)
self.assertEqual(next(c), 1)
c = func(1, -1)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 0)
self.assertEqual(next(c), -1)
c = func(-1, -1)
self.assertEqual(next(c), -1)
self.assertEqual(next(c), -2)
self.assertEqual(next(c), -3)

def test_count(self):
"""Test the count function."""
self._test_count_func(count)

@skip26
def test_own_count(self):
"""Test own count implementation."""
self._test_count_func(it_count)


if __name__ == '__main__':
unittest.main()

0 comments on commit 1b427ba

Please sign in to comment.