Skip to content

Commit

Permalink
Use estimate for assert_less
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Jan 4, 2024
1 parent a210fe4 commit eeeba9f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 27 deletions.
82 changes: 55 additions & 27 deletions test/test_num.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def tearDownClass(cls):
assert_num_counts({
"adds": 2259,
"divs": 2068,
"exps": 1227,
"exps": 1231,
"muls": 1401,
"totl": 6955,
"totl": 6959,
})

def assert_mod(
Expand Down Expand Up @@ -113,10 +113,27 @@ def assert_num(

self.assert_mod(num, mod, rem)

def assert_less(self, val1: Count, val2: Count):
self.assertLess(int(val1), int(val2))
def assert_less(
self,
val1: Count,
val2: Count,
estimate: bool = False,
):
self.assertLess(val1, val2)

if not estimate:
self.assertLess(
int(val1),
int(val2))

else:
assert not isinstance(val1, int)
assert not isinstance(val2, int)

self.assertLessEqual(
val1.estimate(),
val2.estimate())

def assert_less_not_implemented(
self,
val1: Count,
Expand Down Expand Up @@ -728,9 +745,10 @@ def test_comparisons(self):
Exp(10, 3 + Exp(10, Exp(10, 3)))
< Exp(10, 3 + Exp(10, 3)))

self.assertTrue(
Exp(10, 3 + Exp(10, 3))
< Exp(10, 3 + Exp(10, Exp(10, 3))))
self.assert_less(
Exp(10, 3 + Exp(10, 3)),
Exp(10, 3 + Exp(10, Exp(10, 3))),
estimate = True)

self.assert_less_not_implemented(
Exp(2, 13) * (-1 + Exp(2, 13)),
Expand All @@ -740,9 +758,10 @@ def test_comparisons(self):
2 ** (-3 + Exp(2, 13)),
))

self.assertLess(
self.assert_less(
-Exp(10, 14050258128),
Exp(10, 14050259810))
Exp(10, 14050259810),
estimate = True)

self.assert_less_not_implemented(
Exp(3, 5) * (-243 + (3 ** Exp(3, 5))),
Expand All @@ -752,21 +771,25 @@ def test_comparisons(self):
3 ** Exp(3, 5),
))

self.assertLess(
self.assert_less(
Exp(2, 5) * (-1 + (2 ** (-5 + Exp(2, 5)))),
-1 + (Exp(2, 5) * (-1 + (2 ** (-5 + (2 ** Exp(2, 5)))))))
-1 + (Exp(2, 5) * (-1 + (2 ** (-5 + (2 ** Exp(2, 5)))))),
estimate = True)

self.assertLess(
self.assert_less(
Exp(2, 5) * (-1 + (2 ** (-5 + Exp(2, 5)))),
Exp(2, 5) * (-1 + (2 ** (-5 + (2 ** Exp(2, 5))))))
Exp(2, 5) * (-1 + (2 ** (-5 + (2 ** Exp(2, 5))))),
estimate = True)

self.assertLess(
self.assert_less(
-1 + (2 ** (-5 + Exp(2, 5))),
-1 + (2 ** (-5 + (2 ** Exp(2, 5)))))
-1 + (2 ** (-5 + (2 ** Exp(2, 5)))),
estimate = True)

self.assertLess(
self.assert_less(
2 ** (-5 + Exp(2, 5)),
2 ** (-5 + (2 ** Exp(2, 5))))
2 ** (-5 + (2 ** Exp(2, 5))),
estimate = True)

self.assertFalse(
-(Exp(2, 65536) * (-1 + 2 ** (-65536 + Exp(2, 65536))))
Expand All @@ -784,25 +807,28 @@ def test_comparisons(self):
((2 + Exp(2, 3)) * 2 ** ((-4 + (5 * Exp(2, 11))) // 3))
< Exp(2, 3))

self.assertLess(
self.assert_less(
Exp(2, 33) * (1 + Exp(2, 2)),
(1 + Exp(2, 2)) * 2 ** (-3 + (Exp(2, 33) * (1 + Exp(2, 2)))))
(1 + Exp(2, 2)) * 2 ** (-3 + (Exp(2, 33) * (1 + Exp(2, 2)))),
estimate = True)

self.assertFalse(
(10 ** Exp(10, 8274649522))
< 8274649524 + Exp(10, 8274649522))

self.assertLess(
self.assert_less(
-(Exp(2, 11760) * (1 + Exp(2, 5879))) + (Exp(2, 20578) * (-1 + (11 * Exp(2, 1469)))),
(Exp(2, 44097) * (-1 + (11 * Exp(2, 1469)))) + -(Exp(2, 23520) * (1 + (Exp(2, 11759) * (1 + Exp(2, 5879))))))
(Exp(2, 44097) * (-1 + (11 * Exp(2, 1469)))) + -(Exp(2, 23520) * (1 + (Exp(2, 11759) * (1 + Exp(2, 5879))))),
estimate = True)

self.assert_less(
Exp(2, 3),
Exp(2, 3) * (2 + Exp(2, 3)))

self.assertLess(
self.assert_less(
Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + Exp(2, 3)))))))),
Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + Exp(2, 3)))))))))))
Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + (Exp(2, 3) * (2 + Exp(2, 3)))))))))),
estimate = True)

self.assertFalse(
(3 * 2 ** (5 + (Exp(2, 5) * (1 + 2 ** Exp(2, 5)))))
Expand All @@ -812,9 +838,10 @@ def test_comparisons(self):
(-(Exp(2, 8) * (-1 + 2 ** (-8 + Exp(2, 8)))) + (Exp(2, 8) * (-1 + 2 ** (-8 + 2 ** 2 ** Exp(2, 8)))))
< ((Exp(2, 8) * (-1 + 2 ** (-8 + 2 ** Exp(2, 8)))) + -(Exp(2, 8) * (-1 + 2 ** (-8 + Exp(2, 8))))))

self.assertLess(
self.assert_less(
Tet(10, 2),
Tet(10, 3))
Tet(10, 3),
estimate = True)

self.assertGreater(
Tet(10, 3),
Expand Down Expand Up @@ -860,9 +887,10 @@ def test_comparisons(self):
Exp(2, 13),
))

self.assertLess(
self.assert_less(
2 ** (Exp(2, 19) * (-1 + (2 ** (-17 + Exp(2, 19))))),
2 ** (2 + (Exp(2, 19) * (-1 + (2 ** (-17 + Exp(2, 19)))))))
2 ** (2 + (Exp(2, 19) * (-1 + (2 ** (-17 + Exp(2, 19)))))),
estimate = True)

self.assertGreater(
Tet(10, 5),
Expand Down
7 changes: 7 additions & 0 deletions tm/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,9 @@ def __lt__(self, other: Count) -> bool:
if r.exp <= exp:
return (self // r) < l

elif isinstance(other, Tet):
return other > self

return super().__lt__(other) # no-cover

def __pow__(self, other: Count) -> Exp:
Expand Down Expand Up @@ -1119,6 +1122,10 @@ def __lt__(self, other: Count) -> bool:

return self.height < other.height

if isinstance(other, Exp): # no-branch
if isinstance(other.exp, int) and 2 < self.height:
return False

return super().__lt__(other)

def __add__(self, other: Count) -> Count:
Expand Down

0 comments on commit eeeba9f

Please sign in to comment.