• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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"""Testing utilities for tfdbg command-line interface."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import re
21
22import numpy as np
23
24
25def assert_lines_equal_ignoring_whitespace(test, expected_lines, actual_lines):
26  """Assert equality in lines, ignoring all whitespace.
27
28  Args:
29    test: An instance of unittest.TestCase or its subtypes (e.g.,
30      TensorFlowTestCase).
31    expected_lines: Expected lines as an iterable of strings.
32    actual_lines: Actual lines as an iterable of strings.
33  """
34  test.assertEqual(
35      len(expected_lines), len(actual_lines),
36      "Mismatch in the number of lines: %d vs %d" % (
37          len(expected_lines), len(actual_lines)))
38  for expected_line, actual_line in zip(expected_lines, actual_lines):
39    test.assertEqual("".join(expected_line.split()),
40                     "".join(actual_line.split()))
41
42
43# Regular expression for separators between values in a string representation
44# of an ndarray, exclusing whitespace.
45_ARRAY_VALUE_SEPARATOR_REGEX = re.compile(r"(array|\(|\[|\]|\)|\||,)")
46
47
48def assert_array_lines_close(test, expected_array, array_lines):
49  """Assert that the array value represented by lines is close to expected.
50
51  Note that the shape of the array represented by the `array_lines` is ignored.
52
53  Args:
54    test: An instance of TensorFlowTestCase.
55    expected_array: Expected value of the array.
56    array_lines: A list of strings representing the array.
57      E.g., "array([[ 1.0, 2.0 ], [ 3.0, 4.0 ]])"
58      Assumes that values are separated by commas, parentheses, brackets, "|"
59      characters and whitespace.
60  """
61  elements = []
62  for line in array_lines:
63    line = re.sub(_ARRAY_VALUE_SEPARATOR_REGEX, " ", line)
64    elements.extend(float(s) for s in line.split())
65  test.assertAllClose(np.array(expected_array).flatten(), elements)
66