@@ -107,28 +107,45 @@ def test_search_radius(self):
107107 self .assertEqual (nns [i ][0 ][0 ], i )
108108 self .assertAlmostEqual (nns [i ][0 ][1 ], 0 )
109109
110- # Test that the memory is re-used
111- datas = [x .ctypes .data for x in nns ]
110+ # Test that the memory is re-used by comparing memory addresses.
111+ # In case the size of an array equals zero, its memory address is
112+ # random. See darray.hpp for more details.
113+ def addresses (nns ):
114+ return [x .ctypes .data if len (x ) else 0 for x in nns ]
115+
116+ datas = addresses (nns )
112117 t .search_radius (a , search_radius , nns )
113- self .assertEqual ([ x . ctypes . data for x in nns ] , datas )
118+ self .assertEqual (addresses ( nns ) , datas )
114119 t .search_radius (a , search_radius ** 2 , nns )
115- self .assertNotEqual ([ x . ctypes . data for x in nns ] , datas )
120+ self .assertNotEqual (addresses ( nns ) , datas )
116121
117122 def test_search_box (self ):
118123 a = np .array ([[2 , 1 ], [4 , 3 ], [8 , 7 ]], dtype = np .float32 )
119124 t = pt .KdTree (a , pt .Metric .L2Squared , 10 )
120-
121- min = np .array ([[0 , 0 ], [2 , 2 ], [0 , 0 ], [6 , 6 ]], dtype = np .float32 )
122- max = np .array ([[3 , 3 ], [3 , 3 ], [9 , 9 ], [9 , 9 ]], dtype = np .float32 )
123- nns = t .search_box (min , max )
125+ boxes = np .array (
126+ [[0 , 0 ],
127+ [3 , 3 ],
128+ [2 , 2 ],
129+ [3 , 3 ],
130+ [0 , 0 ],
131+ [9 , 9 ],
132+ [6 , 6 ],
133+ [9 , 9 ]],
134+ dtype = np .float32 )
135+ nns = t .search_box (boxes )
124136 self .assertEqual (len (nns ), 4 )
125137 self .assertEqual (nns .dtype , t .dtype_index )
126138 self .assertTrue (nns )
127139
128- # Test that the memory is re-used
129- nns [0 ][0 ] = 42
130- t .search_box (min , max , nns )
131- self .assertEqual (nns [0 ][0 ], 0 )
140+ # Test that the memory is re-used by comparing memory addresses.
141+ # In case the size of an array equals zero, its memory address is
142+ # random. See darray.hpp for more details.
143+ def addresses (nns ):
144+ return [x .ctypes .data if len (x ) else 0 for x in nns ]
145+
146+ datas = addresses (nns )
147+ t .search_box (boxes , nns )
148+ self .assertEqual (addresses (nns ), datas )
132149
133150 # Check the number of indices found.
134151 sizes = [1 , 0 , 3 , 1 ]
0 commit comments