github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/bin/bin_test.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package bin 16 17 import ( 18 "math" 19 "strconv" 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestCountBitLenForInt(t *testing.T) { 26 // count bits for unsigned int 27 // eg: 0(0), 1(1), 2(10), 3(11) 28 require.Equal(t, int64(1+1+2+2), Uint8BitLen([]uint8{0, 1, 2, 3})) 29 require.Equal(t, int64(3+3), Uint16BitLen([]uint16{4, 5})) 30 require.Equal(t, int64(3+3), Uint32BitLen([]uint32{6, 7})) 31 require.Equal(t, int64(4+4), Uint64BitLen([]uint64{8, 9})) 32 33 // count bits for signed int 34 // eg: -1(0xffffffffffffffff, 64bits) 35 // 127(1111111) 36 // -128(1111111111111111111111111111111111111111111111111111111110000000, 64bits) 37 require.Equal(t, int64(64+7+64), Int8BitLen([]int8{-1, 127, -128})) 38 require.Equal(t, int64(5+5), Int16BitLen([]int16{17, 18})) 39 require.Equal(t, int64(7+8), Int32BitLen([]int32{100, 200})) 40 require.Equal(t, int64(64+64+24+30), Int64BitLen([]int64{-1e7, -1e9, 1e7, 1e9})) 41 } 42 43 // due to differences between x86/arm, this TestCountBitLenForFloat function has some compatibility issues and therefore commented out 44 /* 45 func TestCountBitLenForFloat(t *testing.T) { 46 // count bits for float 47 // eg: 0.2(0), 1.8(1), 2.99(10), 3.14(11) 48 require.Equal(t, int64(1+1+2+2), Float32BitLen([]float32{.2, 1.8, 2.99, 3.14})) 49 require.Equal(t, int64(64+7+64), Float32BitLen([]float32{-1.99, 127.99, -128.89})) 50 require.Equal(t, int64(7+8), Float32BitLen([]float32{100.99, 200.99})) 51 52 require.Equal(t, int64(64+64+24+30), Float64BitLen([]float64{-1e7, -1e9, 1e7, 1e9})) 53 // Phi=1.61...(1), E=2.7(10), Pi=3.14(11) 54 require.Equal(t, int64(1+2+2), Float64BitLen([]float64{float64(math.Phi), float64(math.E), float64(math.Pi)})) 55 } 56 57 */ 58 59 func TestUnsignedIntToBinary(t *testing.T) { 60 cases := []uint64{0, 1, 2, 3, 127, 128} 61 ret := make([]string, len(cases)) 62 ret = Uint64ToBinary(cases, ret) 63 for i, c := range cases { 64 require.Equal(t, strconv.FormatUint(c, 2), ret[i]) 65 } 66 } 67 68 func TestIntToBinary(t *testing.T) { 69 cases := []int64{-1, 127, -128, 1e9, 1e7, -1e9} 70 ret := make([]string, len(cases)) 71 ret = Int64ToBinary(cases, ret) 72 for i, c := range cases { 73 require.Equal(t, strconv.FormatUint(uint64(c), 2), ret[i]) 74 } 75 } 76 77 func TestFloatToBinary(t *testing.T) { 78 cases := []float64{float64(math.Phi), float64(math.E), float64(math.Pi)} 79 ret := make([]string, len(cases)) 80 ret = Float64ToBinary(cases, ret) 81 for i, c := range cases { 82 require.Equal(t, strconv.FormatUint(uint64(c), 2), ret[i]) 83 } 84 } 85 86 func TestFormatUintToBinary(t *testing.T) { 87 tt := []struct { 88 num uint64 89 want string 90 }{ 91 {0, "0"}, 92 {1, "1"}, 93 {2, "10"}, 94 {3, "11"}, 95 {127, "1111111"}, 96 {1e7, "100110001001011010000000"}, 97 {1e9, "111011100110101100101000000000"}, 98 } 99 100 for _, tc := range tt { 101 if got := uintToBinary(tc.num); got != tc.want { 102 t.Fatalf("uintToBinary(%d) = %s, want %s", tc.num, got, tc.want) 103 } 104 } 105 }