annotate test/pixelarray_test.py @ 3172:d1b367fc08bc tests-returncode

Use a return code of 1 when the tests fail.
author Thomas Kluyver <takowl@gmail.com>
date Wed, 09 Jan 2013 10:13:57 +0000
parents 7ef8c8c6259d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
1 import sys
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
2 if __name__ == '__main__':
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
3 import os
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
4 pkg_dir = os.path.split(os.path.abspath(__file__))[0]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
5 parent_dir, pkg_name = os.path.split(pkg_dir)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
6 is_pygame_pkg = (pkg_name == 'tests' and
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
7 os.path.split(parent_dir)[1] == 'pygame')
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
8 if not is_pygame_pkg:
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
9 sys.path.insert(0, parent_dir)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
10 else:
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
11 is_pygame_pkg = __name__.startswith('pygame.tests.')
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
12
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
13 if is_pygame_pkg:
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
14 from pygame.tests.test_utils import test_not_implemented, unittest, arrinter
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
15 else:
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
16 from test.test_utils import test_not_implemented, unittest, arrinter
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
17 import pygame
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
18 from pygame.compat import xrange_
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
19 try:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
20 reduce
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
21 except NameError:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
22 from functools import reduce
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
23 import operator
2247
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
24 import weakref
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
25 import gc
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
26
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
27 PY3 = sys.version_info >= (3, 0, 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
28
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
29 class TestMixin (object):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
30 def assert_surfaces_equal (self, s1, s2):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
31 # Assumes the surfaces are the same size.
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
32 w, h = s1.get_size ()
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
33 for x in range (w):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
34 for y in range (h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
35 self.assertEqual (s1.get_at ((x, y)), s2.get_at ((x, y)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
36 "size: (%i, %i), position: (%i, %i)" %
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
37 (w, h, x, y))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
38
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
39 class PixelArrayTypeTest (unittest.TestCase, TestMixin):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
40 def test_compare(self):
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
41 # __doc__ (as of 2008-06-25) for pygame.pixelarray.PixelArray.compare:
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
42
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
43 # PixelArray.compare (array, distance=0, weights=(0.299, 0.587, 0.114)): Return PixelArray
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
44 # Compares the PixelArray with another one.
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
45
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
46 w = 10
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
47 h = 20
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
48 size = w, h
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
49 sf = pygame.Surface (size, 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
50 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
51 sf2 = pygame.Surface (size, 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
52 self.assertRaises (TypeError, ar.compare, sf2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
53 ar2 = pygame.PixelArray (sf2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
54 ar3 = ar.compare (ar2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
55 self.assert_ (isinstance (ar3, pygame.PixelArray))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
56 self.assertEqual (ar3.shape, size)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
57 sf2.fill (pygame.Color ('white'))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
58 self.assert_surfaces_equal (sf2, ar3.surface)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
59 del ar3
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
60 r = pygame.Rect (2, 5, 6, 13)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
61 sf.fill (pygame.Color ('blue'), r)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
62 sf2.fill (pygame.Color ('red'))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
63 sf2.fill (pygame.Color ('blue'), r)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
64 ar3 = ar.compare (ar2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
65 sf.fill (pygame.Color ('white'), r)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
66 self.assert_surfaces_equal (sf, ar3.surface)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
67
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
68 # FINISH ME!
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
69 # Test other bit depths, slices, and distance != 0.
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
70
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
71 def test_pixel_array (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
72 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
73 sf = pygame.Surface ((10, 20), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
74 sf.fill ((0, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
75 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
76
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
77 self.assertEqual (ar._pixels_address, sf._pixels_address)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
78
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
79 if sf.mustlock ():
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
80 self.assertTrue (sf.get_locked ())
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
81
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
82 self.assertEqual (len (ar), 10)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
83
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
84 del ar
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
85 if sf.mustlock ():
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
86 self.assertFalse (sf.get_locked ())
2247
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
87
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
88 def test_as_class (self):
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
89 # Check general new-style class freatures.
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
90 sf = pygame.Surface ((2, 3), 0, 32)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
91 ar = pygame.PixelArray (sf)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
92 self.assertRaises (AttributeError, getattr, ar, 'nonnative')
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
93 ar.nonnative = 'value'
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
94 self.assertEqual (ar.nonnative, 'value')
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
95 self.assertTrue ('nonnative' in ar.__dict__)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
96 r = weakref.ref (ar)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
97 self.assertTrue (r() is ar)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
98 del ar
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
99 gc.collect ()
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
100 self.assertTrue (r() is None)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
101
2247
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
102 class C (pygame.PixelArray):
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
103 def __str__ (self):
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
104 return "string (%i, %i)" % self.shape
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
105
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
106 ar = C (sf)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
107 self.assertEqual (str (ar), "string (2, 3)")
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
108 r = weakref.ref (ar)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
109 self.assertTrue (r() is ar)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
110 del ar
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
111 gc.collect ()
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
112 self.assertTrue (r() is None)
7ef8c8c6259d add PixelArray unit test for __dict__, weakref, and subclassing
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
113
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
114 # Sequence interfaces
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
115 def test_get_column (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
116 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
117 sf = pygame.Surface ((6, 8), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
118 sf.fill ((0, 0, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
119 val = sf.map_rgb ((0, 0, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
120 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
121
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
122 ar2 = ar.__getitem__ (1)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
123 self.assertEqual (len(ar2), 8)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
124 self.assertEqual (ar2.__getitem__ (0), val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
125 self.assertEqual (ar2.__getitem__ (1), val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
126 self.assertEqual (ar2.__getitem__ (2), val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
127
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
128 ar2 = ar.__getitem__ (-1)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
129 self.assertEqual (len(ar2), 8)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
130 self.assertEqual (ar2.__getitem__ (0), val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
131 self.assertEqual (ar2.__getitem__ (1), val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
132 self.assertEqual (ar2.__getitem__ (2), val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
133
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
134 def test_get_pixel (self):
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
135 w = 10
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
136 h = 20
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
137 size = w, h
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
138 bg_color = (0, 0, 255)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
139 fg_color_y = (0, 0, 128)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
140 fg_color_x = (0, 0, 11)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
141 for bpp in (8, 16, 24, 32):
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
142 sf = pygame.Surface (size, 0, bpp)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
143 mapped_bg_color = sf.map_rgb (bg_color)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
144 mapped_fg_color_y = sf.map_rgb (fg_color_y)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
145 mapped_fg_color_x = sf.map_rgb (fg_color_x)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
146 self.assertNotEqual (mapped_fg_color_y, mapped_bg_color,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
147 "Unusable test colors for bpp %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
148 self.assertNotEqual (mapped_fg_color_x, mapped_bg_color,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
149 "Unusable test colors for bpp %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
150 self.assertNotEqual (mapped_fg_color_y, mapped_fg_color_x,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
151 "Unusable test colors for bpp %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
152 sf.fill (bg_color)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
153
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
154 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
155
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
156 ar_y = ar.__getitem__ (1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
157 for y in xrange_ (h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
158 ar2 = ar_y.__getitem__ (y)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
159 self.assertEqual (ar2, mapped_bg_color,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
160 "ar[1][%i] == %i, mapped_bg_color == %i" %
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
161 (y, ar2, mapped_bg_color))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
162
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
163 sf.set_at ((1, y), fg_color_y)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
164 ar2 = ar_y.__getitem__ (y)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
165 self.assertEqual (ar2, mapped_fg_color_y,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
166 "ar[1][%i] == %i, mapped_fg_color_y == %i" %
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
167 (y, ar2, mapped_fg_color_y))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
168
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
169 sf.set_at ((1, 1), bg_color)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
170 for x in xrange_ (w):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
171 ar2 = ar.__getitem__ (x).__getitem__ (1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
172 self.assertEqual (ar2, mapped_bg_color,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
173 "ar[%i][1] = %i, mapped_bg_color = %i" %
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
174 (x, ar2, mapped_bg_color))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
175 sf.set_at ((x, 1), fg_color_x)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
176 ar2 = ar.__getitem__ (x).__getitem__ (1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
177 self.assertEqual (ar2, mapped_fg_color_x,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
178 "ar[%i][1] = %i, mapped_fg_color_x = %i" %
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
179 (x, ar2, mapped_fg_color_x))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
180
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
181 ar2 = ar.__getitem__ (0).__getitem__ (0)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
182 self.assertEqual (ar2, mapped_bg_color, "bpp = %i" % (bpp,))
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
183
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
184 ar2 = ar.__getitem__ (1).__getitem__ (0)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
185 self.assertEqual (ar2, mapped_fg_color_y, "bpp = %i" % (bpp,))
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
186
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
187 ar2 = ar.__getitem__ (-4).__getitem__ (1)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
188 self.assertEqual (ar2, mapped_fg_color_x, "bpp = %i" % (bpp,))
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
189
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
190 ar2 = ar.__getitem__ (-4).__getitem__ (5)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
191 self.assertEqual (ar2, mapped_bg_color, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
192
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
193 ar2 = ar.__getitem__ (-4).__getitem__ (0)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
194 self.assertEqual (ar2, mapped_bg_color, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
195
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
196 ar2 = ar.__getitem__ (-w + 1).__getitem__ (0)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
197 self.assertEqual (ar2, mapped_fg_color_y, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
198
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
199 ar2 = ar.__getitem__ (-w).__getitem__ (0)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
200 self.assertEqual (ar2, mapped_bg_color, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
201
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
202 ar2 = ar.__getitem__ (5).__getitem__ (-4)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
203 self.assertEqual (ar2, mapped_bg_color, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
204
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
205 ar2 = ar.__getitem__ (5).__getitem__ (-h + 1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
206 self.assertEqual (ar2, mapped_fg_color_x, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
207
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
208 ar2 = ar.__getitem__ (5).__getitem__ (-h)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
209 self.assertEqual (ar2, mapped_bg_color, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
210
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
211 ar2 = ar.__getitem__ (0).__getitem__ (-h + 1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
212 self.assertEqual (ar2, mapped_fg_color_x, "bpp = %i" % (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
213
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
214 ar2 = ar.__getitem__ (0).__getitem__ (-h)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
215 self.assertEqual (ar2, mapped_bg_color, "bpp = %i" % (bpp,))
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
216
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
217 def test_set_pixel (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
218 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
219 sf = pygame.Surface ((10, 20), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
220 sf.fill ((0, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
221 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
222
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
223 ar.__getitem__ (0).__setitem__ (0, (0, 255, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
224 self.assertEqual (ar[0][0], sf.map_rgb ((0, 255, 0)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
225
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
226 ar.__getitem__ (1).__setitem__ (1, (128, 128, 128))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
227 self.assertEqual (ar[1][1], sf.map_rgb ((128, 128, 128)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
228
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
229 ar.__getitem__(-1).__setitem__ (-1, (128, 128, 128))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
230 self.assertEqual (ar[9][19], sf.map_rgb ((128, 128, 128)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
231
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
232 ar.__getitem__ (-2).__setitem__ (-2, (128, 128, 128))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
233 self.assertEqual (ar[8][-2], sf.map_rgb ((128, 128, 128)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
234
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
235 def test_set_column (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
236 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
237 sf = pygame.Surface ((6, 8), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
238 sf.fill ((0, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
239 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
240
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
241 sf2 = pygame.Surface ((6, 8), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
242 sf2.fill ((0, 255, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
243 ar2 = pygame.PixelArray (sf2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
244
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
245 # Test single value assignment
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
246 ar.__setitem__ (2, (128, 128, 128))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
247 self.assertEqual (ar[2][0], sf.map_rgb ((128, 128, 128)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
248 self.assertEqual (ar[2][1], sf.map_rgb ((128, 128, 128)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
249
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
250 ar.__setitem__ (-1, (0, 255, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
251 self.assertEqual (ar[5][0], sf.map_rgb ((0, 255, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
252 self.assertEqual (ar[-1][1], sf.map_rgb ((0, 255, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
253
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
254 ar.__setitem__ (-2, (255, 255, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
255 self.assertEqual (ar[4][0], sf.map_rgb ((255, 255, 0)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
256 self.assertEqual (ar[-2][1], sf.map_rgb ((255, 255, 0)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
257
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
258 # Test list assignment.
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
259 ar.__setitem__ (0, [(255, 255, 255)] * 8)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
260 self.assertEqual (ar[0][0], sf.map_rgb ((255, 255, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
261 self.assertEqual (ar[0][1], sf.map_rgb ((255, 255, 255)))
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
262
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
263 # Test tuple assignment.
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
264 # Changed in Pygame 1.9.2 - Raises an exception.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
265 self.assertRaises (ValueError, ar.__setitem__, 1,
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
266 ((204, 0, 204), (17, 17, 17), (204, 0, 204),
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
267 (17, 17, 17), (204, 0, 204), (17, 17, 17),
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
268 (204, 0, 204), (17, 17, 17)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
269
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
270 # Test pixel array assignment.
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
271 ar.__setitem__ (1, ar2.__getitem__ (3))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
272 self.assertEqual (ar[1][0], sf.map_rgb ((0, 255, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
273 self.assertEqual (ar[1][1], sf.map_rgb ((0, 255, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
274
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
275 def test_get_slice (self):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
276 for bpp in (8, 16, 24, 32):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
277 sf = pygame.Surface ((10, 20), 0, bpp)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
278 sf.fill ((0, 0, 0))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
279 ar = pygame.PixelArray (sf)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
280
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
281 self.assertEqual (len (ar[0:2]), 2)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
282 self.assertEqual (len (ar[3:7][3]), 20)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
283
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
284 self.assertEqual (ar[0:0], None)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
285 self.assertEqual (ar[5:5], None)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
286 self.assertEqual (ar[9:9], None)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
287
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
288 # Has to resolve to ar[7:8]
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
289 self.assertEqual (len (ar[-3:-2]), 1) # 2D
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
290 self.assertEqual (len (ar[-3:-2][0]), 20) # 1D
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
291
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
292 # Try assignments.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
293
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
294 # 2D assignment.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
295 ar[2:5] = (255, 255, 255)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
296
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
297 # 1D assignment
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
298 ar[3][3:7] = (10, 10, 10)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
299 self.assertEqual (ar[3][5], sf.map_rgb ((10, 10, 10)))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
300 self.assertEqual (ar[3][6], sf.map_rgb ((10, 10, 10)))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
301
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
302 def test_contains (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
303 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
304 sf = pygame.Surface ((10, 20), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
305 sf.fill ((0, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
306 sf.set_at ((8, 8), (255, 255, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
307
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
308 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
309 self.assertTrue ((0, 0, 0) in ar)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
310 self.assertTrue ((255, 255, 255) in ar)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
311 self.assertFalse ((255, 255, 0) in ar)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
312 self.assertFalse (0x0000ff in ar)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
313
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
314 # Test sliced array
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
315 self.assertTrue ((0, 0, 0) in ar[8])
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
316 self.assertTrue ((255, 255, 255) in ar[8])
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
317 self.assertFalse ((255, 255, 0) in ar[8])
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
318 self.assertFalse (0x0000ff in ar[8])
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
319
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
320 def test_get_surface (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
321 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
322 sf = pygame.Surface ((10, 20), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
323 sf.fill ((0, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
324 ar = pygame.PixelArray (sf)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
325 self.assert_ (ar.surface is sf)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
326
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
327 def test_set_slice (self):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
328 for bpp in (8, 16, 24, 32):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
329 sf = pygame.Surface ((6, 8), 0, bpp)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
330 sf.fill ((0, 0, 0))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
331 ar = pygame.PixelArray (sf)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
332
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
333 # Test single value assignment
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
334 val = sf.map_rgb ((128, 128, 128))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
335 ar[0:2] = val
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
336 self.assertEqual (ar[0][0], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
337 self.assertEqual (ar[0][1], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
338 self.assertEqual (ar[1][0], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
339 self.assertEqual (ar[1][1], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
340
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
341 val = sf.map_rgb ((0, 255, 255))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
342 ar[-3:-1] = val
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
343 self.assertEqual (ar[3][0], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
344 self.assertEqual (ar[-2][1], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
345
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
346 val = sf.map_rgb ((255, 255, 255))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
347 ar[-3:] = (255, 255, 255)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
348 self.assertEqual (ar[4][0], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
349 self.assertEqual (ar[-1][1], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
350
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
351 # Test array size mismatch.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
352 # Changed in ver. 1.9.2
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
353 # (was "Test list assignment, this is a vertical assignment.")
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
354 val = sf.map_rgb ((0, 255, 0))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
355 self.assertRaises (ValueError, ar.__setitem__, slice (2, 4),
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
356 [val] * 8)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
357
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
358 # And the horizontal assignment.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
359 val = sf.map_rgb ((255, 0, 0))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
360 val2 = sf.map_rgb ((128, 0, 255))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
361 ar[0:2] = [val, val2]
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
362 self.assertEqual (ar[0][0], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
363 self.assertEqual (ar[1][0], val2)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
364 self.assertEqual (ar[0][1], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
365 self.assertEqual (ar[1][1], val2)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
366 self.assertEqual (ar[0][4], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
367 self.assertEqual (ar[1][4], val2)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
368 self.assertEqual (ar[0][5], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
369 self.assertEqual (ar[1][5], val2)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
370
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
371 # Test pixelarray assignment.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
372 ar[:] = (0, 0, 0)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
373 sf2 = pygame.Surface ((6, 8), 0, bpp)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
374 sf2.fill ((255, 0, 255))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
375
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
376 val = sf.map_rgb ((255, 0, 255))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
377 ar2 = pygame.PixelArray (sf2)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
378
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
379 ar[:] = ar2[:]
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
380 self.assertEqual (ar[0][0], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
381 self.assertEqual (ar[5][7], val)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
382
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
383 def test_subscript (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
384 # By default we do not need to work with any special __***__
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
385 # methods as map subscripts are the first looked up by the
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
386 # object system.
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
387 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
388 sf = pygame.Surface ((6, 8), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
389 sf.set_at ((1, 3), (0, 255, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
390 sf.set_at ((0, 0), (0, 255, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
391 sf.set_at ((4, 4), (0, 255, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
392 val = sf.map_rgb ((0, 255, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
393
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
394 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
395
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
396 # Test single value requests.
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
397 self.assertEqual (ar[1,3], val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
398 self.assertEqual (ar[0,0], val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
399 self.assertEqual (ar[4,4], val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
400 self.assertEqual (ar[1][3], val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
401 self.assertEqual (ar[0][0], val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
402 self.assertEqual (ar[4][4], val)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
403
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
404 # Test ellipse working.
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
405 self.assertEqual (len (ar[...,...]), 6)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
406 self.assertEqual (len (ar[1,...]), 8)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
407 self.assertEqual (len (ar[...,3]), 6)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
408
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
409 # Test simple slicing
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
410 self.assertEqual (len (ar[:,:]), 6)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
411 self.assertEqual (len (ar[:,]), 6)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
412 self.assertEqual (len (ar[1,:]), 8)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
413 self.assertEqual (len (ar[:,2]), 6)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
414 # Empty slices
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
415 self.assertEqual (ar[4:4,], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
416 self.assertEqual (ar[4:4,...], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
417 self.assertEqual (ar[4:4,2:2], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
418 self.assertEqual (ar[4:4,1:4], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
419 self.assertEqual (ar[4:4:2,], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
420 self.assertEqual (ar[4:4:-2,], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
421 self.assertEqual (ar[4:4:1,...], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
422 self.assertEqual (ar[4:4:-1,...], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
423 self.assertEqual (ar[4:4:1,2:2], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
424 self.assertEqual (ar[4:4:-1,1:4], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
425 self.assertEqual (ar[...,4:4], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
426 self.assertEqual (ar[1:4,4:4], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
427 self.assertEqual (ar[...,4:4:1], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
428 self.assertEqual (ar[...,4:4:-1], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
429 self.assertEqual (ar[2:2,4:4:1], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
430 self.assertEqual (ar[1:4,4:4:-1], None)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
431
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
432 # Test advanced slicing
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
433 ar[0] = 0
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
434 ar[1] = 1
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
435 ar[2] = 2
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
436 ar[3] = 3
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
437 ar[4] = 4
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
438 ar[5] = 5
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
439
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
440 # We should receive something like [0,2,4]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
441 self.assertEqual (ar[::2,1][0], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
442 self.assertEqual (ar[::2,1][1], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
443 self.assertEqual (ar[::2,1][2], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
444 # We should receive something like [2,2,2]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
445 self.assertEqual (ar[2,::2][0], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
446 self.assertEqual (ar[2,::2][1], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
447 self.assertEqual (ar[2,::2][2], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
448
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
449 # Should create a 3x3 array of [0,2,4]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
450 ar2 = ar[::2,::2]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
451 self.assertEqual (len (ar2), 3)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
452 self.assertEqual (ar2[0][0], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
453 self.assertEqual (ar2[0][1], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
454 self.assertEqual (ar2[0][2], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
455 self.assertEqual (ar2[2][0], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
456 self.assertEqual (ar2[2][1], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
457 self.assertEqual (ar2[2][2], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
458 self.assertEqual (ar2[1][0], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
459 self.assertEqual (ar2[2][0], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
460 self.assertEqual (ar2[1][1], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
461
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
462 # Should create a reversed 3x8 array over X of [1,2,3] -> [3,2,1]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
463 ar2 = ar[3:0:-1]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
464 self.assertEqual (len (ar2), 3)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
465 self.assertEqual (ar2[0][0], 3)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
466 self.assertEqual (ar2[0][1], 3)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
467 self.assertEqual (ar2[0][2], 3)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
468 self.assertEqual (ar2[0][7], 3)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
469 self.assertEqual (ar2[2][0], 1)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
470 self.assertEqual (ar2[2][1], 1)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
471 self.assertEqual (ar2[2][2], 1)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
472 self.assertEqual (ar2[2][7], 1)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
473 self.assertEqual (ar2[1][0], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
474 self.assertEqual (ar2[1][1], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
475 # Should completely reverse the array over X -> [5,4,3,2,1,0]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
476 ar2 = ar[::-1]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
477 self.assertEqual (len (ar2), 6)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
478 self.assertEqual (ar2[0][0], 5)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
479 self.assertEqual (ar2[0][1], 5)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
480 self.assertEqual (ar2[0][3], 5)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
481 self.assertEqual (ar2[0][-1], 5)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
482 self.assertEqual (ar2[1][0], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
483 self.assertEqual (ar2[1][1], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
484 self.assertEqual (ar2[1][3], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
485 self.assertEqual (ar2[1][-1], 4)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
486 self.assertEqual (ar2[-1][-1], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
487 self.assertEqual (ar2[-2][-2], 1)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
488 self.assertEqual (ar2[-3][-1], 2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
489
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
490 # Test advanced slicing
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
491 ar[:] = 0
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
492 ar2 = ar[:,1]
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
493 ar2[:] = [99] * len(ar2)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
494 self.assertEqual (ar2[0], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
495 self.assertEqual (ar2[-1], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
496 self.assertEqual (ar2[-2], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
497 self.assertEqual (ar2[2], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
498 self.assertEqual (ar[0,1], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
499 self.assertEqual (ar[1,1], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
500 self.assertEqual (ar[2,1], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
501 self.assertEqual (ar[-1,1], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
502 self.assertEqual (ar[-2,1], 99)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
503
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
504 # Cases where a 2d array should have a dimension of length 1.
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
505 ar2 = ar[1:2,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
506 self.assertEqual (ar2.shape, (1, ar.shape[1]))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
507 ar2 = ar[:,1:2]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
508 self.assertEqual (ar2.shape, (ar.shape[0], 1))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
509 sf2 = pygame.Surface ((1, 5), 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
510 ar2 = pygame.PixelArray (sf2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
511 self.assertEqual (ar2.shape, sf2.get_size ())
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
512 sf2 = pygame.Surface ((7, 1), 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
513 ar2 = pygame.PixelArray (sf2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
514 self.assertEqual (ar2.shape, sf2.get_size ())
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
515
2219
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
516 # Array has a single ellipsis subscript: the identity operator
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
517 ar2 = ar[...]
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
518 self.assert_ (ar2 is ar)
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
519
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
520 def test_ass_subscript (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
521 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
522 sf = pygame.Surface ((6, 8), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
523 sf.fill ((255, 255, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
524 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
525
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
526 # Test ellipse working
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
527 ar[...,...] = (0, 0, 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
528 self.assertEqual (ar[0,0], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
529 self.assertEqual (ar[1,0], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
530 self.assertEqual (ar[-1,-1], 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
531 ar[...,] = (0, 0, 255)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
532 self.assertEqual (ar[0,0], sf.map_rgb ((0, 0, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
533 self.assertEqual (ar[1,0], sf.map_rgb ((0, 0, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
534 self.assertEqual (ar[-1,-1], sf.map_rgb ((0, 0, 255)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
535 ar[:,...] = (255, 0, 0)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
536 self.assertEqual (ar[0,0], sf.map_rgb ((255, 0, 0)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
537 self.assertEqual (ar[1,0], sf.map_rgb ((255, 0, 0)))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
538 self.assertEqual (ar[-1,-1], sf.map_rgb ((255, 0, 0)))
2219
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
539 ar[...] = (0, 255, 0)
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
540 self.assertEqual (ar[0,0], sf.map_rgb ((0, 255, 0)))
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
541 self.assertEqual (ar[1,0], sf.map_rgb ((0, 255, 0)))
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
542 self.assertEqual (ar[-1,-1], sf.map_rgb ((0, 255, 0)))
12d56393ec02 Allow [...] as a valid PixelArray subscript, consistent with NumPy
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
543
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
544 def test_pixels_field(self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
545 for bpp in [1, 2, 3, 4]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
546 sf = pygame.Surface ((11, 7), 0, bpp * 8)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
547 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
548 ar2 = ar[1:,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
549 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
550 ar.itemsize)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
551 ar2 = ar[:,1:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
552 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
553 ar.strides[1])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
554 ar2 = ar[::-1,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
555 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
556 (ar.shape[0] - 1) * ar.itemsize)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
557 ar2 = ar[::-2,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
558 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
559 (ar.shape[0] - 1) * ar.itemsize)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
560 ar2 = ar[:,::-1]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
561 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
562 (ar.shape[1] - 1) * ar.strides[1])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
563 ar3 = ar2[::-1,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
564 self.assertEqual (ar3._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
565 (ar.shape[0] - 1) * ar.strides[0] +
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
566 (ar.shape[1] - 1) * ar.strides[1])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
567 ar2 = ar[:,::-2]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
568 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
569 (ar.shape[1] - 1) * ar.strides[1])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
570 ar2 = ar[2::,3::]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
571 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
572 ar.strides[0] * 2 + ar.strides[1] * 3)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
573 ar2 = ar[2::2,3::4]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
574 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
575 ar.strides[0] * 2 + ar.strides[1] * 3)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
576 ar2 = ar[9:2:-1,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
577 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
578 ar.strides[0] * 9)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
579 ar2 = ar[:,5:2:-1]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
580 self.assertEqual (ar2._pixels_address - ar._pixels_address,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
581 ar.strides[1] * 5)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
582 ##? ar2 = ar[:,9:2:-1]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
583
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
584 def test_make_surface (self):
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
585 bg_color = pygame.Color (255, 255, 255)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
586 fg_color = pygame.Color (128, 100, 0)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
587 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
588 sf = pygame.Surface ((10, 20), 0, bpp)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
589 bg_color_adj = sf.unmap_rgb (sf.map_rgb (bg_color))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
590 fg_color_adj = sf.unmap_rgb (sf.map_rgb (fg_color))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
591 sf.fill (bg_color_adj)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
592 sf.fill (fg_color_adj, (2, 5, 4, 11))
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
593 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
594 newsf = ar[::2,::2].make_surface ()
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
595 rect = newsf.get_rect ()
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
596 self.assertEqual (rect.width, 5)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
597 self.assertEqual (rect.height, 10)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
598 for p in [(0, 2), (0, 3), (1, 2),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
599 (2, 2), (3, 2), (3, 3),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
600 (0, 7), (0, 8), (1, 8),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
601 (2, 8), (3, 8), (3, 7)]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
602 self.assertEqual (newsf.get_at (p), bg_color_adj)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
603 for p in [(1, 3), (2, 3), (1, 5), (2, 5), (1, 7), (2, 7)]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
604 self.assertEqual (newsf.get_at (p), fg_color_adj)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
605
2201
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
606 # Bug when array width is not a multiple of the slice step.
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
607 w = 17
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
608 lst = list(range(w))
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
609 w_slice = len(lst[::2])
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
610 h = 3
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
611 sf = pygame.Surface ((w, h), 0, 32)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
612 ar = pygame.PixelArray (sf)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
613 ar2 = ar[::2,:]
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
614 sf2 = ar2.make_surface ()
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
615 w2, h2 = sf2.get_size ()
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
616 self.assertEqual (w2, w_slice)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
617 self.assertEqual (h2, h)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
618
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
619 # Bug when array height is not a multiple of the slice step.
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
620 # This can hang the Python interpreter.
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
621 h = 17
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
622 lst = list(range(h))
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
623 h_slice = len(lst[::2])
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
624 w = 3
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
625 sf = pygame.Surface ((w, h), 0, 32)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
626 ar = pygame.PixelArray (sf)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
627 ar2 = ar[:,::2]
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
628 sf2 = ar2.make_surface () # Hangs here.
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
629 w2, h2 = sf2.get_size ()
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
630 self.assertEqual (w2, w)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
631 self.assertEqual (h2, h_slice)
835dc88c5c5e add unit test which exposes a PixelArray bug
Lenard Lindstrom <len-l@telus.net>
parents: 2078
diff changeset
632
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
633 def test_iter (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
634 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
635 sf = pygame.Surface ((5, 10), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
636 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
637 iterations = 0
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
638 for col in ar:
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
639 self.assertEqual (len (col), 10)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
640 iterations += 1
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
641 self.assertEqual (iterations, 5)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
642
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
643 def test_replace (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
644 #print "replace start"
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
645 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
646 sf = pygame.Surface ((10, 10), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
647 sf.fill ((255, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
648 rval = sf.map_rgb ((0, 0, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
649 oval = sf.map_rgb ((255, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
650 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
651 ar[::2].replace ((255, 0, 0), (0, 0, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
652 self.assertEqual (ar[0][0], rval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
653 self.assertEqual (ar[1][0], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
654 self.assertEqual (ar[2][3], rval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
655 self.assertEqual (ar[3][6], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
656 self.assertEqual (ar[8][9], rval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
657 self.assertEqual (ar[9][9], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
658
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
659 ar[::2].replace ((0, 0, 255), (255, 0, 0), weights=(10, 20, 50))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
660 self.assertEqual (ar[0][0], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
661 self.assertEqual (ar[2][3], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
662 self.assertEqual (ar[3][6], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
663 self.assertEqual (ar[8][9], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
664 self.assertEqual (ar[9][9], oval)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
665 #print "replace end"
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
666
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
667 def test_extract (self):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
668 #print "extract start"
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
669 for bpp in (8, 16, 24, 32):
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
670 sf = pygame.Surface ((10, 10), 0, bpp)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
671 sf.fill ((0, 0, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
672 sf.fill ((255, 0, 0), (2, 2, 6, 6))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
673
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
674 white = sf.map_rgb ((255, 255, 255))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
675 black = sf.map_rgb ((0, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
676
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
677 ar = pygame.PixelArray (sf)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
678 newar = ar.extract ((255, 0, 0))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
679
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
680 self.assertEqual (newar[0][0], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
681 self.assertEqual (newar[1][0], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
682 self.assertEqual (newar[2][3], white)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
683 self.assertEqual (newar[3][6], white)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
684 self.assertEqual (newar[8][9], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
685 self.assertEqual (newar[9][9], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
686
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
687 newar = ar.extract ((255, 0, 0), weights=(10, 0.1, 50))
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
688 self.assertEqual (newar[0][0], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
689 self.assertEqual (newar[1][0], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
690 self.assertEqual (newar[2][3], white)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
691 self.assertEqual (newar[3][6], white)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
692 self.assertEqual (newar[8][9], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
693 self.assertEqual (newar[9][9], black)
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
694 #print "extract end"
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
695
2204
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
696 def test_2dslice_assignment (self):
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
697 w = 2 * 5 * 8
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
698 h = 3 * 5 * 9
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
699 sf = pygame.Surface ((w, h), 0, 32)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
700 ar = pygame.PixelArray (sf)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
701 size = (w, h)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
702 strides = (1, w)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
703 offset = 0
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
704 self._test_assignment (sf, ar, size, strides, offset)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
705 xslice = slice (None, None, 2)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
706 yslice = slice (None, None, 3)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
707 ar, size, strides, offset = self._array_slice (
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
708 ar, size, (xslice, yslice), strides, offset)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
709 self._test_assignment (sf, ar, size, strides, offset)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
710 xslice = slice (5, None, 5)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
711 yslice = slice (5, None, 5)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
712 ar, size, strides, offset = self._array_slice (
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
713 ar, size, (xslice, yslice), strides, offset)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
714 self._test_assignment (sf, ar, size, strides, offset)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
715
2204
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
716 def _test_assignment (self, sf, ar, ar_size, ar_strides, ar_offset):
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
717 self.assertEqual (ar.shape, ar_size)
2204
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
718 ar_w, ar_h = ar_size
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
719 ar_xstride, ar_ystride = ar_strides
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
720 sf_w, sf_h = sf.get_size ()
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
721 black = pygame.Color ('black')
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
722 color = pygame.Color (0, 0, 12)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
723 pxcolor = sf.map_rgb (color)
2204
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
724 sf.fill (black)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
725 for ar_x, ar_y in [(0, 0),
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
726 (0, ar_h - 4),
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
727 (ar_w - 3, 0),
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
728 (0, ar_h - 1),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
729 (ar_w - 1, 0),
2204
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
730 (ar_w - 1, ar_h - 1)]:
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
731 sf_offset = ar_offset + ar_x * ar_xstride + ar_y * ar_ystride
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
732 sf_y = sf_offset // sf_w
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
733 sf_x = sf_offset - sf_y * sf_w
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
734 sf_posn = (sf_x, sf_y)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
735 sf_pix = sf.get_at (sf_posn)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
736 self.assertEqual (sf_pix, black,
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
737 "at pixarr posn (%i, %i) (surf posn (%i, %i)): "
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
738 "%s != %s" %
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
739 (ar_x, ar_y, sf_x, sf_y, sf_pix, black))
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
740 ar[ar_x, ar_y] = pxcolor
2204
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
741 sf_pix = sf.get_at (sf_posn)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
742 self.assertEqual (sf_pix, color,
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
743 "at pixarr posn (%i, %i) (surf posn (%i, %i)): "
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
744 "%s != %s" %
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
745 (ar_x, ar_y, sf_x, sf_y, sf_pix, color))
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
746
2204
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
747 def _array_slice (self, ar, size, slices, strides, offset):
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
748 ar = ar[slices]
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
749 xslice, yslice = slices
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
750 w, h = size
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
751 xstart, xstop, xstep = xslice.indices(w)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
752 ystart, ystop, ystep = yslice.indices(h)
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
753 w = (xstop - xstart + xstep - 1) // xstep
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
754 h = (ystop - ystart + ystep - 1) // ystep
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
755 xstride, ystride = strides
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
756 offset += xstart * xstride + ystart * ystride
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
757 xstride *= xstep
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
758 ystride *= ystep
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
759 return ar, (w, h), (xstride, ystride), offset
2f017976496c add pixelarray unit test that exposes slicing bug concerning slices of slices
Lenard Lindstrom <len-l@telus.net>
parents: 2201
diff changeset
760
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
761 def test_array_properties(self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
762 # itemsize, ndim, shape, and strides.
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
763 for bpp in [1, 2, 3, 4]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
764 sf = pygame.Surface ((2, 2), 0, bpp * 8)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
765 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
766 self.assertEqual (ar.itemsize, bpp)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
767
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
768 for shape in [(4, 16), (5, 13)]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
769 w, h = shape
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
770 sf = pygame.Surface (shape, 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
771 bpp = sf.get_bytesize ()
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
772 pitch = sf.get_pitch ()
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
773 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
774 self.assertEqual (ar.ndim, 2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
775 self.assertEqual (ar.shape, shape)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
776 self.assertEqual (ar.strides, (bpp, pitch))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
777 ar2 = ar[::2,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
778 w2 = len(([0] * w)[::2])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
779 self.assertEqual (ar2.ndim, 2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
780 self.assertEqual (ar2.shape, (w2, h))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
781 self.assertEqual (ar2.strides, (2 * bpp, pitch))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
782 ar2 = ar[:,::2]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
783 h2 = len(([0] * h)[::2])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
784 self.assertEqual (ar2.ndim, 2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
785 self.assertEqual (ar2.shape, (w, h2))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
786 self.assertEqual (ar2.strides, (bpp, 2 * pitch))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
787 ar2 = ar[1]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
788 self.assertEqual (ar2.ndim, 1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
789 self.assertEqual (ar2.shape, (h,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
790 self.assertEqual (ar2.strides, (pitch,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
791 ar2 = ar[:,1]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
792 self.assertEqual (ar2.ndim, 1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
793 self.assertEqual (ar2.shape, (w,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
794 self.assertEqual (ar2.strides, (bpp,))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
795
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
796 def test_self_assign(self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
797 # This differs from NumPy arrays.
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
798 w = 10
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
799 max_x = w - 1
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
800 h = 20
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
801 max_y = h - 1
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
802 for bpp in [1, 2, 3, 4]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
803 sf = pygame.Surface ((w, h), 0, bpp * 8)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
804 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
805 for i in range (w * h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
806 ar[i % w, i // w] = i
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
807 ar[:,:] = ar[::-1,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
808 for i in range (w * h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
809 self.assertEqual (ar[max_x - i % w, i // w], i)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
810 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
811 for i in range (w * h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
812 ar[i % w, i // w] = i
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
813 ar[:,:] = ar[:,::-1]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
814 for i in range (w * h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
815 self.assertEqual (ar[i % w, max_y - i // w ], i)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
816 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
817 for i in range(w * h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
818 ar[i % w, i // w] = i
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
819 ar[:,:] = ar[::-1,::-1]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
820 for i in range (w * h):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
821 self.assertEqual (ar[max_x - i % w, max_y - i // w], i)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
822
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
823 def test_color_value (self):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
824 # Confirm that a PixelArray slice assignment distinguishes between
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
825 # pygame.Color and tuple objects as single (r, g, b[, a]) colors
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
826 # and other sequences as sequences of colors to be treated as
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
827 # slices.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
828 sf = pygame.Surface ((5, 5), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
829 ar = pygame.PixelArray (sf)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
830 index = slice(None, None, 1)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
831 ar.__setitem__ (index, (1, 2, 3))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
832 self.assertEqual (ar[0, 0], sf.map_rgb ((1, 2, 3)))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
833 ar.__setitem__ (index, pygame.Color (10, 11, 12))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
834 self.assertEqual (ar[0, 0], sf.map_rgb ((10, 11, 12)))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
835 self.assertRaises (ValueError, ar.__setitem__, index, (1, 2, 3, 4, 5))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
836 self.assertRaises (ValueError, ar.__setitem__, (index, index),
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
837 (1, 2, 3, 4, 5))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
838 self.assertRaises (ValueError, ar.__setitem__, index, [1, 2, 3])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
839 self.assertRaises (ValueError, ar.__setitem__, (index, index),
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
840 [1, 2, 3])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
841 sf = pygame.Surface ((3, 3), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
842 ar = pygame.PixelArray (sf)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
843 ar[:] = (20, 30, 40)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
844 self.assertEqual (ar[0, 0], sf.map_rgb ((20, 30, 40)))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
845 ar[:] = [20, 30, 40]
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
846 self.assertEqual (ar[0, 0], 20)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
847 self.assertEqual (ar[1, 0], 30)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
848 self.assertEqual (ar[2, 0], 40)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
849
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
850 def test_transpose (self):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
851 # PixelArray.transpose(): swap axis on a 2D array, add a length
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
852 # 1 x axis to a 1D array.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
853 sf = pygame.Surface ((3, 7), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
854 ar = pygame.PixelArray (sf)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
855 w, h = ar.shape
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
856 dx, dy = ar.strides
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
857 for i in range (w * h):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
858 x = i % w
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
859 y = i // w
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
860 ar[x, y] = i
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
861 ar_t = ar.transpose()
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
862 self.assertEqual (ar_t.shape, (h, w))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
863 self.assertEqual (ar_t.strides, (dy, dx))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
864 for i in range (w * h):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
865 x = i % w
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
866 y = i // w
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
867 self.assertEqual (ar_t[y, x], ar[x, y])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
868 ar1D = ar[0]
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
869 ar2D = ar1D.transpose()
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
870 self.assertEqual (ar2D.shape, (1, h))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
871 for y in range (h):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
872 self.assertEqual (ar1D[y], ar2D[0, y])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
873 ar1D = ar[:,0]
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
874 ar2D = ar1D.transpose()
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
875 self.assertEqual (ar2D.shape, (1, w))
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
876 for x in range (2):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
877 self.assertEqual (ar1D[x], ar2D[0, x])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
878
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
879 def test_length_1_dimension_broadcast (self):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
880 w = 5
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
881 sf = pygame.Surface ((w, w), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
882 ar = pygame.PixelArray (sf)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
883 # y-axis broadcast.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
884 sf_x = pygame.Surface ((w, 1), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
885 ar_x = pygame.PixelArray (sf_x)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
886 for i in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
887 ar_x[i, 0] = (w + 1) * 10
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
888 ar[...] = ar_x
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
889 for y in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
890 for x in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
891 self.assertEqual (ar[x, y], ar_x[x, 0])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
892 # x-axis broadcast.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
893 ar[...] = 0
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
894 sf_y = pygame.Surface ((1, w), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
895 ar_y = pygame.PixelArray (sf_y)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
896 for i in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
897 ar_y[0, i] = (w + 1) * 10
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
898 ar[...] = ar_y
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
899 for x in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
900 for y in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
901 self.assertEqual (ar[x, y], ar_y[0, y])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
902 # (1, 1) array broadcast.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
903 ar[...] = 0
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
904 sf_1px = pygame.Surface ((1, 1), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
905 ar_1px = pygame.PixelArray (sf_1px)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
906 ar_1px[0, 0] = 42 # Well it had to show up somewhere.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
907 ar[...] = ar_1px
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
908 for y in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
909 for x in range (w):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
910 self.assertEqual (ar[x, y], 42)
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
911
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
912 def test_assign_size_mismatch (self):
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
913 sf = pygame.Surface ((7, 11), 0, 32)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
914 ar = pygame.PixelArray (sf)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
915 self.assertRaises (ValueError, ar.__setitem__, Ellipsis, ar[:, 0:2])
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2219
diff changeset
916 self.assertRaises (ValueError, ar.__setitem__, Ellipsis, ar[0:2, :])
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
917
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
918 class PixelArrayArrayInterfaceTest (unittest.TestCase, TestMixin):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
919 def test_basic (self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
920 # Check unchanging fields.
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
921 sf = pygame.Surface ((2, 2), 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
922 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
923 ai = arrinter.ArrayInterface (ar)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
924 self.assertEqual (ai.two, 2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
925 self.assertEqual (ai.typekind, 'u')
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
926 self.assertEqual (ai.nd, 2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
927 self.assertEqual (ai.data, ar._pixels_address)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
928
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
929 def test_shape (self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
930 for shape in [[4, 16], [5, 13]]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
931 w, h = shape
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
932 sf = pygame.Surface (shape, 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
933 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
934 ai = arrinter.ArrayInterface (ar)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
935 ai_shape = [ai.shape[i] for i in range(ai.nd)]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
936 self.assertEqual (ai_shape, shape)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
937 ar2 = ar[::2,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
938 ai2 = arrinter.ArrayInterface (ar2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
939 w2 = len(([0] * w)[::2])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
940 ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
941 self.assertEqual (ai_shape, [w2, h])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
942 ar2 = ar[:,::2]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
943 ai2 = arrinter.ArrayInterface (ar2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
944 h2 = len(([0] * h)[::2])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
945 ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
946 self.assertEqual (ai_shape, [w, h2])
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
947
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
948 def test_itemsize (self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
949 for bytes_per_pixel in range(1, 5):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
950 bits_per_pixel = 8 * bytes_per_pixel
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
951 sf = pygame.Surface ((2, 2), 0, bits_per_pixel)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
952 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
953 ai = arrinter.ArrayInterface (ar)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
954 self.assertEqual (ai.itemsize, bytes_per_pixel)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
955
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
956 def test_flags (self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
957 aim = arrinter
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
958 common_flags = (aim.PAI_NOTSWAPPED | aim.PAI_WRITEABLE |
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
959 aim.PAI_ALIGNED | aim.PAI_FORTRAN)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
960 s = pygame.Surface ((10, 2), 0, 32)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
961 ar = pygame.PixelArray (s)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
962 ai = aim.ArrayInterface (ar)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
963 self.assertEqual (ai.flags, common_flags | aim.PAI_CONTIGUOUS)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
964
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
965 ar2 = ar[::2,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
966 ai = aim.ArrayInterface (ar2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
967 self.assertEqual (ai.flags, common_flags)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
968
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
969 s = pygame.Surface ((7, 2), 0, 24)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
970 ar = pygame.PixelArray (s)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
971 ai = aim.ArrayInterface (ar)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
972 self.assertEqual (ai.flags, common_flags)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
973
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
974 def test_slicing (self):
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
975 # This will implicitly test data and strides fields.
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
976 #
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
977 # Need an 8 bit test surfaces because pixelcopy.make_surface
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
978 # returns an 8 bit surface for a 2d array.
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
979
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
980 factors = [7, 3, 11]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
981
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
982 w = reduce (operator.mul, factors, 1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
983 h = 13
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
984 sf = pygame.Surface ((w, h), 0, 8)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
985 color = sf.map_rgb ((1, 17, 128))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
986 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
987 for f in factors[:-1]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
988 w = w // f
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
989 sf.fill ((0, 0, 0))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
990 ar = ar[f:f + w,:]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
991 ar[0][0] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
992 ar[-1][-2] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
993 ar[0][-3] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
994 sf2 = ar.make_surface ()
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
995 sf3 = pygame.pixelcopy.make_surface (ar)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
996 self.assert_surfaces_equal (sf3, sf2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
997
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
998 h = reduce (operator.mul, factors, 1)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
999 w = 13
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1000 sf = pygame.Surface ((w, h), 0, 8)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1001 color = sf.map_rgb ((1, 17, 128))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1002 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1003 for f in factors[:-1]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1004 h = h // f
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1005 sf.fill ((0, 0, 0))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1006 ar = ar[:,f:f + h]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1007 ar[0][0] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1008 ar[-1][-2] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1009 ar[0][-3] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1010 sf2 = ar.make_surface ()
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1011 sf3 = pygame.pixelcopy.make_surface (ar)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1012 self.assert_surfaces_equal (sf3, sf2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1013
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1014 w = 20
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1015 h = 10
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1016 sf = pygame.Surface ((w, h), 0, 8)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1017 color = sf.map_rgb ((1, 17, 128))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1018 ar = pygame.PixelArray (sf)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1019 for slices in [(slice (w), slice (h)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1020 (slice (0, w, 2), slice (h)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1021 (slice (0, w, 3), slice (h)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1022 (slice (w), slice (0, h, 2)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1023 (slice (w), slice (0, h, 3)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1024 (slice (0, w, 2), slice (0, h, 2)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1025 (slice (0, w, 3), slice (0, h, 3)),
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1026 ]:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1027 sf.fill ((0, 0, 0))
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1028 ar2 = ar[slices]
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1029 ar2[0][0] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1030 ar2[-1][-2] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1031 ar2[0][-3] = color
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1032 sf2 = ar2.make_surface ()
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1033 sf3 = pygame.pixelcopy.make_surface (ar2)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2204
diff changeset
1034 self.assert_surfaces_equal (sf3, sf2)
2078
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
1035
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
1036 if __name__ == '__main__':
2d7ce556083b set svn file properties so svn branch and merge work as intended
Lenard Lindstrom <len-l@telus.net>
parents: 1578
diff changeset
1037 unittest.main()