Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions bson/bson_binary_vector_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package bson
import (
"encoding/hex"
"encoding/json"
"math"
"os"
"path"
"testing"
Expand All @@ -26,13 +25,13 @@ type bsonBinaryVectorTests struct {
}

type bsonBinaryVectorTestCase struct {
Description string `json:"description"`
Valid bool `json:"valid"`
Vector []interface{} `json:"vector"`
DtypeHex string `json:"dtype_hex"`
DtypeAlias string `json:"dtype_alias"`
Padding int `json:"padding"`
CanonicalBson string `json:"canonical_bson"`
Description string `json:"description"`
Valid bool `json:"valid"`
Vector json.RawMessage `json:"vector"`
DtypeHex string `json:"dtype_hex"`
DtypeAlias string `json:"dtype_alias"`
Padding int `json:"padding"`
CanonicalBson string `json:"canonical_bson"`
}

func TestBsonBinaryVectorSpec(t *testing.T) {
Expand Down Expand Up @@ -82,21 +81,18 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
})
}

func convertSlice[T int8 | float32 | byte](s []interface{}) []T {
func convertSlice[T int8 | float32 | byte](t *testing.T, data []byte) []T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func convertSlice[T int8 | float32 | byte](t *testing.T, data []byte) []T {
func convertSlice[T int8 | float32 | byte](t *testing.T, data []byte) []T {
t.Helper()

Alternatively, this method could be re-written to return an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this function be renamed to decodeTestSlice / unmarshalTestSlice?

if len(data) == 0 {
return nil
}
var s []float64
err := UnmarshalExtJSON(data, true, &s)
if err != nil {
t.Fatalf("got %q while handling %s", err, string(data))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest using the require package

Suggested change
if err != nil {
t.Fatalf("got %q while handling %s", err, string(data))
}
require.NoError(t, err)

v := make([]T, len(s))
for i, e := range s {
f := math.NaN()
switch val := e.(type) {
case float64:
f = val
case string:
if val == "inf" {
f = math.Inf(0)
} else if val == "-inf" {
f = math.Inf(-1)
}
}
v[i] = T(f)
v[i] = T(e)
}
return v
}
Expand All @@ -107,17 +103,17 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector
case "0x03":
testVector[testKey] = Vector{
dType: Int8Vector,
int8Data: convertSlice[int8](test.Vector),
int8Data: convertSlice[int8](t, test.Vector),
}
case "0x27":
testVector[testKey] = Vector{
dType: Float32Vector,
float32Data: convertSlice[float32](test.Vector),
float32Data: convertSlice[float32](t, test.Vector),
}
case "0x10":
testVector[testKey] = Vector{
dType: PackedBitVector,
bitData: convertSlice[byte](test.Vector),
bitData: convertSlice[byte](t, test.Vector),
bitPadding: uint8(test.Padding),
}
default:
Expand Down
2 changes: 1 addition & 1 deletion testdata/bson-binary-vector/float32.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{
"description": "Infinity Vector FLOAT32",
"valid": true,
"vector": ["-inf", 0.0, "inf"],
"vector": [{"$numberDouble": "-Infinity"}, 0.0, {"$numberDouble": "Infinity"}],
"dtype_hex": "0x27",
"dtype_alias": "FLOAT32",
"padding": 0,
Expand Down
Loading