-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
133 lines (113 loc) · 3.26 KB
/
test.c
File metadata and controls
133 lines (113 loc) · 3.26 KB
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pie.h"
#define color_0 0x6A, 0xBE, 0x30
#define color_1 0xFF, 0xFF, 0xFF
#define color_2 0x00, 0x00, 0x00
#define color_3 0x5B, 0x6E, 0xE1
pie_u8 bytes[] = {
0x50, 0x49, 0x45, /* Magic "PIE" */ 0x02, /* Version */
0x02, 0x00, 0x00, 0x00, /* Flags (2 - palette included) */
0x08, 0x00, /* Width */ 0x08, 0x00, /* Height */
0x17, 0x00, 0x00, 0x00, /* Data Pair Count */
// RLE encoded data [Index, Run Length]
0x01, 0x01,
0x00, 0x06,
0x01, 0x06,
0x02, 0x02,
0x01, 0x01,
0x02, 0x04,
0x01, 0x01,
0x02, 0x02,
0x01, 0x01,
0x02, 0x04,
0x01, 0x01,
0x02, 0x02,
0x01, 0x06,
0x02, 0x02,
0x01, 0x01,
0x02, 0x06,
0x01, 0x02,
0x02, 0x06,
0x01, 0x01,
0x02, 0x01,
0x01, 0x01,
0x03, 0x06,
0x01, 0x01,
// Optional Palette.
color_0,
color_1,
color_2,
color_3
};
pie_u8 expected_pixel_data[] = {
color_1, color_0, color_0, color_0, color_0, color_0, color_0, color_1,
color_1, color_1, color_1, color_1, color_1, color_2, color_2, color_1,
color_2, color_2, color_2, color_2, color_1, color_2, color_2, color_1,
color_2, color_2, color_2, color_2, color_1, color_2, color_2, color_1,
color_1, color_1, color_1, color_1, color_1, color_2, color_2, color_1,
color_2, color_2, color_2, color_2, color_2, color_2, color_1, color_1,
color_2, color_2, color_2, color_2, color_2, color_2, color_1, color_2,
color_1, color_3, color_3, color_3, color_3, color_3, color_3, color_1,
};
pie_u8 palette[] = {
color_0,
color_1,
color_2,
color_3
};
// Not including this in the library... Why?
pie_u32 pie_pixel_count(pie_u8 *b) {
pie_header *h = (pie_header *)b;
pie_u8 *data = (pie_u8 *)(h + 1);
pie_u32 data_pairs = (pie_u32)h->length;
pie_u32 pixel_count = 0;
for (pie_u32 i = 0; i < data_pairs; i += 1) {
pixel_count += (pie_u32)(data[i * 2 + 1]);
}
return pixel_count;
}
void *test_alloc(pie_u32 s, void *c) {
(void)c;
return malloc(s);
}
void test_free(pie_u32 s, void *p, void *c) {
(void)s;
(void)c;
free(p);
}
int main(void) {
pie_header h = pie_header_from_bytes(bytes);
pie_u32 pixel_count = pie_pixel_count(bytes);
pie_u32 expected_pixel_count = (pie_u32)h.width * (pie_u32)h.height;
assert(pixel_count == expected_pixel_count);
pie_u32 stride = pie_stride(&h);
int ep = pie_has_embedded_palette(&h);
assert(h.magic[0] == 'P');
assert(h.magic[1] == 'I');
assert(h.magic[2] == 'E');
assert(h.version == 2);
assert(h.flags == 0x2);
assert(h.width == 8);
assert(h.height == 8);
assert(h.length == 23);
assert(stride == 3);
assert(ep);
pie_u32 buffer_size = pixel_count * stride;
pie_allocator a = {
.alloc = test_alloc,
.free = test_free
};
pie_pixels p = pie_pixels_from_bytes(bytes, a);
pie_pixels p2 = pie_pixels_from_bytes_and_palette(bytes, palette, a);
int diff = memcmp(p.data, p2.data, buffer_size);
assert(diff == 0);
diff = memcmp(p.data, expected_pixel_data, buffer_size);
assert(diff == 0);
printf("All tests passed.\n");
printf("Press Enter to exit.\n");
getchar();
exit(0);
}