goprotobuf: Write C++-compatible ±Inf and NaN floats in text format.
R=r
CC=golang-dev
https://codereview.appspot.com/10113046
diff --git a/proto/text_test.go b/proto/text_test.go
index e3bb791..f5d0574 100644
--- a/proto/text_test.go
+++ b/proto/text_test.go
@@ -35,6 +35,7 @@
"bytes"
"errors"
"io/ioutil"
+ "math"
"strings"
"testing"
@@ -333,3 +334,24 @@
}
}
}
+
+func TestFloats(t *testing.T) {
+ tests := []struct {
+ f float64
+ want string
+ }{
+ {0, "0"},
+ {4.7, "4.7"},
+ {math.Inf(1), "inf"},
+ {math.Inf(-1), "-inf"},
+ {math.NaN(), "nan"},
+ }
+ for _, test := range tests {
+ msg := &pb.FloatingPoint{F: &test.f}
+ got := strings.TrimSpace(msg.String())
+ want := `f:` + test.want
+ if got != want {
+ t.Errorf("f=%f: got %q, want %q", test.f, got, want)
+ }
+ }
+}