Skip to content

Commit ee860ab

Browse files
committed
Implement floating-point values
1 parent e09c292 commit ee860ab

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: go
33
go:
44
- 1.11.x
55
- 1.12.x
6+
- 1.13.x
67

78
env:
89
- GO111MODULE=on

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ developed to be used in [`dtn7-go`][dtn7-go], an implementation of the
1010

1111
- Supports a selected subset of [CBOR's][cbor] features:
1212
- Unsigned Integer
13+
- Floating-point values
1314
- Byte and Text String
1415
- Arrays, both of definite and indefinite length
1516
- Maps of definite length
@@ -20,6 +21,6 @@ developed to be used in [`dtn7-go`][dtn7-go], an implementation of the
2021
- Surprisingly fast
2122

2223

23-
[bpbis]: https://tools.ietf.org/html/draft-ietf-dtn-bpbis-13
24+
[bpbis]: https://tools.ietf.org/html/draft-ietf-dtn-bpbis-14
2425
[cbor]: https://tools.ietf.org/html/rfc7049
2526
[dtn7-go]: https://github.com/dtn7/dtn7-go

simple.go

+35
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cboring
33
import (
44
"fmt"
55
"io"
6+
"math"
67
)
78

89
const (
@@ -51,3 +52,37 @@ func WriteBoolean(b bool, w io.Writer) (err error) {
5152

5253
return
5354
}
55+
56+
// ReadFloat32 reads a float32 value from the Reader.
57+
func ReadFloat32(r io.Reader) (f float32, err error) {
58+
if fbits, fbitsErr := ReadExpectMajors(SimpleData, r); fbitsErr != nil {
59+
err = fbitsErr
60+
} else {
61+
f = math.Float32frombits(uint32(fbits))
62+
}
63+
64+
return
65+
}
66+
67+
// WriteFloat32 writes a float32 into the Writer.
68+
func WriteFloat32(f float32, w io.Writer) (err error) {
69+
fbits := math.Float32bits(f)
70+
return WriteMajors(SimpleData, uint64(fbits), w)
71+
}
72+
73+
// ReadFloat64 reads a float64 value from the Reader.
74+
func ReadFloat64(r io.Reader) (f float64, err error) {
75+
if fbits, fbitsErr := ReadExpectMajors(SimpleData, r); fbitsErr != nil {
76+
err = fbitsErr
77+
} else {
78+
f = math.Float64frombits(fbits)
79+
}
80+
81+
return
82+
}
83+
84+
// WriteFloat64 writes a float64 into the Writer.
85+
func WriteFloat64(f float64, w io.Writer) (err error) {
86+
fbits := math.Float64bits(f)
87+
return WriteMajors(SimpleData, fbits, w)
88+
}

simple_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,64 @@ func TestBoolean(t *testing.T) {
3535
}
3636
}
3737
}
38+
39+
func TestFloat32(t *testing.T) {
40+
tests := []struct {
41+
data []byte
42+
f float32
43+
}{
44+
{[]byte{0xfa, 0x47, 0xc3, 0x50, 0x00}, 100000.0},
45+
{[]byte{0xfa, 0x7f, 0x7f, 0xff, 0xff}, 3.4028234663852886e+38},
46+
}
47+
48+
for _, test := range tests {
49+
// Read
50+
buff := bytes.NewBuffer(test.data)
51+
if f, err := ReadFloat32(buff); err != nil {
52+
t.Fatal(err)
53+
} else if f != test.f {
54+
t.Fatalf("Resulting float %f is not %f", f, test.f)
55+
}
56+
57+
// Write
58+
buff.Reset()
59+
if err := WriteFloat32(test.f, buff); err != nil {
60+
t.Fatal(err)
61+
}
62+
63+
if bb := buff.Bytes(); !reflect.DeepEqual(bb, test.data) {
64+
t.Fatalf("Serialized data mismatches: %x != %x", bb, test.data)
65+
}
66+
}
67+
}
68+
69+
func TestFloat64(t *testing.T) {
70+
tests := []struct {
71+
data []byte
72+
f float64
73+
}{
74+
{[]byte{0xfb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a}, 1.1},
75+
{[]byte{0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c}, 1.0e+300},
76+
{[]byte{0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}, -4.1},
77+
}
78+
79+
for _, test := range tests {
80+
// Read
81+
buff := bytes.NewBuffer(test.data)
82+
if f, err := ReadFloat64(buff); err != nil {
83+
t.Fatal(err)
84+
} else if f != test.f {
85+
t.Fatalf("Resulting float %f is not %f", f, test.f)
86+
}
87+
88+
// Write
89+
buff.Reset()
90+
if err := WriteFloat64(test.f, buff); err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
if bb := buff.Bytes(); !reflect.DeepEqual(bb, test.data) {
95+
t.Fatalf("Serialized data mismatches: %x != %x", bb, test.data)
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)