| 1 | #ifndef __DEPACKETIZERDSPTEST_H__ |
|---|
| 2 | #define __DEPACKETIZERDSPTEST_H__ |
|---|
| 3 | |
|---|
| 4 | #include <cxxtest/TestSuite.h> |
|---|
| 5 | #include "src/PacketizerDSP.h" |
|---|
| 6 | |
|---|
| 7 | // |
|---|
| 8 | // A simple test suite: Just inherit CxxTest::TestSuite and write tests! |
|---|
| 9 | // |
|---|
| 10 | // NOTE: " : public CxxTest::TestSuite" must be on the same line as the class |
|---|
| 11 | // definition, otherwise the python script does not recognize it |
|---|
| 12 | // as a test class |
|---|
| 13 | class Depacketizer_testsuite1 : public CxxTest::TestSuite, |
|---|
| 14 | public PacketDecoder |
|---|
| 15 | { |
|---|
| 16 | public: |
|---|
| 17 | void test_CorrelateSequence() { |
|---|
| 18 | int r(0); |
|---|
| 19 | |
|---|
| 20 | // Test for positive P/N sync code correlation |
|---|
| 21 | for (unsigned int i=0; i<63; i++) |
|---|
| 22 | binary_sequence_push( pn_sync_buffer, pnSyncCode[i] ); |
|---|
| 23 | |
|---|
| 24 | r = binary_sequence_correlate( pn_sync_buffer, pn_sync_code ); |
|---|
| 25 | TS_ASSERT_EQUALS( r, 63 ); |
|---|
| 26 | |
|---|
| 27 | // Test for negative P/N sync code correlation |
|---|
| 28 | for (unsigned int i=0; i<63; i++) |
|---|
| 29 | binary_sequence_push( pn_sync_buffer, 1-pnSyncCode[i] ); |
|---|
| 30 | |
|---|
| 31 | r = binary_sequence_correlate( pn_sync_buffer, pn_sync_code ); |
|---|
| 32 | TS_ASSERT_EQUALS( r, -63 ); |
|---|
| 33 | |
|---|
| 34 | // Test for positive P/N control code correlation |
|---|
| 35 | for (unsigned int i=0; i<7; i++) |
|---|
| 36 | binary_sequence_push( pn_control_buffer, pnControlCode[i] ); |
|---|
| 37 | |
|---|
| 38 | r = binary_sequence_correlate( pn_control_buffer, pn_control_code ); |
|---|
| 39 | TS_ASSERT_EQUALS( r, 7 ); |
|---|
| 40 | |
|---|
| 41 | // Test for positive P/N control code correlation |
|---|
| 42 | for (unsigned int i=0; i<7; i++) |
|---|
| 43 | binary_sequence_push( pn_control_buffer, 1-pnControlCode[i] ); |
|---|
| 44 | |
|---|
| 45 | r = binary_sequence_correlate( pn_control_buffer, pn_control_code ); |
|---|
| 46 | TS_ASSERT_EQUALS( r, -7 ); |
|---|
| 47 | |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void test_ExtractSubheader_01() { |
|---|
| 51 | ConfigurePacketType( PACKET_RAW_400 ); |
|---|
| 52 | unsigned int N(packet_subheader_length); |
|---|
| 53 | |
|---|
| 54 | unsigned char * input = (unsigned char*) malloc( N ); |
|---|
| 55 | for (unsigned int i=0; i<N; i++) |
|---|
| 56 | input[i] = rand() & 0x01; |
|---|
| 57 | |
|---|
| 58 | unsigned char * output = (unsigned char*) malloc( 256 ); |
|---|
| 59 | |
|---|
| 60 | unsigned int num_read; |
|---|
| 61 | unsigned int num_written(0); |
|---|
| 62 | bool subheader_extracted; |
|---|
| 63 | subheader_extracted = ExtractSubheader( input, N, num_read, output, 256, num_written ); |
|---|
| 64 | |
|---|
| 65 | TS_ASSERT_EQUALS( subheader_extracted, true ); |
|---|
| 66 | TS_ASSERT_EQUALS( num_written, N ); |
|---|
| 67 | TS_ASSERT_SAME_DATA( input, output, N ); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | // reset output; try again but break input bufferinto pieces |
|---|
| 71 | // First attempt: read first 8 bits |
|---|
| 72 | memset(output, 0, 256); |
|---|
| 73 | num_written = 0; |
|---|
| 74 | num_subheader_bits_read = 0; |
|---|
| 75 | unsigned int n(0); |
|---|
| 76 | subheader_extracted = ExtractSubheader( input, 8, num_read, output, 256, num_written ); |
|---|
| 77 | n+=num_written; |
|---|
| 78 | TS_ASSERT_EQUALS( subheader_extracted, false ); |
|---|
| 79 | TS_ASSERT_EQUALS( num_subheader_bits_read, 8 ); |
|---|
| 80 | TS_ASSERT_EQUALS( num_read, 8 ); |
|---|
| 81 | TS_ASSERT_EQUALS( num_written, 8 ); |
|---|
| 82 | TS_ASSERT_EQUALS( n, 8 ); |
|---|
| 83 | |
|---|
| 84 | // Second attempt: read remaining 13 bits |
|---|
| 85 | subheader_extracted = ExtractSubheader( input+n, 13, num_read, output+n, 256-n, num_written ); |
|---|
| 86 | n+=num_written; |
|---|
| 87 | TS_ASSERT_EQUALS( subheader_extracted, true ); |
|---|
| 88 | TS_ASSERT_EQUALS( num_subheader_bits_read, 21 ); |
|---|
| 89 | TS_ASSERT_EQUALS( num_read, 13 ); |
|---|
| 90 | TS_ASSERT_EQUALS( num_written, 13 ); |
|---|
| 91 | TS_ASSERT_EQUALS( n, 21 ); |
|---|
| 92 | |
|---|
| 93 | TS_ASSERT_EQUALS( n, N ); |
|---|
| 94 | TS_ASSERT_SAME_DATA( input, output, N ); |
|---|
| 95 | |
|---|
| 96 | free(input); |
|---|
| 97 | free(output); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | // |
|---|
| 101 | // Basic test for creating and extracting a packet of type PACKET_RAW_400 |
|---|
| 102 | // |
|---|
| 103 | void test_Extract_PACKET_RAW_400_01() { |
|---|
| 104 | PacketEncoder p; |
|---|
| 105 | |
|---|
| 106 | // generate input buffer |
|---|
| 107 | unsigned char * input; |
|---|
| 108 | input = (unsigned char*) calloc( 400, sizeof(char) ); |
|---|
| 109 | input[0] = 1; |
|---|
| 110 | input[1] = 0; |
|---|
| 111 | input[2] = 1; |
|---|
| 112 | |
|---|
| 113 | // generate temporary buffer |
|---|
| 114 | unsigned char * buf = (unsigned char*) calloc( 512, sizeof(char) ); |
|---|
| 115 | |
|---|
| 116 | // generate packet buffer for encoded packet |
|---|
| 117 | unsigned char * packet; |
|---|
| 118 | packet = (unsigned char*) calloc( 1024, sizeof(char) ); |
|---|
| 119 | memset( packet, 0x01, 1024*sizeof(char) ); |
|---|
| 120 | |
|---|
| 121 | // generate output buffer for decoded packet |
|---|
| 122 | unsigned char * output; |
|---|
| 123 | output = (unsigned char*) calloc( 1024, sizeof(char) ); |
|---|
| 124 | memset( output, 0x01, 1024*sizeof(char) ); |
|---|
| 125 | |
|---|
| 126 | bool packet_header_found; |
|---|
| 127 | bool packet_header_extracted; |
|---|
| 128 | bool packet_header_decoded; |
|---|
| 129 | bool packet_subheader_extracted; |
|---|
| 130 | bool packet_subheader_decoded; |
|---|
| 131 | bool packet_data_extracted; |
|---|
| 132 | unsigned int n; |
|---|
| 133 | |
|---|
| 134 | // try to extract packet header before packet has been assembled |
|---|
| 135 | packet_header_found = FindHeader( packet, 1024, n ); |
|---|
| 136 | TS_ASSERT_EQUALS(packet_header_found, false); // should not have found header |
|---|
| 137 | TS_ASSERT_EQUALS(n, 1024); // should have read entire buffer |
|---|
| 138 | |
|---|
| 139 | // |
|---|
| 140 | // ASSEMBLE PACKET |
|---|
| 141 | // |
|---|
| 142 | |
|---|
| 143 | // assemble packet |
|---|
| 144 | unsigned int num_read; |
|---|
| 145 | unsigned int num_written; |
|---|
| 146 | p.packet_id = 6; |
|---|
| 147 | p.ConfigurePacketType( PACKET_RAW_400 ); |
|---|
| 148 | p.AssemblePacket( input, 400, num_read, packet, 1024, num_written ); |
|---|
| 149 | |
|---|
| 150 | // |
|---|
| 151 | // EXTRACT PACKET |
|---|
| 152 | // |
|---|
| 153 | |
|---|
| 154 | // |
|---|
| 155 | // MODE 0: FIND_HEADER |
|---|
| 156 | // |
|---|
| 157 | |
|---|
| 158 | // try to find packet header |
|---|
| 159 | unsigned int num_read_total(0); // total bits read from input |
|---|
| 160 | unsigned int nr; // number of bits read from input on each function call |
|---|
| 161 | unsigned int nw; // number of bits written to output on each function call |
|---|
| 162 | packet_header_found = FindHeader( packet+num_read_total, 1024, nr ); |
|---|
| 163 | num_read_total += nr; |
|---|
| 164 | TS_ASSERT_EQUALS(packet_header_found, true); // should have found header |
|---|
| 165 | TS_ASSERT_EQUALS(nr, PN_SYNC_CODE_LENGTH); // should have only read 63 values |
|---|
| 166 | |
|---|
| 167 | // |
|---|
| 168 | // MODE 1: EXTRACT_HEADER |
|---|
| 169 | // |
|---|
| 170 | |
|---|
| 171 | // try to extract packet type |
|---|
| 172 | packet_header_extracted = ExtractHeader( packet+num_read_total, PN_CONTROL_CODE_LENGTH, nr, buf, 256, nw ); |
|---|
| 173 | num_read_total += nr; |
|---|
| 174 | // should not have extracted packet type because buffer was said to only be |
|---|
| 175 | // 7 samples long |
|---|
| 176 | TS_ASSERT_EQUALS(packet_header_extracted, false); |
|---|
| 177 | TS_ASSERT_EQUALS(nr, PN_CONTROL_CODE_LENGTH); // should have only read 1 control code |
|---|
| 178 | |
|---|
| 179 | packet_header_extracted = ExtractHeader( packet+num_read_total, 500, nr, buf+nw, 256-nw, nw ); |
|---|
| 180 | num_read_total += nr; |
|---|
| 181 | TS_ASSERT_EQUALS(packet_header_extracted, true); |
|---|
| 182 | TS_ASSERT_EQUALS( nr, (NUM_CONTROL_CODES-1)*PN_CONTROL_CODE_LENGTH); // should have read remaining control codes |
|---|
| 183 | TS_ASSERT_EQUALS( num_read_total, PN_SYNC_CODE_LENGTH + NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH); |
|---|
| 184 | |
|---|
| 185 | // |
|---|
| 186 | // MODE 2: DECODE_HEADER |
|---|
| 187 | // |
|---|
| 188 | |
|---|
| 189 | packet_header_decoded = DecodeHeader(buf, 256); |
|---|
| 190 | TS_ASSERT_EQUALS(packet_header_decoded, true); |
|---|
| 191 | TS_ASSERT_EQUALS(packet_type, 0); |
|---|
| 192 | |
|---|
| 193 | // |
|---|
| 194 | // MODE 3: EXTRACT_SUBHEADER |
|---|
| 195 | // |
|---|
| 196 | |
|---|
| 197 | // try to extract the subheader; dump into temporary buffer |
|---|
| 198 | memset(buf, 0x01, 256); |
|---|
| 199 | packet_subheader_extracted = ExtractSubheader( packet+num_read_total, 500, nr, buf, 256, nw ); |
|---|
| 200 | num_read_total += nr; |
|---|
| 201 | TS_ASSERT_EQUALS(packet_subheader_extracted, true); |
|---|
| 202 | TS_ASSERT_EQUALS(nr, 21); |
|---|
| 203 | TS_ASSERT_EQUALS(nw, 21); |
|---|
| 204 | TS_ASSERT_EQUALS(num_read_total, 112); |
|---|
| 205 | |
|---|
| 206 | // |
|---|
| 207 | // MODE 4: DECODE_SUBHEADER |
|---|
| 208 | // |
|---|
| 209 | |
|---|
| 210 | // Decode subheader data |
|---|
| 211 | packet_id = 0; |
|---|
| 212 | packet_subheader_decoded = DecodeSubheader( buf, 256 ); |
|---|
| 213 | TS_ASSERT_EQUALS( packet_subheader_decoded, true ); |
|---|
| 214 | TS_ASSERT_EQUALS( packet_id, 6 ); |
|---|
| 215 | |
|---|
| 216 | // |
|---|
| 217 | // MODE 5: EXTRACT_PACKET_DATA |
|---|
| 218 | // |
|---|
| 219 | |
|---|
| 220 | // try to extract packet data |
|---|
| 221 | packet_data_extracted = ExtractPacketData( packet+num_read_total, 400, nr, output, 1024, nw ); |
|---|
| 222 | num_read_total += nr; |
|---|
| 223 | TS_ASSERT_EQUALS( packet_data_extracted, true ); |
|---|
| 224 | TS_ASSERT_EQUALS( nr, 400 ); |
|---|
| 225 | TS_ASSERT_EQUALS( nw, 400 ); |
|---|
| 226 | TS_ASSERT_SAME_DATA( input, output, 400 ); |
|---|
| 227 | |
|---|
| 228 | // |
|---|
| 229 | // MODE 6: DECODE_PACKET_DATA |
|---|
| 230 | // |
|---|
| 231 | |
|---|
| 232 | // No need to decode packet data for PACKET_RAW_400 |
|---|
| 233 | |
|---|
| 234 | free(input); |
|---|
| 235 | free(buf); |
|---|
| 236 | free(packet); |
|---|
| 237 | free(output); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | // |
|---|
| 241 | // Test extracting a packet with bit inversion |
|---|
| 242 | // |
|---|
| 243 | void test_Extract_PACKET_RAW_400_02() { |
|---|
| 244 | PacketEncoder p; |
|---|
| 245 | |
|---|
| 246 | // generate input buffer |
|---|
| 247 | unsigned char * input; |
|---|
| 248 | input = (unsigned char*) calloc( 400, sizeof(char) ); |
|---|
| 249 | input[0] = 1; |
|---|
| 250 | input[1] = 0; |
|---|
| 251 | input[2] = 1; |
|---|
| 252 | |
|---|
| 253 | // generate temporary buffer |
|---|
| 254 | unsigned char * buf = (unsigned char*) malloc( 512 ); |
|---|
| 255 | |
|---|
| 256 | // generate packet buffer for encoded packet |
|---|
| 257 | unsigned char * packet; |
|---|
| 258 | packet = (unsigned char*) calloc( 1024, sizeof(char) ); |
|---|
| 259 | memset( packet, 0x01, 1024*sizeof(char) ); |
|---|
| 260 | |
|---|
| 261 | // generate output buffer for decoded packet |
|---|
| 262 | unsigned char * output; |
|---|
| 263 | output = (unsigned char*) calloc( 1024, sizeof(char) ); |
|---|
| 264 | memset( output, 0x01, 1024*sizeof(char) ); |
|---|
| 265 | |
|---|
| 266 | bool packet_header_found; |
|---|
| 267 | bool packet_header_extracted; |
|---|
| 268 | bool packet_header_decoded; |
|---|
| 269 | bool packet_subheader_extracted; |
|---|
| 270 | bool packet_subheader_decoded; |
|---|
| 271 | bool packet_data_extracted; |
|---|
| 272 | |
|---|
| 273 | // assemble packet |
|---|
| 274 | unsigned int num_read; |
|---|
| 275 | unsigned int num_written; |
|---|
| 276 | p.packet_id = 6; |
|---|
| 277 | p.ConfigurePacketType( PACKET_RAW_400 ); |
|---|
| 278 | p.AssemblePacket( input, 400, num_read, packet, 1024, num_written ); |
|---|
| 279 | |
|---|
| 280 | // emulate channel |
|---|
| 281 | // flip all the bits |
|---|
| 282 | for (unsigned int i=0; i<1024; i++) |
|---|
| 283 | packet[i] = 1-packet[i]; |
|---|
| 284 | // reset packet id |
|---|
| 285 | packet_id = 0; |
|---|
| 286 | |
|---|
| 287 | // |
|---|
| 288 | // EXTRACT PACKET |
|---|
| 289 | // |
|---|
| 290 | |
|---|
| 291 | // |
|---|
| 292 | // MODE 0: FIND_HEADER |
|---|
| 293 | // |
|---|
| 294 | |
|---|
| 295 | // try to extract packet header |
|---|
| 296 | unsigned int num_read_total(0); // total bits read from input |
|---|
| 297 | unsigned int num_written_total(0); // total bits writtent to output buffer |
|---|
| 298 | unsigned int nr; // number of bits read from input on each function call |
|---|
| 299 | unsigned int nw; // number of bits written to output on each function call |
|---|
| 300 | packet_header_found = FindHeader( packet+num_read_total, 1024, nr ); |
|---|
| 301 | num_read_total += nr; |
|---|
| 302 | TS_ASSERT_EQUALS(packet_header_found, true); // should have found header |
|---|
| 303 | TS_ASSERT_EQUALS(nr, PN_SYNC_CODE_LENGTH); // should have only read 63 values |
|---|
| 304 | TS_ASSERT_EQUALS(rxy, -PN_SYNC_CODE_LENGTH);// correlator should be negative |
|---|
| 305 | |
|---|
| 306 | // |
|---|
| 307 | // MODE 1: EXTRACT_HEADER |
|---|
| 308 | // |
|---|
| 309 | |
|---|
| 310 | // try to extract packet type |
|---|
| 311 | packet_header_extracted = ExtractHeader( packet+num_read_total, 500, nr, buf, 256, nw ); |
|---|
| 312 | num_read_total += nr; |
|---|
| 313 | TS_ASSERT_EQUALS(packet_header_extracted, true); |
|---|
| 314 | TS_ASSERT_EQUALS( nr, NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH); // should have read control codes |
|---|
| 315 | TS_ASSERT_EQUALS( nw, NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH); // should have written control codes |
|---|
| 316 | TS_ASSERT_EQUALS( num_read_total, PN_SYNC_CODE_LENGTH + NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH); |
|---|
| 317 | |
|---|
| 318 | // |
|---|
| 319 | // MODE 2: DECODE_HEADER |
|---|
| 320 | // |
|---|
| 321 | |
|---|
| 322 | packet_header_decoded = DecodeHeader(buf, 256); |
|---|
| 323 | TS_ASSERT_EQUALS(packet_header_decoded, true); |
|---|
| 324 | TS_ASSERT_EQUALS(packet_type, 0); |
|---|
| 325 | |
|---|
| 326 | // |
|---|
| 327 | // MODE 3: EXTRACT_SUBHEADER |
|---|
| 328 | // |
|---|
| 329 | |
|---|
| 330 | // try to extract the subheader; dump into temporary buffer |
|---|
| 331 | packet_subheader_extracted = ExtractSubheader( packet+num_read_total, 500, nr, buf, 256, nw ); |
|---|
| 332 | num_read_total += nr; |
|---|
| 333 | TS_ASSERT_EQUALS(packet_subheader_extracted, true); |
|---|
| 334 | TS_ASSERT_EQUALS(nr, 21); |
|---|
| 335 | // PN sync code : 63 |
|---|
| 336 | // PN control codes : 4x7 |
|---|
| 337 | // PACKET_RAW_400 subheader : 21 |
|---|
| 338 | // total : 112 |
|---|
| 339 | TS_ASSERT_EQUALS(num_read_total, 112); |
|---|
| 340 | |
|---|
| 341 | // |
|---|
| 342 | // MODE 4: DECODE_SUBHEADER |
|---|
| 343 | // |
|---|
| 344 | |
|---|
| 345 | // Decode subheader data |
|---|
| 346 | packet_id = 0; |
|---|
| 347 | packet_subheader_decoded = DecodeSubheader( buf, 256 ); |
|---|
| 348 | TS_ASSERT_EQUALS( packet_subheader_decoded, true ); |
|---|
| 349 | TS_ASSERT_EQUALS( packet_id, 6 ); |
|---|
| 350 | |
|---|
| 351 | // |
|---|
| 352 | // MODE 5: EXTRACT_PACKET_DATA |
|---|
| 353 | // |
|---|
| 354 | |
|---|
| 355 | // try to extract packet data in pieces |
|---|
| 356 | // First attempt: read only 150 bits |
|---|
| 357 | packet_data_extracted = ExtractPacketData( packet+num_read_total, 150, nr, output, 1024, nw ); |
|---|
| 358 | num_read_total += nr; |
|---|
| 359 | num_written_total += nw; |
|---|
| 360 | TS_ASSERT_EQUALS( packet_data_extracted, false ); |
|---|
| 361 | TS_ASSERT_EQUALS( num_written_total, 150 ); |
|---|
| 362 | TS_ASSERT_EQUALS( nr, 150 ); |
|---|
| 363 | TS_ASSERT_EQUALS( nw, 150 ); |
|---|
| 364 | |
|---|
| 365 | // Second attempt: read remaining 250 bits |
|---|
| 366 | packet_data_extracted = ExtractPacketData( packet+num_read_total, 500, nr, output+150, 1024-150, nw ); |
|---|
| 367 | num_read_total += nr; |
|---|
| 368 | num_written_total += nw; |
|---|
| 369 | TS_ASSERT_EQUALS( packet_data_extracted, true ); |
|---|
| 370 | TS_ASSERT_EQUALS( num_read_total, 512 ); |
|---|
| 371 | TS_ASSERT_EQUALS( num_written_total, 400 ); |
|---|
| 372 | TS_ASSERT_EQUALS( nr, 250 ); |
|---|
| 373 | TS_ASSERT_EQUALS( nw, 250 ); |
|---|
| 374 | |
|---|
| 375 | // |
|---|
| 376 | // MODE 6: DECODE_PACKET_DATA |
|---|
| 377 | // |
|---|
| 378 | |
|---|
| 379 | // DecodePacketData(...) |
|---|
| 380 | // NOTE: no need to decode for PACKET_RAW_400 |
|---|
| 381 | |
|---|
| 382 | // output data should be the same, even though the packet was decoded with bit inversions; |
|---|
| 383 | // the P/N sync correlator should have detected this and corrected for it appropriately |
|---|
| 384 | TS_ASSERT_SAME_DATA( input, output, 400 ); |
|---|
| 385 | |
|---|
| 386 | free(input); |
|---|
| 387 | free(buf); |
|---|
| 388 | free(packet); |
|---|
| 389 | free(output); |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | }; |
|---|
| 393 | |
|---|
| 394 | #endif |
|---|
| 395 | |
|---|