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

Commit ca4b7e4

Browse files
chore: enable as_tuple test
- Use predicate so we don't need to implement DecimalTuple for now
1 parent 4ac70d1 commit ca4b7e4

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonDecimal.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ public int hashCode() {
346346
// Other methods
347347
// ***************************
348348
public PythonInteger $method$adjusted() {
349-
return PythonInteger.valueOf(getExponent());
349+
// scale is the negative exponent that the big int is multiplied by
350+
// len(unscaled) - 1 = floor(log_10(unscaled))
351+
// floor(log_10(unscaled)) - scale = exponent in engineering notation
352+
return PythonInteger.valueOf(value.unscaledValue().toString().length() - 1 - value.scale());
350353
}
351354

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

373-
private int getExponent() {
374-
// scale is the negative exponent that the big int is multiplied by
375-
// len(unscaled) - 1 = floor(log_10(unscaled))
376-
// floor(log_10(unscaled)) - scale = exponent in engineering notation
377-
return value.unscaledValue().toString().length() - 1 - value.scale();
378-
}
379-
380376
public PythonLikeTuple $method$as_tuple() {
381377
// TODO: Use named tuple
382-
return PythonLikeTuple.fromItems(PythonInteger.valueOf(value.signum()),
383-
value.unscaledValue().toString()
378+
return PythonLikeTuple.fromItems(PythonInteger.valueOf(value.signum() >= 0 ? 0 : 1),
379+
value.unscaledValue().abs().toString()
384380
.chars()
385381
.mapToObj(digit -> PythonInteger.valueOf(digit - '0'))
386382
.collect(Collectors.toCollection(PythonLikeTuple::new)),
387-
PythonInteger.valueOf(getExponent()));
383+
PythonInteger.valueOf(-value.scale()));
388384
}
389385

390386
public PythonDecimal $method$canonical() {

jpyinterpreter/tests/test_decimal.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,21 @@ def as_integer_ratio(a: Decimal) -> tuple[int, int]:
301301
adjusted_verifier.verify(Decimal('-3.14'), expected_result=(-157, 50))
302302

303303

304-
# TODO: Use named tuples
305-
# def test_as_tuple():
306-
# def as_tuple(a: Decimal) -> tuple[int, int, int]:
307-
# return a.as_tuple()
308-
#
309-
# as_tuple_verifier = verifier_for(as_tuple)
310-
# as_tuple_verifier.verify(Decimal(100), expected_result=(0, (1, 0, 0), 0))
311-
# as_tuple_verifier.verify(Decimal(-100), expected_result=(1, (1, 0, 0), 0))
312-
# as_tuple_verifier.verify(Decimal('123.45'), expected_result=(0, (1, 2, 3, 4, 5), -2))
304+
# TODO: Make as_tuple use NamedTuple
305+
def test_as_tuple():
306+
def as_tuple(a: Decimal) -> tuple[int, tuple[int,...], int]:
307+
return a.as_tuple()
308+
309+
def matches_tuple(t: tuple[int, tuple[int,...], int]) -> Callable[[tuple[int, tuple[int,...], int]], bool]:
310+
def predicate(tested: tuple[int, tuple[int,...], int]) -> bool:
311+
return t == tested
312+
313+
return predicate
314+
315+
as_tuple_verifier = verifier_for(as_tuple)
316+
as_tuple_verifier.verify_property(Decimal(100), predicate=matches_tuple((0, (1, 0, 0), 0)))
317+
as_tuple_verifier.verify_property(Decimal(-100), predicate=matches_tuple((1, (1, 0, 0), 0)))
318+
as_tuple_verifier.verify_property(Decimal('123.45'), predicate=matches_tuple((0, (1, 2, 3, 4, 5), -2)))
313319

314320

315321
def test_canonical():

0 commit comments

Comments
 (0)