github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/samples/go/csv/schema_test.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package csv 6 7 import ( 8 "fmt" 9 "testing" 10 11 "github.com/attic-labs/noms/go/types" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestSchemaDetection(t *testing.T) { 16 assert := assert.New(t) 17 test := func(input [][]string, expect []KindSlice) { 18 options := newSchemaOptions(len(input[0])) 19 for _, values := range input { 20 options.Test(values) 21 } 22 23 assert.Equal(expect, options.ValidKinds()) 24 } 25 test( 26 [][]string{ 27 {"foo", "1", "5"}, 28 {"bar", "0", "10"}, 29 {"true", "1", "23"}, 30 {"1", "1", "60"}, 31 {"1.1", "false", "75"}, 32 }, 33 []KindSlice{ 34 {types.StringKind}, 35 {types.BoolKind, types.StringKind}, 36 { 37 types.NumberKind, 38 types.StringKind, 39 }, 40 }, 41 ) 42 test( 43 [][]string{ 44 {"foo"}, 45 {"bar"}, 46 {"true"}, 47 {"1"}, 48 {"1.1"}, 49 }, 50 []KindSlice{ 51 {types.StringKind}, 52 }, 53 ) 54 test( 55 [][]string{ 56 {"true"}, 57 {"1"}, 58 {"1.1"}, 59 }, 60 []KindSlice{ 61 {types.StringKind}, 62 }, 63 ) 64 test( 65 [][]string{ 66 {"true"}, 67 {"false"}, 68 {"True"}, 69 {"False"}, 70 {"TRUE"}, 71 {"FALSE"}, 72 {"1"}, 73 {"0"}, 74 }, 75 []KindSlice{ 76 {types.BoolKind, types.StringKind}, 77 }, 78 ) 79 test( 80 [][]string{ 81 {"1"}, 82 {"1.1"}, 83 }, 84 []KindSlice{ 85 { 86 types.NumberKind, 87 types.StringKind}, 88 }, 89 ) 90 test( 91 [][]string{ 92 {"1"}, 93 {"1.1"}, 94 {"4.940656458412465441765687928682213723651e-50"}, 95 {"-4.940656458412465441765687928682213723651e-50"}, 96 }, 97 []KindSlice{ 98 { 99 types.NumberKind, 100 types.StringKind}, 101 }, 102 ) 103 104 test( 105 [][]string{ 106 {"1"}, 107 {"1.1"}, 108 {"1.797693134862315708145274237317043567981e+102"}, 109 {"-1.797693134862315708145274237317043567981e+102"}, 110 }, 111 []KindSlice{ 112 { 113 types.NumberKind, 114 types.StringKind}, 115 }, 116 ) 117 test( 118 [][]string{ 119 {"1"}, 120 {"1.1"}, 121 {"1.797693134862315708145274237317043567981e+309"}, 122 {"-1.797693134862315708145274237317043567981e+309"}, 123 }, 124 []KindSlice{ 125 { 126 types.StringKind}, 127 }, 128 ) 129 test( 130 [][]string{ 131 {"1"}, 132 {"0"}, 133 }, 134 []KindSlice{ 135 { 136 types.NumberKind, 137 types.BoolKind, 138 types.StringKind}, 139 }, 140 ) 141 test( 142 [][]string{ 143 {"1"}, 144 {"0"}, 145 {"-1"}, 146 }, 147 []KindSlice{ 148 { 149 types.NumberKind, 150 types.StringKind}, 151 }, 152 ) 153 test( 154 [][]string{ 155 {"0"}, 156 {"-0"}, 157 }, 158 []KindSlice{ 159 { 160 types.NumberKind, 161 types.StringKind}, 162 }, 163 ) 164 test( 165 [][]string{ 166 {"1"}, 167 {"280"}, 168 {"0"}, 169 {"-1"}, 170 }, 171 []KindSlice{ 172 { 173 types.NumberKind, 174 types.StringKind}, 175 }, 176 ) 177 test( 178 [][]string{ 179 {"1"}, 180 {"-180"}, 181 {"0"}, 182 {"-1"}, 183 }, 184 []KindSlice{ 185 { 186 types.NumberKind, 187 types.StringKind}, 188 }, 189 ) 190 test( 191 [][]string{ 192 {"1"}, 193 {"33000"}, 194 {"0"}, 195 {"-1"}, 196 }, 197 []KindSlice{ 198 { 199 types.NumberKind, 200 types.StringKind}, 201 }, 202 ) 203 test( 204 [][]string{ 205 {"1"}, 206 {"-44000"}, 207 {"0"}, 208 {"-1"}, 209 }, 210 []KindSlice{ 211 { 212 types.NumberKind, 213 types.StringKind}, 214 }, 215 ) 216 test( 217 [][]string{ 218 {"1"}, 219 {"2547483648"}, 220 {"0"}, 221 {"-1"}, 222 }, 223 []KindSlice{ 224 { 225 types.NumberKind, 226 types.StringKind}, 227 }, 228 ) 229 test( 230 [][]string{ 231 {"1"}, 232 {"-4347483648"}, 233 {"0"}, 234 {"-1"}, 235 }, 236 []KindSlice{ 237 { 238 types.NumberKind, 239 types.StringKind}, 240 }, 241 ) 242 test( 243 [][]string{ 244 {fmt.Sprintf("%d", uint64(1<<63))}, 245 {fmt.Sprintf("%d", uint64(1<<63)+1)}, 246 }, 247 []KindSlice{ 248 { 249 types.NumberKind, 250 types.StringKind}, 251 }, 252 ) 253 test( 254 [][]string{ 255 {fmt.Sprintf("%d", uint64(1<<32))}, 256 {fmt.Sprintf("%d", uint64(1<<32)+1)}, 257 }, 258 []KindSlice{ 259 { 260 types.NumberKind, 261 types.StringKind}, 262 }, 263 ) 264 } 265 266 func TestCombinationsWithLength(t *testing.T) { 267 assert := assert.New(t) 268 test := func(input []int, length int, expect [][]int) { 269 combinations := make([][]int, 0) 270 combinationsWithLength(input, length, func(combination []int) { 271 combinations = append(combinations, append([]int{}, combination...)) 272 }) 273 274 assert.Equal(expect, combinations) 275 } 276 test([]int{0}, 1, [][]int{ 277 {0}, 278 }) 279 test([]int{1}, 1, [][]int{ 280 {1}, 281 }) 282 test([]int{0, 1}, 1, [][]int{ 283 {0}, 284 {1}, 285 }) 286 test([]int{0, 1}, 2, [][]int{ 287 {0, 1}, 288 }) 289 test([]int{70, 80, 90, 100}, 1, [][]int{ 290 {70}, 291 {80}, 292 {90}, 293 {100}, 294 }) 295 test([]int{70, 80, 90, 100}, 2, [][]int{ 296 {70, 80}, 297 {70, 90}, 298 {70, 100}, 299 {80, 90}, 300 {80, 100}, 301 {90, 100}, 302 }) 303 test([]int{70, 80, 90, 100}, 3, [][]int{ 304 {70, 80, 90}, 305 {70, 80, 100}, 306 {70, 90, 100}, 307 {80, 90, 100}, 308 }) 309 } 310 311 func TestCombinationsWithLengthFromTo(t *testing.T) { 312 assert := assert.New(t) 313 test := func(input []int, smallestLength, largestLength int, expect [][]int) { 314 combinations := make([][]int, 0) 315 combinationsLengthsFromTo(input, smallestLength, largestLength, func(combination []int) { 316 combinations = append(combinations, append([]int{}, combination...)) 317 }) 318 319 assert.Equal(expect, combinations) 320 } 321 test([]int{0}, 1, 1, [][]int{ 322 {0}, 323 }) 324 test([]int{1}, 1, 1, [][]int{ 325 {1}, 326 }) 327 test([]int{0, 1}, 1, 2, [][]int{ 328 {0}, 329 {1}, 330 {0, 1}, 331 }) 332 test([]int{0, 1}, 2, 2, [][]int{ 333 {0, 1}, 334 }) 335 test([]int{70, 80, 90, 100}, 1, 3, [][]int{ 336 {70}, 337 {80}, 338 {90}, 339 {100}, 340 {70, 80}, 341 {70, 90}, 342 {70, 100}, 343 {80, 90}, 344 {80, 100}, 345 {90, 100}, 346 {70, 80, 90}, 347 {70, 80, 100}, 348 {70, 90, 100}, 349 {80, 90, 100}, 350 }) 351 }