Return random structure: (memlen, itemsize, ndim, shape, strides, offset) If 'valid' is true, the returned structure is valid, otherwise invalid. If 'shape' is given, use that instead of creating a random shape.
(itemsize, valid, maxdim=5, maxshape=16, shape=())
| 506 | return False |
| 507 | |
| 508 | def rand_structure(itemsize, valid, maxdim=5, maxshape=16, shape=()): |
| 509 | """Return random structure: |
| 510 | (memlen, itemsize, ndim, shape, strides, offset) |
| 511 | If 'valid' is true, the returned structure is valid, otherwise invalid. |
| 512 | If 'shape' is given, use that instead of creating a random shape. |
| 513 | """ |
| 514 | if not shape: |
| 515 | ndim = randrange(maxdim+1) |
| 516 | if (ndim == 0): |
| 517 | if valid: |
| 518 | return itemsize, itemsize, ndim, (), (), 0 |
| 519 | else: |
| 520 | nitems = randrange(1, 16+1) |
| 521 | memlen = nitems * itemsize |
| 522 | offset = -itemsize if randrange(2) == 0 else memlen |
| 523 | return memlen, itemsize, ndim, (), (), offset |
| 524 | |
| 525 | minshape = 2 |
| 526 | n = randrange(100) |
| 527 | if n >= 95 and valid: |
| 528 | minshape = 0 |
| 529 | elif n >= 90: |
| 530 | minshape = 1 |
| 531 | shape = [0] * ndim |
| 532 | |
| 533 | for i in range(ndim): |
| 534 | shape[i] = randrange(minshape, maxshape+1) |
| 535 | else: |
| 536 | ndim = len(shape) |
| 537 | |
| 538 | maxstride = 5 |
| 539 | n = randrange(100) |
| 540 | zero_stride = True if n >= 95 and n & 1 else False |
| 541 | |
| 542 | strides = [0] * ndim |
| 543 | strides[ndim-1] = itemsize * randrange(-maxstride, maxstride+1) |
| 544 | if not zero_stride and strides[ndim-1] == 0: |
| 545 | strides[ndim-1] = itemsize |
| 546 | |
| 547 | for i in range(ndim-2, -1, -1): |
| 548 | maxstride *= shape[i+1] if shape[i+1] else 1 |
| 549 | if zero_stride: |
| 550 | strides[i] = itemsize * randrange(-maxstride, maxstride+1) |
| 551 | else: |
| 552 | strides[i] = ((1,-1)[randrange(2)] * |
| 553 | itemsize * randrange(1, maxstride+1)) |
| 554 | |
| 555 | imin = imax = 0 |
| 556 | if not 0 in shape: |
| 557 | imin = sum(strides[j]*(shape[j]-1) for j in range(ndim) |
| 558 | if strides[j] <= 0) |
| 559 | imax = sum(strides[j]*(shape[j]-1) for j in range(ndim) |
| 560 | if strides[j] > 0) |
| 561 | |
| 562 | nitems = imax - imin |
| 563 | if valid: |
| 564 | offset = -imin * itemsize |
| 565 | memlen = offset + (imax+1) * itemsize |
no outgoing calls
no test coverage detected
searching dependent graphs…