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 __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python.autograph.converters import logical_expressions 22from tensorflow.python.autograph.core import converter_testing 23from tensorflow.python.framework import constant_op 24from tensorflow.python.framework import test_util 25from tensorflow.python.platform import test 26 27 28class LogicalExpressionTest(converter_testing.TestCase): 29 30 def test_equals(self): 31 32 def f(a, b): 33 return a == b 34 35 tr = self.transform(f, logical_expressions) 36 37 self.assertTrue(self.evaluate(tr(constant_op.constant(1), 1))) 38 self.assertFalse(self.evaluate(tr(constant_op.constant(1), 2))) 39 40 @test_util.run_deprecated_v1 41 def test_bool_ops(self): 42 43 def f(a, b, c): 44 return (a or b) and (a or b or c) and not c 45 46 tr = self.transform(f, logical_expressions) 47 48 self.assertTrue(self.evaluate(tr(constant_op.constant(True), False, False))) 49 self.assertFalse(self.evaluate(tr(constant_op.constant(True), False, True))) 50 51 def test_comparison(self): 52 53 def f(a, b, c, d): 54 return a < b == c > d 55 56 tr = self.transform(f, logical_expressions) 57 58 # Note: having just the first constant a tensor tests that the 59 # operations execute in the correct order. If anything other than 60 # a < b executed first, the result would be a Python scalar and not a 61 # Tensor. This is valid as long as the dispat is automatic based on 62 # type. 63 self.assertTrue(self.evaluate(tr(constant_op.constant(1), 2, 2, 1))) 64 self.assertFalse(self.evaluate(tr(constant_op.constant(1), 2, 2, 3))) 65 66 def test_default_ops(self): 67 68 def f(a, b): 69 return a in b 70 71 tr = self.transform(f, logical_expressions) 72 73 self.assertTrue(tr('a', ('a',))) 74 75 def test_unary_ops(self): 76 77 def f(a): 78 return ~a, -a, +a 79 80 tr = self.transform(f, logical_expressions) 81 82 self.assertEqual(tr(1), (-2, -1, 1)) 83 84 85if __name__ == '__main__': 86 test.main() 87