github.com/mithrandie/csvq@v1.18.1/lib/query/utils_test.go (about) 1 package query 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 "time" 8 9 "github.com/mithrandie/csvq/lib/value" 10 11 "github.com/mithrandie/ternary" 12 ) 13 14 func TestUintPool_Exists(t *testing.T) { 15 pool := &UintPool{ 16 limitToUseSlice: 2, 17 m: map[uint]bool{ 18 1: true, 19 2: true, 20 3: true, 21 }, 22 values: []uint{1, 2, 3}, 23 } 24 25 result := pool.Exists(1) 26 if !result { 27 t.Errorf("result = %t, want %t", false, true) 28 } 29 30 result = pool.Exists(9) 31 if result { 32 t.Errorf("result = %t, want %t", true, false) 33 } 34 35 pool = &UintPool{ 36 limitToUseSlice: 2, 37 m: map[uint]bool{ 38 1: true, 39 }, 40 values: []uint{1}, 41 } 42 43 result = pool.Exists(1) 44 if !result { 45 t.Errorf("result = %t, want %t", false, true) 46 } 47 48 result = pool.Exists(9) 49 if result { 50 t.Errorf("result = %t, want %t", true, false) 51 } 52 } 53 54 func TestUintPool_Add(t *testing.T) { 55 pool := NewUintPool(1, 2) 56 57 pool.Add(1) 58 expect := &UintPool{ 59 limitToUseSlice: 2, 60 m: map[uint]bool{ 61 1: true, 62 }, 63 values: []uint{1}, 64 } 65 66 if !reflect.DeepEqual(pool, expect) { 67 t.Errorf("pool = %v, want %v", pool, expect) 68 } 69 } 70 71 func TestSerializeComparisonKeys(t *testing.T) { 72 values := []value.Primary{ 73 value.NewString("str"), 74 value.NewInteger(1), 75 value.NewInteger(0), 76 value.NewInteger(3), 77 value.NewFloat(1.234), 78 value.NewDatetimeFromString("2012-02-03T09:18:15-08:00", TestTx.Flags.DatetimeFormat, TestTx.Flags.GetTimeLocation()), 79 value.NewDatetimeFromString("2012-02-03T09:18:15.123-08:00", TestTx.Flags.DatetimeFormat, TestTx.Flags.GetTimeLocation()), 80 value.NewDatetimeFromString("2012-02-03T09:18:15.123456789-08:00", TestTx.Flags.DatetimeFormat, TestTx.Flags.GetTimeLocation()), 81 value.NewBoolean(true), 82 value.NewBoolean(false), 83 value.NewTernary(ternary.TRUE), 84 value.NewTernary(ternary.FALSE), 85 value.NewTernary(ternary.UNKNOWN), 86 value.NewNull(), 87 } 88 expect := "[S]STR:[I]1:[I]0:[I]3:[F]1.234:[D]1328289495000000000:[D]1328289495123000000:[D]1328289495123456789:[I]1:[I]0:[I]1:[I]0:[N]:[N]" 89 90 buf := &bytes.Buffer{} 91 SerializeComparisonKeys(buf, values, TestTx.Flags) 92 result := buf.String() 93 if result != expect { 94 t.Errorf("result = %q, want %q", result, expect) 95 } 96 97 TestTx.Flags.StrictEqual = true 98 defer func() { 99 TestTx.Flags.StrictEqual = false 100 }() 101 102 expect = "[S]str:[I]1:[I]0:[I]3:[F]1.234:[D]1328289495000000000:[D]1328289495123000000:[D]1328289495123456789:[B]T:[B]F:[T]T:[T]F:[T]U:[N]" 103 buf.Reset() 104 SerializeComparisonKeys(buf, values, TestTx.Flags) 105 result = buf.String() 106 if result != expect { 107 t.Errorf("result = %q, want %q", result, expect) 108 } 109 } 110 111 func BenchmarkDistinguish(b *testing.B) { 112 values := make([]value.Primary, 10000) 113 for i := 0; i < 100; i++ { 114 for j := 0; j < 100; j++ { 115 values[i*100+j] = value.NewInteger(int64(j)) 116 } 117 } 118 119 b.ResetTimer() 120 121 for i := 0; i < b.N; i++ { 122 Distinguish(values, TestTx.Flags) 123 } 124 } 125 126 func benchmarkComparisonKeys(b *testing.B, plist []value.Primary) { 127 buf := &bytes.Buffer{} 128 129 b.ResetTimer() 130 for i := 0; i < b.N; i++ { 131 buf.Reset() 132 SerializeComparisonKeys(buf, plist, TestTx.Flags) 133 } 134 } 135 136 func BenchmarkComparisonKeysString(b *testing.B) { 137 plist := []value.Primary{ 138 value.NewString("abcdefghi"), 139 value.NewString("jklmn"), 140 } 141 142 benchmarkComparisonKeys(b, plist) 143 } 144 145 func BenchmarkComparisonKeysInteger(b *testing.B) { 146 plist := []value.Primary{ 147 value.NewInteger(123456789), 148 value.NewInteger(1), 149 } 150 151 benchmarkComparisonKeys(b, plist) 152 } 153 154 func BenchmarkComparisonKeysFlaot(b *testing.B) { 155 plist := []value.Primary{ 156 value.NewFloat(1.234e-9), 157 value.NewFloat(123456.7890123), 158 } 159 160 benchmarkComparisonKeys(b, plist) 161 } 162 163 func BenchmarkComparisonKeysDatetime(b *testing.B) { 164 plist := []value.Primary{ 165 value.NewDatetime(time.Date(2012, 2, 4, 9, 18, 15, 0, time.Local)), 166 value.NewDatetime(time.Date(2015, 3, 4, 9, 17, 15, 0, time.Local)), 167 } 168 169 benchmarkComparisonKeys(b, plist) 170 } 171 172 func BenchmarkComparisonKeysBoolean(b *testing.B) { 173 plist := []value.Primary{ 174 value.NewBoolean(true), 175 value.NewBoolean(false), 176 } 177 178 benchmarkComparisonKeys(b, plist) 179 } 180 181 func BenchmarkComparisonKeysTernary(b *testing.B) { 182 plist := []value.Primary{ 183 value.NewTernary(ternary.TRUE), 184 value.NewTernary(ternary.FALSE), 185 value.NewTernary(ternary.UNKNOWN), 186 } 187 188 benchmarkComparisonKeys(b, plist) 189 } 190 191 func BenchmarkComparisonKeysNull(b *testing.B) { 192 plist := []value.Primary{ 193 value.NewNull(), 194 } 195 196 benchmarkComparisonKeys(b, plist) 197 }