-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
135 lines (117 loc) · 3.46 KB
/
test.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bitpack.h"
/* MinUnit: http://www.jera.com/techinfo/jtns/jtn002.html */
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(test) do { char *message = test(); \
if (message) return message; } while (0)
static char* test_write() {
uint8_t buffer[2] = { 0 };
bitpack_t pack = bitpack_create(buffer, 2);
mu_assert("error writing 4 bits", bitpack_write(&pack, 10, 4));
mu_assert("error writing 10 bits", bitpack_write(&pack, 1021, 10));
mu_assert("error writing 2 bits", bitpack_write(&pack, 3, 2));
mu_assert("bad buffer[0] value", buffer[0] == 175);
mu_assert("bad buffer[1] value", buffer[1] == 247);
return 0;
}
static char* test_read() {
/* 0b10101111, 0b11110111 */
uint8_t buffer[2] = { 175, 247 };
bitpack_t pack = bitpack_create(buffer, 2);
uint32_t value;
mu_assert("error reading 4 bits", bitpack_read(&pack, &value, 4));
mu_assert("bad value decoded, expecting 10", value == 10);
mu_assert("error reading 10 bits", bitpack_read(&pack, &value, 10));
mu_assert("bad value decoded, expecting 1021", value == 1021);
mu_assert("error reading 2 bits", bitpack_read(&pack, &value, 2));
mu_assert("bad value decoded, expecting 10", value == 3);
return 0;
}
bool encode_geolocation(bitpack_t *pack, double latitude, double longitude) {
/* Latitude sign */
if (!bitpack_write(pack, (latitude < 0) ? 1 : 0, 1)) {
return false;
}
/* Latitude degrees */
if (!bitpack_write(pack, trunc(abs(latitude)), 7)) {
return false;
}
/* Latitude decimal */
if (!bitpack_write(pack, abs(latitude * 10000) % 10000, 14)) {
return false;
}
/* Longitude sign */
if (!bitpack_write(pack, (longitude < 0) ? 1 : 0, 1)) {
return false;
}
/* Longitude degrees */
if (!bitpack_write(pack, trunc(abs(longitude)), 8)) {
return false;
}
/* Longitude decimal */
if (!bitpack_write(pack, abs(longitude * 10000) % 10000, 14)) {
return false;
}
return true;
}
bool decode_geolocation(bitpack_t *pack, double *latitude, double *longitude) {
uint32_t sign, degrees, decimal;
if (!bitpack_read(pack, &sign, 1)) {
return false;
}
if (!bitpack_read(pack, °rees, 7)) {
return false;
}
if (!bitpack_read(pack, &decimal, 14)) {
return false;
}
*latitude = degrees + (decimal / 10000.0);
if (sign == 1) {
*latitude = 0 - *latitude;
}
if (!bitpack_read(pack, &sign, 1)) {
return false;
}
if (!bitpack_read(pack, °rees, 8)) {
return false;
}
if (!bitpack_read(pack, &decimal, 14)) {
return false;
}
*longitude = degrees + (decimal / 10000.0);
if (sign == 1) {
*longitude = 0 - *longitude;
}
return true;
}
static char* test_geolocation() {
uint8_t buffer[6] = { 0 };
bitpack_t pack = bitpack_create(buffer, 6);
double latitude = -41.235259;
double longitude = +146.412828;
mu_assert("error encoding geolocation",
encode_geolocation(&pack, latitude, longitude));
pack = bitpack_create(buffer, 6);
mu_assert("error decoding geolocation",
decode_geolocation(&pack, &latitude, &longitude));
mu_assert("bad latitude decoded", latitude == -41.2352);
mu_assert("bad longitude decoded", longitude == +146.4128);
return 0;
}
static char* all_tests() {
mu_run_test(test_write);
mu_run_test(test_read);
mu_run_test(test_geolocation);
return 0;
}
int main(int argc, char **argv) {
char *result = all_tests();
if (result != 0) {
printf("%s\n", result);
} else {
printf("ALL TESTS PASSED\n");
}
return result != 0;
}