summaryrefslogtreecommitdiff
path: root/test/test_base.c
blob: 3d9a4404f7ec765614001bbcf856c15554caf191 (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

#include <usual/base.h>

#include "test_common.h"

#include <string.h>

struct somestruct {
	char a, b, c;
};

static void test_ptr(void *p)
{
	/* offsetof */
	int_check(offsetof(struct somestruct, a), 0);
	int_check(offsetof(struct somestruct, b), 1);
	int_check(offsetof(struct somestruct, c), 2);

	/* container_of */
	{
		struct somestruct s = {'a', 'b', 'c'};
		char *pa = &s.a;
		char *pb = &s.b;
		char *pc = &s.c;
		struct somestruct *sa, *sb, *sc;
		sa = container_of(pa, struct somestruct, a);
		sb = container_of(pb, struct somestruct, b);
		sc = container_of(pc, struct somestruct, c);
		int_check(sa->a, 'a');
		int_check(sb->b, 'b');
		int_check(sc->c, 'c');
	}

	/* alignof */
	int_check(alignof(char), 1);
	int_check(alignof(short), 2);
	int_check(alignof(int), 4);

	/* CUSTOM_ALIGN */
	int_check(CUSTOM_ALIGN(1, 4), 4);
	int_check(CUSTOM_ALIGN(2, 4), 4);
	int_check(CUSTOM_ALIGN(3, 4), 4);
	int_check(CUSTOM_ALIGN(4, 4), 4);
	int_check(CUSTOM_ALIGN(5, 4), 8);
end:;
}

#ifdef _PACKED
struct packed {
	char a;
	int b;
	char c;
	short d;
} _PACKED;
#endif

static void test_misc(void *_p)
{
	int i_4[4];
	int i_2[2];
	short s_4[4];
	short s_2[2];

	int_check(ARRAY_NELEM(i_4), 4);
	int_check(ARRAY_NELEM(i_2), 2);
	int_check(ARRAY_NELEM(s_4), 4);
	int_check(ARRAY_NELEM(s_2), 2);

	int_check(strcmp(__func__, "test_misc"), 0);
#ifdef _PACKED
	int_check(sizeof(struct packed), 8);
#endif

end:;
}

struct testcase_t base_tests[] = {
	{ "ptr", test_ptr },
	{ "misc", test_misc },
	END_OF_TESTCASES
};