开发者

How to test equivalence of ranges

开发者 https://www.devze.com 2023-04-12 17:43 出处:网络
One of my unittests checks to see if a range is set up correctly after reading a log file, and I\'d like to just test var == range(0,10).However, range(0,1) == range(0,1) evaluates to 开发者_运维问答F

One of my unittests checks to see if a range is set up correctly after reading a log file, and I'd like to just test var == range(0,10). However, range(0,1) == range(0,1) evaluates to 开发者_运维问答False in Python 3.

Is there a straightforward way to test the equivalence of ranges in Python 3?


In Python3, range returns an iterable of type range. Two ranges are equal if and only if they are identical (i.e. share the same id.) To test equality of its contents, convert the range to a list:

list(range(0,1)) == list(range(0,1))

This works fine for short ranges. For very long ranges, Charles G Waldman's solution is better.


The first proposed solution - use "list" to turn the ranges into lists - is ineffecient, since it will first turn the range objects into lists (potentially consuming a lot of memory, if the ranges are large), then compare each element. Consider e.g. a = range(1000000), the "range" object itself is tiny but if you coerce it to a list it becomes huge. Then you have to compare one million elements.

Answer (2) is even less efficient, since the assertItemsEqual is not only going to instantiate the lists, it is going to sort them as well, before doing the elementwise comparison.

Instead, since you know the objects are ranges, they are equal when their strides, start and end values are equal. E.g.

ranges_equal = len(a)==len(b) and (len(a)==0 or a[0]==b[0] and a[-1]==b[-1])


Try assertItemsEqual, (in the docs):

class MyTestCase(unittest.TestCase):
    def test_mytest(self):
        a = (0,1,2,3,4)
        self.assertItemsEqual(a, range(0,4))


Another way to do it:

ranges_equal = str(a)==str(b)

The string representation indicates the start, end and step of ranges.

This question makes me think that perhaps Python should provide a way to get these attributes from the range object itself!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号