[Mesa-dev] [PATCH 2/2] nir/algebraic: Add unit tests for bitsize validation

Connor Abbott cwabbott0 at gmail.com
Thu Nov 29 18:32:03 UTC 2018


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.
---
 src/compiler/Makefile.nir.am                  |   4 +-
 src/compiler/nir/meson.build                  |   7 ++
 .../nir/tests/algebraic_parser_test.py        | 116 ++++++++++++++++++
 .../nir/tests/algebraic_parser_test.sh        |   3 +
 4 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 src/compiler/nir/tests/algebraic_parser_test.py
 create mode 100644 src/compiler/nir/tests/algebraic_parser_test.sh

diff --git a/src/compiler/Makefile.nir.am b/src/compiler/Makefile.nir.am
index c646c6bdc1e..aa0a92856f1 100644
--- a/src/compiler/Makefile.nir.am
+++ b/src/compiler/Makefile.nir.am
@@ -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 += \
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index b0c3a7feb31..84e58cafb6f 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -259,4 +259,11 @@ if with_tests
       link_with : libmesa_util,
     )
   )
+  test(
+    'nir_algebraic_parser',
+    prog_python,
+    args : [
+      join_paths(meson.current_source_dir(), 'tests/algebraic_parser_test.py')
+    ],
+  )
 endif
diff --git a/src/compiler/nir/tests/algebraic_parser_test.py b/src/compiler/nir/tests/algebraic_parser_test.py
new file mode 100644
index 00000000000..492a09ec7db
--- /dev/null
+++ b/src/compiler/nir/tests/algebraic_parser_test.py
@@ -0,0 +1,116 @@
+#
+# 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 at 32', 'a at 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 at 32'), ('fadd', 'a at 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 at 32', 'b at 64'), ('fadd', a, b)),
+            "Source a at 32 of ('iadd', 'a at 32', 'b at 64') must have bit size 32, " \
+            "while source b at 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 at 64'), ('ishl', a, b)),
+            "b at 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 at 32', 'a at 64', b), ('iadd', a, b)),
+            "('iadd at 32', 'a at 64', 'b') must have the bit size of 32, " \
+            "while its source a at 64 must have incompatible bit size 64")
+
+    def test_replace_dst_bitsize(self):
+        self.common((('iadd', a, b), ('iadd at 32', a, b)),
+            "('iadd at 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 at 64', a), ('ineg', a)),
+            "('ufind_msb at 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 at 64'), ('ufind_msb at 64', a)),
+            "('ufind_msb at 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 at 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()
diff --git a/src/compiler/nir/tests/algebraic_parser_test.sh b/src/compiler/nir/tests/algebraic_parser_test.sh
new file mode 100644
index 00000000000..5c23443fb11
--- /dev/null
+++ b/src/compiler/nir/tests/algebraic_parser_test.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+$PYTHON $srcdir/nir/tests/algebraic_parser_test.py
-- 
2.17.2



More information about the mesa-dev mailing list