@@ -3,6 +3,7 @@ package cboring
33import (
44 "bufio"
55 "bytes"
6+ "io"
67 "reflect"
78 "testing"
89
@@ -207,3 +208,43 @@ func TestReadBigData(t *testing.T) {
207208 }
208209 })
209210}
211+
212+ func asUntyped [T any ](read func (reader io.Reader ) (T , error )) func (reader io.Reader ) (any , error ) {
213+ return func (reader io.Reader ) (any , error ) {
214+ return read (reader )
215+ }
216+ }
217+
218+ func TestNull (t * testing.T ) {
219+ tests := []struct {
220+ data []byte
221+ value any
222+ read func (reader io.Reader ) (any , error )
223+ }{
224+ {[]byte {Null , 0xfb , 0xc0 , 0x10 , 0x66 , 0x66 , 0x66 , 0x66 , 0x66 , 0x66 }, - 4.1 , asUntyped (ReadFloat64 )},
225+ {[]byte {Null , 0x64 , 0x74 , 0x65 , 0x73 , 0x74 }, "test" , asUntyped (ReadTextString )},
226+ {[]byte {Null , SimpleData | simpleTrue }, true , asUntyped (ReadBoolean )},
227+ {[]byte {Null , 0x0a }, uint64 (10 ), asUntyped (ReadUInt )},
228+ }
229+
230+ for _ , test := range tests {
231+ // Read
232+ buff := bytes .NewBuffer (test .data )
233+ if v , err := test .read (buff ); err != FlagNull {
234+ t .Fatalf ("found Value %s" , v )
235+ }
236+ if v , err := test .read (buff ); err != nil {
237+ t .Fatal (err )
238+ } else if v != test .value {
239+ t .Fatalf ("Resulting value %s not expected %s" , v , test .value )
240+ }
241+ }
242+
243+ buff := & bytes.Buffer {}
244+ err := WriteNull (buff )
245+ if err != nil {
246+ t .Fatal (err )
247+ } else if buff .Len () != 1 || buff .Bytes ()[0 ] != Null {
248+ t .Fatal ("written value not null" )
249+ }
250+ }
0 commit comments