summaryrefslogtreecommitdiffstats
path: root/gfx/wr/peek-poke/tests/round_trip.rs
blob: 3134c207ec4d2f71f5fefe6afc2eb41eb21927c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2019 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use peek_poke::{Peek, PeekPoke, Poke};
use std::{fmt::Debug, marker::PhantomData};

fn poke_into<V: Peek + Poke>(a: &V) -> Vec<u8> {
    let mut v = <Vec<u8>>::with_capacity(<V>::max_size());
    let end_ptr = unsafe { a.poke_into(v.as_mut_ptr()) };
    let new_size = end_ptr as usize - v.as_ptr() as usize;
    assert!(new_size <= v.capacity());
    unsafe {
        v.set_len(new_size);
    }
    v
}

#[cfg(not(feature = "option_copy"))]
fn the_same<V>(a: V)
where
    V: Debug + Default + PartialEq + Peek + Poke,
{
    let v = poke_into(&a);
    let (b, end_ptr) = unsafe { peek_poke::peek_from_default(v.as_ptr()) };
    let size = end_ptr as usize - v.as_ptr() as usize;
    assert_eq!(size, v.len());
    assert_eq!(a, b);
}

#[cfg(feature = "option_copy")]
fn the_same<V>(a: V)
where
    V: Copy + Debug + PartialEq + Peek + Poke,
{
    let v = poke_into(&a);
    let mut b = a;
    let end_ptr = unsafe { b.peek_from(v.as_ptr()) };
    let size = end_ptr as usize - v.as_ptr() as usize;
    assert_eq!(size, v.len());
    assert_eq!(a, b);
}

#[test]
fn test_numbers() {
    // unsigned positive
    the_same(5u8);
    the_same(5u16);
    the_same(5u32);
    the_same(5u64);
    the_same(5usize);
    // signed positive
    the_same(5i8);
    the_same(5i16);
    the_same(5i32);
    the_same(5i64);
    the_same(5isize);
    // signed negative
    the_same(-5i8);
    the_same(-5i16);
    the_same(-5i32);
    the_same(-5i64);
    the_same(-5isize);
    // floating
    the_same(-100f32);
    the_same(0f32);
    the_same(5f32);
    the_same(-100f64);
    the_same(5f64);
}

#[test]
fn test_bool() {
    the_same(true);
    the_same(false);
}

#[cfg(any(feature = "option_copy", feature = "option_default"))]
#[test]
fn test_option() {
    the_same(Some(5usize));
    //the_same(Some("foo bar".to_string()));
    the_same(None::<usize>);
}

#[test]
fn test_fixed_size_array() {
    the_same([24u32; 32]);
    the_same([1u64, 2, 3, 4, 5, 6, 7, 8]);
    the_same([0u8; 19]);
}

#[test]
fn test_tuple() {
    the_same((1isize, ));
    the_same((1isize, 2isize, 3isize));
    the_same((1isize, ()));
}

#[test]
fn test_basic_struct() {
    #[derive(Copy, Clone, Debug, Default, PartialEq, PeekPoke)]
    struct Bar {
        a: u32,
        b: u32,
        c: u32,
        #[cfg(any(feature = "option_copy", feature = "option_default"))]
        d: Option<u32>,
    }

    the_same(Bar {
        a: 2,
        b: 4,
        c: 42,
        #[cfg(any(feature = "option_copy", feature = "option_default"))]
        d: None,
    });
}

#[test]
fn test_enum() {
    #[derive(Clone, Copy, Debug, PartialEq, PeekPoke)]
    enum TestEnum {
        NoArg,
        OneArg(usize),
        Args(usize, usize),
        AnotherNoArg,
        StructLike { x: usize, y: f32 },
    }

    impl Default for TestEnum {
        fn default() -> Self {
            TestEnum::NoArg
        }
    }

    the_same(TestEnum::NoArg);
    the_same(TestEnum::OneArg(4));
    the_same(TestEnum::Args(4, 5));
    the_same(TestEnum::AnotherNoArg);
    the_same(TestEnum::StructLike { x: 4, y: 3.14159 });
}

#[test]
fn test_enum_cstyle() {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, PartialEq, Eq, PeekPoke)]
    enum BorderStyle {
        None = 0,
        Solid = 1,
        Double = 2,
        Dotted = 3,
        Dashed = 4,
        Hidden = 5,
        Groove = 6,
        Ridge = 7,
        Inset = 8,
        Outset = 9,
    }

    impl Default for BorderStyle {
        fn default() -> Self {
            BorderStyle::None
        }
    }

    the_same(BorderStyle::None);
    the_same(BorderStyle::Solid);
    the_same(BorderStyle::Double);
    the_same(BorderStyle::Dotted);
    the_same(BorderStyle::Dashed);
    the_same(BorderStyle::Hidden);
    the_same(BorderStyle::Groove);
    the_same(BorderStyle::Ridge);
    the_same(BorderStyle::Inset);
    the_same(BorderStyle::Outset);
}

#[test]
fn test_phantom_data() {
    struct Bar;
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PeekPoke)]
    struct Foo {
        x: u32,
        y: u32,
        _marker: PhantomData<Bar>,
    }
    the_same(Foo {
        x: 19,
        y: 42,
        _marker: PhantomData,
    });
}

#[test]
fn test_generic() {
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PeekPoke)]
    struct Foo<T> {
        x: T,
        y: T,
    }
    the_same(Foo { x: 19.0, y: 42.0 });
}

#[test]
fn test_generic_enum() {
    #[derive(Clone, Copy, Debug, Default, PartialEq, PeekPoke)]
    pub struct PropertyBindingKey<T> {
        pub id: usize,
        _phantom: PhantomData<T>,
    }

    #[derive(Clone, Copy, Debug, PartialEq, PeekPoke)]
    pub enum PropertyBinding<T> {
        Value(T),
        Binding(PropertyBindingKey<T>, T),
    }

    impl<T: Default> Default for PropertyBinding<T> {
        fn default() -> Self {
            PropertyBinding::Value(Default::default())
        }
    }
}

#[cfg(all(feature = "extras", feature = "option_copy"))]
mod extra_tests {
    use super::*;
    use euclid::{Point2D, Rect, SideOffsets2D, Size2D, Transform3D, Vector2D};
    use std::mem::size_of;

    #[test]
    fn euclid_types() {
        the_same(Point2D::<f32>::new(1.0, 2.0));
        assert_eq!(Point2D::<f32>::max_size(), 2 * size_of::<f32>());

        the_same(Rect::<f32>::new(
            Point2D::<f32>::new(0.0, 0.0),
            Size2D::<f32>::new(100.0, 80.0),
        ));
        assert_eq!(Rect::<f32>::max_size(), 4 * size_of::<f32>());

        the_same(SideOffsets2D::<f32>::new(0.0, 10.0, -1.0, -10.0));
        assert_eq!(SideOffsets2D::<f32>::max_size(), 4 * size_of::<f32>());

        the_same(Transform3D::<f32>::identity());
        assert_eq!(Transform3D::<f32>::max_size(), 16 * size_of::<f32>());

        the_same(Vector2D::<f32>::new(1.0, 2.0));
        assert_eq!(Vector2D::<f32>::max_size(), 2 * size_of::<f32>());
    }

    #[test]
    fn webrender_api_types() {
        type PipelineSourceId = i32;
        #[derive(Clone, Copy, Debug, PartialEq, PeekPoke)]
        struct PipelineId(pub PipelineSourceId, pub u32);

        #[derive(Clone, Copy, Debug, PartialEq, PeekPoke)]
        struct ClipChainId(pub u64, pub PipelineId);

        #[derive(Clone, Copy, Debug, PartialEq, PeekPoke)]
        struct SpatialId(pub usize, pub PipelineId);

        the_same(PipelineId(42, 2));
        the_same(ClipChainId(19u64, PipelineId(42, 2)));
        the_same(SpatialId(19usize, PipelineId(42, 2)));
    }
}