~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to test/test_utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#################################### IMPORTS ###################################
2
 
 
3
 
import tempfile, sys, pygame, time, os
4
 
 
5
 
################################################################################
6
 
 
7
 
trunk_dir = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
8
 
 
9
 
def trunk_relative_path(relative):
10
 
    return os.path.normpath(os.path.join(trunk_dir, relative))
11
 
 
12
 
sys.path.insert(0, trunk_relative_path('.'))
13
 
 
14
 
import test.unittest as unittest
15
 
 
16
 
############################### INCOMPLETE TESTS ###############################
17
 
# TODO: PHASE THIS OUT
18
 
# Just prefix TODO test names with todo_. 
19
 
# eg def todo_test_sanity__is_overrated(self): self.fail()
20
 
# Change test loader to load test_ and todo_ TestCase callables as tests
21
 
 
22
 
fail_incomplete_tests = 0
23
 
 
24
 
def test_not_implemented():
25
 
    return not fail_incomplete_tests
26
 
 
27
 
################################## TEMP FILES ##################################
28
 
 
29
 
def get_tmp_dir():
30
 
    return tempfile.mkdtemp()
31
 
 
32
 
################################################################################
33
 
 
34
 
def question(q):
35
 
    return raw_input('%s ' % q.rstrip(' ')).lower().strip() == 'y'
36
 
 
37
 
def prompt(p):
38
 
    return raw_input('%s (and press enter to continue) ' % p.rstrip(' '))
39
 
 
40
 
#################################### HELPERS ###################################
41
 
 
42
 
def rgba_between(value, minimum=0, maximum=255):
43
 
    if value < minimum: return minimum
44
 
    elif value > maximum: return maximum
45
 
    else: return value
46
 
 
47
 
def combinations(seqs):
48
 
    """
49
 
    
50
 
    Recipe 496807 from ActiveState Python CookBook
51
 
    
52
 
    Non recursive technique for getting all possible combinations of a sequence 
53
 
    of sequences.
54
 
    
55
 
    """
56
 
 
57
 
    r=[[]]
58
 
    for x in seqs:
59
 
        r = [ i + [y] for y in x for i in r ]
60
 
    return r
61
 
 
62
 
def gradient(width, height):
63
 
    """
64
 
 
65
 
    Yields a pt and corresponding RGBA tuple, for every (width, height) combo.
66
 
    Useful for generating gradients.
67
 
    
68
 
    Actual gradient may be changed, no tests rely on specific values.
69
 
    
70
 
    Used in transform.rotate lossless tests to generate a fixture.
71
 
 
72
 
    """
73
 
 
74
 
    for l in xrange(width):
75
 
        for t in xrange(height):
76
 
            yield (l,t), tuple(map(rgba_between, (l, t, l, l+t)))
77
 
 
78
 
def unordered_equality(seq1, seq2):
79
 
    """
80
 
    
81
 
    Tests to see if the contents of one sequence is contained in the other
82
 
    and that they are of the same length.
83
 
    
84
 
    """
85
 
    
86
 
    if len(seq1) != len(seq2):
87
 
        return False
88
 
 
89
 
    for val in seq1:
90
 
        if val not in seq2:
91
 
            return False
92
 
        
93
 
    return True
94
 
 
95
 
def rect_area_pts(rect):
96
 
    for l in xrange(rect.left, rect.right):
97
 
        for t in xrange(rect.top, rect.bottom):
98
 
            yield l, t
99
 
 
100
 
def rect_perimeter_pts(rect):
101
 
    """
102
 
    
103
 
    Returns pts ((L, T) tuples) encompassing the perimeter of a rect.
104
 
    
105
 
    The order is clockwise:
106
 
        
107
 
          topleft to topright
108
 
         topright to bottomright
109
 
      bottomright to bottomleft
110
 
       bottomleft to topleft
111
 
    
112
 
    Duplicate pts are not returned
113
 
 
114
 
    """
115
 
    clock_wise_from_top_left = (
116
 
      ((l,       rect.top) for l in xrange(rect.left,      rect.right)      ),
117
 
      ((rect.right -1,  t) for t in xrange(rect.top   + 1, rect.bottom)     ), 
118
 
      ((l, rect.bottom -1) for l in xrange(rect.right  -2, rect.left -1, -1)), 
119
 
      ((rect.left,      t) for t in xrange(rect.bottom -2, rect.top,     -1))
120
 
    )
121
 
    
122
 
    for line in clock_wise_from_top_left:
123
 
        for pt in line: yield pt
124
 
    
125
 
def rect_outer_bounds(rect):
126
 
    """
127
 
 
128
 
    Returns topleft outerbound if possible and then the other pts, that are 
129
 
    "exclusive" bounds of the rect
130
 
        
131
 
   ?------O     
132
 
    |RECT|      ?|0)uterbound
133
 
    |----|     
134
 
   O      O
135
 
 
136
 
    """
137
 
    return (
138
 
         (rect.left is not 0 and [(rect.left-1, rect.top)] or []) +
139
 
        [ rect.topright,
140
 
          rect.bottomleft,                                             
141
 
          rect.bottomright]  
142
 
    ) 
143
 
 
144
 
def helpers_test():
145
 
    """
146
 
    
147
 
    Lightweight test for helpers
148
 
    
149
 
    """
150
 
 
151
 
    r = pygame.Rect(0, 0, 10, 10)
152
 
    assert (
153
 
        rect_outer_bounds ( r ) == [(10,  0), # tr
154
 
                                    ( 0, 10), # bl
155
 
                                    (10, 10)] # br
156
 
    )
157
 
    
158
 
    assert len(list(rect_area_pts(r))) == 100 
159
 
    
160
 
    
161
 
    r = pygame.Rect(0, 0, 3, 3)
162
 
    assert list(rect_perimeter_pts(r)) == [
163
 
        (0, 0), (1, 0), (2, 0),                 # tl -> tr
164
 
        (2, 1), (2, 2),                         # tr -> br  
165
 
        (1, 2), (0, 2),                         # br -> bl
166
 
        (0, 1)                                  # bl -> tl
167
 
    ]
168
 
        
169
 
    print 'Tests: OK'
170
 
 
171
 
if __name__ == '__main__':
172
 
    helpers_test()
173
 
 
174
 
################################################################################