1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools: make v test-cleancode test everything by default (#10050)

This commit is contained in:
Delyan Angelov
2021-05-08 13:32:29 +03:00
committed by GitHub
parent cba2cb6b9c
commit 8a380f4699
132 changed files with 3230 additions and 3440 deletions

View File

@ -73,13 +73,13 @@ fn eval_cf(whole i64, den []i64) Fraction {
// within the default epsilon value (1.0e-4). This means the result will
// be accurate to 3 places after the decimal.
pub fn approximate(val f64) Fraction {
return approximate_with_eps(val, default_eps)
return approximate_with_eps(val, fractions.default_eps)
}
// approximate_with_eps returns a Fraction
pub fn approximate_with_eps(val f64, eps f64) Fraction {
if val == 0.0 {
return zero
return fractions.zero
}
if eps < 0.0 {
panic('Epsilon value cannot be negative.')
@ -96,12 +96,12 @@ pub fn approximate_with_eps(val f64, eps f64) Fraction {
return fraction(whole, 1)
}
mut d := []i64{}
mut partial := zero
mut partial := fractions.zero
// We must complete the approximation within the maximum number of
// itertations allowed. If we can't panic.
// Empirically tested: the hardest constant to approximate is the
// golden ratio (math.phi) and for f64s, it only needs 38 iterations.
for _ in 0 .. max_iterations {
for _ in 0 .. fractions.max_iterations {
// We calculate the reciprocal. That's why the numerator is
// always 1.
frac = 1.0 / frac
@ -115,5 +115,5 @@ pub fn approximate_with_eps(val f64, eps f64) Fraction {
}
frac -= f64(den)
}
panic("Couldn\'t converge. Please create an issue on https://github.com/vlang/v")
panic("Couldn't converge. Please create an issue on https://github.com/vlang/v")
}

View File

@ -69,27 +69,33 @@ fn test_140710_232() {
}
fn test_pi_1_digit() {
assert fractions.approximate_with_eps(math.pi, 5.0e-2).equals(fractions.fraction(22, 7))
assert fractions.approximate_with_eps(math.pi, 5.0e-2).equals(fractions.fraction(22,
7))
}
fn test_pi_2_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-3).equals(fractions.fraction(22, 7))
assert fractions.approximate_with_eps(math.pi, 5.0e-3).equals(fractions.fraction(22,
7))
}
fn test_pi_3_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-4).equals(fractions.fraction(333, 106))
assert fractions.approximate_with_eps(math.pi, 5.0e-4).equals(fractions.fraction(333,
106))
}
fn test_pi_4_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-5).equals(fractions.fraction(355, 113))
assert fractions.approximate_with_eps(math.pi, 5.0e-5).equals(fractions.fraction(355,
113))
}
fn test_pi_5_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-6).equals(fractions.fraction(355, 113))
assert fractions.approximate_with_eps(math.pi, 5.0e-6).equals(fractions.fraction(355,
113))
}
fn test_pi_6_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-7).equals(fractions.fraction(355, 113))
assert fractions.approximate_with_eps(math.pi, 5.0e-7).equals(fractions.fraction(355,
113))
}
fn test_pi_7_digits() {
@ -123,15 +129,18 @@ fn test_pi_12_digits() {
}
fn test_phi_1_digit() {
assert fractions.approximate_with_eps(math.phi, 5.0e-2).equals(fractions.fraction(5, 3))
assert fractions.approximate_with_eps(math.phi, 5.0e-2).equals(fractions.fraction(5,
3))
}
fn test_phi_2_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-3).equals(fractions.fraction(21, 13))
assert fractions.approximate_with_eps(math.phi, 5.0e-3).equals(fractions.fraction(21,
13))
}
fn test_phi_3_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-4).equals(fractions.fraction(55, 34))
assert fractions.approximate_with_eps(math.phi, 5.0e-4).equals(fractions.fraction(55,
34))
}
fn test_phi_4_digits() {