1# Copyright 2019 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 VariableSpec.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python.framework import constant_op 22from tensorflow.python.framework import dtypes 23from tensorflow.python.framework import tensor_shape 24from tensorflow.python.ops import resource_variable_ops 25from tensorflow.python.platform import test 26 27VariableSpec = resource_variable_ops.VariableSpec 28 29 30class VariableSpecTest(test.TestCase): 31 32 def test_properties(self): 33 spec = VariableSpec(shape=(1, 2, 3), dtype=dtypes.float64, name='vs') 34 self.assertEqual('vs', spec.name) 35 self.assertEqual(tensor_shape.TensorShape((1, 2, 3)), spec.shape) 36 self.assertEqual(dtypes.float64, spec.dtype) 37 38 def test_compatibility(self): 39 spec = VariableSpec(shape=None) 40 spec2 = VariableSpec(shape=[None, 15]) 41 spec3 = VariableSpec(shape=[None]) 42 43 self.assertTrue(spec.is_compatible_with(spec2)) 44 self.assertFalse(spec2.is_compatible_with(spec3)) 45 46 var = resource_variable_ops.UninitializedVariable( 47 shape=[3, 15], dtype=dtypes.float32) 48 var2 = resource_variable_ops.UninitializedVariable( 49 shape=[3], dtype=dtypes.int32) 50 51 self.assertTrue(spec2.is_compatible_with(var)) 52 self.assertFalse(spec3.is_compatible_with(var2)) 53 54 spec4 = VariableSpec(shape=None, dtype=dtypes.int32) 55 spec5 = VariableSpec(shape=[None], dtype=dtypes.int32) 56 57 self.assertFalse(spec.is_compatible_with(spec4)) 58 self.assertTrue(spec4.is_compatible_with(spec5)) 59 self.assertTrue(spec4.is_compatible_with(var2)) 60 61 tensor = constant_op.constant([1, 2, 3]) 62 self.assertFalse(spec4.is_compatible_with(tensor)) 63 64 65if __name__ == '__main__': 66 test.main() 67