[Piglit] [PATCH 1/4] gen_const_builtin_equal_tests.py: Use mako

Ilia Mirkin imirkin at alum.mit.edu
Mon May 19 15:02:17 PDT 2014


On Mon, May 19, 2014 at 5:48 PM, Dylan Baker <baker.dylan.c at gmail.com> wrote:
> On Monday, May 19, 2014 17:47:47 Ilia Mirkin wrote:
>
>> On Mon, May 19, 2014 at 5:43 PM, Dylan Baker <baker.dylan.c at gmail.com>
>> wrote:
>
>> > This uses mako to render tests. It's faster than the previous method,
>
>> > and is much easier to understand.
>
>> >
>
>> > Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
>
>> > ---
>
>> > generated_tests/gen_const_builtin_equal_tests.py | 87
>> > ++++++++++++++----------
>
>> > 1 file changed, 50 insertions(+), 37 deletions(-)
>
>> >
>
>> > diff --git a/generated_tests/gen_const_builtin_equal_tests.py
>> > b/generated_tests/gen_const_builtin_equal_tests.py
>
>> > index 77fcedf..583f90e 100644
>
>> > --- a/generated_tests/gen_const_builtin_equal_tests.py
>
>> > +++ b/generated_tests/gen_const_builtin_equal_tests.py
>
>> > @@ -1,39 +1,52 @@
>
>> > -from __future__ import print_function
>
>> > -import re
>
>> > -import os
>
>> > -
>
>> > -def emit_test(f, func, input1, input2, expected):
>
>> > - # Determine the expected return type of the equal function by looking
>> > at
>
>> > - # the string of the expected return value.
>
>> > - s = expected.split("(")
>
>> > +# Copyright (c) 2010, 2014 Intel Corporation
>
>> >
>
>> > - spaces = re.sub("[^ ]", " ", func+s[0])
>
>> > +# Permission is hereby granted, free of charge, to any person obtaining
>> > a copy
>
>> > +# of this software and associated documentation files (the "Software"),
>> > to deal
>
>> > +# in the Software without restriction, including without limitation the
>> > rights
>
>> > +# to use, copy, modify, merge, publish, distribute, sublicense, and/or
>> > sell
>
>> > +# copies of the Software, and to permit persons to whom the Software is
>
>> > +# furnished to do so, subject to the following conditions:
>
>> >
>
>> > - test = """
>
>> > -[require]
>
>> > -GLSL >= 1.20
>
>> > +# The above copyright notice and this permission notice shall be
>> > included in
>
>> > +# all copies or substantial portions of the Software.
>
>> >
>
>> > -[vertex shader]
>
>> > -void main()
>
>> > -{
>
>> > - gl_Position = gl_Vertex;
>
>> > -}
>
>> > +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
>> > EXPRESS OR
>
>> > +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
>> > MERCHANTABILITY,
>
>> > +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
>> > SHALL THE
>
>> > +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
>> > OTHER
>
>> > +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
>> > ARISING FROM,
>
>> > +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
>> > DEALINGS IN THE
>
>> > +# SOFTWARE.
>
>> >
>
>> > -[fragment shader]
>
>> > -void main()
>
>> > -{
>
>> > - const %s res = %s(%s,
>
>> > - %s%s);
>
>> > - gl_FragColor = (res == %s)
>
>> > - ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0);
>
>> > -}
>
>> > -
>
>> > -[test]
>
>> > -draw rect -1 -1 2 2
>
>> > -probe all rgb 0.0 1.0 0.0
>
>> > -""" % (s[0], func, input1, spaces, input2, expected)
>
>> > - f.write(test)
>
>> > +""" Generate tests for builtin const equality tests """
>
>> >
>
>> > +from __future__ import print_function
>
>> > +import re
>
>> > +import os
>
>> > +import textwrap
>
>> > +import mako.template
>
>> > +
>
>> > +TEMPLATE = mako.template.Template(textwrap.dedent("""
>
>> > + [require]
>
>> > + GLSL >= 1.20
>
>> > +
>
>> > + [vertex shader]
>
>> > + void main()
>
>> > + {
>
>> > + gl_Position = gl_Vertex;
>
>> > + }
>
>> > +
>
>> > + [fragment shader]
>
>> > + void main()
>
>> > + {
>
>> > + const ${expected.split('(')[0]} res = ${func}(${input[0]},
>> > ${input[1]});
>
>> > + gl_FragColor = (res == ${expected})
>
>> > + ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0);
>
>> > + }
>
>> > +
>
>> > + [test]
>
>> > + draw rect -1 -1 2 2
>
>> > + probe all rgb 0.0 1.0 0.0"""))
>
>> >
>
>> > test_vectors = [
>
>> > [
>
>> > @@ -96,9 +109,9 @@ for x in test_vectors:
>
>> >
>
>> > print(name)
>
>> >
>
>> > - f = open(name, "w")
>
>> > - emit_test(f, "equal", x[0], x[1], x[2])
>
>> > - f.close()
>
>> > + with open(name, 'w+') as f:
>
>> > + f.write(TEMPLATE.render_unicode(
>
>> > + func='equal', input=x[0:2], expected=x[2]))
>
>> >
>
>> >
>
>> > test_id = 2
>
>> > @@ -115,6 +128,6 @@ for x in test_vectors:
>
>> >
>
>> > print(name)
>
>> >
>
>> > - f = open(name, "w")
>
>> > - emit_test(f, "notEqual", x[0], x[1], expected)
>
>> > - f.close()
>
>> > + with open(name, 'w+') as f:
>
>>
>
>> Why w+?
>
>
>
> Development artifact. I generate the tests with the old generator,
>
> check them in, and generate over them using `git diff` to check that
>
> I'm still generating the same thing.
>
>
>
> I can change that if you like

I would definitely prefer that. The + makes me think about why you're
doing that, not having it makes the code more obvious. With that
change, patches 1-3 Reviewed-by: Ilia Mirkin <imirkin at alum.mit.edu> .
(Do consider my comment on patch 3 though, about the second param to
enumerate.)

As an aside, your mail client appears to have defeated gmail. It seems
to cause gmail to insert an extra newline after each of your lines in
the response. Any idea what's going on there? I've yet to look at the
source to see what's up...

  -ilia


More information about the Piglit mailing list