Commit a0ae12ca authored by Connor Abbott's avatar Connor Abbott

nir/algebraic: Add unit tests for bitsize validation

The non-failure path can be tested by just compiling mesa and then
testing it, but the failure paths won't be hit unless you make a mistake,
so it's best to test them with some unit tests.
Reviewed-by: Jason Ekstrand's avatarJason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker's avatarDylan Baker <dylan@pnwbakers.com>
parent 29a1450e
......@@ -87,10 +87,12 @@ nir_tests_vars_tests_SOURCES = nir/tests/vars_tests.cpp
nir_tests_vars_tests_CFLAGS = $(NIR_TESTS_CFLAGS)
nir_tests_vars_tests_LDADD = $(NIR_TESTS_LDADD)
check_SCRIPTS = nir/tests/algebraic_parser_test.sh
TESTS += \
nir/tests/control_flow_tests \
nir/tests/vars_tests
nir/tests/vars_tests \
nir/tests/algebraic_parser_test.sh
BUILT_SOURCES += \
......
......@@ -262,4 +262,11 @@ if with_tests
),
suite : ['compiler', 'nir'],
)
test(
'nir_algebraic_parser',
prog_python,
args : [
join_paths(meson.current_source_dir(), 'tests/algebraic_parser_test.py')
],
)
endif
#
# Copyright (C) 2018 Valve Corporation
#
# 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:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# 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.
import unittest
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from nir_algebraic import SearchAndReplace
# These tests check that the bitsize validator correctly rejects various
# different kinds of malformed expressions, and documents what the error
# message looks like.
a = 'a'
b = 'b'
c = 'c'
class ValidatorTests(unittest.TestCase):
pattern = ()
message = ''
def common(self, pattern, message):
with self.assertRaises(AssertionError) as context:
SearchAndReplace(pattern)
self.assertEqual(message, str(context.exception))
def test_wrong_src_count(self):
self.common((('iadd', a), ('fadd', a, a)),
"Expression ('iadd', 'a') has 1 sources, expected 2")
def test_var_bitsize(self):
self.common((('iadd', 'a@32', 'a@64'), ('fadd', a, a)),
"Variable a has conflicting bit size requirements: " \
"it must have bit size 32 and 64")
def test_var_bitsize_2(self):
self.common((('iadd', a, 'a@32'), ('fadd', 'a@64', a)),
"Variable a has conflicting bit size requirements: " \
"it must have bit size 32 and 64")
def test_search_src_bitsize(self):
self.common((('iadd', 'a@32', 'b@64'), ('fadd', a, b)),
"Source a@32 of ('iadd', 'a@32', 'b@64') must have bit size 32, " \
"while source b@64 must have incompatible bit size 64")
def test_replace_src_bitsize(self):
self.common((('iadd', a, ('b2i', b)), ('iadd', a, b)),
"Sources a (bit size of a) and b (bit size of 32) " \
"of ('iadd', 'a', 'b') may not have the same bit size " \
"when building the replacement expression.")
def test_search_src_bitsize_fixed(self):
self.common((('ishl', a, 'b@64'), ('ishl', a, b)),
"b@64 must have 64 bits, but as a source of nir_op_ishl " \
"it must have 32 bits")
def test_replace_src_bitsize_fixed(self):
self.common((('iadd', a, b), ('ishl', a, b)),
"b has the bit size of b, but as a source of nir_op_ishl " \
"it must have 32 bits, which may not be the same")
def test_search_dst_bitsize(self):
self.common((('iadd@32', 'a@64', b), ('iadd', a, b)),
"('iadd@32', 'a@64', 'b') must have the bit size of 32, " \
"while its source a@64 must have incompatible bit size 64")
def test_replace_dst_bitsize(self):
self.common((('iadd', a, b), ('iadd@32', a, b)),
"('iadd@32', 'a', 'b') must have 32 bits, but its source a " \
"(bit size of b) may not have that bit size when building " \
"the replacement.")
def test_search_dst_bitsize_fixed(self):
self.common((('ufind_msb@64', a), ('ineg', a)),
"('ufind_msb@64', 'a') must have 64 bits, "\
"but as a destination of nir_op_ufind_msb it must have 32 bits")
def test_replace_dst_bitsize_fixed(self):
self.common((('ineg', 'a@64'), ('ufind_msb@64', a)),
"('ufind_msb@64', 'a') must have 64 bits, " \
"but as a destination of nir_op_ufind_msb it must have 32 bits")
def test_ambiguous_bitsize(self):
self.common((('ineg', 'a@32'), ('i2b', ('b2i', a))),
"Ambiguous bit size for replacement value ('b2i', 'a'): it "\
"cannot be deduced from a variable, a fixed bit size somewhere, "
"or the search expression.")
def test_search_replace_mismatch(self):
self.common((('b2i', ('i2b', a)), a),
"The search expression bit size ('b2i', ('i2b', 'a')) and " \
"replace expression bit size a may not be the same")
unittest.main()
#!/bin/sh
$PYTHON $srcdir/nir/tests/algebraic_parser_test.py
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment