blob: 16f6ea39d048a963dd2c127937ad0c82ab639787 [file] [log] [blame]
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +02001
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#include <openbsc/arfcn_range_encode.h>
8
9#include <osmocom/core/utils.h>
10
11#define DBG(...)
12
13#define VERIFY(res, cmp, wanted) \
14 if (!(res cmp wanted)) { \
15 printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \
16 __FILE__, __LINE__, res, # cmp, wanted); \
17 }
18
19
20static int freqs1[] = {
21 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
22};
23
24static int freqs2[] = {
25 402, 460, 1, 67, 131, 197, 272, 347,
26};
27
28static int freqs3[] = {
29 68, 128, 198, 279, 353, 398, 452,
30
31};
32
33static int w_out[] = {
34 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
35};
36
37static int range128[] = {
38 1, 1 + 127,
39};
40
41static int range256[] = {
42 1, 1 + 128,
43};
44
45static int range512[] = {
46 1, 1+ 511,
47};
48
49
50static void test_arfcn_filter()
51{
52 int arfcns[50], i, res, f0_included;
53 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
54 arfcns[i] = (i + 1) * 2;
55
56 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
57 f0_included = 24;
58 res = range_enc_filter_arfcns(ARFCN_RANGE_512, arfcns, ARRAY_SIZE(arfcns),
59 arfcns[0], &f0_included);
60 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
61 VERIFY(f0_included, ==, 0);
62 for (i = 0; i < res; ++i)
63 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
64
65 /* check with range1024 */
66 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
67 arfcns[i] = (i + 1) * 2;
68 res = range_enc_filter_arfcns(ARFCN_RANGE_1024, arfcns, ARRAY_SIZE(arfcns),
69 arfcns[0], &f0_included);
70 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
71 VERIFY(f0_included, ==, 1);
72 for (i = 0; i < res; ++i)
73 VERIFY(arfcns[i], ==, ((i + 2) * 2) - 1);
74
75 /* check with range1024, not included */
76 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
77 arfcns[i] = (i + 1) * 2;
78 res = range_enc_filter_arfcns(ARFCN_RANGE_1024, arfcns, ARRAY_SIZE(arfcns),
79 11, &f0_included);
80 VERIFY(res, ==, ARRAY_SIZE(arfcns));
81 VERIFY(f0_included, ==, 0);
82 for (i = 0; i < res; ++i)
83 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
84}
85
86static void test_print_encoding()
87{
88 int rc;
89 int w[17];
90 uint8_t chan_list[16];
91 memset(chan_list, 0x23, sizeof(chan_list));
92
93 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
94 switch (rc % 3) {
95 case 0:
96 w[rc] = 0xAAAA;
97 break;
98 case 1:
99 w[rc] = 0x5555;
100 break;
101 case 2:
102 w[rc] = 0x9696;
103 break;
104 }
105
106 rc = range_enc_range512(chan_list, 0x96, w);
107 VERIFY(rc, ==, 0);
108
109 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
110}
111
112int main(int argc, char **argv)
113{
114 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
115 int i, f0 = 0xFFFFFF;
116
117 memset(&ws[0], 0x23, sizeof(ws));
118
119 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
120 printf("Element is: %d => freqs[i] = %d\n", i, freqs1[i]);
121 VERIFY(i, ==, 2);
122
123 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
124 printf("Element is: %d => freqs[i] = %d\n", i, freqs2[i]);
125 VERIFY(i, ==, 2);
126
127 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
128 printf("Element is: %d => freqs[i] = %d\n", i, freqs3[i]);
129 VERIFY(i, ==, 0);
130
131 i = range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
132 VERIFY(i, ==, 0);
133
134 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
135 printf("w[%d]=%d\n", i, ws[i]);
136 VERIFY(ws[i], ==, w_out[i]);
137 }
138
139 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
140 VERIFY(i, ==, ARFCN_RANGE_128);
141 VERIFY(f0, ==, 1);
142
143 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
144 VERIFY(i, ==, ARFCN_RANGE_256);
145 VERIFY(f0, ==, 1);
146
147 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
148 VERIFY(i, ==, ARFCN_RANGE_512);
149 VERIFY(f0, ==, 1);
150
151
152 test_arfcn_filter();
153 test_print_encoding();
154
155 return 0;
156}