Skip to content

Commit

Permalink
Update coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Dec 21, 2023
1 parent c0261c2 commit d2c9f16
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 69 deletions.
112 changes: 47 additions & 65 deletions tm/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,7 @@ def __eq__(self, other: object) -> bool:
return other is self

def __lt__(self, other: Count) -> bool:
if isinstance(other, int):
assert isinstance(self, Add)

l, r = self.l, self.r # pylint: disable = no-member

assert (l < 0 < r) or (r < 0 < l)

return False

assert not isinstance(other, int)
if isinstance(other, Add | Mul):
l, r = other.l, other.r

Expand All @@ -82,12 +74,12 @@ def __lt__(self, other: Count) -> bool:
except NotImplementedError: # no-cover
pass

if self == l:
return 0 < r

if self == r:
return 0 < l

if self == l: # no-cover
return 0 < r

if isinstance(other, Add):
if isinstance(l, int) and abs(l) < 10:
return self < r
Expand All @@ -111,7 +103,7 @@ def __add__(self, other: Count) -> Count:
return other.l + (self + other.r)

if isinstance(other, Mul):
if other.l == -1 and self == other.r:
if other.l == -1 and self == other.r: # no-cover
return 0

if isinstance(other, Div):
Expand All @@ -123,19 +115,19 @@ def __add__(self, other: Count) -> Count:
return make_add(self, other)

def __radd__(self, other: int) -> Count:
if other == 0:
if other == 0: # no-cover
return self

return self + other

def __sub__(self, other: Count) -> Count:
if self == other:
if self == other: # no-cover
return 0

if isinstance(other, Add):
l, r = other.l, other.r

if self == r:
if self == r: # no-branch
return -l

return self + -other
Expand All @@ -144,7 +136,7 @@ def __rsub__(self, other: Count) -> Count:
return other + -self

def __mul__(self, other: Count) -> Count:
if isinstance(other, int):
if isinstance(other, int): # no-cover
return other * self

if isinstance(other, Div):
Expand All @@ -155,7 +147,7 @@ def __mul__(self, other: Count) -> Count:

return make_mul(self, other)

def __rmul__(self, other: int) -> Count:
def __rmul__(self, other: int) -> Count: # no-cover
if other == 0:
return 0

Expand All @@ -175,7 +167,7 @@ def __divmod__(self, other: int) -> tuple[Count, int]:

return (self - mod) // other, mod

def __floordiv__(self, other: Count) -> Count:
def __floordiv__(self, other: Count) -> Count: # no-cover
assert isinstance(other, int)

assert other > 1
Expand All @@ -187,7 +179,7 @@ def __rpow__(self, other: int) -> Exp | Tet:


def make_add(l: Count, r: Num) -> Add:
if isinstance(l, Num) and l.depth > r.depth:
if isinstance(l, Num) and l.depth > r.depth: # no-cover
l, r = r, l

try:
Expand Down Expand Up @@ -236,7 +228,7 @@ def digits(self) -> int:
)

def __mod__(self, mod: int) -> int:
if mod == 1:
if mod == 1: # no-cover
return 0

return ((self.l % mod) + (self.r % mod)) % mod
Expand All @@ -257,13 +249,13 @@ def __add__(self, other: Count) -> Count:
l, r = self.l, self.r

if isinstance(other, int):
if other == 0:
if other == 0: # no-cover
return self

if isinstance(l, int):
return (l + other) + r

return make_add(other, self)
return make_add(other, self) # no-cover

if isinstance(other, Add):
lo, ro = other.l, other.r
Expand All @@ -286,7 +278,7 @@ def __sub__(self, other: Count) -> Count:
if isinstance(other, Add):
l, lo = self.l, other.l

if isinstance(l, int) and isinstance(lo, int):
if isinstance(l, int) and isinstance(lo, int): # no-branch
return (l - lo) + (self.r - other.r)

return self + -other
Expand All @@ -299,7 +291,7 @@ def __mul__(self, other: Count) -> Count:

def __rmul__(self, other: int) -> Count:
match other:
case 0:
case 0: # no-cover
return 0

case 1:
Expand Down Expand Up @@ -334,10 +326,10 @@ def __lt__(self, other: Count) -> bool:
if l < 0 and r < 0: # no-cover
return True

if 0 < l and 0 < r:
if 0 < l and 0 < r: # no-cover
return False

if other == l:
if other == l: # no-cover
return r < 0

if other == r:
Expand All @@ -346,7 +338,7 @@ def __lt__(self, other: Count) -> bool:
if isinstance(other, Add):
lo, ro = other.l, other.r

if self == ro:
if self == ro: # no-cover
return lo > 0

if l == lo:
Expand All @@ -355,7 +347,7 @@ def __lt__(self, other: Count) -> bool:
if r == ro:
return l < lo

if l == ro:
if l == ro: # no-cover
return r < lo

if isinstance(l, int) and isinstance(lo, int):
Expand Down Expand Up @@ -437,7 +429,7 @@ def __neg__(self) -> Count:
return -(self.l) * self.r

def __mod__(self, mod: int) -> int:
if mod == 1:
if mod == 1: # no-cover
return 0

if (l_mod := self.l % mod) == 0:
Expand Down Expand Up @@ -473,12 +465,12 @@ def __rmul__(self, other: int) -> Count:
if isinstance(l, int):
return -l * r

if isinstance(l, Exp) and isinstance(r, Add):
if isinstance(l, Exp) and isinstance(r, Add): # no-branch
return l * -r

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

if other == 1:
if other == 1: # no-cover
return self

return (other * l) * r
Expand Down Expand Up @@ -527,15 +519,6 @@ def __add__(self, other: Count) -> Count:
except NotImplementedError: # no-cover
pass

if lo == -1 and isinstance(ro, Mul):
rol, ror = ro.l, ro.r

if isinstance(ror, Exp):
assert ror.base == r.base

if ror.exp == r.exp:
return (l + -rol) * r

elif isinstance(other, Add):
lo, ro = other.l, other.r

Expand All @@ -553,20 +536,20 @@ def __add__(self, other: Count) -> Count:
if lo.depth < self.depth:
return lo + (self + ro)

if (isinstance(l, int)
if (isinstance(l, int) # no-cover
and isinstance(r, Exp) and isinstance(r.exp, Div)):
return lo + (self + ro)
return lo + (self + ro) # no-cover

elif isinstance(other, Exp):
return other + self

if l == -1 and other == r:
if l == -1 and other == r: # no-cover
return 0

return super().__add__(other)

def __sub__(self, other: Count) -> Count:
if other == 0:
if other == 0: # no-cover
return self

l, r = self.l, self.r
Expand Down Expand Up @@ -697,7 +680,7 @@ def digits(self) -> int:
return self.num.digits() - round(log10(self.den))

def __add__(self, other: Count) -> Count:
if other == 0:
if other == 0: # no-cover
return self

num, den = self.num, self.den
Expand All @@ -719,7 +702,7 @@ def __add__(self, other: Count) -> Count:
return ((oden * num) + (den * other.num)) // (den * oden)

def __radd__(self, other: int) -> Count:
if other == 0:
if other == 0: # no-cover
return self

return ((other * self.den) + self.num) // self.den
Expand Down Expand Up @@ -749,7 +732,7 @@ def __mul__(self, other: Count) -> Count:

def __rmul__(self, other: int) -> Count:
match other:
case 0:
case 0: # no-cover
return 0

case 1:
Expand Down Expand Up @@ -918,7 +901,7 @@ def __add__(self, other: Count) -> Count:

try:
return add_exponents((self, 1), (l, r))
except NotImplementedError:
except NotImplementedError: # no-cover
pass

elif isinstance(other, Exp):
Expand Down Expand Up @@ -948,7 +931,7 @@ def multiplies_with(self, other: Count) -> bool:
if isinstance(other, Exp):
return other.base == self.base

if isinstance(other, Mul):
if isinstance(other, Mul): # no-cover
return (
self.multiplies_with(other.l)
or self.multiplies_with(other.r)
Expand All @@ -968,19 +951,19 @@ def __mul__(self, other: Count) -> Count:
if isinstance(other, Add):
return (self * other.l) + (self * other.r)

if isinstance(other, Mul):
l, r = other.l, other.r
if not isinstance(other, Mul): # no-cover
return super().__mul__(other)

if isinstance(l, int):
return l * (self * r)
l, r = other.l, other.r

if self.multiplies_with(l):
return (self * l) * r
if isinstance(l, int):
return l * (self * r)

if self.multiplies_with(r): # no-branch
return l * (self * r)
if self.multiplies_with(l):
return (self * l) * r

return super().__mul__(other)
if self.multiplies_with(r): # no-branch
return l * (self * r)

def __rmul__(self, other: int) -> Count:
if other == 0:
Expand Down Expand Up @@ -1065,7 +1048,7 @@ def __lt__(self, other: Count) -> bool:
if isinstance(other, Add):
l, r = other.l, other.r

if isinstance(l, int):
if isinstance(l, int): # no-branch
return self < r

elif isinstance(other, Mul): # no-branch
Expand Down Expand Up @@ -1139,10 +1122,9 @@ def __lt__(self, other: Count) -> bool:
return super().__lt__(other)

def __add__(self, other: Count) -> Count:
if isinstance(other, int):
return self if other == 0 else make_add(other, self)
assert isinstance(other, int)

return super().__add__(other)
return self if other == 0 else make_add(other, self)


def add_exponents(
Expand Down Expand Up @@ -1172,7 +1154,7 @@ def add_exponents(


def gcd(l: int, r: Count) -> int:
if l == 1:
if l == 1: # no-cover
return 1

if isinstance(r, int):
Expand Down
6 changes: 2 additions & 4 deletions tm/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ def calculate_op_seq(*counts: Num) -> OpSeq:

match sub:
case Add():
if not isinstance(l := sub.l, int):
raise RuleLimit('sub_add')
assert isinstance(l := sub.l, int)

descent.append(
('+', -l))
Expand Down Expand Up @@ -170,8 +169,7 @@ def calculate_op_seq(*counts: Num) -> OpSeq:

match sup:
case Add():
if not isinstance(l := sup.l, int):
raise RuleLimit('sup_add')
assert isinstance(l := sup.l, int)

ascent.append(
('+', l))
Expand Down

0 comments on commit d2c9f16

Please sign in to comment.