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 @test_util.run_deprecated_v1 31 def test_equals(self): 32 33 def test_fn(a, b): 34 return a == b 35 36 with self.converted(test_fn, logical_expressions, {}) as result: 37 with self.cached_session() as sess: 38 self.assertTrue(sess.run(result.test_fn(constant_op.constant(1), 1))) 39 self.assertFalse(sess.run(result.test_fn(constant_op.constant(1), 2))) 40 41 @test_util.run_deprecated_v1 42 def test_bool_ops(self): 43 44 def test_fn(a, b, c): 45 return (a or b) and (a or b or c) and not c 46 47 with self.converted(test_fn, logical_expressions, {}) as result: 48 with self.cached_session() as sess: 49 self.assertTrue( 50 sess.run(result.test_fn(constant_op.constant(True), False, False))) 51 self.assertFalse( 52 sess.run(result.test_fn(constant_op.constant(True), False, True))) 53 54 @test_util.run_deprecated_v1 55 def test_comparison(self): 56 57 def test_fn(a, b, c, d): 58 return a < b == c > d 59 60 with self.converted(test_fn, logical_expressions, {}) as result: 61 with self.cached_session() as sess: 62 # Note: having just the first constant a tensor tests that the 63 # operations execute in the correct order. If anything other than 64 # a < b executed first, the result would be a Python scalar and not a 65 # Tensor. This is valid as long as the dispat is automatic based on 66 # type. 67 self.assertTrue( 68 sess.run(result.test_fn(constant_op.constant(1), 2, 2, 1))) 69 self.assertFalse( 70 sess.run(result.test_fn(constant_op.constant(1), 2, 2, 3))) 71 72 def test_default_ops(self): 73 74 def test_fn(a, b): 75 return a in b 76 77 with self.converted(test_fn, logical_expressions, {}) as result: 78 self.assertTrue(result.test_fn('a', ('a',))) 79 80 def test_unary_ops(self): 81 def test_fn(a): 82 return ~a, -a, +a 83 84 with self.converted(test_fn, logical_expressions, {}) as result: 85 self.assertEqual(result.test_fn(1), (-2, -1, 1)) 86 87 88if __name__ == '__main__': 89 test.main() 90