github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/inverted/serialization_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 inverted
    13  
    14  import (
    15  	"fmt"
    16  	"math"
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  // TestSerialization makes sure that writing and reading into the
    24  // lexicographically sortable types byte slices ends up with the same values as
    25  // original. There is no focus on the sortability itself, as that is already
    26  // tested extensively in analyzer_test.go
    27  func TestSerialization(t *testing.T) {
    28  	t.Run("float64", func(t *testing.T) {
    29  		subjects := []float64{
    30  			math.SmallestNonzeroFloat64,
    31  			-400.0001,
    32  			-21,
    33  			0,
    34  			21,
    35  			400.0001,
    36  			math.MaxFloat64,
    37  		}
    38  
    39  		for _, sub := range subjects {
    40  			t.Run(fmt.Sprintf("with %f", sub), func(t *testing.T) {
    41  				bytes, err := LexicographicallySortableFloat64(sub)
    42  				require.Nil(t, err)
    43  
    44  				parsed, err := ParseLexicographicallySortableFloat64(bytes)
    45  				require.Nil(t, err)
    46  
    47  				assert.Equal(t, sub, parsed, "before and after must match")
    48  			})
    49  		}
    50  	})
    51  
    52  	t.Run("int64", func(t *testing.T) {
    53  		subjects := []int64{
    54  			math.MinInt64,
    55  			-400,
    56  			-21,
    57  			0,
    58  			21,
    59  			400,
    60  			math.MaxInt64,
    61  		}
    62  
    63  		for _, sub := range subjects {
    64  			t.Run(fmt.Sprintf("with %d", sub), func(t *testing.T) {
    65  				bytes, err := LexicographicallySortableInt64(sub)
    66  				require.Nil(t, err)
    67  
    68  				parsed, err := ParseLexicographicallySortableInt64(bytes)
    69  				require.Nil(t, err)
    70  
    71  				assert.Equal(t, sub, parsed, "before and after must match")
    72  			})
    73  		}
    74  	})
    75  
    76  	t.Run("uint64", func(t *testing.T) {
    77  		subjects := []uint64{
    78  			0,
    79  			21,
    80  			400,
    81  			math.MaxUint64,
    82  		}
    83  
    84  		for _, sub := range subjects {
    85  			t.Run(fmt.Sprintf("with %d", sub), func(t *testing.T) {
    86  				bytes, err := LexicographicallySortableUint64(sub)
    87  				require.Nil(t, err)
    88  
    89  				parsed, err := ParseLexicographicallySortableUint64(bytes)
    90  				require.Nil(t, err)
    91  
    92  				assert.Equal(t, sub, parsed, "before and after must match")
    93  			})
    94  		}
    95  	})
    96  }