2 ##########################################################################
4 # Copyright 2009 VMware, Inc.
5 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
8 # Permission is hereby granted, free of charge, to any person obtaining a
9 # copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sub license, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
16 # The above copyright notice and this permission notice (including the
17 # next paragraph) shall be included in all copies or substantial portions
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 # IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 ##########################################################################
31 """Base classes for tests.
33 Loosely inspired on Python's unittest module.
43 # Enumerate all pixel formats
45 for name
, value
in globals().items():
46 if name
.startswith("PIPE_FORMAT_") and isinstance(value
, int):
49 def is_depth_stencil_format(format
):
50 # FIXME: make and use binding to util_format_is_depth_or_stencil
52 PIPE_FORMAT_Z32_UNORM
,
53 PIPE_FORMAT_Z24S8_UNORM
,
54 PIPE_FORMAT_Z24X8_UNORM
,
55 PIPE_FORMAT_Z16_UNORM
,
58 def make_image(width
, height
, rgba
):
64 outpixels
= outimage
.load()
65 for y
in range(0, height
):
66 for x
in range(0, width
):
67 offset
= (y
*width
+ x
)*4
68 r
, g
, b
, a
= [int(min(max(rgba
[offset
+ ch
], 0.0), 1.0)*255) for ch
in range(4)]
69 outpixels
[x
, y
] = r
, g
, b
72 def save_image(width
, height
, rgba
, filename
):
73 outimage
= make_image(width
, height
, rgba
)
74 outimage
.save(filename
, "PNG")
76 def show_image(width
, height
, **rgbas
):
78 from PIL
import Image
, ImageTk
87 for i
in range(len(labels
)):
89 outimage
= make_image(width
, height
, rgbas
[label
])
92 window
= tk
.Toplevel(root
)
96 image1
= ImageTk
.PhotoImage(outimage
)
99 window
.geometry("%dx%d+%d+%d" % (w
, h
, x
, y
))
100 panel1
= tk
.Label(window
, image
=image1
)
101 panel1
.pack(side
='top', fill
='both', expand
='yes')
102 panel1
.image
= image1
108 class TestFailure(Exception):
112 class TestSkip(Exception):
122 def _run(self
, result
):
123 raise NotImplementedError
126 result
= TestResult()
130 def assert_rgba(self
, surface
, x
, y
, w
, h
, expected_rgba
, pixel_tol
=4.0/256, surface_tol
=0.85):
132 different
= surface
.compare_tile_rgba(x
, y
, w
, h
, expected_rgba
, tol
=pixel_tol
)
134 sys
.stderr
.write("%u out of %u pixels differ\n" % (different
, total
))
136 if float(total
- different
)/float(total
) < surface_tol
:
138 rgba
= FloatArray(h
*w
*4)
139 surface
.get_tile_rgba(x
, y
, w
, h
, rgba
)
140 show_image(w
, h
, Result
=rgba
, Expected
=expected_rgba
)
141 save_image(w
, h
, rgba
, "result.png")
142 save_image(w
, h
, expected_rgba
, "expected.png")
148 class TestCase(Test
):
152 def __init__(self
, dev
, **kargs
):
155 self
.__dict
__.update(kargs
)
157 def description(self
):
159 for tag
in self
.tags
:
160 value
= self
.get(tag
)
161 if value
is not None and value
!= '':
162 descriptions
.append(tag
+ '=' + str(value
))
163 return ' '.join(descriptions
)
167 method
= getattr(self
, '_get_' + tag
)
168 except AttributeError:
169 return getattr(self
, tag
, None)
173 def _get_target(self
):
175 PIPE_TEXTURE_1D
: "1d",
176 PIPE_TEXTURE_2D
: "2d",
177 PIPE_TEXTURE_3D
: "3d",
178 PIPE_TEXTURE_CUBE
: "cube",
181 def _get_format(self
):
182 name
= formats
[self
.format
]
183 if name
.startswith('PIPE_FORMAT_'):
189 if self
.target
== PIPE_TEXTURE_CUBE
:
191 PIPE_TEX_FACE_POS_X
: "+x",
192 PIPE_TEX_FACE_NEG_X
: "-x",
193 PIPE_TEX_FACE_POS_Y
: "+y",
194 PIPE_TEX_FACE_NEG_Y
: "-y",
195 PIPE_TEX_FACE_POS_Z
: "+z",
196 PIPE_TEX_FACE_NEG_Z
: "-z",
202 raise NotImplementedError
204 def _run(self
, result
):
205 result
.test_start(self
)
208 except KeyboardInterrupt:
211 result
.test_skipped(self
)
213 result
.test_failed(self
)
215 result
.test_passed(self
)
218 class TestSuite(Test
):
220 def __init__(self
, tests
= None):
227 def add_test(self
, test
):
228 self
.tests
.append(test
)
230 def _run(self
, result
):
231 for test
in self
.tests
:
243 self
.names
= ['result']
244 self
.types
= ['pass skip fail']
247 def test_start(self
, test
):
248 sys
.stdout
.write("Running %s...\n" % test
.description())
252 def test_passed(self
, test
):
253 sys
.stdout
.write("PASS\n")
256 self
.log_result(test
, 'pass')
258 def test_skipped(self
, test
):
259 sys
.stdout
.write("SKIP\n")
262 #self.log_result(test, 'skip')
264 def test_failed(self
, test
):
265 sys
.stdout
.write("FAIL\n")
268 self
.log_result(test
, 'fail')
270 def log_result(self
, test
, result
):
271 row
= ['']*len(self
.names
)
274 assert self
.names
[0] == 'result'
275 assert result
in ('pass', 'skip', 'fail')
279 for tag
in test
.tags
:
280 value
= test
.get(tag
)
285 elif isinstance(value
, (int, float)):
287 type = 'c' # continous
288 elif isinstance(value
, basestring
):
289 type = 'd' # discrete
293 type = 'd' # discrete
297 col
= self
.names
.index(tag
, 1)
299 self
.names
.append(tag
)
300 self
.types
.append(type)
304 assert self
.types
[col
] == type
306 self
.rows
.append(row
)
309 sys
.stdout
.write("%u tests, %u passed, %u skipped, %u failed\n\n" % (self
.tests
, self
.passed
, self
.skipped
, self
.failed
))
312 name
, ext
= os
.path
.splitext(os
.path
.basename(sys
.argv
[0]))
313 filename
= name
+ '.tsv'
314 stream
= file(filename
, 'wt')
317 stream
.write('\t'.join(self
.names
) + '\n')
318 stream
.write('\t'.join(self
.types
) + '\n')
319 stream
.write('class\n')
322 for row
in self
.rows
:
323 row
+= ['']*(len(self
.names
) - len(row
))
324 stream
.write('\t'.join(row
) + '\n')
328 # See http://www.ailab.si/orange/doc/ofb/c_otherclass.htm
333 sys
.stderr
.write('Install Orange from http://www.ailab.si/orange/ for a classification tree.\n')
336 data
= orange
.ExampleTable(filename
)
338 tree
= orngTree
.TreeLearner(data
, sameMajorityPruning
=1, mForPruning
=2)
340 orngTree
.printTxt(tree
, maxDepth
=4)
342 file(name
+'.txt', 'wt').write(orngTree
.dumpTree(tree
))
344 orngTree
.printDot(tree
, fileName
=name
+'.dot', nodeShape
='ellipse', leafShape
='box')