Skip to content

Commit 1316b7a

Browse files
committed
Test data transfers between sources
1 parent 878007b commit 1316b7a

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

texel/tests/data_transfer.rs

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::sync::OnceLock;
22

3-
use image_texel::image::{DataRef, Image};
3+
use image_texel::image::{AtomicImage, CellImage, DataRef, Image};
44
use image_texel::layout::{MatrixBytes, PlaneBytes};
5+
use image_texel::texels::U8;
56

67
#[test]
78
fn same_layout_io() {
@@ -18,6 +19,140 @@ fn same_layout_io() {
1819
assert_eq!(target.as_buf().as_bytes(), input.data);
1920
}
2021

22+
#[test]
23+
fn layout_copy() {
24+
let input = TestData::hello_img();
25+
26+
let source = Image::with_bytes(input.layout, input.data);
27+
28+
let initially_empty = MatrixBytes::empty(input.layout.element());
29+
let mut target = Image::new(initially_empty);
30+
31+
target.assign(source.as_source());
32+
33+
// Layout must match, as must the bytes within the layout.
34+
assert_eq!(*target.layout(), input.layout);
35+
assert_eq!(target.as_buf().as_bytes(), input.data);
36+
}
37+
38+
#[test]
39+
fn layout_plane_copy() {
40+
let input = TestData::hello_img();
41+
42+
let mut subplanes = {
43+
let layout = PlaneBytes::new([input.layout, input.layout]);
44+
let offset = layout.plane_ref(1).unwrap().offset.get();
45+
46+
let mut data = vec![0u8; offset];
47+
data.extend_from_slice(input.data);
48+
49+
Image::with_bytes(layout, &data)
50+
};
51+
52+
let (source, buffer) = {
53+
let [buffer, plane] = subplanes.as_mut().into_planes([0, 1]).unwrap();
54+
let inner = plane.layout().inner;
55+
(plane.with_layout(inner).unwrap(), buffer)
56+
};
57+
58+
let initially_empty = MatrixBytes::empty(input.layout.element());
59+
let mut target = Image::new(initially_empty);
60+
61+
target.assign(source.as_source());
62+
63+
// Layout must match, as must the bytes within the layout.
64+
assert_eq!(*target.layout(), input.layout);
65+
assert_eq!(target.as_buf().as_bytes(), input.data);
66+
67+
{
68+
let inner = buffer.layout().inner;
69+
let mut target = buffer.with_layout(inner).unwrap();
70+
target.assign(source.as_source()).expect("Enough space");
71+
72+
// Layout must match, as must the bytes within the layout.
73+
assert_eq!(*target.layout(), input.layout);
74+
assert_eq!(target.as_buf().as_bytes(), input.data);
75+
}
76+
}
77+
78+
#[test]
79+
fn layout_copy_cell() {
80+
let input = TestData::hello_img();
81+
82+
let mut subplanes = {
83+
let layout = PlaneBytes::new([input.layout, input.layout]);
84+
let offset = layout.plane_ref(1).unwrap().offset.get();
85+
86+
let mut data = vec![0u8; offset];
87+
data.extend_from_slice(input.data);
88+
89+
Image::with_bytes(layout, &data)
90+
};
91+
92+
let (source, buffer) = {
93+
let [buffer, plane] = subplanes.as_mut().into_planes([0, 1]).unwrap();
94+
let inner = plane.layout().inner;
95+
(plane.with_layout(inner).unwrap(), buffer)
96+
};
97+
98+
// Different to the owned case, we must reserve enough buffer.
99+
let mut cells = CellImage::new(input.layout);
100+
cells.assign(source.as_source()).expect("Enough space");
101+
102+
// Layout must match, as must the bytes within the layout.
103+
assert_eq!(*cells.layout(), input.layout);
104+
assert!(cells.as_cell_buf() == input.data);
105+
106+
{
107+
let inner = buffer.layout().inner;
108+
let mut target = buffer.with_layout(inner).unwrap();
109+
target.assign(cells.as_source()).expect("Enough space");
110+
111+
// Layout must match, as must the bytes within the layout.
112+
assert_eq!(*target.layout(), input.layout);
113+
assert_eq!(target.as_buf().as_bytes(), input.data);
114+
}
115+
}
116+
117+
#[test]
118+
fn layout_copy_atomic() {
119+
let input = TestData::hello_img();
120+
121+
let mut subplanes = {
122+
let layout = PlaneBytes::new([input.layout, input.layout]);
123+
let offset = layout.plane_ref(1).unwrap().offset.get();
124+
125+
let mut data = vec![0u8; offset];
126+
data.extend_from_slice(input.data);
127+
128+
Image::with_bytes(layout, &data)
129+
};
130+
131+
let (source, buffer) = {
132+
let [buffer, plane] = subplanes.as_mut().into_planes([0, 1]).unwrap();
133+
let inner = plane.layout().inner;
134+
(plane.with_layout(inner).unwrap(), buffer)
135+
};
136+
137+
// Different to the owned case, we must reserve enough buffer.
138+
let mut atomics = AtomicImage::new(input.layout);
139+
atomics.assign(source.as_source()).expect("Enough space");
140+
141+
// Layout must match, as must the bytes within the layout.
142+
assert_eq!(*atomics.layout(), input.layout);
143+
assert!(atomics.as_texels(U8).to_vec() == input.data);
144+
145+
{
146+
let inner = buffer.layout().inner;
147+
let mut target = buffer.with_layout(inner).unwrap();
148+
target.assign(atomics.as_source()).expect("Enough space");
149+
150+
// Layout must match, as must the bytes within the layout.
151+
assert_eq!(*target.layout(), input.layout);
152+
assert_eq!(target.as_buf().as_bytes(), input.data);
153+
}
154+
}
155+
21156
#[test]
22157
fn planar_io() {
23158
let input = TestData::hello_img();

0 commit comments

Comments
 (0)