@@ -9,7 +9,6 @@ package bson
9
9
import (
10
10
"encoding/hex"
11
11
"encoding/json"
12
- "fmt"
13
12
"math"
14
13
"os"
15
14
"path"
@@ -64,53 +63,13 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
64
63
})
65
64
}
66
65
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
-
97
66
t .Run ("Padding specified with no vector data PACKED_BIT" , func (t * testing.T ) {
98
67
t .Parallel ()
99
68
100
69
t .Run ("Marshaling" , func (t * testing.T ) {
101
70
_ , err := NewPackedBitVector (nil , 1 )
102
71
require .EqualError (t , err , errNonZeroVectorPadding .Error ())
103
72
})
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
- })
114
73
})
115
74
116
75
t .Run ("Exceeding maximum padding PACKED_BIT" , func (t * testing.T ) {
@@ -120,47 +79,9 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
120
79
_ , err := NewPackedBitVector (nil , 8 )
121
80
require .EqualError (t , err , errVectorPaddingTooLarge .Error ())
122
81
})
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
- })
133
82
})
134
83
}
135
84
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
-
164
85
func convertSlice [T int8 | float32 | byte ](s []interface {}) []T {
165
86
v := make ([]T , len (s ))
166
87
for i , e := range s {
@@ -208,33 +129,44 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector
208
129
209
130
t .Run ("Unmarshaling" , func (t * testing.T ) {
210
131
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" ,
222
139
}
223
140
if reason , ok := skipCases [test .Description ]; ok {
224
141
t .Skipf ("skip test case %s: %s" , test .Description , reason )
225
142
}
226
143
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
+
227
151
t .Parallel ()
228
152
229
153
var got map [string ]Vector
230
154
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
+ }
233
163
})
234
164
235
165
t .Run ("Marshaling" , func (t * testing.T ) {
236
166
skipCases := map [string ]string {
237
167
"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" ,
238
170
"Overflow Vector INT8" : "compile-time restriction" ,
239
171
"Underflow Vector INT8" : "compile-time restriction" ,
240
172
"INT8 with padding" : "private padding field" ,
0 commit comments