github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/test/ken/divconst.go (about) 1 // run 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Test integer division by constants. 8 9 package main 10 11 import "math/rand" 12 13 const Count = 1e5 14 15 func i64rand() int64 { 16 for { 17 a := int64(rand.Uint32()) 18 a = (a << 32) | int64(rand.Uint32()) 19 a >>= uint(rand.Intn(64)) 20 if -a != a { 21 return a 22 } 23 } 24 return 0 // impossible 25 } 26 27 func i64test(a, b, c int64) { 28 d := a / c 29 if d != b { 30 println("i64", a, b, c, d) 31 panic("fail") 32 } 33 } 34 35 func i64run() { 36 var a, b int64 37 38 for i := 0; i < Count; i++ { 39 a = i64rand() 40 41 b = a / 1 42 i64test(a, b, 1) 43 b = a / 2 44 i64test(a, b, 2) 45 b = a / 3 46 i64test(a, b, 3) 47 b = a / 4 48 i64test(a, b, 4) 49 b = a / 5 50 i64test(a, b, 5) 51 b = a / 6 52 i64test(a, b, 6) 53 b = a / 7 54 i64test(a, b, 7) 55 b = a / 8 56 i64test(a, b, 8) 57 b = a / 10 58 i64test(a, b, 10) 59 b = a / 16 60 i64test(a, b, 16) 61 b = a / 20 62 i64test(a, b, 20) 63 b = a / 32 64 i64test(a, b, 32) 65 b = a / 60 66 i64test(a, b, 60) 67 b = a / 64 68 i64test(a, b, 64) 69 b = a / 128 70 i64test(a, b, 128) 71 b = a / 256 72 i64test(a, b, 256) 73 b = a / 16384 74 i64test(a, b, 16384) 75 76 b = a / -1 77 i64test(a, b, -1) 78 b = a / -2 79 i64test(a, b, -2) 80 b = a / -3 81 i64test(a, b, -3) 82 b = a / -4 83 i64test(a, b, -4) 84 b = a / -5 85 i64test(a, b, -5) 86 b = a / -6 87 i64test(a, b, -6) 88 b = a / -7 89 i64test(a, b, -7) 90 b = a / -8 91 i64test(a, b, -8) 92 b = a / -10 93 i64test(a, b, -10) 94 b = a / -16 95 i64test(a, b, -16) 96 b = a / -20 97 i64test(a, b, -20) 98 b = a / -32 99 i64test(a, b, -32) 100 b = a / -60 101 i64test(a, b, -60) 102 b = a / -64 103 i64test(a, b, -64) 104 b = a / -128 105 i64test(a, b, -128) 106 b = a / -256 107 i64test(a, b, -256) 108 b = a / -16384 109 i64test(a, b, -16384) 110 } 111 } 112 113 func u64rand() uint64 { 114 a := uint64(rand.Uint32()) 115 a = (a << 32) | uint64(rand.Uint32()) 116 a >>= uint(rand.Intn(64)) 117 return a 118 } 119 120 func u64test(a, b, c uint64) { 121 d := a / c 122 if d != b { 123 println("u64", a, b, c, d) 124 panic("fail") 125 } 126 } 127 128 func u64run() { 129 var a, b uint64 130 131 for i := 0; i < Count; i++ { 132 a = u64rand() 133 134 b = a / 1 135 u64test(a, b, 1) 136 b = a / 2 137 u64test(a, b, 2) 138 b = a / 3 139 u64test(a, b, 3) 140 b = a / 4 141 u64test(a, b, 4) 142 b = a / 5 143 u64test(a, b, 5) 144 b = a / 6 145 u64test(a, b, 6) 146 b = a / 7 147 u64test(a, b, 7) 148 b = a / 8 149 u64test(a, b, 8) 150 b = a / 10 151 u64test(a, b, 10) 152 b = a / 16 153 u64test(a, b, 16) 154 b = a / 20 155 u64test(a, b, 20) 156 b = a / 32 157 u64test(a, b, 32) 158 b = a / 60 159 u64test(a, b, 60) 160 b = a / 64 161 u64test(a, b, 64) 162 b = a / 128 163 u64test(a, b, 128) 164 b = a / 256 165 u64test(a, b, 256) 166 b = a / 16384 167 u64test(a, b, 16384) 168 } 169 } 170 171 func i32rand() int32 { 172 for { 173 a := int32(rand.Uint32()) 174 a >>= uint(rand.Intn(32)) 175 if -a != a { 176 return a 177 } 178 } 179 return 0 // impossible 180 } 181 182 func i32test(a, b, c int32) { 183 d := a / c 184 if d != b { 185 println("i32", a, b, c, d) 186 panic("fail") 187 } 188 } 189 190 func i32run() { 191 var a, b int32 192 193 for i := 0; i < Count; i++ { 194 a = i32rand() 195 196 b = a / 1 197 i32test(a, b, 1) 198 b = a / 2 199 i32test(a, b, 2) 200 b = a / 3 201 i32test(a, b, 3) 202 b = a / 4 203 i32test(a, b, 4) 204 b = a / 5 205 i32test(a, b, 5) 206 b = a / 6 207 i32test(a, b, 6) 208 b = a / 7 209 i32test(a, b, 7) 210 b = a / 8 211 i32test(a, b, 8) 212 b = a / 10 213 i32test(a, b, 10) 214 b = a / 16 215 i32test(a, b, 16) 216 b = a / 20 217 i32test(a, b, 20) 218 b = a / 32 219 i32test(a, b, 32) 220 b = a / 60 221 i32test(a, b, 60) 222 b = a / 64 223 i32test(a, b, 64) 224 b = a / 128 225 i32test(a, b, 128) 226 b = a / 256 227 i32test(a, b, 256) 228 b = a / 16384 229 i32test(a, b, 16384) 230 231 b = a / -1 232 i32test(a, b, -1) 233 b = a / -2 234 i32test(a, b, -2) 235 b = a / -3 236 i32test(a, b, -3) 237 b = a / -4 238 i32test(a, b, -4) 239 b = a / -5 240 i32test(a, b, -5) 241 b = a / -6 242 i32test(a, b, -6) 243 b = a / -7 244 i32test(a, b, -7) 245 b = a / -8 246 i32test(a, b, -8) 247 b = a / -10 248 i32test(a, b, -10) 249 b = a / -16 250 i32test(a, b, -16) 251 b = a / -20 252 i32test(a, b, -20) 253 b = a / -32 254 i32test(a, b, -32) 255 b = a / -60 256 i32test(a, b, -60) 257 b = a / -64 258 i32test(a, b, -64) 259 b = a / -128 260 i32test(a, b, -128) 261 b = a / -256 262 i32test(a, b, -256) 263 } 264 } 265 266 func u32rand() uint32 { 267 a := uint32(rand.Uint32()) 268 a >>= uint(rand.Intn(32)) 269 return a 270 } 271 272 func u32test(a, b, c uint32) { 273 d := a / c 274 if d != b { 275 println("u32", a, b, c, d) 276 panic("fail") 277 } 278 } 279 280 func u32run() { 281 var a, b uint32 282 283 for i := 0; i < Count; i++ { 284 a = u32rand() 285 286 b = a / 1 287 u32test(a, b, 1) 288 b = a / 2 289 u32test(a, b, 2) 290 b = a / 3 291 u32test(a, b, 3) 292 b = a / 4 293 u32test(a, b, 4) 294 b = a / 5 295 u32test(a, b, 5) 296 b = a / 6 297 u32test(a, b, 6) 298 b = a / 7 299 u32test(a, b, 7) 300 b = a / 8 301 u32test(a, b, 8) 302 b = a / 10 303 u32test(a, b, 10) 304 b = a / 16 305 u32test(a, b, 16) 306 b = a / 20 307 u32test(a, b, 20) 308 b = a / 32 309 u32test(a, b, 32) 310 b = a / 60 311 u32test(a, b, 60) 312 b = a / 64 313 u32test(a, b, 64) 314 b = a / 128 315 u32test(a, b, 128) 316 b = a / 256 317 u32test(a, b, 256) 318 b = a / 16384 319 u32test(a, b, 16384) 320 } 321 } 322 323 func i16rand() int16 { 324 for { 325 a := int16(rand.Uint32()) 326 a >>= uint(rand.Intn(16)) 327 if -a != a { 328 return a 329 } 330 } 331 return 0 // impossible 332 } 333 334 func i16test(a, b, c int16) { 335 d := a / c 336 if d != b { 337 println("i16", a, b, c, d) 338 panic("fail") 339 } 340 } 341 342 func i16run() { 343 var a, b int16 344 345 for i := 0; i < Count; i++ { 346 a = i16rand() 347 348 b = a / 1 349 i16test(a, b, 1) 350 b = a / 2 351 i16test(a, b, 2) 352 b = a / 3 353 i16test(a, b, 3) 354 b = a / 4 355 i16test(a, b, 4) 356 b = a / 5 357 i16test(a, b, 5) 358 b = a / 6 359 i16test(a, b, 6) 360 b = a / 7 361 i16test(a, b, 7) 362 b = a / 8 363 i16test(a, b, 8) 364 b = a / 10 365 i16test(a, b, 10) 366 b = a / 16 367 i16test(a, b, 16) 368 b = a / 20 369 i16test(a, b, 20) 370 b = a / 32 371 i16test(a, b, 32) 372 b = a / 60 373 i16test(a, b, 60) 374 b = a / 64 375 i16test(a, b, 64) 376 b = a / 128 377 i16test(a, b, 128) 378 b = a / 256 379 i16test(a, b, 256) 380 b = a / 16384 381 i16test(a, b, 16384) 382 383 b = a / -1 384 i16test(a, b, -1) 385 b = a / -2 386 i16test(a, b, -2) 387 b = a / -3 388 i16test(a, b, -3) 389 b = a / -4 390 i16test(a, b, -4) 391 b = a / -5 392 i16test(a, b, -5) 393 b = a / -6 394 i16test(a, b, -6) 395 b = a / -7 396 i16test(a, b, -7) 397 b = a / -8 398 i16test(a, b, -8) 399 b = a / -10 400 i16test(a, b, -10) 401 b = a / -16 402 i16test(a, b, -16) 403 b = a / -20 404 i16test(a, b, -20) 405 b = a / -32 406 i16test(a, b, -32) 407 b = a / -60 408 i16test(a, b, -60) 409 b = a / -64 410 i16test(a, b, -64) 411 b = a / -128 412 i16test(a, b, -128) 413 b = a / -256 414 i16test(a, b, -256) 415 b = a / -16384 416 i16test(a, b, -16384) 417 } 418 } 419 420 func u16rand() uint16 { 421 a := uint16(rand.Uint32()) 422 a >>= uint(rand.Intn(16)) 423 return a 424 } 425 426 func u16test(a, b, c uint16) { 427 d := a / c 428 if d != b { 429 println("u16", a, b, c, d) 430 panic("fail") 431 } 432 } 433 434 func u16run() { 435 var a, b uint16 436 437 for i := 0; i < Count; i++ { 438 a = u16rand() 439 440 b = a / 1 441 u16test(a, b, 1) 442 b = a / 2 443 u16test(a, b, 2) 444 b = a / 3 445 u16test(a, b, 3) 446 b = a / 4 447 u16test(a, b, 4) 448 b = a / 5 449 u16test(a, b, 5) 450 b = a / 6 451 u16test(a, b, 6) 452 b = a / 7 453 u16test(a, b, 7) 454 b = a / 8 455 u16test(a, b, 8) 456 b = a / 10 457 u16test(a, b, 10) 458 b = a / 16 459 u16test(a, b, 16) 460 b = a / 20 461 u16test(a, b, 20) 462 b = a / 32 463 u16test(a, b, 32) 464 b = a / 60 465 u16test(a, b, 60) 466 b = a / 64 467 u16test(a, b, 64) 468 b = a / 128 469 u16test(a, b, 128) 470 b = a / 256 471 u16test(a, b, 256) 472 b = a / 16384 473 u16test(a, b, 16384) 474 } 475 } 476 477 func i8rand() int8 { 478 for { 479 a := int8(rand.Uint32()) 480 a >>= uint(rand.Intn(8)) 481 if -a != a { 482 return a 483 } 484 } 485 return 0 // impossible 486 } 487 488 func i8test(a, b, c int8) { 489 d := a / c 490 if d != b { 491 println("i8", a, b, c, d) 492 panic("fail") 493 } 494 } 495 496 func i8run() { 497 var a, b int8 498 499 for i := 0; i < Count; i++ { 500 a = i8rand() 501 502 b = a / 1 503 i8test(a, b, 1) 504 b = a / 2 505 i8test(a, b, 2) 506 b = a / 3 507 i8test(a, b, 3) 508 b = a / 4 509 i8test(a, b, 4) 510 b = a / 5 511 i8test(a, b, 5) 512 b = a / 6 513 i8test(a, b, 6) 514 b = a / 7 515 i8test(a, b, 7) 516 b = a / 8 517 i8test(a, b, 8) 518 b = a / 10 519 i8test(a, b, 10) 520 b = a / 8 521 i8test(a, b, 8) 522 b = a / 20 523 i8test(a, b, 20) 524 b = a / 32 525 i8test(a, b, 32) 526 b = a / 60 527 i8test(a, b, 60) 528 b = a / 64 529 i8test(a, b, 64) 530 b = a / 127 531 i8test(a, b, 127) 532 533 b = a / -1 534 i8test(a, b, -1) 535 b = a / -2 536 i8test(a, b, -2) 537 b = a / -3 538 i8test(a, b, -3) 539 b = a / -4 540 i8test(a, b, -4) 541 b = a / -5 542 i8test(a, b, -5) 543 b = a / -6 544 i8test(a, b, -6) 545 b = a / -7 546 i8test(a, b, -7) 547 b = a / -8 548 i8test(a, b, -8) 549 b = a / -10 550 i8test(a, b, -10) 551 b = a / -8 552 i8test(a, b, -8) 553 b = a / -20 554 i8test(a, b, -20) 555 b = a / -32 556 i8test(a, b, -32) 557 b = a / -60 558 i8test(a, b, -60) 559 b = a / -64 560 i8test(a, b, -64) 561 b = a / -128 562 i8test(a, b, -128) 563 } 564 } 565 566 func u8rand() uint8 { 567 a := uint8(rand.Uint32()) 568 a >>= uint(rand.Intn(8)) 569 return a 570 } 571 572 func u8test(a, b, c uint8) { 573 d := a / c 574 if d != b { 575 println("u8", a, b, c, d) 576 panic("fail") 577 } 578 } 579 580 func u8run() { 581 var a, b uint8 582 583 for i := 0; i < Count; i++ { 584 a = u8rand() 585 586 b = a / 1 587 u8test(a, b, 1) 588 b = a / 2 589 u8test(a, b, 2) 590 b = a / 3 591 u8test(a, b, 3) 592 b = a / 4 593 u8test(a, b, 4) 594 b = a / 5 595 u8test(a, b, 5) 596 b = a / 6 597 u8test(a, b, 6) 598 b = a / 7 599 u8test(a, b, 7) 600 b = a / 8 601 u8test(a, b, 8) 602 b = a / 10 603 u8test(a, b, 10) 604 b = a / 8 605 u8test(a, b, 8) 606 b = a / 20 607 u8test(a, b, 20) 608 b = a / 32 609 u8test(a, b, 32) 610 b = a / 60 611 u8test(a, b, 60) 612 b = a / 64 613 u8test(a, b, 64) 614 b = a / 128 615 u8test(a, b, 128) 616 b = a / 184 617 u8test(a, b, 184) 618 } 619 } 620 621 func main() { 622 xtest() 623 i64run() 624 u64run() 625 i32run() 626 u32run() 627 i16run() 628 u16run() 629 i8run() 630 u8run() 631 } 632 633 func xtest() { 634 }