aboutsummaryrefslogtreecommitdiff
path: root/common/object/src/tests.rs
blob: 143782c1d170186000e2b9ca0a8751bf6ff30b56 (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
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{ObjectBuffer, Tag, TypedTag};
use std::marker::PhantomData;

const NAME: TypedTag<&str> = TypedTag(Tag(15), PhantomData);
const AGE: TypedTag<u32> = TypedTag(Tag(13), PhantomData);
const FRIEND: TypedTag<&str> = TypedTag(Tag(54321), PhantomData);

fn test_object() -> ObjectBuffer {
    ObjectBuffer::new(&mut [
        (NAME.0, &"Bob"),
        (AGE.0, &35_u32),
        (FRIEND.0, &"Alice"),
        (FRIEND.0, &"Charlie"),
    ])
}

#[test]
fn read_single_field() {
    let bob = test_object();
    let bob = bob.as_object();
    assert_eq!(bob.get(NAME), Some("Bob"));
    assert_eq!(bob.get(AGE), Some(35));
}

#[test]
fn read_multi_field() {
    let bob = test_object();
    let bob = bob.as_object();

    let mut friends = bob.iter(FRIEND);
    assert_eq!(friends.next(), Some("Alice"));
    assert_eq!(friends.next(), Some("Charlie"));
    assert_eq!(friends.next(), None);
}

#[test]
fn vec_align_test() {
    let mut c = 0;
    for _ in 0..10_000 {
        let x = Vec::<u8>::with_capacity(16);
        if x.as_ptr().align_offset(4) == 0 {
            c += 1;
        }
    }
    assert_eq!(c, 10_000, "correctly aligned vecs by system allocator")
}