Repository navigation
Expand file tree
/
Copy pathnp_logic_test.py
More file actions
110 lines (94 loc) · 3.75 KB
/
Copy pathnp_logic_test.py
File metadata and controls
110 lines (94 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for tf numpy random number methods."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops.numpy_ops import np_array_ops
from tensorflow.python.ops.numpy_ops import np_arrays
from tensorflow.python.ops.numpy_ops import np_math_ops
from tensorflow.python.platform import test
class LogicTest(test.TestCase):
def setUp(self):
super(LogicTest, self).setUp()
self.array_transforms = [
lambda x: x, # Identity,
ops.convert_to_tensor,
np.array,
lambda x: np.array(x, dtype=np.int32),
lambda x: np.array(x, dtype=np.int64),
lambda x: np.array(x, dtype=np.float32),
lambda x: np.array(x, dtype=np.float64),
np_array_ops.array,
lambda x: np_array_ops.array(x, dtype=dtypes.int32),
lambda x: np_array_ops.array(x, dtype=dtypes.int64),
lambda x: np_array_ops.array(x, dtype=dtypes.float32),
lambda x: np_array_ops.array(x, dtype=dtypes.float64),
]
def testEqual(self):
def run_test(x1, x2=None):
if x2 is None:
x2 = x1
for fn1 in self.array_transforms:
for fn2 in self.array_transforms:
arg1 = fn1(x1)
arg2 = fn2(x2)
self.match(
np_math_ops.equal(arg1, arg2),
np.equal(
make_numpy_compatible(arg1), make_numpy_compatible(arg2)))
run_test(1)
run_test(1, 2)
run_test([1, 2])
run_test([1, 2, 3], [2])
run_test([[1, 2], [3, 4]], [1, 2])
run_test([[1, 2], [1, 4]], [1, 2])
run_test([1, 2], [[1, 2], [1, 4]])
run_test([[1, 2], [3, 4]], [[1, 2], [3, 4]])
run_test([[1, 2], [3, 4]], [[1, 3], [3, 4]])
def match_shape(self, actual, expected, msg=None):
if msg:
msg = 'Shape match failed for: {}. Expected: {} Actual: {}'.format(
msg, expected.shape, actual.shape)
self.assertEqual(actual.shape, expected.shape, msg=msg)
if msg:
msg = 'Shape: {} is not a tuple for {}'.format(actual.shape, msg)
self.assertIsInstance(actual.shape, tuple, msg=msg)
def match_dtype(self, actual, expected, msg=None):
if msg:
msg = 'Dtype match failed for: {}. Expected: {} Actual: {}.'.format(
msg, expected.dtype, actual.dtype)
self.assertEqual(actual.dtype, expected.dtype, msg=msg)
def match(self, actual, expected, msg=None):
msg_ = 'Expected: {} Actual: {}'.format(expected, actual)
if msg:
msg = '{} {}'.format(msg_, msg)
else:
msg = msg_
self.assertIsInstance(actual, np_arrays.ndarray)
self.match_dtype(actual, expected, msg)
self.match_shape(actual, expected, msg)
if not actual.shape:
self.assertEqual(actual.tolist(), expected.tolist())
else:
self.assertSequenceEqual(actual.tolist(), expected.tolist())
def make_numpy_compatible(s):
return s if not isinstance(s, np_arrays.ndarray) else s.data.numpy()
if __name__ == '__main__':
ops.enable_eager_execution()
test.main()