]> Cypherpunks repositories - gostls13.git/commitdiff
testing: fix rounding error in roundDown10
authorDave Cheney <dave@cheney.net>
Sat, 1 Jun 2013 23:13:12 +0000 (09:13 +1000)
committerDave Cheney <dave@cheney.net>
Sat, 1 Jun 2013 23:13:12 +0000 (09:13 +1000)
Fixes #5599.

Thanks to minux.ma for the suggested fix.

As we now have a harness to test testing internal functions I added some coverage for testing.roundUp, as it is the main consumer of roundDown10.

R=minux.ma, kr, r
CC=golang-dev
https://golang.org/cl/9926043

src/pkg/testing/benchmark.go
src/pkg/testing/benchmark_test.go
src/pkg/testing/export_test.go

index 25fb2d61918dbfc37d7d9290438ccafb756fc70f..3473c5b2cbffdb3c4de3b4a0430bdcc50d61138f 100644 (file)
@@ -138,7 +138,7 @@ func max(x, y int) int {
 func roundDown10(n int) int {
        var tens = 0
        // tens = floor(log_10(n))
-       for n > 10 {
+       for n >= 10 {
                n = n / 10
                tens++
        }
@@ -153,13 +153,16 @@ func roundDown10(n int) int {
 // roundUp rounds x up to a number of the form [1eX, 2eX, 5eX].
 func roundUp(n int) int {
        base := roundDown10(n)
-       if n < (2 * base) {
+       switch {
+       case n <= base:
+               return base
+       case n <= (2 * base):
                return 2 * base
-       }
-       if n < (5 * base) {
+       case n <= (5 * base):
                return 5 * base
+       default:
+               return 10 * base
        }
-       return 10 * base
 }
 
 // run times the benchmark function in a separate goroutine.
index 5ed4e03a4a9c9deb8412f893ceceaa526d46b83e..94e994dfae08b6af86d09c94ba7dd62ddfa111c0 100644 (file)
@@ -13,19 +13,46 @@ var roundDownTests = []struct {
 }{
        {1, 1},
        {9, 1},
-       {10, 1},
+       {10, 10},
        {11, 10},
-       {100, 10},
-       //      {101, 100}, // issue 5599
-       {1000, 100},
-       //      {1001, 1000}, // issue 5599
+       {100, 100},
+       {101, 100},
+       {999, 100},
+       {1000, 1000},
+       {1001, 1000},
 }
 
 func TestRoundDown10(t *testing.T) {
        for _, tt := range roundDownTests {
                actual := testing.RoundDown10(tt.v)
                if tt.expected != actual {
-                       t.Errorf("roundDown10: expected %v, actual %v", tt.expected, actual)
+                       t.Errorf("roundDown10(%d): expected %d, actual %d", tt.v, tt.expected, actual)
+               }
+       }
+}
+
+var roundUpTests = []struct {
+       v, expected int
+}{
+       {0, 1},
+       {1, 1},
+       {2, 2},
+       {5, 5},
+       {9, 10},
+       {999, 1000},
+       {1000, 1000},
+       {1400, 2000},
+       {1700, 2000},
+       {4999, 5000},
+       {5000, 5000},
+       {5001, 10000},
+}
+
+func TestRoundUp(t *testing.T) {
+       for _, tt := range roundUpTests {
+               actual := testing.RoundUp(tt.v)
+               if tt.expected != actual {
+                       t.Errorf("roundUp(%d): expected %d, actual %d", tt.v, tt.expected, actual)
                }
        }
 }
index 3084efd87d398bfc5fed0b6872870e18ca97a35b..89781b439f42328915480b55c1de9e0ffb65ddea 100644 (file)
@@ -4,4 +4,7 @@
 
 package testing
 
-var RoundDown10 = roundDown10
+var (
+       RoundDown10 = roundDown10
+       RoundUp     = roundUp
+)