From 27465e6b24e80fdcdaddd015fe8f690122f78ef8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 10 Feb 2020 22:22:12 +1100 Subject: [PATCH] tests/basics: Add tests for equality between bool and int/float/complex. False/True should be implicitly converted to 0/1 when compared with numeric types. --- tests/basics/bool1.py | 24 ++++++++++++++++++++++++ tests/basics/dict1.py | 28 ++++++++++++++++++++++++++++ tests/basics/set_add.py | 20 ++++++++++++++++++++ tests/basics/set_basic.py | 4 ++++ tests/float/complex1.py | 2 ++ tests/float/float1.py | 2 ++ 6 files changed, 80 insertions(+) diff --git a/tests/basics/bool1.py b/tests/basics/bool1.py index 35d9ed8ccc..daabfb17d8 100644 --- a/tests/basics/bool1.py +++ b/tests/basics/bool1.py @@ -10,6 +10,30 @@ print(False or True) print(+True) print(-True) +# comparison with itself +print(False == False) +print(False == True) +print(True == False) +print(True == True) +print(False != False) +print(False != True) +print(True != False) +print(True != True) + +# comparison with integers +print(False == 0) +print(0 == False) +print(True == 1) +print(1 == True) +print(True == 2) +print(2 == True) +print(False != 0) +print(0 != False) +print(True != 1) +print(1 != True) +print(True != 2) +print(2 != True) + # unsupported unary op try: len(False) diff --git a/tests/basics/dict1.py b/tests/basics/dict1.py index 0cec51173a..cd793c9ed2 100644 --- a/tests/basics/dict1.py +++ b/tests/basics/dict1.py @@ -23,6 +23,34 @@ print({} == {1:1}) # equality operator on dicts of same size but with different keys print({1:1} == {2:1}) +# 0 replacing False's item +d = {} +d[False] = 'false' +d[0] = 'zero' +print(d) + +# False replacing 0's item +d = {} +d[0] = 'zero' +d[False] = 'false' +print(d) + +# 1 replacing True's item +d = {} +d[True] = 'true' +d[1] = 'one' +print(d) + +# True replacing 1's item +d = {} +d[1] = 'one' +d[True] = 'true' +print(d) + +# mixed bools and integers +d = {False:10, True:11, 2:12} +print(d[0], d[1], d[2]) + # value not found try: {}[0] diff --git a/tests/basics/set_add.py b/tests/basics/set_add.py index ac81f48491..d8cfdaad97 100644 --- a/tests/basics/set_add.py +++ b/tests/basics/set_add.py @@ -1,3 +1,23 @@ s = {1, 2, 3, 4} print(s.add(5)) print(sorted(s)) + +s = set() +s.add(0) +s.add(False) +print(s) + +s = set() +s.add(False) +s.add(0) +print(s) + +s = set() +s.add(1) +s.add(True) +print(s) + +s = set() +s.add(True) +s.add(1) +print(s) diff --git a/tests/basics/set_basic.py b/tests/basics/set_basic.py index 6ea69e4f05..2d1653622e 100644 --- a/tests/basics/set_basic.py +++ b/tests/basics/set_basic.py @@ -10,6 +10,10 @@ print(sorted(s)) s = {1 + len(s)} print(s) +# bools mixed with integers +s = {False, True, 0, 1, 2} +print(len(s)) + # Sets are not hashable try: {s: 1} diff --git a/tests/float/complex1.py b/tests/float/complex1.py index 479b4b3485..c1fa61ba3c 100644 --- a/tests/float/complex1.py +++ b/tests/float/complex1.py @@ -36,6 +36,8 @@ ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag)) # comparison print(1j == 1) print(1j == 1j) +print(0 + 0j == False, 1 + 0j == True) +print(False == 0 + 0j, True == 1 + 0j) # comparison of nan is special nan = float('nan') * 1j diff --git a/tests/float/float1.py b/tests/float/float1.py index 54807e5ac9..f6d69e3904 100644 --- a/tests/float/float1.py +++ b/tests/float/float1.py @@ -64,6 +64,8 @@ print(1.2 <= 3.4) print(1.2 <= -3.4) print(1.2 >= 3.4) print(1.2 >= -3.4) +print(0.0 == False, 1.0 == True) +print(False == 0.0, True == 1.0) # comparison of nan is special nan = float('nan')