Skip to content

Commit aed1ff6

Browse files
committed
add tests for MKLMemory class
1 parent ea21116 commit aed1ff6

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

mkl/tests/test_mkl_memory.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Copyright (c) 2018, Intel Corporation
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions are met:
5+
#
6+
# * Redistributions of source code must retain the above copyright notice,
7+
# this list of conditions and the following disclaimer.
8+
# * Redistributions in binary form must reproduce the above copyright
9+
# notice, this list of conditions and the following disclaimer in the
10+
# documentation and/or other materials provided with the distribution.
11+
# * Neither the name of Intel Corporation nor the names of its contributors
12+
# may be used to endorse or promote products derived from this software
13+
# without specific prior written permission.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
import sys
27+
28+
import pytest
29+
30+
import mkl
31+
32+
33+
def test_mkl_memory_create_malloc():
34+
nbytes = 1024
35+
mem = mkl.MKLMemory(nbytes)
36+
assert mem.nbytes == nbytes
37+
# default alignment is 64 bytes
38+
assert mem.alignment == 64
39+
40+
41+
def test_mkl_memory_create_calloc():
42+
size = 32
43+
num = 32
44+
nbytes = num * size
45+
# test creating with mkl_calloc
46+
mem = mkl.MKLMemory(num, size)
47+
assert mem.nbytes == nbytes
48+
# default alignment is 64 bytes
49+
assert mem.alignment == 64
50+
51+
52+
def test_mkl_memory_create_with_malloc_and_alignment():
53+
size = 32
54+
num = 32
55+
nbytes = num * size
56+
alignment = 128
57+
mem = mkl.MKLMemory(nbytes, alignment=alignment)
58+
assert mem.nbytes == nbytes
59+
assert mem.alignment == alignment
60+
61+
62+
def test_mkl_memory_create_with_calloc_and_alignment():
63+
size = 32
64+
num = 32
65+
nbytes = num * size
66+
alignment = 128
67+
mem = mkl.MKLMemory(num, size, alignment=alignment)
68+
assert mem.nbytes == nbytes
69+
70+
71+
def test_mkl_memory_create_from_mkl_memory():
72+
mem1 = mkl.MKLMemory(1024)
73+
mem2 = mkl.MKLMemory(mem1)
74+
assert mem2.nbytes == mem1.nbytes
75+
76+
77+
def test_mkl_memory_create_from_mkl_memory_with_alignment():
78+
mem1 = mkl.MKLMemory(1024)
79+
alignment = 128
80+
mem2 = mkl.MKLMemory(mem1, alignment=alignment)
81+
assert mem2.nbytes == mem1.nbytes
82+
assert mem2.alignment == alignment
83+
84+
85+
def test_mkl_memory_propagates_alignment():
86+
mem1 = mkl.MKLMemory(1024, alignment=128)
87+
mem2 = mkl.MKLMemory(mem1)
88+
assert mem2.nbytes == mem1.nbytes
89+
assert mem2.alignment == mem1.alignment
90+
91+
92+
def test_mkl_memory_properties():
93+
nbytes = 1024
94+
mem = mkl.MKLMemory(nbytes)
95+
assert len(mem) == nbytes
96+
assert type(repr(mem)) is str
97+
assert type(bytes(mem)) is bytes
98+
assert sys.getsizeof(mem) >= nbytes
99+
100+
101+
def test_buffer_protocol():
102+
mem = mkl.MKLMemory(1024)
103+
mv1 = memoryview(mem)
104+
assert mv1.nbytes == mem.nbytes
105+
mv2 = memoryview(mem)
106+
assert mv1 == mv2
107+
108+
109+
def test_pickling():
110+
import pickle
111+
112+
mem = mkl.MKLMemory(1024)
113+
mv = memoryview(mem)
114+
for i in range(len(mem)):
115+
mv[i] = (i % 32) + ord("a")
116+
117+
mem_reconstructed = pickle.loads(pickle.dumps(mem))
118+
assert type(mem) is type(mem_reconstructed), "Pickling should preserve type"
119+
assert (
120+
mem.tobytes() == mem_reconstructed.tobytes()
121+
), "Pickling should preserve buffer content"
122+
assert (
123+
mem._pointer != mem_reconstructed._pointer
124+
), "Pickling/unpickling should be changing pointer"
125+
126+
127+
def test_pickling_with_alignment():
128+
import pickle
129+
130+
mem = mkl.MKLMemory(1024, alignment=128)
131+
mem_reconstructed = pickle.loads(pickle.dumps(mem))
132+
assert type(mem) is type(mem_reconstructed), "Pickling should preserve type"
133+
assert (
134+
mem.tobytes() == mem_reconstructed.tobytes()
135+
), "Pickling should preserve buffer content"
136+
assert (
137+
mem._pointer != mem_reconstructed._pointer
138+
), "Pickling/unpickling should be changing pointer"
139+
assert (
140+
mem.alignment == mem_reconstructed.alignment
141+
), "Pickling should preserve alignment"

0 commit comments

Comments
 (0)