Skip to content

Commit

Permalink
Move some tests where they belong
Browse files Browse the repository at this point in the history
  • Loading branch information
db47h committed May 4, 2020
1 parent cb1bfec commit 515766f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 75 deletions.
59 changes: 59 additions & 0 deletions dec_arith_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package decimal

import (
"math/rand"
"reflect"
"strconv"
"testing"
"time"
)

func TestAdd10VW(t *testing.T) {
td := []struct {
i dec
x Word
o dec
c Word
s int64
}{
{dec{_BD - 2, _BD - 1}, 2, dec{}, 1, 0},
{dec{_BD - 2, _BD - 1}, 1, dec{_BD - 1, _BD - 1}, 0, 0},
{dec{_BD - 2, _BD - 2}, 2, dec{0, _BD - 1}, 0, 0},
}
for i, d := range td {
t.Run(strconv.Itoa(i), func(t *testing.T) {
z := d.i
c := add10VW(z, z, d.x)
var s int64
z = z.norm()
if len(z) > 0 {
s = dnorm(z)
}
if !reflect.DeepEqual(z, d.o) || s != d.s || c != d.c {
t.Fatalf("addW failed: expected z = %v, s = %d, c = %d, got d = %v, s = %v, c = %v", d.o, d.s, d.c, z, s, c)
}

})
}
}

func TestDecDigits(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for i := 0; i < 10000; i++ {
n := uint(rand.Uint64())
d := uint(0)
for m := n; m != 0; m /= 10 {
d++
}
if dd := decDigits(n); dd != d {
t.Fatalf("decDigits(%d) = %d, expected %d", n, dd, d)
}
}
}

func BenchmarkDecDigits(b *testing.B) {
rand.Seed(0xdeadbeefbadf00d)
for i := 0; i < b.N; i++ {
benchU = decDigits(uint(rand.Uint64()) % _BD)
}
}
2 changes: 1 addition & 1 deletion dec_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (z dec) scan(r io.ByteScanner, base int, fracOk bool) (res dec, b, count in
d1 = MaxBase + 1
}
if d1 >= b1 {
r.UnreadByte() // ch does not belong to number anymore
err = r.UnreadByte() // ch does not belong to number anymore
break
}
prev = '0'
Expand Down
75 changes: 1 addition & 74 deletions dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package decimal
import (
"math/bits"
"math/rand"
"reflect"
"strconv"
"testing"
"time"
Expand All @@ -28,58 +27,6 @@ func TestDec_ntz(t *testing.T) {
}
}

func Test_mag(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for i := 0; i < 10000; i++ {
n := uint(rand.Uint64())
d := uint(0)
for m := n; m != 0; m /= 10 {
d++
}
if dd := decDigits(n); dd != d {
t.Fatalf("mag(%d) = %d, expected %d", n, dd, d)
}
}
}

// TODO(db47h): remove this function
func Test_dec_setInt(t *testing.T) {
// // TODO(db47h): next step
// b, _ := new(big.Int).SetString("12345678901234567890", 0)
// d, exp := dec{}.make(3).setInt(b)
// t.Log(d, exp)
// t.Log(string(dtoa(d, 10)))
}

func Test_add10VW(t *testing.T) {
td := []struct {
i dec
x Word
o dec
c Word
s int64
}{
{dec{_BD - 2, _BD - 1}, 2, dec{}, 1, 0},
{dec{_BD - 2, _BD - 1}, 1, dec{_BD - 1, _BD - 1}, 0, 0},
{dec{_BD - 2, _BD - 2}, 2, dec{0, _BD - 1}, 0, 0},
}
for i, d := range td {
t.Run(strconv.Itoa(i), func(t *testing.T) {
z := d.i
c := add10VW(z, z, d.x)
var s int64
z = z.norm()
if len(z) > 0 {
s = dnorm(z)
}
if !reflect.DeepEqual(z, d.o) || s != d.s || c != d.c {
t.Fatalf("addW failed: expected z = %v, s = %d, c = %d, got d = %v, s = %v, c = %v", d.o, d.s, d.c, z, s, c)
}

})
}
}

func TestDec_digit(t *testing.T) {
data := []struct {
d dec
Expand Down Expand Up @@ -107,27 +54,7 @@ var (
benchU uint
)

// func Benchmark_dnorm(b *testing.B) {
// rand.Seed(0xdeadbeefbadf00d)
// d := dec{}.make(10000)
// for i := range d {
// d[i] = Word(rand.Uint64()) % _BD
// }
// for i := 0; i < b.N; i++ {
// d[0] = Word(rand.Uint64()) % _BD
// d[len(d)-1] = Word(rand.Uint64()) % _BD
// benchD, benchU = d.dnorm()
// }
// }

func Benchmark_mag(b *testing.B) {
rand.Seed(0xdeadbeefbadf00d)
for i := 0; i < b.N; i++ {
benchU = decDigits(uint(rand.Uint64()) % _BD)
}
}

func Benchmark_dec_Digits(b *testing.B) {
func BenchmarkDec_Digits(b *testing.B) {
rand.Seed(0xdeadbeefbadf00d)
d := dec{}.make(10000)
for i := range d {
Expand Down
13 changes: 13 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ func TestDecimal_SetString(t *testing.T) {
// })
// }
}

func BenchmarkDnorm(b *testing.B) {
rand.Seed(0xdeadbeefbadf00d)
d := dec{}.make(1000)
for i := range d {
d[i] = Word(rand.Uint64()) % _BD
}
for i := 0; i < b.N; i++ {
d[0] = Word(rand.Uint64()) % _BD
d[len(d)-1] = Word(rand.Uint64()) % _BD
benchU = uint(dnorm(d))
}
}

0 comments on commit 515766f

Please sign in to comment.