MCPcopy Index your code
hub / github.com/PyO3/rust-numpy / as_slice

Function as_slice

tests/array.rs:188–242  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

186
187#[test]
188fn as_slice() {
189 Python::attach(|py| {
190 let arr = PyArray::<i32, _>::zeros(py, [3, 2, 4], false);
191 assert_eq!(arr.readonly().as_slice().unwrap().len(), 3 * 2 * 4);
192
193 let not_contiguous = not_contiguous_array(py);
194 let err = not_contiguous.readonly().as_slice().unwrap_err();
195 assert_eq!(
196 err.to_string(),
197 "The given array is not contiguous or is misaligned."
198 );
199 let err = not_contiguous.readwrite().as_slice_mut().unwrap_err();
200 assert_eq!(
201 err.to_string(),
202 "The given array is not contiguous or is misaligned."
203 );
204
205 let not_aligned = not_aligned_array(py);
206 assert!(!not_aligned.is_aligned());
207 let err = not_aligned.readonly().as_slice().unwrap_err();
208 assert_eq!(
209 err.to_string(),
210 "The given array is not contiguous or is misaligned."
211 );
212 let err = not_aligned.readwrite().as_slice_mut().unwrap_err();
213 assert_eq!(
214 err.to_string(),
215 "The given array is not contiguous or is misaligned."
216 );
217
218 let misaligned_empty: Bound<'_, PyArray1<u16>> = {
219 let arr = not_aligned_array(py);
220 py.eval(
221 c_str!("arr[0:0]"),
222 None,
223 Some(&[("arr", arr)].into_py_dict(py).unwrap()),
224 )
225 .unwrap()
226 .cast_into()
227 .unwrap()
228 };
229 assert_eq!(
230 misaligned_empty.readonly().as_slice().unwrap(),
231 &[] as &[u16]
232 );
233 assert_eq!(
234 misaligned_empty.readwrite().as_slice_mut().unwrap(),
235 &mut [] as &mut [u16]
236 );
237 assert_eq!(
238 misaligned_empty.readonly().to_vec().unwrap(),
239 Vec::<u16>::new()
240 );
241 });
242}
243
244#[test]
245fn is_instance() {

Callers

nothing calls this directly

Calls 7

zerosFunction · 0.85
not_contiguous_arrayFunction · 0.85
not_aligned_arrayFunction · 0.85
readonlyMethod · 0.80
readwriteMethod · 0.80
as_sliceMethod · 0.45
as_slice_mutMethod · 0.45

Tested by

no test coverage detected