From 7f4c3cda4d0c5ed8b645f375a62f18e380fd92e2 Mon Sep 17 00:00:00 2001 From: eulerkochy Date: Fri, 12 Jul 2019 11:57:10 +0530 Subject: [PATCH] reduce redundant code, add tests for reduce and gcd --- vlib/math/fraction.v | 2 +- vlib/math/fraction_test.v | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/vlib/math/fraction.v b/vlib/math/fraction.v index c388210ad0..d4e25ff4c9 100644 --- a/vlib/math/fraction.v +++ b/vlib/math/fraction.v @@ -87,7 +87,7 @@ pub fn (f1 Fraction) gcd() i64 { // Fraction method which reduces the fraction pub fn (f1 Fraction) reduce() Fraction { - cf := gcd(f1.n, f1.d) + cf := f1.gcd() return Fraction{f1.n / cf, f1.d / cf} } diff --git a/vlib/math/fraction_test.v b/vlib/math/fraction_test.v index 613a001a19..49b7515800 100644 --- a/vlib/math/fraction_test.v +++ b/vlib/math/fraction_test.v @@ -132,4 +132,10 @@ fn test_fraction_equals() { f1 = math.fraction(1,2) f2 = math.fraction(3,4) assert !f1.equals(f2) +} + +fn test_gcd_and_reduce(){ + mut f := math.fraction(3, 9) + assert f.gcd() == 3 + assert f.reduce().equals(math.fraction(1, 3)) } \ No newline at end of file