github.com/weaviate/weaviate@v1.24.6/usecases/vectorizer/combine_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package vectorizer
    13  
    14  import (
    15  	"reflect"
    16  	"testing"
    17  )
    18  
    19  func TestCombineVectors(t *testing.T) {
    20  	type args struct {
    21  		vectors [][]float32
    22  	}
    23  	tests := []struct {
    24  		name string
    25  		args args
    26  		want []float32
    27  	}{
    28  		{
    29  			"Combine simple vectors",
    30  			args{
    31  				vectors: [][]float32{
    32  					{1, 2, 3},
    33  					{2, 3, 4},
    34  				},
    35  			},
    36  			[]float32{1.5, 2.5, 3.5},
    37  		},
    38  		{
    39  			"Combine empty vectors",
    40  			args{
    41  				vectors: [][]float32{},
    42  			},
    43  			[]float32{},
    44  		},
    45  		{
    46  			"Combine more complex vectors",
    47  			args{
    48  				vectors: [][]float32{
    49  					{-0.214571, -0.605529, -0.335769, -0.185277, -0.212256, 0.478032, -0.536662, 0.298211},
    50  					{-0.14713769, -0.06872862, 0.09911085, -0.06342313, 0.10092197, -0.06624051, -0.06812558, 0.07360107},
    51  					{-0.18123996, -0.2089612, 0.03738429, -0.26224917, 0.18499854, -0.2620146, -0.12802331, -0.07601682},
    52  					{-0.06659584, -0.17120242, 0.07603133, -0.07171547, 0.12537181, -0.19367254, -0.18376349, -0.05517439},
    53  				},
    54  			},
    55  			[]float32{-0.15238613, -0.2636053, -0.030810636, -0.1456662, 0.049759082, -0.010973915, -0.2291436, 0.060155217},
    56  		},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			if got := CombineVectors(tt.args.vectors); !reflect.DeepEqual(got, tt.want) {
    61  				t.Errorf("CombineVectors() = %v, want %v", got, tt.want)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestCombineVectorsWithWeights(t *testing.T) {
    68  	type args struct {
    69  		vectors [][]float32
    70  		weights []float32
    71  	}
    72  	tests := []struct {
    73  		name string
    74  		args args
    75  		want []float32
    76  	}{
    77  		{
    78  			"Combine simple vectors with 0 weights",
    79  			args{
    80  				vectors: [][]float32{
    81  					{1, 2, 3},
    82  					{2, 3, 4},
    83  				},
    84  				weights: []float32{0, 0, 0},
    85  			},
    86  			[]float32{0, 0, 0},
    87  		},
    88  		{
    89  			"Combine simple vectors with 1 weights",
    90  			args{
    91  				vectors: [][]float32{
    92  					{1, 2, 3},
    93  					{2, 3, 4},
    94  				},
    95  				weights: []float32{1, 1, 1},
    96  			},
    97  			[]float32{1.5, 2.5, 3.5},
    98  		},
    99  		{
   100  			"Combine empty vectors",
   101  			args{
   102  				vectors: [][]float32{},
   103  				weights: []float32{},
   104  			},
   105  			[]float32{},
   106  		},
   107  		{
   108  			"Combine simple vectors without weights",
   109  			args{
   110  				vectors: [][]float32{
   111  					{1, 2, 3},
   112  					{2, 3, 4},
   113  				},
   114  				weights: nil,
   115  			},
   116  			[]float32{1.5, 2.5, 3.5},
   117  		},
   118  	}
   119  	for _, tt := range tests {
   120  		t.Run(tt.name, func(t *testing.T) {
   121  			if got := CombineVectorsWithWeights(tt.args.vectors, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
   122  				t.Errorf("CombineVectors() = %v, want %v", got, tt.want)
   123  			}
   124  		})
   125  	}
   126  }
   127  
   128  func BenchmarkCombine(b *testing.B) {
   129  	for i := 0; i < b.N; i++ {
   130  		CombineVectors([][]float32{
   131  			{-0.214571, -0.605529, -0.335769, -0.185277, -0.212256, 0.478032, -0.536662, 0.298211},
   132  			{-0.14713769, -0.06872862, 0.09911085, -0.06342313, 0.10092197, -0.06624051, -0.06812558, 0.07360107},
   133  			{-0.18123996, -0.2089612, 0.03738429, -0.26224917, 0.18499854, -0.2620146, -0.12802331, -0.07601682},
   134  			{-0.06659584, -0.17120242, 0.07603133, -0.07171547, 0.12537181, -0.19367254, -0.18376349, -0.05517439},
   135  		})
   136  	}
   137  }