Skip to content

Commit 4f48be9

Browse files
committed
Update scope of I/O tests
1 parent f907e59 commit 4f48be9

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import unittest
2+
3+
import numpy
4+
import pytest
5+
6+
import dpnp as cupy
7+
from dpnp.tests.third_party.cupy import testing
8+
9+
pytest.skip("text formatting is not supported yet", allow_module_level=True)
10+
11+
12+
class TestFormatting(unittest.TestCase):
13+
14+
def test_array_repr(self):
15+
a = testing.shaped_arange((2, 3, 4), cupy)
16+
b = testing.shaped_arange((2, 3, 4), numpy)
17+
assert cupy.array_repr(a) == numpy.array_repr(b)
18+
19+
def test_array_str(self):
20+
a = testing.shaped_arange((2, 3, 4), cupy)
21+
b = testing.shaped_arange((2, 3, 4), numpy)
22+
assert cupy.array_str(a) == numpy.array_str(b)
23+
24+
def test_array2string(self):
25+
a = testing.shaped_arange((2, 3, 4), cupy)
26+
b = testing.shaped_arange((2, 3, 4), numpy)
27+
assert cupy.array2string(a) == numpy.array2string(b)
28+
29+
def test_format_float_positional_python_scalar(self):
30+
x = 1.0
31+
assert cupy.format_float_positional(x) == numpy.format_float_positional(
32+
x
33+
)
34+
35+
def test_format_float_positional(self):
36+
a = testing.shaped_arange((), cupy)
37+
b = testing.shaped_arange((), numpy)
38+
assert cupy.format_float_positional(a) == numpy.format_float_positional(
39+
b
40+
)
41+
42+
def test_format_float_scientific(self):
43+
a = testing.shaped_arange((), cupy)
44+
b = testing.shaped_arange((), numpy)
45+
assert cupy.format_float_scientific(a) == numpy.format_float_scientific(
46+
b
47+
)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import io
2+
import pickle
3+
import unittest
4+
5+
import pytest
6+
7+
import dpnp as cupy
8+
from dpnp.tests.third_party.cupy import testing
9+
10+
pytest.skip("text functions are not supported yet", allow_module_level=True)
11+
12+
13+
class TestNpz(unittest.TestCase):
14+
15+
@testing.for_all_dtypes()
16+
def test_save_load(self, dtype):
17+
a = testing.shaped_arange((2, 3, 4), dtype=dtype)
18+
sio = io.BytesIO()
19+
cupy.save(sio, a)
20+
s = sio.getvalue()
21+
sio.close()
22+
23+
sio = io.BytesIO(s)
24+
b = cupy.load(sio)
25+
sio.close()
26+
27+
testing.assert_array_equal(a, b)
28+
29+
def test_save_pickle(self):
30+
data = object()
31+
32+
sio = io.BytesIO()
33+
with self.assertRaises(ValueError):
34+
cupy.save(sio, data, allow_pickle=False)
35+
sio.close()
36+
37+
sio = io.BytesIO()
38+
cupy.save(sio, data, allow_pickle=True)
39+
sio.close()
40+
41+
def test_load_pickle(self):
42+
a = testing.shaped_arange((2, 3, 4), dtype=cupy.float32)
43+
44+
sio = io.BytesIO()
45+
a.dump(sio)
46+
s = sio.getvalue()
47+
sio.close()
48+
49+
sio = io.BytesIO(s)
50+
b = cupy.load(sio, allow_pickle=True)
51+
testing.assert_array_equal(a, b)
52+
sio.close()
53+
54+
sio = io.BytesIO(s)
55+
with self.assertRaises(ValueError):
56+
cupy.load(sio, allow_pickle=False)
57+
sio.close()
58+
59+
@testing.for_all_dtypes()
60+
def check_savez(self, savez, dtype):
61+
a1 = testing.shaped_arange((2, 3, 4), dtype=dtype)
62+
a2 = testing.shaped_arange((3, 4, 5), dtype=dtype)
63+
64+
sio = io.BytesIO()
65+
savez(sio, a1, a2)
66+
s = sio.getvalue()
67+
sio.close()
68+
69+
sio = io.BytesIO(s)
70+
with cupy.load(sio) as d:
71+
b1 = d["arr_0"]
72+
b2 = d["arr_1"]
73+
sio.close()
74+
75+
testing.assert_array_equal(a1, b1)
76+
testing.assert_array_equal(a2, b2)
77+
78+
def test_savez(self):
79+
self.check_savez(cupy.savez)
80+
81+
def test_savez_compressed(self):
82+
self.check_savez(cupy.savez_compressed)
83+
84+
@testing.for_all_dtypes()
85+
def test_pickle(self, dtype):
86+
a = testing.shaped_arange((2, 3, 4), dtype=dtype)
87+
s = pickle.dumps(a)
88+
b = pickle.loads(s)
89+
testing.assert_array_equal(a, b)
90+
91+
@testing.for_all_dtypes()
92+
def test_dump(self, dtype):
93+
a = testing.shaped_arange((2, 3, 4), dtype=dtype)
94+
95+
sio = io.BytesIO()
96+
a.dump(sio)
97+
s = sio.getvalue()
98+
sio.close()
99+
100+
sio = io.BytesIO(s)
101+
b = cupy.load(sio, allow_pickle=True)
102+
sio.close()
103+
104+
testing.assert_array_equal(a, b)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import filecmp
2+
import os
3+
import tempfile
4+
import unittest
5+
6+
import numpy
7+
import pytest
8+
9+
import dpnp as cupy
10+
11+
pytest.skip("text functions are not supported yet", allow_module_level=True)
12+
13+
14+
class TestText(unittest.TestCase):
15+
16+
def test_savetxt(self):
17+
tmp_cupy = tempfile.NamedTemporaryFile(delete=False)
18+
tmp_numpy = tempfile.NamedTemporaryFile(delete=False)
19+
try:
20+
tmp_cupy.close()
21+
tmp_numpy.close()
22+
array = [[1, 2, 3], [2, 3, 4]]
23+
cupy.savetxt(tmp_cupy.name, cupy.array(array))
24+
numpy.savetxt(tmp_numpy.name, numpy.array(array))
25+
assert filecmp.cmp(tmp_cupy.name, tmp_numpy.name)
26+
finally:
27+
os.remove(tmp_cupy.name)
28+
os.remove(tmp_numpy.name)

0 commit comments

Comments
 (0)