Best Python code snippet using hypothesis
debug_data_test.py
Source:debug_data_test.py
1# Copyright 2016 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 at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14# ==============================================================================15"""Tests for tfdbg module debug_data."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19import os20import platform21import shutil22import tempfile23import numpy as np24from tensorflow.core.framework import graph_pb225from tensorflow.core.framework import tensor_pb226from tensorflow.python.debug.lib import debug_data27from tensorflow.python.framework import test_util28from tensorflow.python.platform import gfile29from tensorflow.python.platform import googletest30from tensorflow.python.platform import test31class DeviceNamePathConversionTest(test_util.TensorFlowTestCase):32 def testDeviceNameToDevicePath(self):33 self.assertEqual(34 debug_data.METADATA_FILE_PREFIX + debug_data.DEVICE_TAG +35 ",job_ps,replica_1,task_2,cpu_0",36 debug_data.device_name_to_device_path("/job:ps/replica:1/task:2/cpu:0"))37 def testDevicePathToDeviceName(self):38 self.assertEqual(39 "/job:ps/replica:1/task:2/cpu:0",40 debug_data.device_path_to_device_name(41 debug_data.METADATA_FILE_PREFIX + debug_data.DEVICE_TAG +42 ",job_ps,replica_1,task_2,cpu_0"))43class ParseNodeOrTensorNameTest(test_util.TensorFlowTestCase):44 def testParseNodeName(self):45 node_name, slot = debug_data.parse_node_or_tensor_name("namespace1/node_1")46 self.assertEqual("namespace1/node_1", node_name)47 self.assertIsNone(slot)48 def testParseTensorName(self):49 node_name, slot = debug_data.parse_node_or_tensor_name(50 "namespace1/node_2:3")51 self.assertEqual("namespace1/node_2", node_name)52 self.assertEqual(3, slot)53class NodeNameChecksTest(test_util.TensorFlowTestCase):54 def testIsCopyNode(self):55 self.assertTrue(debug_data.is_copy_node("__copy_ns1/ns2/node3_0"))56 self.assertFalse(debug_data.is_copy_node("copy_ns1/ns2/node3_0"))57 self.assertFalse(debug_data.is_copy_node("_copy_ns1/ns2/node3_0"))58 self.assertFalse(debug_data.is_copy_node("_copyns1/ns2/node3_0"))59 self.assertFalse(debug_data.is_copy_node("__dbg_ns1/ns2/node3_0"))60 def testIsDebugNode(self):61 self.assertTrue(62 debug_data.is_debug_node("__dbg_ns1/ns2/node3:0_0_DebugIdentity"))63 self.assertFalse(64 debug_data.is_debug_node("dbg_ns1/ns2/node3:0_0_DebugIdentity"))65 self.assertFalse(66 debug_data.is_debug_node("_dbg_ns1/ns2/node3:0_0_DebugIdentity"))67 self.assertFalse(68 debug_data.is_debug_node("_dbgns1/ns2/node3:0_0_DebugIdentity"))69 self.assertFalse(debug_data.is_debug_node("__copy_ns1/ns2/node3_0"))70class ParseDebugNodeNameTest(test_util.TensorFlowTestCase):71 def testParseDebugNodeName_valid(self):72 debug_node_name_1 = "__dbg_ns_a/ns_b/node_c:1_0_DebugIdentity"73 (watched_node, watched_output_slot, debug_op_index,74 debug_op) = debug_data.parse_debug_node_name(debug_node_name_1)75 self.assertEqual("ns_a/ns_b/node_c", watched_node)76 self.assertEqual(1, watched_output_slot)77 self.assertEqual(0, debug_op_index)78 self.assertEqual("DebugIdentity", debug_op)79 def testParseDebugNodeName_invalidPrefix(self):80 invalid_debug_node_name_1 = "__copy_ns_a/ns_b/node_c:1_0_DebugIdentity"81 with self.assertRaisesRegexp(ValueError, "Invalid prefix"):82 debug_data.parse_debug_node_name(invalid_debug_node_name_1)83 def testParseDebugNodeName_missingDebugOpIndex(self):84 invalid_debug_node_name_1 = "__dbg_node1:0_DebugIdentity"85 with self.assertRaisesRegexp(ValueError, "Invalid debug node name"):86 debug_data.parse_debug_node_name(invalid_debug_node_name_1)87 def testParseDebugNodeName_invalidWatchedTensorName(self):88 invalid_debug_node_name_1 = "__dbg_node1_0_DebugIdentity"89 with self.assertRaisesRegexp(ValueError,90 "Invalid tensor name in debug node name"):91 debug_data.parse_debug_node_name(invalid_debug_node_name_1)92class HasNanOrInfTest(test_util.TensorFlowTestCase):93 def setUp(self):94 self._dummy_datum = dummy_datum = debug_data.DebugTensorDatum(95 "/foo", "bar_0_DebugIdentity_42")96 def testNaN(self):97 a = np.array([np.nan, np.nan, 7.0])98 self.assertTrue(debug_data.has_inf_or_nan(self._dummy_datum, a))99 def testInf(self):100 a = np.array([np.inf, np.inf, 7.0])101 self.assertTrue(debug_data.has_inf_or_nan(self._dummy_datum, a))102 def testNanAndInf(self):103 a = np.array([np.inf, np.nan, 7.0])104 self.assertTrue(debug_data.has_inf_or_nan(self._dummy_datum, a))105 def testNoNanOrInf(self):106 a = np.array([0.0, 0.0, 7.0])107 self.assertFalse(debug_data.has_inf_or_nan(self._dummy_datum, a))108 def testEmpty(self):109 a = np.array([])110 self.assertFalse(debug_data.has_inf_or_nan(self._dummy_datum, a))111 def testInconvertibleTensorProto(self):112 self.assertFalse(debug_data.has_inf_or_nan(113 self._dummy_datum,114 debug_data.InconvertibleTensorProto(tensor_pb2.TensorProto(),115 initialized=False)))116 self.assertFalse(debug_data.has_inf_or_nan(117 self._dummy_datum,118 debug_data.InconvertibleTensorProto(tensor_pb2.TensorProto(),119 initialized=True)))120 def testDTypeComplexWorks(self):121 a = np.array([1j, 3j, 3j, 7j], dtype=np.complex128)122 self.assertFalse(debug_data.has_inf_or_nan(self._dummy_datum, a))123 b = np.array([1j, 3j, 3j, 7j, np.nan], dtype=np.complex128)124 self.assertTrue(debug_data.has_inf_or_nan(self._dummy_datum, b))125 def testDTypeIntegerWorks(self):126 a = np.array([1, 3, 3, 7], dtype=np.int16)127 self.assertFalse(debug_data.has_inf_or_nan(self._dummy_datum, a))128 def testDTypeStringGivesFalse(self):129 """isnan and isinf are not applicable to strings."""130 a = np.array(["s", "p", "a", "m"])131 self.assertFalse(debug_data.has_inf_or_nan(self._dummy_datum, a))132 def testDTypeObjectGivesFalse(self):133 dt = np.dtype([("spam", np.str_, 16), ("eggs", np.float64, (2,))])134 a = np.array([("spam", (8.0, 7.0)), ("eggs", (6.0, 5.0))], dtype=dt)135 self.assertFalse(debug_data.has_inf_or_nan(self._dummy_datum, a))136class DebugTensorDatumTest(test_util.TensorFlowTestCase):137 def testDebugDatum(self):138 dump_root = "/tmp/tfdbg_1"139 debug_dump_rel_path = (140 debug_data.METADATA_FILE_PREFIX + debug_data.DEVICE_TAG +141 ",job_localhost,replica_0,task_0,cpu_0" +142 "/ns1/ns2/node_a_1_2_DebugIdentity_1472563253536385")143 datum = debug_data.DebugTensorDatum(dump_root, debug_dump_rel_path)144 self.assertEqual("DebugIdentity", datum.debug_op)145 self.assertEqual("ns1/ns2/node_a_1", datum.node_name)146 self.assertEqual(2, datum.output_slot)147 self.assertEqual("ns1/ns2/node_a_1:2", datum.tensor_name)148 self.assertEqual(1472563253536385, datum.timestamp)149 self.assertEqual("ns1/ns2/node_a_1:2:DebugIdentity", datum.watch_key)150 self.assertEqual(151 os.path.join(dump_root, debug_dump_rel_path), datum.file_path)152 self.assertEqual(153 "{DebugTensorDatum (/job:localhost/replica:0/task:0/cpu:0) "154 "%s:%d @ %s @ %d}" % (datum.node_name,155 datum.output_slot,156 datum.debug_op,157 datum.timestamp), str(datum))158 self.assertEqual(159 "{DebugTensorDatum (/job:localhost/replica:0/task:0/cpu:0) "160 "%s:%d @ %s @ %d}" % (datum.node_name,161 datum.output_slot,162 datum.debug_op,163 datum.timestamp), repr(datum))164 def testDumpSizeBytesIsNoneForNonexistentFilePath(self):165 dump_root = "/tmp/tfdbg_1"166 debug_dump_rel_path = "ns1/ns2/node_foo_1_2_DebugIdentity_1472563253536385"167 datum = debug_data.DebugTensorDatum(dump_root, debug_dump_rel_path)168 self.assertIsNone(datum.dump_size_bytes)169class DebugDumpDirTest(test_util.TensorFlowTestCase):170 def setUp(self):171 self._dump_root = tempfile.mktemp()172 os.mkdir(self._dump_root)173 def tearDown(self):174 # Tear down temporary dump directory.175 shutil.rmtree(self._dump_root)176 def _makeDataDirWithMultipleDevicesAndDuplicateNodeNames(self):177 cpu_0_dir = os.path.join(178 self._dump_root,179 debug_data.METADATA_FILE_PREFIX + debug_data.DEVICE_TAG +180 ",job_localhost,replica_0,task_0,cpu_0")181 gpu_0_dir = os.path.join(182 self._dump_root,183 debug_data.METADATA_FILE_PREFIX + debug_data.DEVICE_TAG +184 ",job_localhost,replica_0,task_0,gpu_0")185 gpu_1_dir = os.path.join(186 self._dump_root,187 debug_data.METADATA_FILE_PREFIX + debug_data.DEVICE_TAG +188 ",job_localhost,replica_0,task_0,gpu_1")189 os.makedirs(cpu_0_dir)190 os.makedirs(gpu_0_dir)191 os.makedirs(gpu_1_dir)192 open(os.path.join(193 cpu_0_dir, "node_foo_1_2_DebugIdentity_1472563253536386"), "wb")194 open(os.path.join(195 gpu_0_dir, "node_foo_1_2_DebugIdentity_1472563253536385"), "wb")196 open(os.path.join(197 gpu_1_dir, "node_foo_1_2_DebugIdentity_1472563253536387"), "wb")198 def testDebugDumpDir_nonexistentDumpRoot(self):199 with self.assertRaisesRegexp(IOError, "does not exist"):200 debug_data.DebugDumpDir(tempfile.mktemp() + "_foo")201 def testDebugDumpDir_invalidFileNamingPattern(self):202 # File name with too few underscores should lead to an exception.203 device_dir = os.path.join(204 self._dump_root,205 debug_data.METADATA_FILE_PREFIX + debug_data.DEVICE_TAG +206 ",job_localhost,replica_0,task_0,cpu_0")207 os.makedirs(device_dir)208 open(os.path.join(device_dir, "node1_DebugIdentity_1234"), "wb")209 with self.assertRaisesRegexp(ValueError,210 "does not conform to the naming pattern"):211 debug_data.DebugDumpDir(self._dump_root)212 def testDebugDumpDir_validDuplicateNodeNamesWithMultipleDevices(self):213 self._makeDataDirWithMultipleDevicesAndDuplicateNodeNames()214 graph_cpu_0 = graph_pb2.GraphDef()215 node = graph_cpu_0.node.add()216 node.name = "node_foo_1"217 node.op = "FooOp"218 node.device = "/job:localhost/replica:0/task:0/cpu:0"219 graph_gpu_0 = graph_pb2.GraphDef()220 node = graph_gpu_0.node.add()221 node.name = "node_foo_1"222 node.op = "FooOp"223 node.device = "/job:localhost/replica:0/task:0/gpu:0"224 graph_gpu_1 = graph_pb2.GraphDef()225 node = graph_gpu_1.node.add()226 node.name = "node_foo_1"227 node.op = "FooOp"228 node.device = "/job:localhost/replica:0/task:0/gpu:1"229 dump_dir = debug_data.DebugDumpDir(230 self._dump_root,231 partition_graphs=[graph_cpu_0, graph_gpu_0, graph_gpu_1])232 self.assertItemsEqual(233 ["/job:localhost/replica:0/task:0/cpu:0",234 "/job:localhost/replica:0/task:0/gpu:0",235 "/job:localhost/replica:0/task:0/gpu:1"], dump_dir.devices())236 self.assertEqual(1472563253536385, dump_dir.t0)237 self.assertEqual(3, dump_dir.size)238 with self.assertRaisesRegexp(239 ValueError, r"Invalid device name: "):240 dump_dir.nodes("/job:localhost/replica:0/task:0/gpu:2")241 self.assertItemsEqual(["node_foo_1", "node_foo_1", "node_foo_1"],242 dump_dir.nodes())243 self.assertItemsEqual(244 ["node_foo_1"],245 dump_dir.nodes(device_name="/job:localhost/replica:0/task:0/cpu:0"))246 def testDuplicateNodeNamesInGraphDefOfSingleDeviceRaisesException(self):247 self._makeDataDirWithMultipleDevicesAndDuplicateNodeNames()248 graph_cpu_0 = graph_pb2.GraphDef()249 node = graph_cpu_0.node.add()250 node.name = "node_foo_1"251 node.op = "FooOp"252 node.device = "/job:localhost/replica:0/task:0/cpu:0"253 graph_gpu_0 = graph_pb2.GraphDef()254 node = graph_gpu_0.node.add()255 node.name = "node_foo_1"256 node.op = "FooOp"257 node.device = "/job:localhost/replica:0/task:0/gpu:0"258 graph_gpu_1 = graph_pb2.GraphDef()259 node = graph_gpu_1.node.add()260 node.name = "node_foo_1"261 node.op = "FooOp"262 node.device = "/job:localhost/replica:0/task:0/gpu:1"263 node = graph_gpu_1.node.add() # Here is the duplicate.264 node.name = "node_foo_1"265 node.op = "FooOp"266 node.device = "/job:localhost/replica:0/task:0/gpu:1"267 with self.assertRaisesRegexp(268 ValueError, r"Duplicate node name on device "):269 debug_data.DebugDumpDir(270 self._dump_root,271 partition_graphs=[graph_cpu_0, graph_gpu_0, graph_gpu_1])272 def testDebugDumpDir_emptyDumpDir(self):273 dump_dir = debug_data.DebugDumpDir(self._dump_root)274 self.assertIsNone(dump_dir.t0)275 self.assertEqual([], dump_dir.dumped_tensor_data)276 def testDebugDumpDir_usesGfileGlob(self):277 if platform.system() == "Windows":278 self.skipTest("gfile.Glob is not used on Windows.")279 self._makeDataDirWithMultipleDevicesAndDuplicateNodeNames()280 def fake_gfile_glob(glob_pattern):281 del glob_pattern282 return []283 with test.mock.patch.object(284 gfile, "Glob", side_effect=fake_gfile_glob, autospec=True) as fake:285 debug_data.DebugDumpDir(self._dump_root)286 expected_calls = [287 test.mock.call(os.path.join(288 self._dump_root,289 (debug_data.METADATA_FILE_PREFIX +290 debug_data.CORE_METADATA_TAG + "*"))),291 test.mock.call(os.path.join(292 self._dump_root,293 (debug_data.METADATA_FILE_PREFIX +294 debug_data.FETCHES_INFO_FILE_TAG + "*"))),295 test.mock.call(os.path.join(296 self._dump_root,297 (debug_data.METADATA_FILE_PREFIX +298 debug_data.FEED_KEYS_INFO_FILE_TAG + "*"))),299 test.mock.call(os.path.join(300 self._dump_root,301 (debug_data.METADATA_FILE_PREFIX +302 debug_data.DEVICE_TAG + "*")))]303 fake.assert_has_calls(expected_calls, any_order=True)304class GetNodeNameAndOutputSlotTest(test_util.TensorFlowTestCase):305 def testParseTensorNameInputWorks(self):306 self.assertEqual("a", debug_data.get_node_name("a:0"))307 self.assertEqual(0, debug_data.get_output_slot("a:0"))308 self.assertEqual("_b", debug_data.get_node_name("_b:1"))309 self.assertEqual(1, debug_data.get_output_slot("_b:1"))310 def testParseNodeNameInputWorks(self):311 self.assertEqual("a", debug_data.get_node_name("a"))312 self.assertEqual(0, debug_data.get_output_slot("a"))313if __name__ == "__main__":...
monitoring.py
Source:monitoring.py
1import time, threading2from struct import *3from pymodbus.client.sync import ModbusTcpClient45class debug_var():6 name = ''7 location = ''8 type = ''9 forced = 'No'10 value = 01112debug_vars = []13monitor_active = False14mb_client = None1516def parse_st(st_file):17 global debug_vars18 filepath = './st_files/' + st_file19 20 st_program = open(filepath, 'r')21 22 for line in st_program.readlines():23 if line.find(' AT ') > 0 and line.find('%') > 0 and line.find('(*') < 0 and line.find('*)') < 0:24 debug_data = debug_var()25 tmp = line.strip().split(' ')26 debug_data.name = tmp[0]27 debug_data.location = tmp[2]28 debug_data.type = tmp[4].split(';')[0]29 30 #don't add special functions (%ML1024 and up) as they are not accessible31 if (debug_data.location.find('ML')) > 0:32 mb_address = debug_data.location.split('%ML')[1]33 if (int(mb_address) < 1024):34 debug_vars.append(debug_data)35 else:36 debug_vars.append(debug_data)37 38 for debugs in debug_vars:39 print('Name: ' + debugs.name)40 print('Location: ' + debugs.location)41 print('Type: ' + debugs.type)42 print('')434445def cleanup():46 del debug_vars[:]47 48def modbus_monitor():49 global mb_client50 for debug_data in debug_vars:51 if (debug_data.location.find('IX')) > 0:52 #Reading Input Status53 mb_address = debug_data.location.split('%IX')[1].split('.')54 result = mb_client.read_discrete_inputs(int(mb_address[0])*8 + int(mb_address[1]), 1)55 debug_data.value = result.bits[0]56 57 elif (debug_data.location.find('QX')) > 0:58 #Reading Coils59 mb_address = debug_data.location.split('%QX')[1].split('.')60 if (len(mb_address) < 2):61 result = mb_client.read_coils(int(mb_address[0])*8, 1)62 else:63 result = mb_client.read_coils(int(mb_address[0])*8 + int(mb_address[1]), 1)64 debug_data.value = result.bits[0]65 66 elif (debug_data.location.find('IW')) > 0:67 #Reading Input Registers68 mb_address = debug_data.location.split('%IW')[1]69 result = mb_client.read_input_registers(int(mb_address), 1)70 debug_data.value = result.registers[0]71 72 elif (debug_data.location.find('QW')) > 0:73 #Reading Holding Registers74 mb_address = debug_data.location.split('%QW')[1]75 result = mb_client.read_holding_registers(int(mb_address), 1)76 debug_data.value = result.registers[0]77 78 elif (debug_data.location.find('MW')) > 0:79 #Reading Word Memory80 mb_address = debug_data.location.split('%MW')[1]81 result = mb_client.read_holding_registers(int(mb_address) + 1024, 1)82 debug_data.value = result.registers[0]83 84 elif (debug_data.location.find('MD')) > 0:85 #Reading Double Memory86 mb_address = debug_data.location.split('%MD')[1]87 result = mb_client.read_holding_registers((int(mb_address)*2) + 2048, 2)88 if (debug_data.type == 'SINT') or (debug_data.type == 'INT') or (debug_data.type == 'DINT'):89 #signed integer90 float_pack = pack('>HH', result.registers[0], result.registers[1])91 debug_data.value = unpack('>i', float_pack)[0]92 93 if (debug_data.type == 'USINT') or (debug_data.type == 'UINT') or (debug_data.type == 'UDINT'):94 #unsigned integer95 float_pack = pack('>HH', result.registers[0], result.registers[1])96 debug_data.value = unpack('>I', float_pack)[0]97 98 if (debug_data.type == 'REAL'):99 #32-bit float100 float_pack = pack('>HH', result.registers[0], result.registers[1])101 debug_data.value = unpack('>f', float_pack)[0]102 103 elif (debug_data.location.find('ML')) > 0:104 #Reading Long Memory105 mb_address = debug_data.location.split('%ML')[1]106 result = mb_client.read_holding_registers((int(mb_address)*4) + 4096, 4)107 if (debug_data.type == 'SINT') or (debug_data.type == 'INT') or (debug_data.type == 'DINT') or (debug_data.type == 'LINT'):108 #signed integer109 float_pack = pack('>HHHH', result.registers[0], result.registers[1], result.registers[2], result.registers[3])110 debug_data.value = unpack('>q', float_pack)[0]111 112 if (debug_data.type == 'USINT') or (debug_data.type == 'UINT') or (debug_data.type == 'UDINT') or (debug_data.type == 'ULINT'):113 #unsigned integer114 float_pack = pack('>HHHH', result.registers[0], result.registers[1], result.registers[2], result.registers[3])115 debug_data.value = unpack('>Q', float_pack)[0]116 117 if (debug_data.type == 'REAL') or (debug_data.type == 'LREAL'):118 #64-bit float119 float_pack = pack('>HHHH', result.registers[0], result.registers[1], result.registers[2], result.registers[3])120 debug_data.value = unpack('>d', float_pack)[0]121 122 123 if (monitor_active == True):124 threading.Timer(0.5, modbus_monitor).start()125 126def start_monitor(modbus_port_cfg):127 global monitor_active128 global mb_client129 130 if (monitor_active != True):131 monitor_active = True132 mb_client = ModbusTcpClient('127.0.0.1', port=modbus_port_cfg)133 134 modbus_monitor()135136def stop_monitor():137 global monitor_active138 global mb_client139 140 if (monitor_active != False):141 monitor_active = False
...
test_1.py
Source:test_1.py
1def test_after_sessionstart(status_plugin_debug):2 debug_data = status_plugin_debug["after_sessionstart"]3 assert debug_data["collect"] == []4 assert debug_data["pass"] == []5 assert debug_data["fail"] == []6 assert debug_data["skip"] == []7 assert debug_data["state"] == "start"8def test_after_first_collectstart(status_plugin_debug):9 debug_data = status_plugin_debug["after_first_collectstart"]10 assert debug_data["collect"] == []11 assert debug_data["pass"] == []12 assert debug_data["fail"] == []13 assert debug_data["skip"] == []14 assert debug_data["state"] == "collect"15def test_after_last_itemcollected(status_plugin_debug):16 debug_data = status_plugin_debug["after_last_itemcollected"]17 # if the tests in this file change, adjust this list too18 assert debug_data["collect"] == ["test_after_sessionstart",19 "test_after_first_collectstart",20 "test_after_last_itemcollected",21 "test_after_first_runtest_logstart",22 "test_after_this_runtest_logstart"]23 assert debug_data["pass"] == []24 assert debug_data["fail"] == []25 assert debug_data["skip"] == []26 assert debug_data["state"] == "collect"27def test_after_first_runtest_logstart(status_plugin_debug):28 debug_data = status_plugin_debug["after_first_runtest_logstart"]29 assert debug_data["pass"] == []30 assert debug_data["fail"] == []31 assert debug_data["skip"] == []32 assert debug_data["state"] == "runtest"33def test_after_this_runtest_logstart(status_plugin_debug):34 debug_data = status_plugin_debug["after_this_runtest_logstart"]35 assert len(debug_data["pass"] + debug_data["fail"] + debug_data["skip"]) > 0...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!