Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
chore: enable as_tuple test
Browse files Browse the repository at this point in the history
- Use predicate so we don't need to implement
  DecimalTuple for now
  • Loading branch information
Christopher-Chianelli committed Jul 12, 2024
1 parent 4ac70d1 commit ca4b7e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ public int hashCode() {
// Other methods
// ***************************
public PythonInteger $method$adjusted() {
return PythonInteger.valueOf(getExponent());
// scale is the negative exponent that the big int is multiplied by
// len(unscaled) - 1 = floor(log_10(unscaled))
// floor(log_10(unscaled)) - scale = exponent in engineering notation
return PythonInteger.valueOf(value.unscaledValue().toString().length() - 1 - value.scale());
}

public PythonLikeTuple $method$as_integer_ratio() {
Expand All @@ -370,21 +373,14 @@ public int hashCode() {
PythonInteger.valueOf(reducedDenominator));
}

private int getExponent() {
// scale is the negative exponent that the big int is multiplied by
// len(unscaled) - 1 = floor(log_10(unscaled))
// floor(log_10(unscaled)) - scale = exponent in engineering notation
return value.unscaledValue().toString().length() - 1 - value.scale();
}

public PythonLikeTuple $method$as_tuple() {
// TODO: Use named tuple
return PythonLikeTuple.fromItems(PythonInteger.valueOf(value.signum()),
value.unscaledValue().toString()
return PythonLikeTuple.fromItems(PythonInteger.valueOf(value.signum() >= 0 ? 0 : 1),
value.unscaledValue().abs().toString()
.chars()
.mapToObj(digit -> PythonInteger.valueOf(digit - '0'))
.collect(Collectors.toCollection(PythonLikeTuple::new)),
PythonInteger.valueOf(getExponent()));
PythonInteger.valueOf(-value.scale()));
}

public PythonDecimal $method$canonical() {
Expand Down
24 changes: 15 additions & 9 deletions jpyinterpreter/tests/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,21 @@ def as_integer_ratio(a: Decimal) -> tuple[int, int]:
adjusted_verifier.verify(Decimal('-3.14'), expected_result=(-157, 50))


# TODO: Use named tuples
# def test_as_tuple():
# def as_tuple(a: Decimal) -> tuple[int, int, int]:
# return a.as_tuple()
#
# as_tuple_verifier = verifier_for(as_tuple)
# as_tuple_verifier.verify(Decimal(100), expected_result=(0, (1, 0, 0), 0))
# as_tuple_verifier.verify(Decimal(-100), expected_result=(1, (1, 0, 0), 0))
# as_tuple_verifier.verify(Decimal('123.45'), expected_result=(0, (1, 2, 3, 4, 5), -2))
# TODO: Make as_tuple use NamedTuple
def test_as_tuple():
def as_tuple(a: Decimal) -> tuple[int, tuple[int,...], int]:
return a.as_tuple()

def matches_tuple(t: tuple[int, tuple[int,...], int]) -> Callable[[tuple[int, tuple[int,...], int]], bool]:
def predicate(tested: tuple[int, tuple[int,...], int]) -> bool:
return t == tested

return predicate

as_tuple_verifier = verifier_for(as_tuple)
as_tuple_verifier.verify_property(Decimal(100), predicate=matches_tuple((0, (1, 0, 0), 0)))
as_tuple_verifier.verify_property(Decimal(-100), predicate=matches_tuple((1, (1, 0, 0), 0)))
as_tuple_verifier.verify_property(Decimal('123.45'), predicate=matches_tuple((0, (1, 2, 3, 4, 5), -2)))


def test_canonical():
Expand Down

0 comments on commit ca4b7e4

Please sign in to comment.