<s> import ctypes <EOL> import unittest <EOL> import cupy . cuda <EOL> from cupy . cuda import memory <EOL> from cupy import testing <EOL> class MockMemory ( memory . Memory ) : <EOL> cur_ptr = <NUM_LIT:1> <EOL> def __init__ ( self , size ) : <EOL> self . ptr = MockMemory . cur_ptr <EOL> MockMemory . cur_ptr += size <EOL> self . size = size <EOL> self . device = None <EOL> def __del__ ( self ) : <EOL> self . ptr = <NUM_LIT:0> <EOL> pass <EOL> def mock_alloc ( size ) : <EOL> mem = MockMemory ( size ) <EOL> return memory . MemoryPointer ( mem , <NUM_LIT:0> ) <EOL> @ testing . gpu <EOL> class TestMemoryPointer ( unittest . TestCase ) : <EOL> def test_int ( self ) : <EOL> pval = MockMemory . cur_ptr <EOL> memptr = mock_alloc ( <NUM_LIT:1> ) <EOL> self . assertEqual ( pval , int ( memptr ) ) <EOL> def test_add ( self ) : <EOL> pval = MockMemory . cur_ptr <EOL> memptr = mock_alloc ( <NUM_LIT:8> ) <EOL> memptr2 = memptr + <NUM_LIT:4> <EOL> self . assertIsInstance ( memptr2 , memory . MemoryPointer ) <EOL> self . assertEqual ( pval + <NUM_LIT:4> , int ( memptr2 ) ) <EOL> memptr3 = <NUM_LIT:4> + memptr <EOL> self . assertIsInstance ( memptr3 , memory . MemoryPointer ) <EOL> self . assertEqual ( pval + <NUM_LIT:4> , int ( memptr3 ) ) <EOL> memptr += <NUM_LIT:4> <EOL> self . assertIsInstance ( memptr , memory . MemoryPointer ) <EOL> self . assertEqual ( pval + <NUM_LIT:4> , int ( memptr ) ) <EOL> def test_sub ( self ) : <EOL> pval = MockMemory . cur_ptr <EOL> memptr = mock_alloc ( <NUM_LIT:8> ) + <NUM_LIT:4> <EOL> memptr2 = memptr - <NUM_LIT:4> <EOL> self . assertIsInstance ( memptr2 , memory . MemoryPointer ) <EOL> self . assertEqual ( pval , int ( memptr2 ) ) <EOL> memptr -= <NUM_LIT:4> <EOL> self . assertIsInstance ( memptr , memory . MemoryPointer ) <EOL> self . assertEqual ( pval , int ( memptr ) ) <EOL> def test_copy_to_and_from_host ( self ) : <EOL> a_gpu = memory . alloc ( <NUM_LIT:4> ) <EOL> a_cpu = ctypes . c_int ( <NUM_LIT:100> ) <EOL> a_gpu . copy_from ( ctypes . cast ( ctypes . byref ( a_cpu ) , ctypes . c_void_p ) , <NUM_LIT:4> ) <EOL> b_cpu = ctypes . c_int ( ) <EOL> a_gpu . copy_to_host ( <EOL> ctypes . cast ( ctypes . byref ( b_cpu ) , ctypes . c_void_p ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( b_cpu . value , a_cpu . value ) <EOL> def test_copy_from_device ( self ) : <EOL> a_gpu = memory . alloc ( <NUM_LIT:4> ) <EOL> a_cpu = ctypes . c_int ( <NUM_LIT:100> ) <EOL> a_gpu . copy_from ( ctypes . cast ( ctypes . byref ( a_cpu ) , ctypes . c_void_p ) , <NUM_LIT:4> ) <EOL> b_gpu = memory . alloc ( <NUM_LIT:4> ) <EOL> b_gpu . copy_from ( a_gpu , <NUM_LIT:4> ) <EOL> b_cpu = ctypes . c_int ( ) <EOL> b_gpu . copy_to_host ( <EOL> ctypes . cast ( ctypes . byref ( b_cpu ) , ctypes . c_void_p ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( b_cpu . value , a_cpu . value ) <EOL> def test_memset ( self ) : <EOL> a_gpu = memory . alloc ( <NUM_LIT:4> ) <EOL> a_gpu . memset ( <NUM_LIT:1> , <NUM_LIT:4> ) <EOL> a_cpu = ctypes . c_ubyte ( ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> a_gpu . copy_to_host ( <EOL> ctypes . cast ( ctypes . byref ( a_cpu ) , ctypes . c_void_p ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( a_cpu . value , <NUM_LIT:1> ) <EOL> a_gpu += <NUM_LIT:1> <EOL> @ testing . gpu <EOL> class TestSingleDeviceMemoryPool ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . pool = memory . SingleDeviceMemoryPool ( allocator = mock_alloc ) <EOL> def test_alloc ( self ) : <EOL> p1 = self . pool . malloc ( <NUM_LIT:1000> ) <EOL> p2 = self . pool . malloc ( <NUM_LIT:1000> ) <EOL> p3 = self . pool . malloc ( <NUM_LIT> ) <EOL> self . assertNotEqual ( p1 . ptr , p2 . ptr ) <EOL> self . assertNotEqual ( p1 . ptr , p3 . ptr ) <EOL> self . assertNotEqual ( p2 . ptr , p3 . ptr ) <EOL> def test_free ( self ) : <EOL> p1 = self . pool . malloc ( <NUM_LIT:1000> ) <EOL> ptr1 = p1 . ptr <EOL> del p1 <EOL> p2 = self . pool . malloc ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( ptr1 , p2 . ptr ) <EOL> def test_free_different_size ( self ) : <EOL> p1 = self . pool . malloc ( <NUM_LIT:1000> ) <EOL> ptr1 = p1 . ptr <EOL> del p1 <EOL> p2 = self . pool . malloc ( <NUM_LIT> ) <EOL> self . assertNotEqual ( ptr1 , p2 . ptr ) <EOL> def test_free_all_free ( self ) : <EOL> p1 = self . pool . malloc ( <NUM_LIT:1000> ) <EOL> ptr1 = p1 . ptr <EOL> del p1 <EOL> self . pool . free_all_free ( ) <EOL> p2 = self . pool . malloc ( <NUM_LIT:1000> ) <EOL> self . assertNotEqual ( ptr1 , p2 . ptr ) <EOL> @ testing . gpu <EOL> class TestMemoryPool ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . pool = memory . MemoryPool ( ) <EOL> def test_zero_size_alloc ( self ) : <EOL> with cupy . cuda . Device ( <NUM_LIT:0> ) : <EOL> mem = self . pool . malloc ( <NUM_LIT:0> ) . mem <EOL> self . assertIsInstance ( mem , memory . Memory ) <EOL> self . assertNotIsInstance ( mem , memory . PooledMemory ) <EOL> def test_double_free ( self ) : <EOL> with cupy . cuda . Device ( <NUM_LIT:0> ) : <EOL> mem = self . pool . malloc ( <NUM_LIT:1> ) . mem <EOL> mem . free ( ) <EOL> mem . free ( ) <EOL> def test_free_all_free ( self ) : <EOL> with cupy . cuda . Device ( <NUM_LIT:0> ) : <EOL> mem = self . pool . malloc ( <NUM_LIT:1> ) . mem <EOL> self . assertIsInstance ( mem , memory . Memory ) <EOL> self . assertIsInstance ( mem , memory . PooledMemory ) <EOL> self . assertEqual ( self . pool . n_free_blocks ( ) , <NUM_LIT:0> ) <EOL> mem . free ( ) <EOL> self . assertEqual ( self . pool . n_free_blocks ( ) , <NUM_LIT:1> ) <EOL> self . pool . free_all_free ( ) <EOL> self . assertEqual ( self . pool . n_free_blocks ( ) , <NUM_LIT:0> ) <EOL> def test_free_all_free_without_malloc ( self ) : <EOL> with cupy . cuda . Device ( <NUM_LIT:0> ) : <EOL> self . pool . free_all_free ( ) <EOL> self . assertEqual ( self . pool . n_free_blocks ( ) , <NUM_LIT:0> ) <EOL> def test_n_free_blocks_without_malloc ( self ) : <EOL> with cupy . cuda . Device ( <NUM_LIT:0> ) : <EOL> self . assertEqual ( self . pool . n_free_blocks ( ) , <NUM_LIT:0> ) </s>
