(self)
| 2599 | self.assertRaises(TypeError, msrc.cast, 'c') |
| 2600 | |
| 2601 | def test_memoryview_cast_invalid(self): |
| 2602 | # invalid format |
| 2603 | for sfmt in NON_BYTE_FORMAT: |
| 2604 | sformat = '@' + sfmt if randrange(2) else sfmt |
| 2605 | ssize = struct.calcsize(sformat) |
| 2606 | for dfmt in NON_BYTE_FORMAT: |
| 2607 | dformat = '@' + dfmt if randrange(2) else dfmt |
| 2608 | dsize = struct.calcsize(dformat) |
| 2609 | ex = ndarray(list(range(32)), shape=[32//ssize], format=sformat) |
| 2610 | msrc = memoryview(ex) |
| 2611 | self.assertRaises(TypeError, msrc.cast, dfmt, [32//dsize]) |
| 2612 | |
| 2613 | for sfmt, sitems, _ in iter_format(1): |
| 2614 | ex = ndarray(sitems, shape=[1], format=sfmt) |
| 2615 | msrc = memoryview(ex) |
| 2616 | for dfmt, _, _ in iter_format(1): |
| 2617 | if not is_memoryview_format(dfmt): |
| 2618 | self.assertRaises(ValueError, msrc.cast, dfmt, |
| 2619 | [32//dsize]) |
| 2620 | else: |
| 2621 | if not is_byte_format(sfmt) and not is_byte_format(dfmt): |
| 2622 | self.assertRaises(TypeError, msrc.cast, dfmt, |
| 2623 | [32//dsize]) |
| 2624 | |
| 2625 | # invalid shape |
| 2626 | size_h = struct.calcsize('h') |
| 2627 | size_d = struct.calcsize('d') |
| 2628 | ex = ndarray(list(range(2*2*size_d)), shape=[2,2,size_d], format='h') |
| 2629 | msrc = memoryview(ex) |
| 2630 | self.assertRaises(TypeError, msrc.cast, shape=[2,2,size_h], format='d') |
| 2631 | |
| 2632 | ex = ndarray(list(range(120)), shape=[1,2,3,4,5]) |
| 2633 | m = memoryview(ex) |
| 2634 | |
| 2635 | # incorrect number of args |
| 2636 | self.assertRaises(TypeError, m.cast) |
| 2637 | self.assertRaises(TypeError, m.cast, 1, 2, 3) |
| 2638 | |
| 2639 | # incorrect dest format type |
| 2640 | self.assertRaises(TypeError, m.cast, {}) |
| 2641 | |
| 2642 | # incorrect dest format |
| 2643 | self.assertRaises(ValueError, m.cast, "X") |
| 2644 | self.assertRaises(ValueError, m.cast, "@X") |
| 2645 | self.assertRaises(ValueError, m.cast, "@XY") |
| 2646 | |
| 2647 | # dest format not implemented |
| 2648 | self.assertRaises(ValueError, m.cast, "=B") |
| 2649 | self.assertRaises(ValueError, m.cast, "!L") |
| 2650 | self.assertRaises(ValueError, m.cast, "<P") |
| 2651 | self.assertRaises(ValueError, m.cast, ">l") |
| 2652 | self.assertRaises(ValueError, m.cast, "BI") |
| 2653 | self.assertRaises(ValueError, m.cast, "xBI") |
| 2654 | |
| 2655 | # src format not implemented |
| 2656 | ex = ndarray([(1,2), (3,4)], shape=[2], format="II") |
| 2657 | m = memoryview(ex) |
| 2658 | self.assertRaises(NotImplementedError, m.__getitem__, 0) |
nothing calls this directly
no test coverage detected