gitee.com/quant1x/num@v0.3.2/internal/rand/regress_test.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Test that random number sequences generated by a specific seed 6 // do not change from version to version. 7 // 8 // If the generator changes, the golden outputs need updating, and 9 // client programs may break. Although the desire for compatibility 10 // is not as stringent as in the original math/rand package, 11 // when possible avoid changing the generator. 12 13 package rand_test 14 15 import ( 16 "flag" 17 "fmt" 18 "reflect" 19 "testing" 20 21 . "gitee.com/quant1x/num/internal/rand" 22 ) 23 24 var printgolden = flag.Bool("printgolden", false, "print golden results for regression test") 25 26 // TestSource verifies that the output of the default Source is locked down. 27 func TestSourceRegress(t *testing.T) { 28 src := NewSource(1) 29 var got [20]uint64 30 for i := range got { 31 got[i] = src.Uint64() 32 } 33 want := [20]uint64{ 34 0x34e936394905d167, 35 0x817c0ef62fe4c731, 36 0x937987e6e24f5a40, 37 0x0c0a8307fe226199, 38 0xf96363568d8bab56, 39 0xbaef3af36bd02620, 40 0x8f18e416eb6b936b, 41 0x05a43fc149f3a67a, 42 0xdab012eb3ce01697, 43 0xf76c495a133c6aa9, 44 0x304b24c5040ce457, 45 0x47d77e0abb413159, 46 0x52a810fa9e452f04, 47 0x2d24b66380cf4780, 48 0x5ec7691b92018ef5, 49 0x5076dfa749261ea0, 50 0xac8f11ad3941d213, 51 0x13fa8d67de91db25, 52 0xb50883a9893274eb, 53 0xeb8f59263f9109ac, 54 } 55 if got != want { 56 t.Errorf("got:\n\t%#016x\nwant:\n\t%#016x", got, want) 57 if *printgolden { 58 for _, x := range got { 59 fmt.Printf("\t\t%#016x,\n", x) 60 } 61 } 62 } 63 } 64 65 // TestRegress validates that the output stream is locked down, for instance so 66 // optimizations do not change the output. It iterates over methods of the 67 // Rand type to find functions to evaluate and checks the first 20 results 68 // against the golden results. 69 func TestRegress(t *testing.T) { 70 var int32s = []int32{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1} 71 var int64s = []int64{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1, 1000000000000000000, 1 << 60, 1<<63 - 2, 1<<63 - 1} 72 var uint64s = []uint64{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1, 1000000000000000000, 1 << 60, 1<<64 - 2, 1<<64 - 1} 73 var permSizes = []int{0, 1, 5, 8, 9, 10, 16} 74 var readBufferSizes = []int{1, 7, 8, 9, 10} 75 r := New(NewSource(0)) 76 77 rv := reflect.ValueOf(r) 78 n := rv.NumMethod() 79 p := 0 80 if *printgolden { 81 fmt.Printf("var regressGolden = []interface{}{\n") 82 } 83 for i := 0; i < n; i++ { 84 m := rv.Type().Method(i) 85 mv := rv.Method(i) 86 mt := mv.Type() 87 if mt.NumOut() == 0 { 88 continue 89 } 90 r.Seed(0) 91 if *printgolden && i > 0 { 92 fmt.Println() 93 } 94 for repeat := 0; repeat < 20; repeat++ { 95 var args []reflect.Value 96 var argstr string 97 if mt.NumIn() == 1 { 98 var x interface{} 99 switch mt.In(0).Kind() { 100 default: 101 t.Fatalf("unexpected argument type for r.%s", m.Name) 102 103 case reflect.Int: 104 if m.Name == "Perm" { 105 x = permSizes[repeat%len(permSizes)] 106 break 107 } 108 big := int64s[repeat%len(int64s)] 109 if int64(int(big)) != big { 110 r.Int63n(big) // what would happen on 64-bit machine, to keep stream in sync 111 if *printgolden { 112 fmt.Printf("\tskipped, // must run printgolden on 64-bit machine\n") 113 } 114 p++ 115 continue 116 } 117 x = int(big) 118 119 case reflect.Int32: 120 x = int32s[repeat%len(int32s)] 121 122 case reflect.Int64: 123 x = int64s[repeat%len(int64s)] 124 125 case reflect.Uint64: 126 x = uint64s[repeat%len(uint64s)] 127 128 case reflect.Slice: 129 if m.Name == "Read" { 130 n := readBufferSizes[repeat%len(readBufferSizes)] 131 x = make([]byte, n) 132 } 133 } 134 argstr = fmt.Sprint(x) 135 args = append(args, reflect.ValueOf(x)) 136 } 137 138 var out interface{} 139 out = mv.Call(args)[0].Interface() 140 if m.Name == "Int" || m.Name == "Intn" { 141 out = int64(out.(int)) 142 } 143 if m.Name == "Read" { 144 out = args[0].Interface().([]byte) 145 } 146 if *printgolden { 147 var val string 148 big := int64(1 << 60) 149 if int64(int(big)) != big && (m.Name == "Int" || m.Name == "Intn") { 150 // 32-bit machine cannot print 64-bit results 151 val = "truncated" 152 } else if reflect.TypeOf(out).Kind() == reflect.Slice { 153 val = fmt.Sprintf("%#v", out) 154 } else { 155 val = fmt.Sprintf("%T(%v)", out, out) 156 } 157 fmt.Printf("\t%s, // %s(%s)\n", val, m.Name, argstr) 158 } else { 159 want := regressGolden[p] 160 if m.Name == "Int" { 161 want = int64(int(uint(want.(int64)) << 1 >> 1)) 162 } 163 if !reflect.DeepEqual(out, want) { 164 t.Errorf("r.%s(%s) = %v, want %v", m.Name, argstr, out, want) 165 } 166 } 167 p++ 168 } 169 } 170 if *printgolden { 171 fmt.Printf("}\n") 172 } 173 } 174 175 var regressGolden = []interface{}{ 176 float64(0.6279600685109523), // ExpFloat64() 177 float64(0.16198826513357806), // ExpFloat64() 178 float64(0.007880404652650552), // ExpFloat64() 179 float64(0.41649788761745654), // ExpFloat64() 180 float64(1.6958707787276301), // ExpFloat64() 181 float64(2.7227327706138036), // ExpFloat64() 182 float64(2.4235600263079657), // ExpFloat64() 183 float64(1.277967771105418), // ExpFloat64() 184 float64(0.7111660437031769), // ExpFloat64() 185 float64(0.23090401427981888), // ExpFloat64() 186 float64(1.4746763588379928), // ExpFloat64() 187 float64(1.4868726779832278), // ExpFloat64() 188 float64(0.1686257242078103), // ExpFloat64() 189 float64(0.2732721816228957), // ExpFloat64() 190 float64(0.4644536065869748), // ExpFloat64() 191 float64(0.01319850986379164), // ExpFloat64() 192 float64(0.7184492551742854), // ExpFloat64() 193 float64(0.1913536422195827), // ExpFloat64() 194 float64(0.16034475958495667), // ExpFloat64() 195 float64(0.40599859014785644), // ExpFloat64() 196 197 float32(0.7979972), // Float32() 198 float32(0.7725961), // Float32() 199 float32(0.21894403), // Float32() 200 float32(0.96194494), // Float32() 201 float32(0.2915732), // Float32() 202 float32(0.59569645), // Float32() 203 float32(0.99596655), // Float32() 204 float32(0.4979039), // Float32() 205 float32(0.98148686), // Float32() 206 float32(0.01380035), // Float32() 207 float32(0.086487144), // Float32() 208 float32(0.6114401), // Float32() 209 float32(0.71081316), // Float32() 210 float32(0.6342346), // Float32() 211 float32(0.008082573), // Float32() 212 float32(0.33020085), // Float32() 213 float32(0.032625034), // Float32() 214 float32(0.9278005), // Float32() 215 float32(0.34497985), // Float32() 216 float32(0.66506875), // Float32() 217 218 float64(0.797997151016231), // Float64() 219 float64(0.7725961454373316), // Float64() 220 float64(0.21894402538580782), // Float64() 221 float64(0.9619449481780457), // Float64() 222 float64(0.2915731877602916), // Float64() 223 float64(0.5956964580775652), // Float64() 224 float64(0.9959665347028619), // Float64() 225 float64(0.49790390966591147), // Float64() 226 float64(0.9814868602566785), // Float64() 227 float64(0.013800350332924483), // Float64() 228 float64(0.08648714463652596), // Float64() 229 float64(0.6114401479210267), // Float64() 230 float64(0.7108131531183706), // Float64() 231 float64(0.6342346133706837), // Float64() 232 float64(0.008082572853887138), // Float64() 233 float64(0.3302008651926287), // Float64() 234 float64(0.03262503454637655), // Float64() 235 float64(0.9278004634858956), // Float64() 236 float64(0.3449798628384906), // Float64() 237 float64(0.665068719316529), // Float64() 238 239 int64(5474557666971700975), // Int() 240 int64(5591422465364813936), // Int() 241 int64(74029666500212977), // Int() 242 int64(8088122161323000979), // Int() 243 int64(7298457654139700474), // Int() 244 int64(1590632625527662686), // Int() 245 int64(9052198920789078554), // Int() 246 int64(7381380909356947872), // Int() 247 int64(1738222704626512495), // Int() 248 int64(3278744831230954970), // Int() 249 int64(7062423222661652521), // Int() 250 int64(6715870808026712034), // Int() 251 int64(528819992478005418), // Int() 252 int64(2284534088986354339), // Int() 253 int64(945828723091990082), // Int() 254 int64(3813019469742317492), // Int() 255 int64(1369388146907482806), // Int() 256 int64(7367238674766648970), // Int() 257 int64(8217673022687244206), // Int() 258 int64(3185531743396549562), // Int() 259 260 int32(1711064216), // Int31() 261 int32(650927245), // Int31() 262 int32(8618187), // Int31() 263 int32(941581344), // Int31() 264 int32(1923394120), // Int31() 265 int32(1258915833), // Int31() 266 int32(1053814650), // Int31() 267 int32(859305834), // Int31() 268 int32(1276097579), // Int31() 269 int32(1455437958), // Int31() 270 int32(1895916096), // Int31() 271 int32(781830261), // Int31() 272 int32(61562749), // Int31() 273 int32(265954771), // Int31() 274 int32(1183850779), // Int31() 275 int32(443893888), // Int31() 276 int32(1233159585), // Int31() 277 int32(857659461), // Int31() 278 int32(956663049), // Int31() 279 int32(370844703), // Int31() 280 281 int32(0), // Int31n(1) 282 int32(6), // Int31n(10) 283 int32(17), // Int31n(32) 284 int32(1000595), // Int31n(1048576) 285 int32(424333), // Int31n(1048577) 286 int32(382438494), // Int31n(1000000000) 287 int32(902738458), // Int31n(1073741824) 288 int32(1204933878), // Int31n(2147483646) 289 int32(1376191263), // Int31n(2147483647) 290 int32(0), // Int31n(1) 291 int32(9), // Int31n(10) 292 int32(2), // Int31n(32) 293 int32(440490), // Int31n(1048576) 294 int32(176312), // Int31n(1048577) 295 int32(946765890), // Int31n(1000000000) 296 int32(665034676), // Int31n(1073741824) 297 int32(1947285452), // Int31n(2147483646) 298 int32(1702344608), // Int31n(2147483647) 299 int32(0), // Int31n(1) 300 int32(2), // Int31n(10) 301 302 int64(5474557666971700975), // Int63() 303 int64(5591422465364813936), // Int63() 304 int64(74029666500212977), // Int63() 305 int64(8088122161323000979), // Int63() 306 int64(7298457654139700474), // Int63() 307 int64(1590632625527662686), // Int63() 308 int64(9052198920789078554), // Int63() 309 int64(7381380909356947872), // Int63() 310 int64(1738222704626512495), // Int63() 311 int64(3278744831230954970), // Int63() 312 int64(7062423222661652521), // Int63() 313 int64(6715870808026712034), // Int63() 314 int64(528819992478005418), // Int63() 315 int64(2284534088986354339), // Int63() 316 int64(945828723091990082), // Int63() 317 int64(3813019469742317492), // Int63() 318 int64(1369388146907482806), // Int63() 319 int64(7367238674766648970), // Int63() 320 int64(8217673022687244206), // Int63() 321 int64(3185531743396549562), // Int63() 322 323 int64(0), // Int63n(1) 324 int64(6), // Int63n(10) 325 int64(17), // Int63n(32) 326 int64(1000595), // Int63n(1048576) 327 int64(424333), // Int63n(1048577) 328 int64(382438494), // Int63n(1000000000) 329 int64(902738458), // Int63n(1073741824) 330 int64(1204933878), // Int63n(2147483646) 331 int64(1376191263), // Int63n(2147483647) 332 int64(502116868085730778), // Int63n(1000000000000000000) 333 int64(144894195020570665), // Int63n(1152921504606846976) 334 int64(6715870808026712034), // Int63n(9223372036854775806) 335 int64(528819992478005418), // Int63n(9223372036854775807) 336 int64(0), // Int63n(1) 337 int64(0), // Int63n(10) 338 int64(20), // Int63n(32) 339 int64(854710), // Int63n(1048576) 340 int64(649893), // Int63n(1048577) 341 int64(687244206), // Int63n(1000000000) 342 int64(836883386), // Int63n(1073741824) 343 344 int64(0), // Intn(1) 345 int64(6), // Intn(10) 346 int64(17), // Intn(32) 347 int64(1000595), // Intn(1048576) 348 int64(424333), // Intn(1048577) 349 int64(382438494), // Intn(1000000000) 350 int64(902738458), // Intn(1073741824) 351 int64(1204933878), // Intn(2147483646) 352 int64(1376191263), // Intn(2147483647) 353 int64(502116868085730778), // Intn(1000000000000000000) 354 int64(144894195020570665), // Intn(1152921504606846976) 355 int64(6715870808026712034), // Intn(9223372036854775806) 356 int64(528819992478005418), // Intn(9223372036854775807) 357 int64(0), // Intn(1) 358 int64(0), // Intn(10) 359 int64(20), // Intn(32) 360 int64(854710), // Intn(1048576) 361 int64(649893), // Intn(1048577) 362 int64(687244206), // Intn(1000000000) 363 int64(836883386), // Intn(1073741824) 364 365 float64(-0.5410658516792047), // NormFloat64() 366 float64(0.615296849055287), // NormFloat64() 367 float64(0.007477442280032887), // NormFloat64() 368 float64(1.3443892057169684), // NormFloat64() 369 float64(-0.17508902754863512), // NormFloat64() 370 float64(-2.03494397556937), // NormFloat64() 371 float64(2.5213558871972306), // NormFloat64() 372 float64(1.4572921639613627), // NormFloat64() 373 float64(-1.5164961164210644), // NormFloat64() 374 float64(-0.4861150771891445), // NormFloat64() 375 float64(-0.8699409548614199), // NormFloat64() 376 float64(1.6271559815452794), // NormFloat64() 377 float64(0.1659465769926195), // NormFloat64() 378 float64(0.2921716191987018), // NormFloat64() 379 float64(-1.2550269636927838), // NormFloat64() 380 float64(0.11257973349467548), // NormFloat64() 381 float64(0.5437525915836436), // NormFloat64() 382 float64(0.781754430770282), // NormFloat64() 383 float64(0.5201256313962235), // NormFloat64() 384 float64(1.3826174159276245), // NormFloat64() 385 386 []int{}, // Perm(0) 387 []int{0}, // Perm(1) 388 []int{0, 2, 3, 1, 4}, // Perm(5) 389 []int{5, 6, 3, 7, 4, 2, 0, 1}, // Perm(8) 390 []int{8, 4, 5, 2, 7, 3, 0, 6, 1}, // Perm(9) 391 []int{6, 1, 5, 3, 2, 9, 7, 0, 8, 4}, // Perm(10) 392 []int{12, 5, 1, 9, 15, 7, 13, 6, 10, 11, 8, 0, 4, 2, 14, 3}, // Perm(16) 393 []int{}, // Perm(0) 394 []int{0}, // Perm(1) 395 []int{0, 2, 3, 4, 1}, // Perm(5) 396 []int{3, 2, 7, 4, 0, 6, 5, 1}, // Perm(8) 397 []int{0, 6, 2, 1, 3, 7, 5, 8, 4}, // Perm(9) 398 []int{2, 5, 6, 4, 7, 3, 0, 8, 1, 9}, // Perm(10) 399 []int{3, 6, 5, 4, 9, 15, 13, 7, 1, 11, 10, 8, 12, 0, 2, 14}, // Perm(16) 400 []int{}, // Perm(0) 401 []int{0}, // Perm(1) 402 []int{2, 4, 3, 1, 0}, // Perm(5) 403 []int{1, 6, 7, 5, 4, 3, 2, 0}, // Perm(8) 404 []int{7, 6, 8, 2, 0, 1, 3, 4, 5}, // Perm(9) 405 []int{2, 9, 7, 1, 5, 4, 0, 6, 8, 3}, // Perm(10) 406 407 []byte{0xef}, // Read([0]) 408 []byte{0x4e, 0x3d, 0x52, 0x31, 0x89, 0xf9, 0xcb}, // Read([0 0 0 0 0 0 0]) 409 []byte{0x70, 0x68, 0x35, 0x8d, 0x1b, 0xb9, 0x98, 0x4d}, // Read([0 0 0 0 0 0 0 0]) 410 []byte{0xf1, 0xf8, 0x95, 0xe6, 0x96, 0x1, 0x7, 0x1, 0x93}, // Read([0 0 0 0 0 0 0 0 0]) 411 []byte{0x44, 0x9f, 0xc5, 0x40, 0xc8, 0x3e, 0x70, 0xfa, 0x44, 0x3a}, // Read([0 0 0 0 0 0 0 0 0 0]) 412 []byte{0x4b}, // Read([0]) 413 []byte{0x91, 0x54, 0x49, 0xe5, 0x5e, 0x28, 0xb9}, // Read([0 0 0 0 0 0 0]) 414 []byte{0x4, 0xf2, 0xf, 0x13, 0x96, 0x1a, 0xb2, 0xce}, // Read([0 0 0 0 0 0 0 0]) 415 []byte{0x35, 0xf5, 0xde, 0x9f, 0x7d, 0xa0, 0x19, 0x12, 0x2e}, // Read([0 0 0 0 0 0 0 0 0]) 416 []byte{0xd4, 0xee, 0x6f, 0x66, 0x6f, 0x32, 0xc8, 0x21, 0x57, 0x68}, // Read([0 0 0 0 0 0 0 0 0 0]) 417 []byte{0x1f}, // Read([0]) 418 []byte{0x98, 0xda, 0x4d, 0xab, 0x6e, 0xd, 0x71}, // Read([0 0 0 0 0 0 0]) 419 []byte{0x80, 0xad, 0x29, 0xa0, 0x37, 0xb0, 0x80, 0xc4}, // Read([0 0 0 0 0 0 0 0]) 420 []byte{0x2, 0xe2, 0xe2, 0x7, 0xd9, 0xed, 0xea, 0x90, 0x33}, // Read([0 0 0 0 0 0 0 0 0]) 421 []byte{0x5d, 0xaa, 0xb8, 0xc6, 0x39, 0xfb, 0xbe, 0x56, 0x7, 0xa3}, // Read([0 0 0 0 0 0 0 0 0 0]) 422 []byte{0x62}, // Read([0]) 423 []byte{0x4d, 0x63, 0xa6, 0x4b, 0xb4, 0x1f, 0x42}, // Read([0 0 0 0 0 0 0]) 424 []byte{0x66, 0x42, 0x62, 0x36, 0x42, 0x20, 0x8d, 0xb4}, // Read([0 0 0 0 0 0 0 0]) 425 []byte{0x9f, 0xa3, 0x67, 0x1, 0x91, 0xea, 0x34, 0xb6, 0xa}, // Read([0 0 0 0 0 0 0 0 0]) 426 []byte{0xd, 0xa8, 0x43, 0xb, 0x1, 0x93, 0x8a, 0x56, 0xfc, 0x98}, // Read([0 0 0 0 0 0 0 0 0 0]) 427 428 uint32(3422128433), // Uint32() 429 uint32(1301854491), // Uint32() 430 uint32(17236374), // Uint32() 431 uint32(1883162688), // Uint32() 432 uint32(3846788241), // Uint32() 433 uint32(2517831666), // Uint32() 434 uint32(2107629301), // Uint32() 435 uint32(1718611668), // Uint32() 436 uint32(2552195159), // Uint32() 437 uint32(2910875917), // Uint32() 438 uint32(3791832192), // Uint32() 439 uint32(1563660522), // Uint32() 440 uint32(123125499), // Uint32() 441 uint32(531909542), // Uint32() 442 uint32(2367701558), // Uint32() 443 uint32(887787777), // Uint32() 444 uint32(2466319171), // Uint32() 445 uint32(1715318922), // Uint32() 446 uint32(1913326099), // Uint32() 447 uint32(741689406), // Uint32() 448 449 uint64(14697929703826476783), // Uint64() 450 uint64(5591422465364813936), // Uint64() 451 uint64(74029666500212977), // Uint64() 452 uint64(8088122161323000979), // Uint64() 453 uint64(16521829690994476282), // Uint64() 454 uint64(10814004662382438494), // Uint64() 455 uint64(9052198920789078554), // Uint64() 456 uint64(7381380909356947872), // Uint64() 457 uint64(10961594741481288303), // Uint64() 458 uint64(12502116868085730778), // Uint64() 459 uint64(16285795259516428329), // Uint64() 460 uint64(6715870808026712034), // Uint64() 461 uint64(528819992478005418), // Uint64() 462 uint64(2284534088986354339), // Uint64() 463 uint64(10169200759946765890), // Uint64() 464 uint64(3813019469742317492), // Uint64() 465 uint64(10592760183762258614), // Uint64() 466 uint64(7367238674766648970), // Uint64() 467 uint64(8217673022687244206), // Uint64() 468 uint64(3185531743396549562), // Uint64() 469 470 uint64(0), // Uint64n(1) 471 uint64(6), // Uint64n(10) 472 uint64(17), // Uint64n(32) 473 uint64(1000595), // Uint64n(1048576) 474 uint64(424333), // Uint64n(1048577) 475 uint64(382438494), // Uint64n(1000000000) 476 uint64(902738458), // Uint64n(1073741824) 477 uint64(1204933878), // Uint64n(2147483646) 478 uint64(1376191263), // Uint64n(2147483647) 479 uint64(502116868085730778), // Uint64n(1000000000000000000) 480 uint64(144894195020570665), // Uint64n(1152921504606846976) 481 uint64(6715870808026712034), // Uint64n(18446744073709551614) 482 uint64(528819992478005418), // Uint64n(18446744073709551615) 483 uint64(0), // Uint64n(1) 484 uint64(0), // Uint64n(10) 485 uint64(20), // Uint64n(32) 486 uint64(854710), // Uint64n(1048576) 487 uint64(649893), // Uint64n(1048577) 488 uint64(687244206), // Uint64n(1000000000) 489 uint64(836883386), // Uint64n(1073741824) 490 }