2019-07-15 22:16:41 +03:00
|
|
|
import math.fractions as fractions
|
2019-07-04 14:24:53 +03:00
|
|
|
|
|
|
|
// Results are verified using https://www.calculatorsoup.com/calculators/math/fractions.php
|
|
|
|
|
|
|
|
fn test_fraction_creation() {
|
2019-07-15 22:16:41 +03:00
|
|
|
mut f1 := fractions.fraction(4,8)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.f64() == 0.5
|
|
|
|
assert f1.str().eq('4/8')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(10,5)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.f64() == 2.0
|
|
|
|
assert f1.str().eq('10/5')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(9,3)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.f64() == 3.0
|
|
|
|
assert f1.str().eq('9/3')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fraction_add() {
|
2019-07-15 22:16:41 +03:00
|
|
|
mut f1 := fractions.fraction(4,8)
|
|
|
|
mut f2 := fractions.fraction(5,10)
|
2019-07-04 14:24:53 +03:00
|
|
|
mut sum := f1 + f2
|
|
|
|
assert sum.f64() == 1.0
|
|
|
|
assert sum.str().eq('80/80')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(5,5)
|
|
|
|
f2 = fractions.fraction(8,8)
|
2019-07-04 14:24:53 +03:00
|
|
|
sum = f1 + f2
|
|
|
|
assert sum.f64() == 2.0
|
|
|
|
assert sum.str().eq('80/40')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(9,3)
|
|
|
|
f2 = fractions.fraction(1,3)
|
2019-07-04 14:24:53 +03:00
|
|
|
sum = f1 + f2
|
2019-08-20 11:18:12 +03:00
|
|
|
$if debug {
|
|
|
|
println(sum.f64())
|
|
|
|
}
|
2019-07-04 14:24:53 +03:00
|
|
|
assert sum.str().eq('10/3')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(3,7)
|
|
|
|
f2 = fractions.fraction(1,4)
|
2019-07-04 14:24:53 +03:00
|
|
|
sum = f1 + f2
|
2019-08-20 11:18:12 +03:00
|
|
|
$if debug {
|
|
|
|
println(sum.f64())
|
|
|
|
}
|
2019-07-04 14:24:53 +03:00
|
|
|
assert sum.str().eq('19/28')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fraction_subtract() {
|
2019-07-15 22:16:41 +03:00
|
|
|
mut f1 := fractions.fraction(4,8)
|
|
|
|
mut f2 := fractions.fraction(5,10)
|
2019-07-04 14:24:53 +03:00
|
|
|
mut diff := f2 - f1
|
|
|
|
assert diff.f64() == 0
|
|
|
|
assert diff.str().eq('0/80')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(5,5)
|
|
|
|
f2 = fractions.fraction(8,8)
|
2019-07-04 14:24:53 +03:00
|
|
|
diff = f2 - f1
|
|
|
|
assert diff.f64() == 0
|
|
|
|
assert diff.str().eq('0/40')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(9,3)
|
|
|
|
f2 = fractions.fraction(1,3)
|
2019-07-04 14:24:53 +03:00
|
|
|
diff = f1 - f2
|
2019-08-20 11:18:12 +03:00
|
|
|
$if debug {
|
|
|
|
println(diff.f64())
|
|
|
|
}
|
2019-07-04 14:24:53 +03:00
|
|
|
assert diff.str().eq('8/3')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(3,7)
|
|
|
|
f2 = fractions.fraction(1,4)
|
2019-07-04 14:24:53 +03:00
|
|
|
diff = f1 - f2
|
2019-08-20 11:18:12 +03:00
|
|
|
$if debug {
|
|
|
|
println(diff.f64())
|
|
|
|
}
|
2019-07-04 14:24:53 +03:00
|
|
|
assert diff.str().eq('5/28')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fraction_multiply() {
|
2019-07-15 22:16:41 +03:00
|
|
|
mut f1 := fractions.fraction(4,8)
|
|
|
|
mut f2 := fractions.fraction(5,10)
|
2019-07-04 14:24:53 +03:00
|
|
|
mut product := f1.multiply(f2)
|
|
|
|
assert product.f64() == 0.25
|
|
|
|
assert product.str().eq('20/80')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(5,5)
|
|
|
|
f2 = fractions.fraction(8,8)
|
2019-07-04 14:24:53 +03:00
|
|
|
product = f1.multiply(f2)
|
|
|
|
assert product.f64() == 1.0
|
|
|
|
assert product.str().eq('40/40')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(9,3)
|
|
|
|
f2 = fractions.fraction(1,3)
|
2019-07-04 14:24:53 +03:00
|
|
|
product = f1.multiply(f2)
|
|
|
|
assert product.f64() == 1.0
|
|
|
|
assert product.str().eq('9/9')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(3,7)
|
|
|
|
f2 = fractions.fraction(1,4)
|
2019-07-04 14:24:53 +03:00
|
|
|
product = f1.multiply(f2)
|
2019-08-20 11:18:12 +03:00
|
|
|
$if debug {
|
|
|
|
println(product.f64())
|
|
|
|
}
|
2019-07-04 14:24:53 +03:00
|
|
|
assert product.str().eq('3/28')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fraction_divide() {
|
2019-07-15 22:16:41 +03:00
|
|
|
mut f1 := fractions.fraction(4,8)
|
|
|
|
mut f2 := fractions.fraction(5,10)
|
2019-07-04 14:24:53 +03:00
|
|
|
mut re := f1.divide(f2)
|
|
|
|
assert re.f64() == 1.0
|
|
|
|
assert re.str().eq('40/40')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(5,5)
|
|
|
|
f2 = fractions.fraction(8,8)
|
2019-07-04 14:24:53 +03:00
|
|
|
re = f1.divide(f2)
|
|
|
|
assert re.f64() == 1.0
|
|
|
|
assert re.str().eq('40/40')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(9,3)
|
|
|
|
f2 = fractions.fraction(1,3)
|
2019-07-04 14:24:53 +03:00
|
|
|
re = f1.divide(f2)
|
|
|
|
assert re.f64() == 9.0
|
|
|
|
assert re.str().eq('27/3')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(3,7)
|
|
|
|
f2 = fractions.fraction(1,4)
|
2019-07-04 14:24:53 +03:00
|
|
|
re = f1.divide(f2)
|
2019-08-20 11:18:12 +03:00
|
|
|
$if debug {
|
|
|
|
println(re.f64())
|
|
|
|
}
|
2019-07-04 14:24:53 +03:00
|
|
|
assert re.str().eq('12/7')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fraction_reciprocal() {
|
2019-07-15 22:16:41 +03:00
|
|
|
mut f1 := fractions.fraction(4,8)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('8/4')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(5,10)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('10/5')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(5,5)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('5/5')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(8,8)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('8/8')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(9,3)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('3/9')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(1,3)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('3/1')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(3,7)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('7/3')
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(1,4)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.reciprocal().str().eq('4/1')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fraction_equals() {
|
2019-07-15 22:16:41 +03:00
|
|
|
mut f1 := fractions.fraction(4,8)
|
|
|
|
mut f2 := fractions.fraction(5,10)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert f1.equals(f2)
|
2019-07-15 22:16:41 +03:00
|
|
|
f1 = fractions.fraction(1,2)
|
|
|
|
f2 = fractions.fraction(3,4)
|
2019-07-04 14:24:53 +03:00
|
|
|
assert !f1.equals(f2)
|
2019-07-12 09:27:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_gcd_and_reduce(){
|
2019-11-06 06:57:04 +03:00
|
|
|
f := fractions.fraction(3, 9)
|
2019-07-12 09:27:10 +03:00
|
|
|
assert f.gcd() == 3
|
2019-11-06 06:57:04 +03:00
|
|
|
assert f.reduce().equals(fractions.fraction(1, 3))
|
2019-07-16 18:59:07 +03:00
|
|
|
}
|