MCPcopy Index your code
hub / github.com/dgrunwald/rust-cpython / test_array_buffer

Function test_array_buffer

src/buffer.rs:718–753  ·  view source on GitHub ↗

array.array doesn't implement the buffer protocol in python 2.7

()

Source from the content-addressed store, hash-verified

716 #[test]
717 #[cfg(feature = "python3-sys")] // array.array doesn't implement the buffer protocol in python 2.7
718 fn test_array_buffer() {
719 let gil = Python::acquire_gil();
720 let py = gil.python();
721 let array = py
722 .import("array")
723 .unwrap()
724 .as_object()
725 .call_method(py, "array", ("f", (1.0, 1.5, 2.0, 2.5)), None)
726 .unwrap();
727 let buffer = PyBuffer::get(py, &array).unwrap();
728 assert_eq!(buffer.dimensions(), 1);
729 assert_eq!(buffer.item_count(), 4);
730 assert_eq!(buffer.format().to_str().unwrap(), "f");
731 assert_eq!(buffer.shape(), [4]);
732
733 assert!(buffer.as_slice::<f64>(py).is_none());
734 assert!(buffer.as_slice::<i32>(py).is_none());
735
736 let slice = buffer.as_slice::<f32>(py).unwrap();
737 assert_eq!(slice.len(), 4);
738 assert_eq!(slice[0].get(), 1.0);
739 assert_eq!(slice[3].get(), 2.5);
740
741 let mut_slice = buffer.as_mut_slice::<f32>(py).unwrap();
742 assert_eq!(mut_slice.len(), 4);
743 assert_eq!(mut_slice[0].get(), 1.0);
744 mut_slice[3].set(2.75);
745 assert_eq!(slice[3].get(), 2.75);
746
747 buffer
748 .copy_from_slice(py, &[10.0f32, 11.0, 12.0, 13.0])
749 .unwrap();
750 assert_eq!(slice[2].get(), 12.0);
751
752 assert_eq!(buffer.to_vec::<f32>(py).unwrap(), [10.0, 11.0, 12.0, 13.0]);
753 }
754}

Callers

nothing calls this directly

Calls 5

pythonMethod · 0.80
call_methodMethod · 0.80
copy_from_sliceMethod · 0.80
as_objectMethod · 0.45
importMethod · 0.45

Tested by

no test coverage detected