Browse Source

codec: test: add half float test for cbor

Ugorji Nwoke 8 years ago
parent
commit
d03304bb1d
3 changed files with 28 additions and 6 deletions
  1. 25 3
      codec/cbor_test.go
  2. 0 3
      codec/codec_test.go
  3. 3 0
      codec/z_all_test.go

+ 25 - 3
codec/cbor_test.go

@@ -97,7 +97,7 @@ type testCborGolden struct {
 }
 }
 
 
 // Some tests are skipped because they include numbers outside the range of int64/uint64
 // Some tests are skipped because they include numbers outside the range of int64/uint64
-func doTestCborGoldens(t *testing.T) {
+func TestCborGoldens(t *testing.T) {
 	oldMapType := testCborH.MapType
 	oldMapType := testCborH.MapType
 	defer func() {
 	defer func() {
 		testCborH.MapType = oldMapType
 		testCborH.MapType = oldMapType
@@ -200,6 +200,28 @@ func testCborError(t *testing.T, i int, v0, v1 interface{}, err error, equal *bo
 	// fmt.Printf("%v testCborError passed (checks passed)\n", i)
 	// fmt.Printf("%v testCborError passed (checks passed)\n", i)
 }
 }
 
 
-func TestCborGoldens(t *testing.T) {
-	doTestCborGoldens(t)
+func TestCborHalfFloat(t *testing.T) {
+	m := map[uint16]float64{
+		0x3c00: 1,
+		0x3c01: 1 + math.Pow(2, -10),
+		0xc000: -2,
+		//0x0800: math.Pow(2, -14),
+		0x03ff: math.Pow(2, -14) - math.Pow(2, -24),
+		0x0001: math.Pow(2, -24),
+		0x0000: 0,
+		0x8000: -0.0,
+	}
+	var ba [3]byte
+	ba[0] = cborBdFloat16
+	var res float64
+	for k, v := range m {
+		res = 0
+		bigen.PutUint16(ba[1:], k)
+		testUnmarshalErr(&res, ba[:3], testCborH, t, "-")
+		if res == v {
+			logT(t, "equal floats: from %x %b, %v", k, k, v)
+		} else {
+			failT(t, "unequal floats: from %v %b, %v != %v", k, k, res, v)
+		}
+	}
 }
 }

+ 0 - 3
codec/codec_test.go

@@ -2515,6 +2515,3 @@ func TestSimpleUintToInt(t *testing.T) {
 // - mapbyslice (for non-fastpath things)
 // - mapbyslice (for non-fastpath things)
 // - nil and 0-len slices and maps for non-fastpath things
 // - nil and 0-len slices and maps for non-fastpath things
 // - pointers to scalars at top-level e.g. v := uint(7), encode(&v)
 // - pointers to scalars at top-level e.g. v := uint(7), encode(&v)
-//
-// cbor:
-// - halffloat

+ 3 - 0
codec/z_all_test.go

@@ -213,6 +213,7 @@ func testCodecGroup(t *testing.T) {
 	t.Run("TestSimpleUintToInt", TestSimpleUintToInt)
 	t.Run("TestSimpleUintToInt", TestSimpleUintToInt)
 
 
 	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
 	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
+	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 	// <tear-down code>
 	// <tear-down code>
 }
 }
 
 
@@ -282,6 +283,8 @@ func testCborGroup(t *testing.T) {
 	t.Run("TestCborMammothMapsAndSlices", TestCborMammothMapsAndSlices)
 	t.Run("TestCborMammothMapsAndSlices", TestCborMammothMapsAndSlices)
 	t.Run("TestCborTime", TestCborTime)
 	t.Run("TestCborTime", TestCborTime)
 	t.Run("TestCborUintToInt", TestCborUintToInt)
 	t.Run("TestCborUintToInt", TestCborUintToInt)
+
+	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 }
 }
 
 
 func testMsgpackGroup(t *testing.T) {
 func testMsgpackGroup(t *testing.T) {