1# 2# Copyright (C) 2018 Valve Corporation 3# 4# Permission is hereby granted, free of charge, to any person obtaining a 5# copy of this software and associated documentation files (the "Software"), 6# to deal in the Software without restriction, including without limitation 7# the rights to use, copy, modify, merge, publish, distribute, sublicense, 8# and/or sell copies of the Software, and to permit persons to whom the 9# Software is furnished to do so, subject to the following conditions: 10# 11# The above copyright notice and this permission notice (including the next 12# paragraph) shall be included in all copies or substantial portions of the 13# Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21# IN THE SOFTWARE. 22 23import unittest 24 25import sys 26import os 27sys.path.insert(1, os.path.join(sys.path[0], '..')) 28 29from nir_algebraic import SearchAndReplace 30 31# These tests check that the bitsize validator correctly rejects various 32# different kinds of malformed expressions, and documents what the error 33# message looks like. 34 35a = 'a' 36b = 'b' 37c = 'c' 38 39class ValidatorTests(unittest.TestCase): 40 pattern = () 41 message = '' 42 43 def common(self, pattern, message): 44 with self.assertRaises(AssertionError) as context: 45 SearchAndReplace(pattern) 46 47 self.assertEqual(message, str(context.exception)) 48 49 def test_wrong_src_count(self): 50 self.common((('iadd', a), ('fadd', a, a)), 51 "Expression ('iadd', 'a') has 1 sources, expected 2") 52 53 def test_var_bitsize(self): 54 self.common((('iadd', 'a@32', 'a@64'), ('fadd', a, a)), 55 "Variable a has conflicting bit size requirements: " \ 56 "it must have bit size 32 and 64") 57 58 def test_var_bitsize_2(self): 59 self.common((('iadd', a, 'a@32'), ('fadd', 'a@64', a)), 60 "Variable a has conflicting bit size requirements: " \ 61 "it must have bit size 32 and 64") 62 63 def test_search_src_bitsize(self): 64 self.common((('iadd', 'a@32', 'b@64'), ('fadd', a, b)), 65 "Source a@32 of ('iadd', 'a@32', 'b@64') must have bit size 32, " \ 66 "while source b@64 must have incompatible bit size 64") 67 68 def test_replace_src_bitsize(self): 69 self.common((('iadd', a, ('b2i', b)), ('iadd', a, b)), 70 "Sources a (bit size of a) and b (bit size of b) " \ 71 "of ('iadd', 'a', 'b') may not have the same bit size " \ 72 "when building the replacement expression.") 73 74 def test_search_src_bitsize_fixed(self): 75 self.common((('ishl', a, 'b@64'), ('ishl', a, b)), 76 "b@64 must have 64 bits, but as a source of nir_op_ishl " \ 77 "it must have 32 bits") 78 79 def test_replace_src_bitsize_fixed(self): 80 self.common((('iadd', a, b), ('ishl', a, b)), 81 "b has the bit size of b, but as a source of nir_op_ishl " \ 82 "it must have 32 bits, which may not be the same") 83 84 def test_search_dst_bitsize(self): 85 self.common((('iadd@32', 'a@64', b), ('iadd', a, b)), 86 "('iadd@32', 'a@64', 'b') must have the bit size of 32, " \ 87 "while its source a@64 must have incompatible bit size 64") 88 89 def test_replace_dst_bitsize(self): 90 self.common((('iadd', a, b), ('iadd@32', a, b)), 91 "('iadd@32', 'a', 'b') must have 32 bits, but its source a " \ 92 "(bit size of b) may not have that bit size when building " \ 93 "the replacement.") 94 95 def test_search_dst_bitsize_fixed(self): 96 self.common((('ufind_msb@64', a), ('ineg', a)), 97 "('ufind_msb@64', 'a') must have 64 bits, "\ 98 "but as a destination of nir_op_ufind_msb it must have 32 bits") 99 100 def test_replace_dst_bitsize_fixed(self): 101 self.common((('ineg', 'a@64'), ('ufind_msb@64', a)), 102 "('ufind_msb@64', 'a') must have 64 bits, " \ 103 "but as a destination of nir_op_ufind_msb it must have 32 bits") 104 105 def test_ambiguous_bitsize(self): 106 self.common((('ineg', 'a@32'), ('i2b', ('b2i', a))), 107 "Ambiguous bit size for replacement value ('b2i', 'a'): it "\ 108 "cannot be deduced from a variable, a fixed bit size somewhere, " 109 "or the search expression.") 110 111 def test_search_replace_mismatch(self): 112 self.common((('b2i', ('i2b', a)), a), 113 "The search expression bit size ('b2i', ('i2b', 'a')) and " \ 114 "replace expression bit size a may not be the same") 115 116unittest.main() 117