blob: 7dac1558fa8c8bfe3f88d5a9b9f0a8816a90e44d [file] [log] [blame]
Vadim Yanitskiy68930e82017-01-19 03:33:24 +07001#include <stdlib.h>
2#include <string.h>
3#include <stdio.h>
4#include <time.h>
5
6#include <osmocom/core/bits.h>
7#include <osmocom/core/conv.h>
8#include <osmocom/core/utils.h>
9
10#include "conv.h"
11
12static void fill_random(ubit_t *b, int n)
13{
14 int i;
15
16 for (i = 0; i < n; i++)
17 b[i] = random() & 1;
18}
19
20int do_check(const struct conv_test_vector *test)
21{
22 ubit_t *bu0, *bu1;
23 sbit_t *bs;
24 int len, j;
25
26 bu0 = malloc(sizeof(ubit_t) * MAX_LEN_BITS);
27 bu1 = malloc(sizeof(ubit_t) * MAX_LEN_BITS);
28 bs = malloc(sizeof(sbit_t) * MAX_LEN_BITS);
29
30 srandom(time(NULL));
31
32 /* Test name */
33 printf("[+] Testing: %s\n", test->name);
34
35 /* Check length */
36 len = osmo_conv_get_input_length(test->code, 0);
37 printf("[.] Input length : ret = %3d exp = %3d -> %s\n",
38 len, test->in_len, len == test->in_len ? "OK" : "Bad !");
39
40 if (len != test->in_len) {
41 fprintf(stderr, "[!] Failure for input length computation\n");
42 return -1;
43 }
44
45 len = osmo_conv_get_output_length(test->code, 0);
46 printf("[.] Output length : ret = %3d exp = %3d -> %s\n",
47 len, test->out_len, len == test->out_len ? "OK" : "Bad !");
48
49 if (len != test->out_len) {
50 fprintf(stderr, "[!] Failure for output length computation\n");
51 return -1;
52 }
53
54 /* Check pre-computed vector */
55 if (test->has_vec) {
56 printf("[.] Pre computed vector checks:\n");
57
58 printf("[..] Encoding: ");
59
60 osmo_pbit2ubit(bu0, test->vec_in, test->in_len);
61
62 len = osmo_conv_encode(test->code, bu0, bu1);
63 if (len != test->out_len) {
64 printf("ERROR !\n");
65 fprintf(stderr, "[!] Failed encoding length check\n");
66 return -1;
67 }
68
69 osmo_pbit2ubit(bu0, test->vec_out, test->out_len);
70
71 if (memcmp(bu0, bu1, test->out_len)) {
72 printf("ERROR !\n");
73 fprintf(stderr, "[!] Failed encoding: Results don't match\n");
74 return -1;
75 };
76
77 printf("OK\n");
78
79
80 printf("[..] Decoding: ");
81
82 osmo_ubit2sbit(bs, bu0, len);
83
84 len = osmo_conv_decode(test->code, bs, bu1);
85 if (len != 0) {
86 printf("ERROR !\n");
87 fprintf(stderr, "[!] Failed decoding: non-zero path (%d)\n", len);
88 return -1;
89 }
90
91 osmo_pbit2ubit(bu0, test->vec_in, test->in_len);
92
93 if (memcmp(bu0, bu1, test->in_len)) {
94 printf("ERROR !\n");
95 fprintf(stderr, "[!] Failed decoding: Results don't match\n");
96 return -1;
97 }
98
99 printf("OK\n");
100 }
101
102 /* Check random vector */
103 printf("[.] Random vector checks:\n");
104
105 for (j = 0; j < 3; j++) {
106 printf("[..] Encoding / Decoding cycle : ");
107
108 fill_random(bu0, test->in_len);
109
110 len = osmo_conv_encode(test->code, bu0, bu1);
111 if (len != test->out_len) {
112 printf("ERROR !\n");
113 fprintf(stderr, "[!] Failed encoding length check\n");
114 return -1;
115 }
116
117 osmo_ubit2sbit(bs, bu1, len);
118
119 len = osmo_conv_decode(test->code, bs, bu1);
120 if (len != 0) {
121 printf("ERROR !\n");
122 fprintf(stderr, "[!] Failed decoding: non-zero path (%d)\n", len);
123 return -1;
124 }
125
126 if (memcmp(bu0, bu1, test->in_len)) {
127 printf("ERROR !\n");
128 fprintf(stderr, "[!] Failed decoding: Results don't match\n");
129 return -1;
130 }
131
132 printf("OK\n");
133 }
134
135 /* Spacing */
136 printf("\n");
137
138 free(bs);
139 free(bu1);
140 free(bu0);
141
142 return 0;
143}