Skip to content

Commit 95fb92b

Browse files
committed
TST add missing test for astype backport
1 parent 7ac6c9b commit 95fb92b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

sklearn/utils/tests/test_fixes.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import numpy as np
77

88
from nose.tools import assert_equal
9+
from nose.tools import assert_false
10+
from nose.tools import assert_true
911
from numpy.testing import (assert_almost_equal,
1012
assert_array_almost_equal)
1113

1214
from sklearn.utils.fixes import divide, expit
15+
from sklearn.utils.fixes import astype
1316

1417

1518
def test_expit():
@@ -28,3 +31,25 @@ def test_expit():
2831

2932
def test_divide():
3033
assert_equal(divide(.6, 1), .600000000000)
34+
35+
36+
def test_astype_copy_memory():
37+
a_int32 = np.ones(3, np.int32)
38+
39+
# Check that dtype conversion works
40+
b_float32 = astype(a_int32, dtype=np.float32, copy=False)
41+
assert_equal(b_float32.dtype, np.float32)
42+
43+
# Changing dtype forces a copy even if copy=False
44+
assert_false(np.may_share_memory(b_float32, a_int32))
45+
46+
# Check that copy can be skipped if requested dtype match
47+
c_int32 = astype(a_int32, dtype=np.int32, copy=False)
48+
assert_true(c_int32 is a_int32)
49+
50+
# Check that copy can be forced, and is the case by default:
51+
d_int32 = astype(a_int32, dtype=np.int32, copy=True)
52+
assert_false(np.may_share_memory(d_int32, a_int32))
53+
54+
e_int32 = astype(a_int32, dtype=np.int32)
55+
assert_false(np.may_share_memory(e_int32, a_int32))

0 commit comments

Comments
 (0)