forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtf_stack_test.py
More file actions
121 lines (94 loc) · 3.54 KB
/
tf_stack_test.py
File metadata and controls
121 lines (94 loc) · 3.54 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
111
112
113
114
115
116
117
118
119
120
121
# Copyright 2019 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 functions used to extract and analyze stacks."""
import traceback
from tensorflow.python.platform import test
from tensorflow.python.util import tf_stack
class TFStackTest(test.TestCase):
def testFormatStackSelfConsistency(self):
# Both defined on the same line to produce identical stacks.
stacks = tf_stack.extract_stack(), traceback.extract_stack()
self.assertEqual(
traceback.format_list(stacks[0]), traceback.format_list(stacks[1]))
def testFrameSummaryEquality(self):
frames1 = tf_stack.extract_stack()
frames2 = tf_stack.extract_stack()
self.assertNotEqual(frames1[0], frames1[1])
self.assertEqual(frames1[0], frames1[0])
self.assertEqual(frames1[0], frames2[0])
def testFrameSummaryEqualityAndHash(self):
# Both defined on the same line to produce identical stacks.
frame1, frame2 = tf_stack.extract_stack(), tf_stack.extract_stack()
self.assertEqual(len(frame1), len(frame2))
for f1, f2 in zip(frame1, frame2):
self.assertEqual(f1, f2)
self.assertEqual(hash(f1), hash(f1))
self.assertEqual(hash(f1), hash(f2))
self.assertEqual(frame1, frame2)
self.assertEqual(hash(tuple(frame1)), hash(tuple(frame2)))
def testLastUserFrame(self):
trace = tf_stack.extract_stack() # COMMENT
frame = trace.last_user_frame()
self.assertRegex(frame.line, "# COMMENT")
def testGetUserFrames(self):
def func():
trace = tf_stack.extract_stack() # COMMENT
frames = list(trace.get_user_frames())
return frames
frames = func() # CALLSITE
self.assertRegex(frames[-1].line, "# COMMENT")
self.assertRegex(frames[-2].line, "# CALLSITE")
def testGelItem(self):
def func(n):
if n == 0:
return tf_stack.extract_stack() # COMMENT
else:
return func(n - 1)
trace = func(5)
self.assertIn("COMMENT", trace[-1].line)
with self.assertRaises(IndexError):
_ = trace[-len(trace) - 1]
with self.assertRaises(IndexError):
_ = trace[len(trace)]
def testDelItem(self):
def func(n):
if n == 0:
return tf_stack.extract_stack() # COMMENT
else:
return func(n - 1)
# Test deleting a slice.
trace = func(5)
self.assertGreater(len(trace), 5)
full_list = list(trace)
del trace[-5:]
head_list = list(trace)
self.assertLen(head_list, len(full_list) - 5)
self.assertEqual(head_list, full_list[:-5])
# Test deleting an item.
trace = func(1)
self.assertGreater(len(trace), 1)
full_list = list(trace)
del trace[-1]
head_list = list(trace)
self.assertLen(head_list, len(full_list) - 1)
self.assertEqual(head_list, full_list[:-1])
# Errors
trace = func(5)
with self.assertRaises(IndexError):
del trace[-len(trace) - 1]
with self.assertRaises(IndexError):
del trace[len(trace)]
if __name__ == "__main__":
test.main()