blob: 19be6a61e807c2741f0d5b36e7d1dd40fb5a0c5b [file] [log] [blame]
Neels Hofmeyr086bd332020-09-18 18:00:50 +02001#include <stdio.h>
2
3#include <osmocom/core/utils.h>
4#include <osmocom/core/msgb.h>
5#include <osmocom/gsm/gad.h>
6
Harald Weltee61d4592022-11-03 11:05:58 +01007void test_gad_lat_lon_dec_enc_stability(void)
Neels Hofmeyr086bd332020-09-18 18:00:50 +02008{
9 uint32_t lat_enc;
10 uint32_t lon_enc;
11 printf("--- %s\n", __func__);
12 for (lat_enc = 0x0; lat_enc <= 0xffffff; lat_enc++) {
13 int32_t lat_dec = osmo_gad_dec_lat(lat_enc);
14 uint32_t enc2 = osmo_gad_enc_lat(lat_dec);
15 uint32_t want_enc = lat_enc;
16 /* "-0" == 0, because the highest bit is defined as a sign bit. */
17 if (lat_enc == 0x800000)
18 want_enc = 0;
19 if (enc2 != want_enc) {
20 printf("ERR: lat=%u --> %d --> %u\n", lat_enc, lat_dec, enc2);
21 printf("%d -> %u\n", lat_dec + 1, osmo_gad_enc_lat(lat_dec + 1));
22 OSMO_ASSERT(false);
23 }
24 }
25 printf("osmo_gad_dec_lat() -> osmo_gad_enc_lat() of %u values successful\n", lat_enc);
26 for (lon_enc = 0; lon_enc <= 0xffffff; lon_enc++) {
27 int32_t lon_dec = osmo_gad_dec_lon(lon_enc);
28 uint32_t enc2 = osmo_gad_enc_lon(lon_dec);
29 uint32_t want_enc = lon_enc;
30 if (enc2 != want_enc) {
31 printf("ERR: lon=%u 0x%x --> %d --> %u\n", lon_enc, lon_enc, lon_dec, enc2);
32 printf("%d -> %u\n", lon_dec + 1, osmo_gad_enc_lon(lon_dec + 1));
33 printf("%d -> %u\n", lon_dec - 1, osmo_gad_enc_lon(lon_dec - 1));
34 OSMO_ASSERT(false);
35 }
36 }
37 printf("osmo_gad_dec_lon() -> osmo_gad_enc_lon() of %u values successful\n", lon_enc);
38}
39
40struct osmo_gad gad_test_values[] = {
41 {
42 .type = GAD_TYPE_ELL_POINT_UNC_CIRCLE,
43 .ell_point_unc_circle = {
44 /* Values rounded to the nearest encodable value, for test result matching */
45 .lat = 23000006,
46 .lon = 42000002,
47 .unc = 442592,
48 },
49 },
50};
51
Harald Weltee61d4592022-11-03 11:05:58 +010052void test_gad_enc_dec(void)
Neels Hofmeyr086bd332020-09-18 18:00:50 +020053{
54 int i;
55 printf("--- %s\n", __func__);
56
57 for (i = 0; i < ARRAY_SIZE(gad_test_values); i++) {
58 struct osmo_gad *t = &gad_test_values[i];
59 struct msgb *msg = msgb_alloc(1024, __func__);
60 union gad_raw raw_write;
61 union gad_raw raw_read;
62 struct osmo_gad dec_pdu;
63 int rc;
64 struct osmo_gad_err *err;
65 void *loop_ctx = msg;
66 rc = osmo_gad_enc(&raw_write, t);
67 if (rc <= 0) {
68 printf("[%d] %s: ERROR: osmo_gad_enc() failed\n", i, osmo_gad_type_name(t->type));
69 goto loop_end;
70 }
71 rc = osmo_gad_raw_write(msg, &raw_write);
72 if (rc <= 0) {
73 printf("[%d] %s: ERROR: osmo_gad_raw_write() failed\n", i, osmo_gad_type_name(t->type));
74 goto loop_end;
75 }
76 if (rc != msg->len) {
77 printf("[%d] %s: ERROR: osmo_gad_raw_write() returned length %d but msgb has %d bytes\n",
78 i, osmo_gad_type_name(t->type),
79 rc, msg->len);
80 goto loop_end;
81 }
82
83 memset(&raw_read, 0xff, sizeof(raw_read));
84 rc = osmo_gad_raw_read(&raw_read, &err, loop_ctx, msg->data, msg->len);
85 if (rc) {
86 printf("[%d] ERROR: osmo_gad_raw_read() failed: %s\n", i, err->logmsg);
87 printf(" encoded data: %s\n", osmo_hexdump(msg->data, msg->len));
88 goto loop_end;
89 }
90
91 memset(&dec_pdu, 0xff, sizeof(dec_pdu));
92 rc = osmo_gad_dec(&dec_pdu, &err, loop_ctx, &raw_read);
93 if (rc) {
94 printf("[%d] ERROR: failed to decode pdu: %s\n", i, err->logmsg);
95 printf(" encoded data: %s\n", osmo_hexdump(msg->data, msg->len));
96 goto loop_end;
97 }
98
99 if (memcmp(t, &dec_pdu, sizeof(dec_pdu))) {
100 char strbuf[128];
101 printf("[%d] %s: ERROR: decoded PDU != encoded PDU\n", i,
102 osmo_gad_type_name(t->type));
103 osmo_gad_to_str_buf(strbuf, sizeof(strbuf), t);
104 printf(" original struct: %s\n", strbuf);
105 osmo_gad_to_str_buf(strbuf, sizeof(strbuf), &dec_pdu);
106 printf(" decoded struct: %s\n", strbuf);
107 goto loop_end;
108 }
109
110 printf("[%d] %s: ok\n", i, osmo_gad_type_name(t->type));
111 printf(" encoded data: %s\n", msgb_hexdump(msg));
112
113loop_end:
114 msgb_free(msg);
115 }
116}
117
Harald Weltee61d4592022-11-03 11:05:58 +0100118void test_gad_to_str(void)
Neels Hofmeyr086bd332020-09-18 18:00:50 +0200119{
120 int i;
121 printf("--- %s\n", __func__);
122
123 for (i = 0; i < ARRAY_SIZE(gad_test_values); i++) {
124 struct osmo_gad *t = &gad_test_values[i];
125 char buf[1024];
126 int rc;
127 rc = osmo_gad_to_str_buf(buf, sizeof(buf), t);
128
129 printf("[%d] ", i);
130 if (rc <= 0)
131 printf("%s: ERROR: osmo_gad_to_str_buf() failed\n", osmo_gad_type_name(t->type));
132 else
133 printf("%s\n", buf);
134 }
135}
136
Harald Weltee61d4592022-11-03 11:05:58 +0100137int main(int argc, char **argv)
Neels Hofmeyr086bd332020-09-18 18:00:50 +0200138{
139 test_gad_lat_lon_dec_enc_stability();
140 test_gad_enc_dec();
141 test_gad_to_str();
142 return 0;
143}