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 for_loops module.""" 16 17 from __future__ import absolute_import 18 from __future__ import division 19 from __future__ import print_function 20 21 from tensorflow.contrib.py2tf.converters import converter_test_base 22 from tensorflow.contrib.py2tf.converters import for_loops 23 from tensorflow.python.platform import test 24 25 26 class ControlFlowTest(converter_test_base.TestCase): 27 28 def test_basic_for(self): 29 30 def test_fn(l): 31 s = 0 32 for e in l: 33 s += e 34 return s 35 36 node = self.parse_and_analyze(test_fn, {}) 37 node = for_loops.transform(node, self.ctx) 38 39 with self.compiled(node) as result: 40 l = [1, 2, 3] 41 self.assertEqual(test_fn(l), result.test_fn(l)) 42 l = [] 43 self.assertEqual(test_fn(l), result.test_fn(l)) 44 45 46 if __name__ == '__main__': 47 test.main() 48