vitess.io/vitess@v0.16.2/go/vt/vtgate/evalengine/hash_code_test.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package evalengine 18 19 import ( 20 "fmt" 21 "math/rand" 22 "testing" 23 "time" 24 25 "github.com/stretchr/testify/require" 26 27 "vitess.io/vitess/go/mysql/collations" 28 "vitess.io/vitess/go/sqltypes" 29 ) 30 31 // The following test tries to produce lots of different values and compares them both using hash code and compare, 32 // to make sure that these two methods agree on what values are equal 33 func TestHashCodesRandom(t *testing.T) { 34 tested := 0 35 equal := 0 36 collation := collations.Local().LookupByName("utf8mb4_general_ci").ID() 37 endTime := time.Now().Add(1 * time.Second) 38 for time.Now().Before(endTime) { 39 tested++ 40 v1, v2 := randomValues() 41 cmp, err := NullsafeCompare(v1, v2, collation) 42 require.NoErrorf(t, err, "%s compared with %s", v1.String(), v2.String()) 43 typ, err := CoerceTo(v1.Type(), v2.Type()) 44 require.NoError(t, err) 45 46 hash1, err := NullsafeHashcode(v1, collation, typ) 47 require.NoError(t, err) 48 hash2, err := NullsafeHashcode(v2, collation, typ) 49 require.NoError(t, err) 50 if cmp == 0 { 51 equal++ 52 require.Equalf(t, hash1, hash2, "values %s and %s are considered equal but produce different hash codes: %d & %d", v1.String(), v2.String(), hash1, hash2) 53 } 54 } 55 t.Logf("tested %d values, with %d equalities found\n", tested, equal) 56 } 57 58 func randomValues() (sqltypes.Value, sqltypes.Value) { 59 if rand.Int()%2 == 0 { 60 // create a single value, and turn it into two different types 61 v := rand.Int() 62 return randomNumericType(v), randomNumericType(v) 63 } 64 65 // just produce two arbitrary random values and compare 66 return randomValue(), randomValue() 67 } 68 69 func randomNumericType(i int) sqltypes.Value { 70 r := rand.Intn(len(numericTypes)) 71 return numericTypes[r](i) 72 73 } 74 75 var numericTypes = []func(int) sqltypes.Value{ 76 func(i int) sqltypes.Value { return sqltypes.NULL }, 77 func(i int) sqltypes.Value { return sqltypes.NewInt8(int8(i)) }, 78 func(i int) sqltypes.Value { return sqltypes.NewInt32(int32(i)) }, 79 func(i int) sqltypes.Value { return sqltypes.NewInt64(int64(i)) }, 80 func(i int) sqltypes.Value { return sqltypes.NewUint64(uint64(i)) }, 81 func(i int) sqltypes.Value { return sqltypes.NewUint32(uint32(i)) }, 82 func(i int) sqltypes.Value { return sqltypes.NewFloat64(float64(i)) }, 83 func(i int) sqltypes.Value { return sqltypes.NewDecimal(fmt.Sprintf("%d", i)) }, 84 func(i int) sqltypes.Value { return sqltypes.NewVarChar(fmt.Sprintf("%d", i)) }, 85 func(i int) sqltypes.Value { return sqltypes.NewVarChar(fmt.Sprintf(" %f aa", float64(i))) }, 86 } 87 88 var randomGenerators = []func() sqltypes.Value{ 89 randomNull, 90 randomInt8, 91 randomInt32, 92 randomInt64, 93 randomUint64, 94 randomUint32, 95 randomVarChar, 96 randomComplexVarChar, 97 } 98 99 func randomValue() sqltypes.Value { 100 r := rand.Intn(len(randomGenerators)) 101 return randomGenerators[r]() 102 } 103 104 func randomNull() sqltypes.Value { return sqltypes.NULL } 105 func randomInt8() sqltypes.Value { return sqltypes.NewInt8(int8(rand.Intn(255))) } 106 func randomInt32() sqltypes.Value { return sqltypes.NewInt32(rand.Int31()) } 107 func randomInt64() sqltypes.Value { return sqltypes.NewInt64(rand.Int63()) } 108 func randomUint32() sqltypes.Value { return sqltypes.NewUint32(rand.Uint32()) } 109 func randomUint64() sqltypes.Value { return sqltypes.NewUint64(rand.Uint64()) } 110 func randomVarChar() sqltypes.Value { return sqltypes.NewVarChar(fmt.Sprintf("%d", rand.Int63())) } 111 func randomComplexVarChar() sqltypes.Value { 112 return sqltypes.NewVarChar(fmt.Sprintf(" \t %f apa", float64(rand.Intn(1000))*1.10)) 113 }