Best Python code snippet using pytest-django_python
memory_leak_tests.py
Source:memory_leak_tests.py
1# Python modules2import gc3import os4import subprocess5import random6import re7import sys8# My module9import sysv_ipc10SKIP_SEMAPHORE_TESTS = False11SKIP_SHARED_MEMORY_TESTS = False12SKIP_MESSAGE_QUEUE_TESTS = False13# TEST_COUNT = 1014TEST_COUNT = 1024 * 10015# ps output looks like this:16# RSZ VSZ17# 944 7596418ps_output_regex = re.compile(r"""19 ^20 \s* # whitespace before first heading21 \S* # first heading (e.g. RSZ)22 \s+ # whitespace between headings23 \S* # second heading (e.g VSZ)24 \s+ # newline and whitespace before first numeric value25 (\d+) # first value26 \s+ # whitespace between values27 (\d+) # second value28 \s* # trailing whitespace if any29 $30""", re.MULTILINE | re.VERBOSE)31# On OS X, Ubuntu and OpenSolaris, both create/destroy tests show some growth32# is rsz and vsz. (e.g. 3248 versus 3240 -- I guess these are measured33# in kilobytes?) When I increased the number of iterations by a factor of 10,34# the delta didn't change which makes me think it isn't an actual leak35# but just some memory consumed under normal circumstances.36def random_string(length):37 return ''.join(random.sample("abcdefghijklmnopqrstuvwxyz", length))38def print_mem_before():39 s = "Memory usage before, RSS = %d, VSZ = %d" % get_memory_usage()40 print(s)41def print_mem_after():42 gc.collect()43 if gc.garbage:44 s = "Leftover garbage:" + str(gc.garbage)45 print(s)46 else:47 print("Python's GC reports no leftover garbage")48 s = "Memory usage after, RSS = %d, VSZ = %d" % get_memory_usage()49 print(s)50def get_memory_usage():51 # `ps` has lots of format options that vary from OS to OS, and some of52 # those options have aliases (e.g. vsz, vsize). The ones I use below53 # appear to be the most portable.54 s = subprocess.Popen(["ps", "-p", str(os.getpid()), "-o", "rss,vsz"],55 stdout=subprocess.PIPE).communicate()[0]56 # Output looks like this:57 # RSZ VSZ58 # 944 7596459 s = s.decode(sys.getfilesystemencoding())60 m = ps_output_regex.match(s)61 rsz = int(m.groups()[0])62 vsz = int(m.groups()[1])63 return rsz, vsz64# Assert manual control over the garbage collector65gc.disable()66if SKIP_SEMAPHORE_TESTS:67 print("Skipping semaphore tests")68else:69 print("Running semaphore create/destroy test...")70 print_mem_before()71 for i in range(1, TEST_COUNT):72 sem = sysv_ipc.Semaphore(None, sysv_ipc.IPC_CREX)73 sem.remove()74 print_mem_after()75 print("Running semaphore acquire/release test...")76 print_mem_before()77 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)78 for i in range(1, TEST_COUNT):79 sem.release()80 sem.acquire()81 sem.remove()82 print_mem_after()83 print("Running semaphore Z test...")84 print_mem_before()85 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)86 for i in range(1, TEST_COUNT):87 sem.Z()88 sem.remove()89 print_mem_after()90 if sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED:91 print("Running semaphore acquire timeout test...")92 print_mem_before()93 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)94 for i in range(1, TEST_COUNT):95 try:96 sem.acquire(.001)97 except sysv_ipc.BusyError:98 pass99 sem.remove()100 print_mem_after()101 else:102 print("Skipping semaphore acquire timeout test (not supported on this platform)")103 if sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED:104 print("Running semaphore Z timeout test...")105 print_mem_before()106 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)107 # Release the semaphore to make the value is non-zero so that .Z()108 # has to wait for the timeout.109 sem.release()110 for i in range(1, TEST_COUNT):111 try:112 sem.Z(.001)113 except sysv_ipc.BusyError:114 pass115 sem.remove()116 print_mem_after()117 else:118 print("Skipping semaphore Z timeout test (not supported on this platform)")119 print("Running semaphore key read test...")120 print_mem_before()121 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)122 for i in range(1, TEST_COUNT):123 foo = sem.key124 sem.remove()125 print_mem_after()126 print("Running semaphore id read test...")127 print_mem_before()128 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)129 for i in range(1, TEST_COUNT):130 foo = sem.id131 sem.remove()132 print_mem_after()133 print("Running semaphore value read test...")134 print_mem_before()135 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)136 for i in range(1, TEST_COUNT):137 foo = sem.value138 sem.remove()139 print_mem_after()140 print("Running semaphore value write test...")141 print_mem_before()142 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)143 for i in range(1, TEST_COUNT):144 value = random.randint(0, sysv_ipc.SEMAPHORE_VALUE_MAX)145 sem.value = value146 assert(sem.value == value)147 sem.remove()148 print_mem_after()149 print("Running semaphore undo read test...")150 print_mem_before()151 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)152 for i in range(1, TEST_COUNT):153 foo = sem.undo154 sem.remove()155 print_mem_after()156 print("Running semaphore undo write test...")157 print_mem_before()158 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)159 for i in range(1, TEST_COUNT):160 undo = random.randint(0, 1)161 sem.undo = undo162 assert(sem.undo == undo)163 sem.remove()164 print_mem_after()165 print("Running semaphore block read test...")166 print_mem_before()167 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)168 for i in range(1, TEST_COUNT):169 foo = sem.block170 sem.remove()171 print_mem_after()172 print("Running semaphore block write test...")173 print_mem_before()174 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)175 for i in range(1, TEST_COUNT):176 block = random.randint(0, 1)177 sem.block = block178 assert(sem.block == block)179 sem.remove()180 print_mem_after()181 print("Running semaphore mode read test...")182 print_mem_before()183 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)184 for i in range(1, TEST_COUNT):185 foo = sem.mode186 sem.remove()187 print_mem_after()188 print("Running semaphore mode write test...")189 print_mem_before()190 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)191 for i in range(1, TEST_COUNT):192 # octal 600 = decimal 384193 sem.mode = 384194 sem.remove()195 print_mem_after()196 print("Running semaphore uid read test...")197 print_mem_before()198 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)199 for i in range(1, TEST_COUNT):200 foo = sem.uid201 sem.remove()202 print_mem_after()203 print("Running semaphore uid write test...")204 print_mem_before()205 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)206 uid = sem.uid207 for i in range(1, TEST_COUNT):208 sem.uid = uid209 sem.remove()210 print_mem_after()211 print("Running semaphore gid read test...")212 print_mem_before()213 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)214 for i in range(1, TEST_COUNT):215 foo = sem.gid216 sem.remove()217 print_mem_after()218 print("Running semaphore gid write test...")219 print_mem_before()220 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)221 gid = sem.gid222 for i in range(1, TEST_COUNT):223 sem.gid = gid224 sem.remove()225 print_mem_after()226 print("Running semaphore cuid read test...")227 print_mem_before()228 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)229 for i in range(1, TEST_COUNT):230 foo = sem.cuid231 sem.remove()232 print_mem_after()233 print("Running semaphore cgid read test...")234 print_mem_before()235 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)236 for i in range(1, TEST_COUNT):237 foo = sem.cgid238 sem.remove()239 print_mem_after()240 print("Running semaphore last_pid read test...")241 print_mem_before()242 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)243 for i in range(1, TEST_COUNT):244 foo = sem.last_pid245 sem.remove()246 print_mem_after()247 print("Running semaphore waiting_for_nonzero read test...")248 print_mem_before()249 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)250 for i in range(1, TEST_COUNT):251 foo = sem.waiting_for_nonzero252 sem.remove()253 print_mem_after()254 print("Running semaphore waiting_for_zero read test...")255 print_mem_before()256 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)257 for i in range(1, TEST_COUNT):258 foo = sem.waiting_for_zero259 sem.remove()260 print_mem_after()261 print("Running semaphore o_time read test...")262 print_mem_before()263 sem = sysv_ipc.Semaphore(42, sysv_ipc.IPC_CREX)264 for i in range(1, TEST_COUNT):265 foo = sem.o_time266 sem.remove()267 print_mem_after()268# ============== Memory tests ==============269if SKIP_SHARED_MEMORY_TESTS:270 print("Skipping shared memory tests")271else:272 print("Running memory create/destroy test...")273 print_mem_before()274 init_character = 'z'.encode()275 init_character_toggle = True276 for i in range(1, TEST_COUNT):277 # Test with and w/o init character278 if init_character_toggle:279 mem = sysv_ipc.SharedMemory(None, sysv_ipc.IPC_CREX, size=sysv_ipc.PAGE_SIZE,280 init_character=init_character)281 else:282 mem = sysv_ipc.SharedMemory(None, sysv_ipc.IPC_CREX, size=sysv_ipc.PAGE_SIZE)283 init_character_toggle = not init_character_toggle284 mem.detach()285 mem.remove()286 print_mem_after()287 print("Running memory read/write test with strings...")288 print_mem_before()289 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX, size=sysv_ipc.PAGE_SIZE)290 alphabet = "abcdefghijklmnopqrstuvwxyz"291 s = alphabet292 length = len(s)293 for i in range(1, TEST_COUNT):294 # length = random.randint(1, sysv_ipc.PAGE_SIZE)295 # s = ''.join( [ random.choice(alphabet) for j in range(1, length + 1) ] )296 mem.write(s)297 assert(s == mem.read(length).decode())298 mem.detach()299 mem.remove()300 print_mem_after()301 print("Running memory read/write test with bytes...")302 print_mem_before()303 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX, size=sysv_ipc.PAGE_SIZE)304 alphabet = "abcdefghijklmnopqrstuvwxyz".encode()305 s = alphabet306 length = len(s)307 for i in range(1, TEST_COUNT):308 # length = random.randint(1, sysv_ipc.PAGE_SIZE)309 # s = ''.join( [ random.choice(alphabet) for j in range(1, length + 1) ] )310 mem.write(s)311 assert(s == mem.read(length))312 mem.detach()313 mem.remove()314 print_mem_after()315 print("Running memory key read test...")316 print_mem_before()317 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)318 for i in range(1, TEST_COUNT):319 foo = mem.key320 mem.detach()321 mem.remove()322 print_mem_after()323 print("Running memory id read test...")324 print_mem_before()325 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)326 for i in range(1, TEST_COUNT):327 foo = mem.id328 mem.detach()329 mem.remove()330 print_mem_after()331 print("Running memory size read test...")332 print_mem_before()333 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)334 for i in range(1, TEST_COUNT):335 foo = mem.size336 mem.detach()337 mem.remove()338 print_mem_after()339 print("Running memory address read test...")340 print_mem_before()341 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)342 for i in range(1, TEST_COUNT):343 foo = mem.address344 mem.detach()345 mem.remove()346 print_mem_after()347 print("Running memory attached read test...")348 print_mem_before()349 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)350 for i in range(1, TEST_COUNT):351 foo = mem.attached352 mem.detach()353 mem.remove()354 print_mem_after()355 print("Running memory last_attach_time test...")356 print_mem_before()357 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)358 for i in range(1, TEST_COUNT):359 foo = mem.last_attach_time360 mem.detach()361 mem.remove()362 print_mem_after()363 print("Running memory last_detach_time test...")364 print_mem_before()365 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)366 for i in range(1, TEST_COUNT):367 foo = mem.last_detach_time368 mem.detach()369 mem.remove()370 print_mem_after()371 print("Running memory last_change_time test...")372 print_mem_before()373 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)374 for i in range(1, TEST_COUNT):375 foo = mem.last_change_time376 mem.detach()377 mem.remove()378 print_mem_after()379 print("Running memory creator_pid test...")380 print_mem_before()381 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)382 for i in range(1, TEST_COUNT):383 foo = mem.creator_pid384 mem.detach()385 mem.remove()386 print_mem_after()387 print("Running memory last_pid test...")388 print_mem_before()389 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)390 for i in range(1, TEST_COUNT):391 foo = mem.last_pid392 mem.detach()393 mem.remove()394 print_mem_after()395 print("Running memory number_attached test...")396 print_mem_before()397 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)398 for i in range(1, TEST_COUNT):399 foo = mem.number_attached400 mem.detach()401 mem.remove()402 print_mem_after()403 print("Running memory mode read test...")404 print_mem_before()405 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)406 for i in range(1, TEST_COUNT):407 foo = mem.mode408 mem.detach()409 mem.remove()410 print_mem_after()411 print("Running memory mode write test...")412 print_mem_before()413 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)414 for i in range(1, TEST_COUNT):415 # octal 600 = decimal 384416 mem.mode = 384417 mem.detach()418 mem.remove()419 print_mem_after()420 print("Running memory uid read test...")421 print_mem_before()422 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)423 for i in range(1, TEST_COUNT):424 foo = mem.uid425 mem.detach()426 mem.remove()427 print_mem_after()428 print("Running memory uid write test...")429 print_mem_before()430 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)431 uid = mem.uid432 for i in range(1, TEST_COUNT):433 mem.uid = uid434 mem.detach()435 mem.remove()436 print_mem_after()437 print("Running memory gid read test...")438 print_mem_before()439 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)440 for i in range(1, TEST_COUNT):441 foo = mem.gid442 mem.detach()443 mem.remove()444 print_mem_after()445 print("Running memory gid write test...")446 print_mem_before()447 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)448 gid = mem.gid449 for i in range(1, TEST_COUNT):450 mem.gid = gid451 mem.detach()452 mem.remove()453 print_mem_after()454 print("Running memory cuid read test...")455 print_mem_before()456 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)457 for i in range(1, TEST_COUNT):458 foo = mem.cuid459 mem.detach()460 mem.remove()461 print_mem_after()462 print("Running memory cgid read test...")463 print_mem_before()464 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)465 for i in range(1, TEST_COUNT):466 foo = mem.cgid467 mem.detach()468 mem.remove()469 print_mem_after()470 print("Running memory create bytearray test...")471 print_mem_before()472 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)473 for i in range(1, TEST_COUNT):474 foo = bytearray(mem)475 mem.detach()476 mem.remove()477 print_mem_after()478 print("Running memory create memoryview test...")479 print_mem_before()480 mem = sysv_ipc.SharedMemory(42, sysv_ipc.IPC_CREX)481 for i in range(1, TEST_COUNT):482 foo = memoryview(mem)483 mem.detach()484 mem.remove()485 print_mem_after()486# ================ Message queue tests ==============487if SKIP_MESSAGE_QUEUE_TESTS:488 print("Skipping Message queue tests")489else:490 print("Running Message queue create/destroy test...")491 print_mem_before()492 for i in range(1, TEST_COUNT):493 mq = sysv_ipc.MessageQueue(None, sysv_ipc.IPC_CREX)494 mq.remove()495 print_mem_after()496 print("Running message queue send/receive test with strings...")497 print_mem_before()498 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)499 for i in range(1, TEST_COUNT):500 s = random_string(15)501 mq.send(s)502 assert(s.encode() == mq.receive()[0])503 mq.remove()504 print_mem_after()505 print("Running message queue send/receive test with bytes...")506 print_mem_before()507 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)508 for i in range(1, TEST_COUNT):509 s = random_string(15).encode()510 mq.send(s)511 assert(s == mq.receive()[0])512 mq.remove()513 print_mem_after()514 print("Running message queue key read test...")515 print_mem_before()516 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)517 for i in range(1, TEST_COUNT):518 foo = mq.key519 mq.remove()520 print_mem_after()521 print("Running message queue id read test...")522 print_mem_before()523 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)524 for i in range(1, TEST_COUNT):525 foo = mq.id526 mq.remove()527 print_mem_after()528 print("Running message queue max size read test...")529 print_mem_before()530 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)531 for i in range(1, TEST_COUNT):532 foo = mq.max_size533 mq.remove()534 print_mem_after()535 print("Running message queue last_send_time read test...")536 print_mem_before()537 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)538 for i in range(1, TEST_COUNT):539 foo = mq.last_send_time540 mq.remove()541 print_mem_after()542 print("Running message queue last_receive_time read test...")543 print_mem_before()544 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)545 for i in range(1, TEST_COUNT):546 foo = mq.last_receive_time547 mq.remove()548 print_mem_after()549 print("Running message queue last_change_time read test...")550 print_mem_before()551 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)552 for i in range(1, TEST_COUNT):553 foo = mq.last_change_time554 mq.remove()555 print_mem_after()556 print("Running message queue last_send_pid read test...")557 print_mem_before()558 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)559 for i in range(1, TEST_COUNT):560 foo = mq.last_send_pid561 mq.remove()562 print_mem_after()563 print("Running message queue last_receive_pid read test...")564 print_mem_before()565 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)566 for i in range(1, TEST_COUNT):567 foo = mq.last_receive_pid568 mq.remove()569 print_mem_after()570 print("Running message queue current_messages read test...")571 print_mem_before()572 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)573 for i in range(1, TEST_COUNT):574 foo = mq.current_messages575 mq.remove()576 print_mem_after()577 print("Running message queue uid read test...")578 print_mem_before()579 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)580 for i in range(1, TEST_COUNT):581 foo = mq.uid582 mq.remove()583 print_mem_after()584 print("Running message queue uid write test...")585 print_mem_before()586 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)587 uid = mq.uid588 for i in range(1, TEST_COUNT):589 mq.uid = uid590 mq.remove()591 print_mem_after()592 print("Running message queue gid read test...")593 print_mem_before()594 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)595 for i in range(1, TEST_COUNT):596 foo = mq.gid597 mq.remove()598 print_mem_after()599 print("Running message queue gid write test...")600 print_mem_before()601 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)602 gid = mq.gid603 for i in range(1, TEST_COUNT):604 mq.gid = gid605 mq.remove()606 print_mem_after()607 print("Running message queue cuid read test...")608 print_mem_before()609 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)610 for i in range(1, TEST_COUNT):611 foo = mq.cuid612 mq.remove()613 print_mem_after()614 print("Running message queue cgid read test...")615 print_mem_before()616 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)617 for i in range(1, TEST_COUNT):618 foo = mq.cgid619 mq.remove()620 print_mem_after()621 print("Running message queue mode read test...")622 print_mem_before()623 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)624 for i in range(1, TEST_COUNT):625 foo = mq.mode626 mq.remove()627 print_mem_after()628 print("Running message queue mode write test...")629 print_mem_before()630 mq = sysv_ipc.MessageQueue(42, sysv_ipc.IPC_CREX)631 for i in range(1, TEST_COUNT):632 # octal 600 = decimal 384633 mq.mode = 384634 mq.remove()...
errorcounter_test.py
Source:errorcounter_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 errorcounter."""16import tensorflow as tf17import errorcounter as ec18class ErrorcounterTest(tf.test.TestCase):19 def testComputeErrorRate(self):20 """Tests that the percent calculation works as expected.21 """22 rate = ec.ComputeErrorRate(error_count=0, truth_count=0)23 self.assertEqual(rate, 100.0)24 rate = ec.ComputeErrorRate(error_count=1, truth_count=0)25 self.assertEqual(rate, 100.0)26 rate = ec.ComputeErrorRate(error_count=10, truth_count=1)27 self.assertEqual(rate, 100.0)28 rate = ec.ComputeErrorRate(error_count=0, truth_count=1)29 self.assertEqual(rate, 0.0)30 rate = ec.ComputeErrorRate(error_count=3, truth_count=12)31 self.assertEqual(rate, 25.0)32 def testCountErrors(self):33 """Tests that the error counter works as expected.34 """35 truth_str = 'farm barn'36 counts = ec.CountErrors(ocr_text=truth_str, truth_text=truth_str)37 self.assertEqual(38 counts, ec.ErrorCounts(39 fn=0, fp=0, truth_count=9, test_count=9))40 # With a period on the end, we get a char error.41 dot_str = 'farm barn.'42 counts = ec.CountErrors(ocr_text=dot_str, truth_text=truth_str)43 self.assertEqual(44 counts, ec.ErrorCounts(45 fn=0, fp=1, truth_count=9, test_count=10))46 counts = ec.CountErrors(ocr_text=truth_str, truth_text=dot_str)47 self.assertEqual(48 counts, ec.ErrorCounts(49 fn=1, fp=0, truth_count=10, test_count=9))50 # Space is just another char.51 no_space = 'farmbarn'52 counts = ec.CountErrors(ocr_text=no_space, truth_text=truth_str)53 self.assertEqual(54 counts, ec.ErrorCounts(55 fn=1, fp=0, truth_count=9, test_count=8))56 counts = ec.CountErrors(ocr_text=truth_str, truth_text=no_space)57 self.assertEqual(58 counts, ec.ErrorCounts(59 fn=0, fp=1, truth_count=8, test_count=9))60 # Lose them all.61 counts = ec.CountErrors(ocr_text='', truth_text=truth_str)62 self.assertEqual(63 counts, ec.ErrorCounts(64 fn=9, fp=0, truth_count=9, test_count=0))65 counts = ec.CountErrors(ocr_text=truth_str, truth_text='')66 self.assertEqual(67 counts, ec.ErrorCounts(68 fn=0, fp=9, truth_count=0, test_count=9))69 def testCountWordErrors(self):70 """Tests that the error counter works as expected.71 """72 truth_str = 'farm barn'73 counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=truth_str)74 self.assertEqual(75 counts, ec.ErrorCounts(76 fn=0, fp=0, truth_count=2, test_count=2))77 # With a period on the end, we get a word error.78 dot_str = 'farm barn.'79 counts = ec.CountWordErrors(ocr_text=dot_str, truth_text=truth_str)80 self.assertEqual(81 counts, ec.ErrorCounts(82 fn=1, fp=1, truth_count=2, test_count=2))83 counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=dot_str)84 self.assertEqual(85 counts, ec.ErrorCounts(86 fn=1, fp=1, truth_count=2, test_count=2))87 # Space is special.88 no_space = 'farmbarn'89 counts = ec.CountWordErrors(ocr_text=no_space, truth_text=truth_str)90 self.assertEqual(91 counts, ec.ErrorCounts(92 fn=2, fp=1, truth_count=2, test_count=1))93 counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=no_space)94 self.assertEqual(95 counts, ec.ErrorCounts(96 fn=1, fp=2, truth_count=1, test_count=2))97 # Lose them all.98 counts = ec.CountWordErrors(ocr_text='', truth_text=truth_str)99 self.assertEqual(100 counts, ec.ErrorCounts(101 fn=2, fp=0, truth_count=2, test_count=0))102 counts = ec.CountWordErrors(ocr_text=truth_str, truth_text='')103 self.assertEqual(104 counts, ec.ErrorCounts(105 fn=0, fp=2, truth_count=0, test_count=2))106 # With a space in ba rn, there is an extra add.107 sp_str = 'farm ba rn'108 counts = ec.CountWordErrors(ocr_text=sp_str, truth_text=truth_str)109 self.assertEqual(110 counts, ec.ErrorCounts(111 fn=1, fp=2, truth_count=2, test_count=3))112 counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=sp_str)113 self.assertEqual(114 counts, ec.ErrorCounts(115 fn=2, fp=1, truth_count=3, test_count=2))116if __name__ == '__main__':...
testing.py
Source:testing.py
1from index import *2def tests():3 test_count = 04 index = Index()5 6 #adding an entry to an empty index7 if(index.add_entry("word1",1,{1,2,3,4}) == True):8 test_count = test_count + 1910 #adding an entry to an index that has the same doc_id as a previous entry11 if(index.add_entry("word2",1,{5,6,7,8}) == True):12 test_count = test_count + 11314 #adding an entry to an index that has a new doc_id and a new word15 if(index.add_entry("word3",2,{12,17,55}) == True):16 test_count = test_count + 11718 #adding an entry to an index that has a previously entered word but a different doc_id19 if(index.add_entry("word1",2,{122,171,255}) == True):20 test_count = test_count + 12122 if(index.add_entry("word3",3,{144,217,55}) == True):23 test_count = test_count + 12425 #adding an entry that has no positions (can't do it)26 if(index.add_entry("word4",1) == False):27 test_count = test_count + 12829 #adding an entry with a position set size of 0 (can't do it)30 if(index.add_entry("word4",2,{}) == False):31 test_count = test_count + 13233 #finding an entry that only appears in one document in the index34 if(index.find_entry("word2") == {1: {5, 6, 7, 8}}):35 test_count = test_count + 13637 #finding an entry that appears in multiple documents in the index38 if(index.find_entry("word1") == {1: {1, 2, 3, 4}, 2: {122, 171, 255}}):39 test_count = test_count + 14041 #removing an entry that only appears in one document from the index42 if(index.remove_entry("word2") == {1: {5, 6, 7, 8}}):43 test_count = test_count + 14445 #removing an entry that appears in multiple document from the index46 if(index.remove_entry("word3") == {2: {12, 17, 55}, 3: {144, 217, 55}}):47 test_count = test_count + 14849 #removing an entry with a specific doc_id from the index50 if(index.remove_entry("word1",2) == {122,171,255}):51 test_count = test_count + 152 53 #after removing pa54 if(index.find_entry("word1") == {1: {1, 2, 3, 4}}):55 test_count = test_count + 15657 #removing an entry with a word that is present in the index but with a doc_id that is not (can't do it)58 if(index.remove_entry("word1",2) == None):59 test_count = test_count + 16061 #finding an entry that is not in the index from the index (can't do it)62 if(index.find_entry("word4") == None):63 test_count = test_count + 16465 #removing an entry that is not in the index from the index (can't do it)66 if(index.remove_entry("word4") == None):67 test_count = test_count + 16869 #removing an entry with a word that is not present in the index but with a doc_id that is (can't do it)70 if(index.remove_entry("word4",2) == None):71 test_count = test_count + 17273 #removing an entry with a word and doc_id that that aren't present in the index (can't do it)74 if(index.remove_entry("word4",55) == None):75 test_count = test_count + 17677 return test_count78
...
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!!