Merge remote branch 'origin/master' into radeon-rewrite
[mesa.git] / src / gallium / state_trackers / python / tests / base.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sub license, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice (including the
16 # next paragraph) shall be included in all copies or substantial portions
17 # of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 # IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #
27 ##########################################################################
28
29
30 """Base classes for tests.
31
32 Loosely inspired on Python's unittest module.
33 """
34
35
36 from gallium import *
37
38
39 # Enumerate all pixel formats
40 formats = {}
41 for name, value in globals().items():
42 if name.startswith("PIPE_FORMAT_") and isinstance(value, int):
43 formats[value] = name
44
45
46 def make_image(width, height, rgba):
47 import Image
48 outimage = Image.new(
49 mode='RGB',
50 size=(width, height),
51 color=(0,0,0))
52 outpixels = outimage.load()
53 for y in range(0, height):
54 for x in range(0, width):
55 offset = (y*width + x)*4
56 r, g, b, a = [int(min(max(rgba[offset + ch], 0.0), 1.0)*255) for ch in range(4)]
57 outpixels[x, y] = r, g, b
58 return outimage
59
60 def save_image(width, height, rgba, filename):
61 outimage = make_image(width, height, rgba)
62 outimage.save(filename, "PNG")
63
64 def show_image(width, height, **rgbas):
65 import Tkinter as tk
66 from PIL import Image, ImageTk
67
68 root = tk.Tk()
69
70 x = 64
71 y = 64
72
73 labels = rgbas.keys()
74 labels.sort()
75 for i in range(len(labels)):
76 label = labels[i]
77 outimage = make_image(width, height, rgbas[label])
78
79 if i:
80 window = tk.Toplevel(root)
81 else:
82 window = root
83 window.title(label)
84 image1 = ImageTk.PhotoImage(outimage)
85 w = image1.width()
86 h = image1.height()
87 window.geometry("%dx%d+%d+%d" % (w, h, x, y))
88 panel1 = tk.Label(window, image=image1)
89 panel1.pack(side='top', fill='both', expand='yes')
90 panel1.image = image1
91 x += w + 2
92
93 root.mainloop()
94
95
96 class TestFailure(Exception):
97
98 pass
99
100 class TestSkip(Exception):
101
102 pass
103
104
105 class Test:
106
107 def __init__(self):
108 pass
109
110 def _run(self, result):
111 raise NotImplementedError
112
113 def run(self):
114 result = TestResult()
115 self._run(result)
116 result.summary()
117
118
119 class TestCase(Test):
120
121 def __init__(self, dev, **kargs):
122 Test.__init__(self)
123 self.dev = dev
124 self.__dict__.update(kargs)
125
126 def description(self):
127 raise NotImplementedError
128
129 def test(self):
130 raise NotImplementedError
131
132 def _run(self, result):
133 result.test_start(self)
134 try:
135 self.test()
136 except KeyboardInterrupt:
137 raise
138 except TestSkip:
139 result.test_skipped(self)
140 except TestFailure:
141 result.test_failed(self)
142 else:
143 result.test_passed(self)
144
145
146 class TestSuite(Test):
147
148 def __init__(self, tests = None):
149 Test.__init__(self)
150 if tests is None:
151 self.tests = []
152 else:
153 self.tests = tests
154
155 def add_test(self, test):
156 self.tests.append(test)
157
158 def _run(self, result):
159 for test in self.tests:
160 test._run(result)
161
162
163 class TestResult:
164
165 def __init__(self):
166 self.tests = 0
167 self.passed = 0
168 self.skipped = 0
169 self.failed = 0
170 self.failed_descriptions = []
171
172 def test_start(self, test):
173 self.tests += 1
174 print "Running %s..." % test.description()
175
176 def test_passed(self, test):
177 self.passed += 1
178 print "PASS"
179
180 def test_skipped(self, test):
181 self.skipped += 1
182 print "SKIP"
183
184 def test_failed(self, test):
185 self.failed += 1
186 self.failed_descriptions.append(test.description())
187 print "FAIL"
188
189 def summary(self):
190 print "%u tests, %u passed, %u skipped, %u failed" % (self.tests, self.passed, self.skipped, self.failed)
191 for description in self.failed_descriptions:
192 print " %s" % description
193