cmp3 0.6.3__tar.gz → 0.6.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cmp3-0.6.3/src/cmp3.egg-info → cmp3-0.6.4}/PKG-INFO +1 -1
- {cmp3-0.6.3 → cmp3-0.6.4}/pyproject.toml +1 -1
- cmp3-0.6.4/src/cmp3/tests/test_all.py +113 -0
- cmp3-0.6.3/src/cmp3/tests/test_cmp3_core.py → cmp3-0.6.4/src/cmp3/tests/test_core.py +1 -0
- {cmp3-0.6.3 → cmp3-0.6.4/src/cmp3.egg-info}/PKG-INFO +1 -1
- {cmp3-0.6.3 → cmp3-0.6.4}/src/cmp3.egg-info/SOURCES.txt +2 -1
- {cmp3-0.6.3 → cmp3-0.6.4}/LICENSE.txt +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/MANIFEST.in +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/README.rst +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/setup.cfg +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/src/cmp3/__init__.py +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/src/cmp3/core/__init__.py +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/src/cmp3/tests/__init__.py +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/src/cmp3.egg-info/dependency_links.txt +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/src/cmp3.egg-info/requires.txt +0 -0
- {cmp3-0.6.3 → cmp3-0.6.4}/src/cmp3.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import unittest
|
|
3
|
+
from typing import *
|
|
4
|
+
|
|
5
|
+
from cmp3.core import CmpABC, cmp, cmp_poset, cmpDeco
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestCmpFunction(unittest.TestCase):
|
|
9
|
+
def test_portingguide_less(self: Self) -> None:
|
|
10
|
+
self.assertEqual(cmp(1, 2), -1)
|
|
11
|
+
self.assertEqual(cmp(-5, -1), -1)
|
|
12
|
+
|
|
13
|
+
def test_portingguide_equal(self: Self) -> None:
|
|
14
|
+
self.assertEqual(cmp(3, 3), 0)
|
|
15
|
+
self.assertEqual(cmp("a", "a"), 0)
|
|
16
|
+
|
|
17
|
+
def test_portingguide_greater(self: Self) -> None:
|
|
18
|
+
self.assertEqual(cmp(2, 1), 1)
|
|
19
|
+
self.assertEqual(cmp("b", "a"), 1)
|
|
20
|
+
|
|
21
|
+
def test_poset_mode_uses_poset_semantics(self: Self) -> None:
|
|
22
|
+
# For totally ordered ints, this should behave like normal cmp
|
|
23
|
+
self.assertEqual(cmp(1, 2, mode="poset"), -1)
|
|
24
|
+
self.assertEqual(cmp(2, 1, mode="poset"), 1)
|
|
25
|
+
self.assertEqual(cmp(5, 5, mode="poset"), 0)
|
|
26
|
+
|
|
27
|
+
def test_invalid_mode_raises(self: Self) -> None:
|
|
28
|
+
with self.assertRaises(ValueError):
|
|
29
|
+
cmp(1, 2, mode="something-else")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TestCmpPoset(unittest.TestCase):
|
|
33
|
+
def test_total_order_ints(self: Self) -> None:
|
|
34
|
+
self.assertEqual(cmp_poset(1, 2), -1)
|
|
35
|
+
self.assertEqual(cmp_poset(2, 1), 1)
|
|
36
|
+
self.assertEqual(cmp_poset(3, 3), 0)
|
|
37
|
+
|
|
38
|
+
def test_incomparable_returns_nan(self: Self) -> None:
|
|
39
|
+
class Incomparable:
|
|
40
|
+
def __le__(self: Self, other: Any) -> bool:
|
|
41
|
+
# Always False, so no order relation is established
|
|
42
|
+
return False
|
|
43
|
+
|
|
44
|
+
result: Any
|
|
45
|
+
x: Incomparable
|
|
46
|
+
y: Incomparable
|
|
47
|
+
|
|
48
|
+
x = Incomparable()
|
|
49
|
+
y = Incomparable()
|
|
50
|
+
result = cmp_poset(x, y)
|
|
51
|
+
self.assertTrue(math.isnan(result))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# A concrete implementation of CmpABC for testing
|
|
55
|
+
class Box(CmpABC):
|
|
56
|
+
def __init__(self: Self, value: Any) -> None:
|
|
57
|
+
self.value = value
|
|
58
|
+
|
|
59
|
+
def __cmp__(self: Self, other: Any) -> Any:
|
|
60
|
+
if not isinstance(other, Box):
|
|
61
|
+
return NotImplemented
|
|
62
|
+
return (self.value > other.value) - (self.value < other.value)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@cmpDeco
|
|
66
|
+
class DecoratedBox:
|
|
67
|
+
def __init__(self: Self, value: Any) -> None:
|
|
68
|
+
self.value = value
|
|
69
|
+
|
|
70
|
+
def __cmp__(self: Self, other: Any) -> Any:
|
|
71
|
+
if not isinstance(other, DecoratedBox):
|
|
72
|
+
return NotImplemented
|
|
73
|
+
return (self.value > other.value) - (self.value < other.value)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class TestCmpABCAndDeco(unittest.TestCase):
|
|
77
|
+
def test_cmpabc_is_abstract(self: Self) -> None:
|
|
78
|
+
with self.assertRaises(TypeError):
|
|
79
|
+
CmpABC() # type: ignore[abstract] # must not be instantiable
|
|
80
|
+
|
|
81
|
+
def test_cmpabc_comparisons(self: Self) -> None:
|
|
82
|
+
a: Box
|
|
83
|
+
b: Box
|
|
84
|
+
c: Box
|
|
85
|
+
a = Box(1)
|
|
86
|
+
b = Box(2)
|
|
87
|
+
c = Box(1)
|
|
88
|
+
|
|
89
|
+
self.assertTrue(a < b)
|
|
90
|
+
self.assertTrue(a <= b)
|
|
91
|
+
self.assertTrue(b > a)
|
|
92
|
+
self.assertTrue(b >= a)
|
|
93
|
+
self.assertTrue(a == c)
|
|
94
|
+
self.assertTrue(a != b)
|
|
95
|
+
|
|
96
|
+
def test_cmpdeco_on_regular_class(self: Self) -> None:
|
|
97
|
+
x: DecoratedBox
|
|
98
|
+
y: DecoratedBox
|
|
99
|
+
z: DecoratedBox
|
|
100
|
+
x = DecoratedBox(10)
|
|
101
|
+
y = DecoratedBox(20)
|
|
102
|
+
z = DecoratedBox(10)
|
|
103
|
+
|
|
104
|
+
self.assertTrue(x < y)
|
|
105
|
+
self.assertTrue(x <= y)
|
|
106
|
+
self.assertTrue(y > x)
|
|
107
|
+
self.assertTrue(y >= x)
|
|
108
|
+
self.assertTrue(x == z)
|
|
109
|
+
self.assertTrue(x != y)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
if __name__ == "__main__":
|
|
113
|
+
unittest.main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|