66import numpy as np
77
88from nose .tools import assert_equal
9+ from nose .tools import assert_false
10+ from nose .tools import assert_true
911from numpy .testing import (assert_almost_equal ,
1012 assert_array_almost_equal )
1113
1214from sklearn .utils .fixes import divide , expit
15+ from sklearn .utils .fixes import astype
1316
1417
1518def test_expit ():
@@ -28,3 +31,25 @@ def test_expit():
2831
2932def 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