Home | History | Annotate | Download | only in converters
      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 list_comprehension 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 list_comprehension
     23 from tensorflow.python.platform import test
     24 
     25 
     26 class ListCompTest(converter_test_base.TestCase):
     27 
     28   def test_basic(self):
     29 
     30     def test_fn(l):
     31       s = [e * e for e in l]
     32       return s
     33 
     34     node = self.parse_and_analyze(test_fn, {})
     35     node = list_comprehension.transform(node, self.ctx)
     36 
     37     with self.compiled(node) as result:
     38       l = [1, 2, 3]
     39       self.assertEqual(test_fn(l), result.test_fn(l))
     40       l = []
     41       self.assertEqual(test_fn(l), result.test_fn(l))
     42 
     43   def test_multiple_generators(self):
     44 
     45     def test_fn(l):
     46       s = [e * e for sublist in l for e in sublist]
     47       return s
     48 
     49     node = self.parse_and_analyze(test_fn, {})
     50     node = list_comprehension.transform(node, self.ctx)
     51 
     52     with self.compiled(node) as result:
     53       l = [[1], [2], [3]]
     54       self.assertEqual(test_fn(l), result.test_fn(l))
     55       l = []
     56       self.assertEqual(test_fn(l), result.test_fn(l))
     57 
     58   def test_conds(self):
     59 
     60     def test_fn(l):
     61       s = [e * e for e in l if e > 1]
     62       return s
     63 
     64     node = self.parse_and_analyze(test_fn, {})
     65     node = list_comprehension.transform(node, self.ctx)
     66 
     67     with self.compiled(node) as result:
     68       l = [1, 2, 3]
     69       self.assertEqual(test_fn(l), result.test_fn(l))
     70       l = []
     71       self.assertEqual(test_fn(l), result.test_fn(l))
     72 
     73 
     74 if __name__ == '__main__':
     75   test.main()
     76