| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package rand_test |
| 14 |
|
| 15 |
import ( |
| 16 |
"flag" |
| 17 |
"fmt" |
| 18 |
"reflect" |
| 19 |
"testing" |
| 20 |
|
| 21 |
. "golang.org/x/exp/rand" |
| 22 |
) |
| 23 |
|
| 24 |
var printgolden = flag.Bool("printgolden", false, "print golden results for regression test") |
| 25 |
|
| 26 |
|
| 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 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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) |
| 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 |
|
| 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), |
| 177 |
float64(0.16198826513357806), |
| 178 |
float64(0.007880404652650552), |
| 179 |
float64(0.41649788761745654), |
| 180 |
float64(1.6958707787276301), |
| 181 |
float64(2.7227327706138036), |
| 182 |
float64(2.4235600263079657), |
| 183 |
float64(1.277967771105418), |
| 184 |
float64(0.7111660437031769), |
| 185 |
float64(0.23090401427981888), |
| 186 |
float64(1.4746763588379928), |
| 187 |
float64(1.4868726779832278), |
| 188 |
float64(0.1686257242078103), |
| 189 |
float64(0.2732721816228957), |
| 190 |
float64(0.4644536065869748), |
| 191 |
float64(0.01319850986379164), |
| 192 |
float64(0.7184492551742854), |
| 193 |
float64(0.1913536422195827), |
| 194 |
float64(0.16034475958495667), |
| 195 |
float64(0.40599859014785644), |
| 196 |
|
| 197 |
float32(0.7979972), |
| 198 |
float32(0.7725961), |
| 199 |
float32(0.21894403), |
| 200 |
float32(0.96194494), |
| 201 |
float32(0.2915732), |
| 202 |
float32(0.59569645), |
| 203 |
float32(0.99596655), |
| 204 |
float32(0.4979039), |
| 205 |
float32(0.98148686), |
| 206 |
float32(0.01380035), |
| 207 |
float32(0.086487144), |
| 208 |
float32(0.6114401), |
| 209 |
float32(0.71081316), |
| 210 |
float32(0.6342346), |
| 211 |
float32(0.008082573), |
| 212 |
float32(0.33020085), |
| 213 |
float32(0.032625034), |
| 214 |
float32(0.9278005), |
| 215 |
float32(0.34497985), |
| 216 |
float32(0.66506875), |
| 217 |
|
| 218 |
float64(0.797997151016231), |
| 219 |
float64(0.7725961454373316), |
| 220 |
float64(0.21894402538580782), |
| 221 |
float64(0.9619449481780457), |
| 222 |
float64(0.2915731877602916), |
| 223 |
float64(0.5956964580775652), |
| 224 |
float64(0.9959665347028619), |
| 225 |
float64(0.49790390966591147), |
| 226 |
float64(0.9814868602566785), |
| 227 |
float64(0.013800350332924483), |
| 228 |
float64(0.08648714463652596), |
| 229 |
float64(0.6114401479210267), |
| 230 |
float64(0.7108131531183706), |
| 231 |
float64(0.6342346133706837), |
| 232 |
float64(0.008082572853887138), |
| 233 |
float64(0.3302008651926287), |
| 234 |
float64(0.03262503454637655), |
| 235 |
float64(0.9278004634858956), |
| 236 |
float64(0.3449798628384906), |
| 237 |
float64(0.665068719316529), |
| 238 |
|
| 239 |
int64(5474557666971700975), |
| 240 |
int64(5591422465364813936), |
| 241 |
int64(74029666500212977), |
| 242 |
int64(8088122161323000979), |
| 243 |
int64(7298457654139700474), |
| 244 |
int64(1590632625527662686), |
| 245 |
int64(9052198920789078554), |
| 246 |
int64(7381380909356947872), |
| 247 |
int64(1738222704626512495), |
| 248 |
int64(3278744831230954970), |
| 249 |
int64(7062423222661652521), |
| 250 |
int64(6715870808026712034), |
| 251 |
int64(528819992478005418), |
| 252 |
int64(2284534088986354339), |
| 253 |
int64(945828723091990082), |
| 254 |
int64(3813019469742317492), |
| 255 |
int64(1369388146907482806), |
| 256 |
int64(7367238674766648970), |
| 257 |
int64(8217673022687244206), |
| 258 |
int64(3185531743396549562), |
| 259 |
|
| 260 |
int32(1711064216), |
| 261 |
int32(650927245), |
| 262 |
int32(8618187), |
| 263 |
int32(941581344), |
| 264 |
int32(1923394120), |
| 265 |
int32(1258915833), |
| 266 |
int32(1053814650), |
| 267 |
int32(859305834), |
| 268 |
int32(1276097579), |
| 269 |
int32(1455437958), |
| 270 |
int32(1895916096), |
| 271 |
int32(781830261), |
| 272 |
int32(61562749), |
| 273 |
int32(265954771), |
| 274 |
int32(1183850779), |
| 275 |
int32(443893888), |
| 276 |
int32(1233159585), |
| 277 |
int32(857659461), |
| 278 |
int32(956663049), |
| 279 |
int32(370844703), |
| 280 |
|
| 281 |
int32(0), |
| 282 |
int32(6), |
| 283 |
int32(17), |
| 284 |
int32(1000595), |
| 285 |
int32(424333), |
| 286 |
int32(382438494), |
| 287 |
int32(902738458), |
| 288 |
int32(1204933878), |
| 289 |
int32(1376191263), |
| 290 |
int32(0), |
| 291 |
int32(9), |
| 292 |
int32(2), |
| 293 |
int32(440490), |
| 294 |
int32(176312), |
| 295 |
int32(946765890), |
| 296 |
int32(665034676), |
| 297 |
int32(1947285452), |
| 298 |
int32(1702344608), |
| 299 |
int32(0), |
| 300 |
int32(2), |
| 301 |
|
| 302 |
int64(5474557666971700975), |
| 303 |
int64(5591422465364813936), |
| 304 |
int64(74029666500212977), |
| 305 |
int64(8088122161323000979), |
| 306 |
int64(7298457654139700474), |
| 307 |
int64(1590632625527662686), |
| 308 |
int64(9052198920789078554), |
| 309 |
int64(7381380909356947872), |
| 310 |
int64(1738222704626512495), |
| 311 |
int64(3278744831230954970), |
| 312 |
int64(7062423222661652521), |
| 313 |
int64(6715870808026712034), |
| 314 |
int64(528819992478005418), |
| 315 |
int64(2284534088986354339), |
| 316 |
int64(945828723091990082), |
| 317 |
int64(3813019469742317492), |
| 318 |
int64(1369388146907482806), |
| 319 |
int64(7367238674766648970), |
| 320 |
int64(8217673022687244206), |
| 321 |
int64(3185531743396549562), |
| 322 |
|
| 323 |
int64(0), |
| 324 |
int64(6), |
| 325 |
int64(17), |
| 326 |
int64(1000595), |
| 327 |
int64(424333), |
| 328 |
int64(382438494), |
| 329 |
int64(902738458), |
| 330 |
int64(1204933878), |
| 331 |
int64(1376191263), |
| 332 |
int64(502116868085730778), |
| 333 |
int64(144894195020570665), |
| 334 |
int64(6715870808026712034), |
| 335 |
int64(528819992478005418), |
| 336 |
int64(0), |
| 337 |
int64(0), |
| 338 |
int64(20), |
| 339 |
int64(854710), |
| 340 |
int64(649893), |
| 341 |
int64(687244206), |
| 342 |
int64(836883386), |
| 343 |
|
| 344 |
int64(0), |
| 345 |
int64(6), |
| 346 |
int64(17), |
| 347 |
int64(1000595), |
| 348 |
int64(424333), |
| 349 |
int64(382438494), |
| 350 |
int64(902738458), |
| 351 |
int64(1204933878), |
| 352 |
int64(1376191263), |
| 353 |
int64(502116868085730778), |
| 354 |
int64(144894195020570665), |
| 355 |
int64(6715870808026712034), |
| 356 |
int64(528819992478005418), |
| 357 |
int64(0), |
| 358 |
int64(0), |
| 359 |
int64(20), |
| 360 |
int64(854710), |
| 361 |
int64(649893), |
| 362 |
int64(687244206), |
| 363 |
int64(836883386), |
| 364 |
|
| 365 |
float64(-0.5410658516792047), |
| 366 |
float64(0.615296849055287), |
| 367 |
float64(0.007477442280032887), |
| 368 |
float64(1.3443892057169684), |
| 369 |
float64(-0.17508902754863512), |
| 370 |
float64(-2.03494397556937), |
| 371 |
float64(2.5213558871972306), |
| 372 |
float64(1.4572921639613627), |
| 373 |
float64(-1.5164961164210644), |
| 374 |
float64(-0.4861150771891445), |
| 375 |
float64(-0.8699409548614199), |
| 376 |
float64(1.6271559815452794), |
| 377 |
float64(0.1659465769926195), |
| 378 |
float64(0.2921716191987018), |
| 379 |
float64(-1.2550269636927838), |
| 380 |
float64(0.11257973349467548), |
| 381 |
float64(0.5437525915836436), |
| 382 |
float64(0.781754430770282), |
| 383 |
float64(0.5201256313962235), |
| 384 |
float64(1.3826174159276245), |
| 385 |
|
| 386 |
[]int{}, |
| 387 |
[]int{0}, |
| 388 |
[]int{0, 2, 3, 1, 4}, |
| 389 |
[]int{5, 6, 3, 7, 4, 2, 0, 1}, |
| 390 |
[]int{8, 4, 5, 2, 7, 3, 0, 6, 1}, |
| 391 |
[]int{6, 1, 5, 3, 2, 9, 7, 0, 8, 4}, |
| 392 |
[]int{12, 5, 1, 9, 15, 7, 13, 6, 10, 11, 8, 0, 4, 2, 14, 3}, |
| 393 |
[]int{}, |
| 394 |
[]int{0}, |
| 395 |
[]int{0, 2, 3, 4, 1}, |
| 396 |
[]int{3, 2, 7, 4, 0, 6, 5, 1}, |
| 397 |
[]int{0, 6, 2, 1, 3, 7, 5, 8, 4}, |
| 398 |
[]int{2, 5, 6, 4, 7, 3, 0, 8, 1, 9}, |
| 399 |
[]int{3, 6, 5, 4, 9, 15, 13, 7, 1, 11, 10, 8, 12, 0, 2, 14}, |
| 400 |
[]int{}, |
| 401 |
[]int{0}, |
| 402 |
[]int{2, 4, 3, 1, 0}, |
| 403 |
[]int{1, 6, 7, 5, 4, 3, 2, 0}, |
| 404 |
[]int{7, 6, 8, 2, 0, 1, 3, 4, 5}, |
| 405 |
[]int{2, 9, 7, 1, 5, 4, 0, 6, 8, 3}, |
| 406 |
|
| 407 |
[]byte{0xef}, |
| 408 |
[]byte{0x4e, 0x3d, 0x52, 0x31, 0x89, 0xf9, 0xcb}, |
| 409 |
[]byte{0x70, 0x68, 0x35, 0x8d, 0x1b, 0xb9, 0x98, 0x4d}, |
| 410 |
[]byte{0xf1, 0xf8, 0x95, 0xe6, 0x96, 0x1, 0x7, 0x1, 0x93}, |
| 411 |
[]byte{0x44, 0x9f, 0xc5, 0x40, 0xc8, 0x3e, 0x70, 0xfa, 0x44, 0x3a}, |
| 412 |
[]byte{0x4b}, |
| 413 |
[]byte{0x91, 0x54, 0x49, 0xe5, 0x5e, 0x28, 0xb9}, |
| 414 |
[]byte{0x4, 0xf2, 0xf, 0x13, 0x96, 0x1a, 0xb2, 0xce}, |
| 415 |
[]byte{0x35, 0xf5, 0xde, 0x9f, 0x7d, 0xa0, 0x19, 0x12, 0x2e}, |
| 416 |
[]byte{0xd4, 0xee, 0x6f, 0x66, 0x6f, 0x32, 0xc8, 0x21, 0x57, 0x68}, |
| 417 |
[]byte{0x1f}, |
| 418 |
[]byte{0x98, 0xda, 0x4d, 0xab, 0x6e, 0xd, 0x71}, |
| 419 |
[]byte{0x80, 0xad, 0x29, 0xa0, 0x37, 0xb0, 0x80, 0xc4}, |
| 420 |
[]byte{0x2, 0xe2, 0xe2, 0x7, 0xd9, 0xed, 0xea, 0x90, 0x33}, |
| 421 |
[]byte{0x5d, 0xaa, 0xb8, 0xc6, 0x39, 0xfb, 0xbe, 0x56, 0x7, 0xa3}, |
| 422 |
[]byte{0x62}, |
| 423 |
[]byte{0x4d, 0x63, 0xa6, 0x4b, 0xb4, 0x1f, 0x42}, |
| 424 |
[]byte{0x66, 0x42, 0x62, 0x36, 0x42, 0x20, 0x8d, 0xb4}, |
| 425 |
[]byte{0x9f, 0xa3, 0x67, 0x1, 0x91, 0xea, 0x34, 0xb6, 0xa}, |
| 426 |
[]byte{0xd, 0xa8, 0x43, 0xb, 0x1, 0x93, 0x8a, 0x56, 0xfc, 0x98}, |
| 427 |
|
| 428 |
uint32(3422128433), |
| 429 |
uint32(1301854491), |
| 430 |
uint32(17236374), |
| 431 |
uint32(1883162688), |
| 432 |
uint32(3846788241), |
| 433 |
uint32(2517831666), |
| 434 |
uint32(2107629301), |
| 435 |
uint32(1718611668), |
| 436 |
uint32(2552195159), |
| 437 |
uint32(2910875917), |
| 438 |
uint32(3791832192), |
| 439 |
uint32(1563660522), |
| 440 |
uint32(123125499), |
| 441 |
uint32(531909542), |
| 442 |
uint32(2367701558), |
| 443 |
uint32(887787777), |
| 444 |
uint32(2466319171), |
| 445 |
uint32(1715318922), |
| 446 |
uint32(1913326099), |
| 447 |
uint32(741689406), |
| 448 |
|
| 449 |
uint64(14697929703826476783), |
| 450 |
uint64(5591422465364813936), |
| 451 |
uint64(74029666500212977), |
| 452 |
uint64(8088122161323000979), |
| 453 |
uint64(16521829690994476282), |
| 454 |
uint64(10814004662382438494), |
| 455 |
uint64(9052198920789078554), |
| 456 |
uint64(7381380909356947872), |
| 457 |
uint64(10961594741481288303), |
| 458 |
uint64(12502116868085730778), |
| 459 |
uint64(16285795259516428329), |
| 460 |
uint64(6715870808026712034), |
| 461 |
uint64(528819992478005418), |
| 462 |
uint64(2284534088986354339), |
| 463 |
uint64(10169200759946765890), |
| 464 |
uint64(3813019469742317492), |
| 465 |
uint64(10592760183762258614), |
| 466 |
uint64(7367238674766648970), |
| 467 |
uint64(8217673022687244206), |
| 468 |
uint64(3185531743396549562), |
| 469 |
|
| 470 |
uint64(0), |
| 471 |
uint64(6), |
| 472 |
uint64(17), |
| 473 |
uint64(1000595), |
| 474 |
uint64(424333), |
| 475 |
uint64(382438494), |
| 476 |
uint64(902738458), |
| 477 |
uint64(1204933878), |
| 478 |
uint64(1376191263), |
| 479 |
uint64(502116868085730778), |
| 480 |
uint64(144894195020570665), |
| 481 |
uint64(6715870808026712034), |
| 482 |
uint64(528819992478005418), |
| 483 |
uint64(0), |
| 484 |
uint64(0), |
| 485 |
uint64(20), |
| 486 |
uint64(854710), |
| 487 |
uint64(649893), |
| 488 |
uint64(687244206), |
| 489 |
uint64(836883386), |
| 490 |
} |