Skip to content

Commit f4547ae

Browse files
committed
rename int_power → pow + extend prop test generators
1 parent 03dd591 commit f4547ae

File tree

3 files changed

+70
-53
lines changed

3 files changed

+70
-53
lines changed

aiken.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ requirements = []
1313
source = "github"
1414

1515
[etags]
16-
"aiken-lang/fuzz@v2" = [{ secs_since_epoch = 1726162866, nanos_since_epoch = 876390466 }, "34ffec10cce786bf823c7505589a3b5e0663792ef8efd31f870d7bcc37e0f593"]
16+
"aiken-lang/fuzz@v2" = [{ secs_since_epoch = 1726317927, nanos_since_epoch = 830395000 }, "34ffec10cce786bf823c7505589a3b5e0663792ef8efd31f870d7bcc37e0f593"]

lib/aiken/math/rational.ak

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -821,51 +821,51 @@ test truncate_1() {
821821
}
822822
}
823823

824+
/// Computes the rational number x raised to the power y. Returns `None` for
825+
/// invalid exponentiation.
824826
///
825827
/// ```aiken
826-
/// It computes the rational number x raised to the power y and returns an
827-
/// Option<Rational> (either Some(Rational) or None).
828-
///
829828
/// expect Some(x) = rational.new(50, 2500)
830-
/// rational.reduce(rational.int_power(x, 3)) == rational.new(1, 125000)
831-
///
829+
/// rational.reduce(rational.pow(x, 3)) == rational.new(1, 125000)
830+
///
832831
/// expect Some(x) = rational.new(50, 2500)
833-
/// rational.reduce(rational.int_power(x, -3)) == rational.new(125000, 1)
832+
/// rational.reduce(rational.pow(x, -3)) == rational.new(125000, 1)
834833
/// ```
835-
pub fn int_power(x: Rational, y: Int) -> Option<Rational> {
834+
pub fn pow(x: Rational, y: Int) -> Option<Rational> {
836835
let Rational { numerator: a, denominator: b } = x
837-
836+
838837
if a == 0 && y <= 0 {
839-
None
838+
None
840839
} else if y > 0 {
841-
Some(Rational { numerator: math.pow(a, y), denominator: math.pow(b, y) })
840+
Some(Rational { numerator: math.pow(a, y), denominator: math.pow(b, y) })
842841
} else if y < 0 {
843-
Some(Rational { numerator: math.pow(b, math.abs(y)), denominator: math.pow(a, math.abs(y)) })
842+
Some(Rational { numerator: math.pow(b, -y), denominator: math.pow(a, -y) })
844843
} else {
845-
Some(Rational { numerator: 1, denominator: 1 })
844+
Some(Rational { numerator: 1, denominator: 1 })
846845
}
847846
}
848847

849-
test int_power_negative_exponent_non_zero_fraction() {
848+
test pow_negative_exponent_non_zero_fraction() {
850849
expect Some(base) = new(50, 2500)
851-
expect Some(calculated_result) = int_power(base, -3)
850+
expect Some(calculated_result) = pow(base, -3)
852851
expect Some(expected_result) = new(125000, 1)
853852
reduce(calculated_result) == expected_result
854853
}
855854

856-
test int_power_positive_exponent() {
855+
test pow_positive_exponent() {
857856
expect Some(base) = new(50, 2500)
858-
expect Some(calculated_result) = int_power(base, 3)
857+
expect Some(calculated_result) = pow(base, 3)
859858
expect Some(expected_result) = new(1, 125000)
860859
reduce(calculated_result) == expected_result
861860
}
862861

863-
test int_power_exponent_zero() {
862+
test pow_exponent_zero() {
864863
expect Some(base) = new(50, 2500)
865-
int_power(base, 0) == new(1, 1)
864+
pow(base, 0) == new(1, 1)
865+
}
866+
867+
test pow_rational_zero_exponent_zero() {
868+
expect Some(base) = new(0, 1)
869+
pow(base, 0) == None
866870
}
867871

868-
test int_power_rational_zero_exponent_zero(){
869-
expect Some(base) = new(0,1)
870-
int_power(base,0) == None
871-
}

lib/aiken/math/rational.tests.ak

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,65 @@
1-
use aiken/math/rational.{Rational, int_power, new}
21
use aiken/fuzz.{both, either, map}
2+
use aiken/math/rational.{Rational, new, pow}
33

4-
fn positive_rational_fuzz() -> Fuzzer<Rational>{
5-
map(
6-
both(fuzz.int_at_least(1), fuzz.int_at_least(1)),
7-
fn((num,den)){
8-
expect Some(new_fraction) = new(num,den)
9-
new_fraction
10-
}
4+
const any_positive_rational: Fuzzer<Rational> =
5+
either(
6+
map(
7+
both(fuzz.int_at_least(1), fuzz.int_at_least(1)),
8+
fn((num, den)) {
9+
expect Some(new_fraction) = new(num, den)
10+
new_fraction
11+
},
12+
),
13+
map(
14+
both(fuzz.int_at_most(-1), fuzz.int_at_most(-1)),
15+
fn((num, den)) {
16+
expect Some(new_fraction) = new(num, den)
17+
new_fraction
18+
},
19+
),
1120
)
12-
}
1321

14-
fn negative_rational_fuzz() -> Fuzzer<Rational>{
15-
map(both(fuzz.int_at_most(-1),fuzz.int_at_least(1)),
16-
fn((num,den)){
17-
expect Some(new_fraction) = new(num,den)
18-
new_fraction
19-
}
22+
const any_negative_rational: Fuzzer<Rational> =
23+
either(
24+
map(
25+
both(fuzz.int_at_most(-1), fuzz.int_at_least(1)),
26+
fn((num, den)) {
27+
expect Some(new_fraction) = new(num, den)
28+
new_fraction
29+
},
30+
),
31+
map(
32+
both(fuzz.int_at_least(1), fuzz.int_at_most(-1)),
33+
fn((num, den)) {
34+
expect Some(new_fraction) = new(num, den)
35+
new_fraction
36+
},
37+
),
2038
)
21-
}
2239

23-
fn rational_fuzz() -> Fuzzer<Rational> {
24-
either(negative_rational_fuzz(),positive_rational_fuzz())
25-
}
40+
const any_non_zero_rational: Fuzzer<Rational> =
41+
either(any_negative_rational, any_positive_rational)
2642

27-
test prop_power_of_zero_returns_one(rational via rational_fuzz()){
28-
expect Some(calculated_result) = int_power(rational, 0)
29-
expect Some(expected_result) = new(1,1)
43+
test prop_power_of_zero_returns_one(rational via any_non_zero_rational) {
44+
expect Some(calculated_result) = pow(rational, 0)
45+
expect Some(expected_result) = new(1, 1)
3046
calculated_result == expected_result
3147
}
3248

33-
test prop_power_of_one_returns_same_fraction(rational via rational_fuzz()){
34-
expect Some(calculated_result) = int_power(rational, 1)
49+
test prop_power_of_one_returns_same_fraction(rational via any_non_zero_rational) {
50+
expect Some(calculated_result) = pow(rational, 1)
3551
calculated_result == rational
3652
}
3753

38-
test prop_power_numerator_zero_exponent_negative_returns_none((denominator,exponent) via both(fuzz.int_at_least(1),fuzz.int_at_most(-1))){
54+
test prop_power_numerator_zero_exponent_negative_returns_none(
55+
(denominator, exponent) via both(fuzz.int_at_least(1), fuzz.int_at_most(-1)),
56+
) {
3957
expect Some(fraction) = new(0, denominator)
40-
expect None = int_power(fraction,exponent)
58+
expect None = pow(fraction, exponent)
4159
}
4260

43-
4461
test prop_power_unit_fraction_is_immutable(exponent via fuzz.int()) {
45-
expect Some(unit) = new(1,1)
46-
expect Some(calculated_result) = int_power(unit, exponent)
62+
expect Some(unit) = new(1, 1)
63+
expect Some(calculated_result) = pow(unit, exponent)
4764
calculated_result == unit
48-
}
65+
}

0 commit comments

Comments
 (0)