1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Tests for logical_expressions module.""" 16 17from tensorflow.python.autograph.converters import logical_expressions 18from tensorflow.python.autograph.core import converter_testing 19from tensorflow.python.framework import constant_op 20from tensorflow.python.framework import test_util 21from tensorflow.python.platform import test 22 23 24class LogicalExpressionTest(converter_testing.TestCase): 25 26 def test_equals(self): 27 28 def f(a, b): 29 return a == b 30 31 tr = self.transform(f, logical_expressions) 32 33 self.assertTrue(self.evaluate(tr(constant_op.constant(1), 1))) 34 self.assertFalse(self.evaluate(tr(constant_op.constant(1), 2))) 35 36 @test_util.run_deprecated_v1 37 def test_bool_ops(self): 38 39 def f(a, b, c): 40 return (a or b) and (a or b or c) and not c 41 42 tr = self.transform(f, logical_expressions) 43 44 self.assertTrue(self.evaluate(tr(constant_op.constant(True), False, False))) 45 self.assertFalse(self.evaluate(tr(constant_op.constant(True), False, True))) 46 47 def test_comparison(self): 48 49 def f(a, b, c, d): 50 return a < b == c > d 51 52 tr = self.transform(f, logical_expressions) 53 54 # Note: having just the first constant a tensor tests that the 55 # operations execute in the correct order. If anything other than 56 # a < b executed first, the result would be a Python scalar and not a 57 # Tensor. This is valid as long as the dispat is automatic based on 58 # type. 59 self.assertTrue(self.evaluate(tr(constant_op.constant(1), 2, 2, 1))) 60 self.assertFalse(self.evaluate(tr(constant_op.constant(1), 2, 2, 3))) 61 62 def test_default_ops(self): 63 64 def f(a, b): 65 return a in b 66 67 tr = self.transform(f, logical_expressions) 68 69 self.assertTrue(tr('a', ('a',))) 70 71 def test_unary_ops(self): 72 73 def f(a): 74 return ~a, -a, +a 75 76 tr = self.transform(f, logical_expressions) 77 78 self.assertEqual(tr(1), (-2, -1, 1)) 79 80 81if __name__ == '__main__': 82 test.main() 83