@@ -9,7 +9,6 @@ package bson
99import (
1010 "encoding/hex"
1111 "encoding/json"
12- "fmt"
1312 "math"
1413 "os"
1514 "path"
@@ -64,53 +63,13 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
6463 })
6564 }
6665
67- t .Run ("FLOAT32 with padding" , func (t * testing.T ) {
68- t .Parallel ()
69-
70- t .Run ("Unmarshaling" , func (t * testing.T ) {
71- val := D {{"vector" , Binary {Subtype : TypeBinaryVector , Data : []byte {Float32Vector , 3 }}}}
72- b , err := Marshal (val )
73- require .NoError (t , err , "marshaling test BSON" )
74- var got struct {
75- Vector Vector
76- }
77- err = Unmarshal (b , & got )
78- require .ErrorContains (t , err , errNonZeroVectorPadding .Error ())
79- })
80- })
81-
82- t .Run ("INT8 with padding" , func (t * testing.T ) {
83- t .Parallel ()
84-
85- t .Run ("Unmarshaling" , func (t * testing.T ) {
86- val := D {{"vector" , Binary {Subtype : TypeBinaryVector , Data : []byte {Int8Vector , 3 }}}}
87- b , err := Marshal (val )
88- require .NoError (t , err , "marshaling test BSON" )
89- var got struct {
90- Vector Vector
91- }
92- err = Unmarshal (b , & got )
93- require .ErrorContains (t , err , errNonZeroVectorPadding .Error ())
94- })
95- })
96-
9766 t .Run ("Padding specified with no vector data PACKED_BIT" , func (t * testing.T ) {
9867 t .Parallel ()
9968
10069 t .Run ("Marshaling" , func (t * testing.T ) {
10170 _ , err := NewPackedBitVector (nil , 1 )
10271 require .EqualError (t , err , errNonZeroVectorPadding .Error ())
10372 })
104- t .Run ("Unmarshaling" , func (t * testing.T ) {
105- val := D {{"vector" , Binary {Subtype : TypeBinaryVector , Data : []byte {PackedBitVector , 1 }}}}
106- b , err := Marshal (val )
107- require .NoError (t , err , "marshaling test BSON" )
108- var got struct {
109- Vector Vector
110- }
111- err = Unmarshal (b , & got )
112- require .ErrorContains (t , err , errNonZeroVectorPadding .Error ())
113- })
11473 })
11574
11675 t .Run ("Exceeding maximum padding PACKED_BIT" , func (t * testing.T ) {
@@ -120,47 +79,9 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
12079 _ , err := NewPackedBitVector (nil , 8 )
12180 require .EqualError (t , err , errVectorPaddingTooLarge .Error ())
12281 })
123- t .Run ("Unmarshaling" , func (t * testing.T ) {
124- val := D {{"vector" , Binary {Subtype : TypeBinaryVector , Data : []byte {PackedBitVector , 8 }}}}
125- b , err := Marshal (val )
126- require .NoError (t , err , "marshaling test BSON" )
127- var got struct {
128- Vector Vector
129- }
130- err = Unmarshal (b , & got )
131- require .ErrorContains (t , err , errVectorPaddingTooLarge .Error ())
132- })
13382 })
13483}
13584
136- // TODO: This test may be added into the spec tests.
137- func TestFloat32VectorWithInsufficientData (t * testing.T ) {
138- t .Parallel ()
139-
140- val := Binary {Subtype : TypeBinaryVector }
141-
142- for _ , tc := range [][]byte {
143- {Float32Vector , 0 , 42 },
144- {Float32Vector , 0 , 42 , 42 },
145- {Float32Vector , 0 , 42 , 42 , 42 },
146-
147- {Float32Vector , 0 , 42 , 42 , 42 , 42 , 42 },
148- {Float32Vector , 0 , 42 , 42 , 42 , 42 , 42 , 42 },
149- {Float32Vector , 0 , 42 , 42 , 42 , 42 , 42 , 42 , 42 },
150- } {
151- t .Run (fmt .Sprintf ("marshaling %d bytes" , len (tc )- 2 ), func (t * testing.T ) {
152- val .Data = tc
153- b , err := Marshal (D {{"vector" , val }})
154- require .NoError (t , err , "marshaling test BSON" )
155- var got struct {
156- Vector Vector
157- }
158- err = Unmarshal (b , & got )
159- require .ErrorContains (t , err , errInsufficientVectorData .Error ())
160- })
161- }
162- }
163-
16485func convertSlice [T int8 | float32 | byte ](s []interface {}) []T {
16586 v := make ([]T , len (s ))
16687 for i , e := range s {
@@ -208,33 +129,44 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector
208129
209130 t .Run ("Unmarshaling" , func (t * testing.T ) {
210131 skipCases := map [string ]string {
211- "FLOAT32 with padding" : "run in alternative case" ,
212- "Overflow Vector INT8" : "compile-time restriction" ,
213- "Underflow Vector INT8" : "compile-time restriction" ,
214- "INT8 with padding" : "run in alternative case" ,
215- "INT8 with float inputs" : "compile-time restriction" ,
216- "Overflow Vector PACKED_BIT" : "compile-time restriction" ,
217- "Underflow Vector PACKED_BIT" : "compile-time restriction" ,
218- "Vector with float values PACKED_BIT" : "compile-time restriction" ,
219- "Padding specified with no vector data PACKED_BIT" : "run in alternative case" ,
220- "Exceeding maximum padding PACKED_BIT" : "run in alternative case" ,
221- "Negative padding PACKED_BIT" : "compile-time restriction" ,
132+ "Overflow Vector INT8" : "compile-time restriction" ,
133+ "Underflow Vector INT8" : "compile-time restriction" ,
134+ "INT8 with float inputs" : "compile-time restriction" ,
135+ "Overflow Vector PACKED_BIT" : "compile-time restriction" ,
136+ "Underflow Vector PACKED_BIT" : "compile-time restriction" ,
137+ "Vector with float values PACKED_BIT" : "compile-time restriction" ,
138+ "Negative padding PACKED_BIT" : "compile-time restriction" ,
222139 }
223140 if reason , ok := skipCases [test .Description ]; ok {
224141 t .Skipf ("skip test case %s: %s" , test .Description , reason )
225142 }
226143
144+ errMap := map [string ]string {
145+ "FLOAT32 with padding" : "padding must be 0" ,
146+ "INT8 with padding" : "padding must be 0" ,
147+ "Padding specified with no vector data PACKED_BIT" : "padding must be 0" ,
148+ "Exceeding maximum padding PACKED_BIT" : "padding cannot be larger than 7" ,
149+ }
150+
227151 t .Parallel ()
228152
229153 var got map [string ]Vector
230154 err := Unmarshal (testBSON , & got )
231- require .NoError (t , err )
232- require .Equal (t , testVector , got )
155+ if test .Valid {
156+ require .NoError (t , err )
157+ require .Equal (t , testVector , got )
158+ } else if errMsg , ok := errMap [test .Description ]; ok {
159+ require .ErrorContains (t , err , errMsg )
160+ } else {
161+ require .Error (t , err )
162+ }
233163 })
234164
235165 t .Run ("Marshaling" , func (t * testing.T ) {
236166 skipCases := map [string ]string {
237167 "FLOAT32 with padding" : "private padding field" ,
168+ "Insufficient vector data with 3 bytes FLOAT32" : "invalid case" ,
169+ "Insufficient vector data with 5 bytes FLOAT32" : "invalid case" ,
238170 "Overflow Vector INT8" : "compile-time restriction" ,
239171 "Underflow Vector INT8" : "compile-time restriction" ,
240172 "INT8 with padding" : "private padding field" ,
0 commit comments