<s> import unittest <EOL> import ufora . native . TCMalloc as TCMallocNative <EOL> import threading <EOL> import Queue <EOL> def memoryFreeingThreadLoop ( queue ) : <EOL> while True : <EOL> element = queue . get ( ) <EOL> if element == "<STR_LIT>" : <EOL> return <EOL> address , event = element <EOL> TCMallocNative . freeAtAddress ( address ) <EOL> event . set ( ) <EOL> class ComponentModelTest ( unittest . TestCase ) : <EOL> def test_allocInOneThreadAndDeallocateInAnother ( self ) : <EOL> queue = Queue . Queue ( ) <EOL> thread = threading . Thread ( target = memoryFreeingThreadLoop , args = ( queue , ) ) <EOL> thread . start ( ) <EOL> for ix in range ( <NUM_LIT:1000> ) : <EOL> event = threading . Event ( ) <EOL> address = TCMallocNative . mallocAndReturnAddress ( <NUM_LIT:100> * <NUM_LIT> * <NUM_LIT> ) <EOL> queue . put ( ( address , event ) ) <EOL> event . wait ( ) <EOL> queue . put ( "<STR_LIT>" ) <EOL> thread . join ( ) <EOL> def test_realloc ( self ) : <EOL> measurements = [ TCMallocNative . getBytesUsed ( ) ] <EOL> for ix in range ( <NUM_LIT:10> ) : <EOL> addr = TCMallocNative . mallocAndReturnAddress ( <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> ) <EOL> addr = TCMallocNative . reallocAtAddress ( addr , <NUM_LIT:20> * <NUM_LIT> * <NUM_LIT> ) <EOL> measurements . append ( TCMallocNative . getBytesUsed ( ) ) <EOL> addr = TCMallocNative . reallocAtAddress ( addr , <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> ) <EOL> addr = TCMallocNative . reallocAtAddress ( addr , <NUM_LIT:5> * <NUM_LIT> * <NUM_LIT> ) <EOL> TCMallocNative . freeAtAddress ( addr ) <EOL> measurements . append ( TCMallocNative . getBytesUsed ( ) ) <EOL> self . assertTrue ( <EOL> measurements [ - <NUM_LIT:1> ] < measurements [ <NUM_LIT:0> ] + <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> , <EOL> "<STR_LIT>" % ( measurements [ - <NUM_LIT:1> ] , measurements [ <NUM_LIT:0> ] ) <EOL> ) <EOL> def test_strings ( self ) : <EOL> bytes = TCMallocNative . getBytesUsed ( ) <EOL> for ix in range ( <NUM_LIT:100> ) : <EOL> s = str ( ix ) * <NUM_LIT> <EOL> s2 = TCMallocNative . returnStringArg ( s ) <EOL> self . assertEqual ( s , s2 ) <EOL> finalBytes = TCMallocNative . getBytesUsed ( ) <EOL> self . assertTrue ( finalBytes < bytes + <NUM_LIT> ) </s>
