Skip to content

Commit

Permalink
binary16: use text/template for test case generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Aug 27, 2018
1 parent afb3e28 commit 28caf66
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 107 deletions.
1 change: 0 additions & 1 deletion binary16/extra_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions binary16/extra_test.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Code generated by go run gen.go; DO NOT EDIT.

package binary16

import (
"math"
"testing"
)

func TestNewFromBitsNormalized(t *testing.T) {
for _, g := range goldenNormalized {
f := NewFromBits(g.bits)
got, _ := f.Float64()
wantBits := math.Float64bits(g.want)
gotBits := math.Float64bits(got)
if wantBits != gotBits {
t.Errorf("0x%04X: number mismatch; expected 0x%016X (%v), got 0x%016X (%v)", g.bits, wantBits, g.want, gotBits, got)
}
}
}

func TestNewFromBitsDenormalized(t *testing.T) {
for _, g := range goldenDenormalized {
f := NewFromBits(g.bits)
got, _ := f.Float64()
wantBits := math.Float64bits(g.want)
gotBits := math.Float64bits(got)
if wantBits != gotBits {
t.Errorf("0x%04X: number mismatch; expected 0x%016X (%v), got 0x%016X (%v)", g.bits, wantBits, g.want, gotBits, got)
}
}
}

func TestNewFromFloat32Normalized(t *testing.T) {
for _, g := range goldenNormalized {
in := float32(g.want)
f, acc := NewFromFloat32(in)
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", in, g.bits, got)
}
}
}

func TestNewFromFloat64Normalized(t *testing.T) {
for _, g := range goldenNormalized {
f, acc := NewFromFloat64(g.want)
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", g.want, g.bits, got)
}
}
}

func TestNewFromFloat32Denormalized(t *testing.T) {
for _, g := range goldenDenormalized {
f, acc := NewFromFloat32(float32(g.want))
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", g.want, g.bits, got)
}
}
}

func TestNewFromFloat64Denormalized(t *testing.T) {
for _, g := range goldenDenormalized {
f, acc := NewFromFloat64(g.want)
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", g.want, g.bits, got)
}
}
}

var goldenNormalized = []struct {
bits uint16
want float64
}{
// Normalized values.
{{- range .normalized }}
{{ . }},
{{- end }}
}

var goldenDenormalized = []struct {
bits uint16
want float64
}{
// Denormalized values.
{{- range .denormalized }}
{{ . }},
{{- end }}
}
130 changes: 24 additions & 106 deletions binary16/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package main
import (
"flag"
"fmt"
"io"
"html/template"
"log"
"math"
"math/big"
"os"

"github.com/pkg/errors"
)

func main() {
Expand All @@ -24,108 +26,27 @@ func main() {
func dumpTest(path string) error {
f, err := os.Create(path)
if err != nil {
return err
return errors.WithStack(err)
}
defer f.Close()

const pre = `
// Code generated by go run gen.go; DO NOT EDIT.
package binary16
import (
"math"
"testing"
)
func TestNewFromBitsNormalized(t *testing.T) {
for _, g := range goldenNormalized {
f := NewFromBits(g.bits)
got, _ := f.Float64()
wantBits := math.Float64bits(g.want)
gotBits := math.Float64bits(got)
if wantBits != gotBits {
t.Errorf("0x%04X: number mismatch; expected 0x%016X (%v), got 0x%016X (%v)", g.bits, wantBits, g.want, gotBits, got)
}
}
}
func TestNewFromBitsDenormalized(t *testing.T) {
for _, g := range goldenDenormalized {
f := NewFromBits(g.bits)
got, _ := f.Float64()
wantBits := math.Float64bits(g.want)
gotBits := math.Float64bits(got)
if wantBits != gotBits {
t.Errorf("0x%04X: number mismatch; expected 0x%016X (%v), got 0x%016X (%v)", g.bits, wantBits, g.want, gotBits, got)
}
}
}
func TestNewFromFloat32Normalized(t *testing.T) {
for _, g := range goldenNormalized {
in := float32(g.want)
f, acc := NewFromFloat32(in)
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", in, g.bits, got)
}
t, err := template.ParseFiles("extra_test.tmpl")
if err != nil {
return errors.WithStack(err)
}
}
func TestNewFromFloat64Normalized(t *testing.T) {
for _, g := range goldenNormalized {
f, acc := NewFromFloat64(g.want)
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", g.want, g.bits, got)
}
data := map[string][]string{
"normalized": getNormalized(),
"denormalized": getDenormalized(),
}
}
func TestNewFromFloat32Denormalized(t *testing.T) {
for _, g := range goldenDenormalized {
f, acc := NewFromFloat32(float32(g.want))
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", g.want, g.bits, got)
}
if err := t.Execute(f, data); err != nil {
return errors.WithStack(err)
}
}
func TestNewFromFloat64Denormalized(t *testing.T) {
for _, g := range goldenDenormalized {
f, acc := NewFromFloat64(g.want)
_ = acc
got := f.Bits()
if g.bits != got {
t.Errorf("%v: bits mismatch; expected 0x%04X, got 0x%04X", g.want, g.bits, got)
}
}
}
`
fmt.Fprintln(f, pre[1:])
printNormalized(f)
fmt.Fprintln(f)
printDenormalized(f)

return nil
}

const bias = 15

func printNormalized(w io.Writer) {
const pre = `
var goldenNormalized = []struct {
bits uint16
want float64
}{
// Normalized values.`
fmt.Fprintln(w, pre[1:])
func getNormalized() []string {
var ns []string
// normalized
// 0b00001 - 0b11110
//
Expand All @@ -148,21 +69,16 @@ var goldenNormalized = []struct {
bits := uint16(signbit) << 15
bits |= uint16(exp) << 10
bits |= uint16(mant)
fmt.Fprintf(w, "\t{bits: 0x%04X, want: %v},\n", bits, x)
n := fmt.Sprintf("{bits: 0x%04X, want: %v}", bits, x)
ns = append(ns, n)
}
}
}
fmt.Fprintln(w, "}")
return ns
}

func printDenormalized(w io.Writer) {
const pre = `
var goldenDenormalized = []struct {
bits uint16
want float64
}{
// Denormalized values.`
fmt.Fprintln(w, pre[1:])
func getDenormalized() []string {
var ds []string
// denormalized
// 0b00000
//
Expand All @@ -187,11 +103,13 @@ var goldenDenormalized = []struct {
bits |= uint16(mant)
if bits == 0x8000 {
// -zero
fmt.Fprintf(w, "\t{bits: 0x%04X, want: math.Copysign(0, -1)},\n", bits)
d := fmt.Sprintf("{bits: 0x%04X, want: math.Copysign(0, -1)}", bits)
ds = append(ds, d)
} else {
fmt.Fprintf(w, "\t{bits: 0x%04X, want: %v},\n", bits, x)
d := fmt.Sprintf("{bits: 0x%04X, want: %v}", bits, x)
ds = append(ds, d)
}
}
}
fmt.Fprintln(w, "}")
return ds
}

0 comments on commit 28caf66

Please sign in to comment.