root/experimental/components/Packetizer/trunk/Packetizer-metadata/tests/Depacketizer_testsuite.h @ 5428

Revision 5428, 13.6 KB (checked in by jgaeddert, 6 years ago)

changing API once again... *sigh* someday I will make up my mind

  • Property svn:eol-style set to native
Line 
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
13class Depacketizer_testsuite1 : public CxxTest::TestSuite,
14    public PacketDecoder
15{
16public:
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        char * input = (char*) malloc( N );
55        for (unsigned int i=0; i<N; i++)
56            input[i] = rand() & 0x01;
57
58        char * output = (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        char * input;
108        input = (char*) calloc( 400, sizeof(char) );
109        input[0] = 1;
110        input[1] = 0;
111        input[2] = 1;
112
113        // generate temporary buffer
114        char * buf = (char*) calloc( 512, sizeof(char) );
115
116        // generate packet buffer for encoded packet
117        char * packet;
118        packet = (char*) calloc( 1024, sizeof(char) );
119        memset( packet, 0x01, 1024*sizeof(char) );
120
121        // generate output buffer for decoded packet
122        char * output;
123        output = (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 num_written_total(0);  // total bits writtent to output buffer
161        unsigned int nr;                    // number of bits read from input on each function call
162        unsigned int nw;                    // number of bits written to output on each function call
163        packet_header_found = FindHeader( packet+num_read_total, 1024, nr );
164        num_read_total += nr;
165        TS_ASSERT_EQUALS(packet_header_found, true);       // should have found header
166        TS_ASSERT_EQUALS(nr, PN_SYNC_CODE_LENGTH);   // should have only read 63 values
167
168        //
169        // MODE 1: EXTRACT_HEADER
170        //
171
172        // try to extract packet type
173        packet_header_extracted = ExtractHeader( packet+num_read_total, PN_CONTROL_CODE_LENGTH, nr, buf, 256, nw );
174        num_read_total += nr;
175        // should not have extracted packet type because buffer was said to only be
176        // 7 samples long
177        TS_ASSERT_EQUALS(packet_header_extracted, false);
178        TS_ASSERT_EQUALS(nr, PN_CONTROL_CODE_LENGTH);    // should have only read 1 control code
179
180        packet_header_extracted = ExtractHeader( packet+num_read_total, 500, nr, buf+nw, 256-nw, nw );
181        num_read_total += nr;
182        TS_ASSERT_EQUALS(packet_header_extracted, true);
183        TS_ASSERT_EQUALS( nr, (NUM_CONTROL_CODES-1)*PN_CONTROL_CODE_LENGTH);    // should have read remaining control codes
184        TS_ASSERT_EQUALS( num_read_total, PN_SYNC_CODE_LENGTH + NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH);
185
186        //
187        // MODE 2: DECODE_HEADER
188        //
189
190        packet_header_decoded = DecodeHeader(buf, 256);
191        TS_ASSERT_EQUALS(packet_header_decoded, true);
192        TS_ASSERT_EQUALS(packet_type, 0);
193
194        //
195        // MODE 3: EXTRACT_SUBHEADER
196        //
197
198        // try to extract the subheader; dump into temporary buffer
199        memset(buf, 0x01, 256);
200        packet_subheader_extracted = ExtractSubheader( packet+num_read_total, 500, nr, buf, 256, nw );
201        num_read_total += nr;
202        TS_ASSERT_EQUALS(packet_subheader_extracted, true);
203        TS_ASSERT_EQUALS(nr, 21);
204        TS_ASSERT_EQUALS(nw, 21);
205        TS_ASSERT_EQUALS(num_read_total, 112);
206
207        //
208        // MODE 4: DECODE_SUBHEADER
209        //
210
211        // Decode subheader data
212        packet_id = 0;
213        packet_subheader_decoded = DecodeSubheader( buf, 256 );
214        TS_ASSERT_EQUALS( packet_subheader_decoded, true );
215        TS_ASSERT_EQUALS( packet_id, 6 );
216
217        //
218        // MODE 5: EXTRACT_PACKET_DATA
219        //
220
221        // try to extract packet data
222        packet_data_extracted = ExtractPacketData( packet+num_read_total, 400, nr, output, 1024, nw );
223        num_read_total += nr;
224        TS_ASSERT_EQUALS( packet_data_extracted, true );
225        TS_ASSERT_EQUALS( nr, 400 );
226        TS_ASSERT_EQUALS( nw, 400 );
227        TS_ASSERT_SAME_DATA( input, output, 400 );
228       
229        //
230        // MODE 6: DECODE_PACKET_DATA
231        //
232
233        // No need to decode packet data for PACKET_RAW_400
234
235        free(input);
236        free(buf);
237        free(packet);
238        free(output);
239    }
240
241    //
242    // Test extracting a packet with bit inversion
243    //
244    void test_Extract_PACKET_RAW_400_02() {
245        PacketEncoder p;
246
247        // generate input buffer
248        char * input;
249        input = (char*) calloc( 400, sizeof(char) );
250        input[0] = 1;
251        input[1] = 0;
252        input[2] = 1;
253
254        // generate temporary buffer
255        char * buf = (char*) malloc( 512 );
256
257        // generate packet buffer for encoded packet
258        char * packet;
259        packet = (char*) calloc( 1024, sizeof(char) );
260        memset( packet, 0x01, 1024*sizeof(char) );
261
262        // generate output buffer for decoded packet
263        char * output;
264        output = (char*) calloc( 1024, sizeof(char) );
265        memset( output, 0x01, 1024*sizeof(char) );
266
267        bool packet_header_found;
268        bool packet_header_extracted;
269        bool packet_header_decoded;
270        bool packet_subheader_extracted;
271        bool packet_subheader_decoded;
272        bool packet_data_extracted;
273
274        // assemble packet
275        unsigned int num_read;
276        unsigned int num_written;
277        p.packet_id = 6;
278        p.ConfigurePacketType( PACKET_RAW_400 );
279        p.AssemblePacket( input, 400, num_read, packet, 1024, num_written );
280
281        // emulate channel
282        // flip all the bits
283        for (unsigned int i=0; i<1024; i++)
284            packet[i] = 1-packet[i];
285        // reset packet id
286        packet_id = 0;
287
288        //
289        // EXTRACT PACKET
290        //
291
292        //
293        // MODE 0: FIND_HEADER
294        //
295
296        // try to extract packet header
297        unsigned int num_read_total(0);     // total bits read from input
298        unsigned int num_written_total(0);  // total bits writtent to output buffer
299        unsigned int nr;                    // number of bits read from input on each function call
300        unsigned int nw;                    // number of bits written to output on each function call
301        packet_header_found = FindHeader( packet+num_read_total, 1024, nr );
302        num_read_total += nr;
303        TS_ASSERT_EQUALS(packet_header_found, true);       // should have found header
304        TS_ASSERT_EQUALS(nr, PN_SYNC_CODE_LENGTH);   // should have only read 63 values
305        TS_ASSERT_EQUALS(rxy, -PN_SYNC_CODE_LENGTH);// correlator should be negative
306
307        //
308        // MODE 1: EXTRACT_HEADER
309        //
310
311        // try to extract packet type
312        packet_header_extracted = ExtractHeader( packet+num_read_total, 500, nr, buf, 256, nw );
313        num_read_total += nr;
314        TS_ASSERT_EQUALS(packet_header_extracted, true);
315        TS_ASSERT_EQUALS( nr, NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH);    // should have read control codes
316        TS_ASSERT_EQUALS( nw, NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH);    // should have written control codes
317        TS_ASSERT_EQUALS( num_read_total, PN_SYNC_CODE_LENGTH + NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH);
318
319        //
320        // MODE 2: DECODE_HEADER
321        //
322
323        packet_header_decoded = DecodeHeader(buf, 256);
324        TS_ASSERT_EQUALS(packet_header_decoded, true);
325        TS_ASSERT_EQUALS(packet_type, 0);
326
327        //
328        // MODE 3: EXTRACT_SUBHEADER
329        //
330
331        // try to extract the subheader; dump into temporary buffer
332        packet_subheader_extracted = ExtractSubheader( packet+num_read_total, 500, nr, buf, 256, nw );
333        num_read_total += nr;
334        TS_ASSERT_EQUALS(packet_subheader_extracted, true);
335        TS_ASSERT_EQUALS(nr, 21);
336        // PN sync code :               63
337        // PN control codes :           4x7
338        // PACKET_RAW_400 subheader :   21
339        // total :                      112
340        TS_ASSERT_EQUALS(num_read_total, 112);
341
342        //
343        // MODE 4: DECODE_SUBHEADER
344        //
345
346        // Decode subheader data
347        packet_id = 0;
348        packet_subheader_decoded = DecodeSubheader( buf, 256 );
349        TS_ASSERT_EQUALS( packet_subheader_decoded, true );
350        TS_ASSERT_EQUALS( packet_id, 6 );
351
352        //
353        // MODE 5: EXTRACT_PACKET_DATA
354        //
355
356        // try to extract packet data in pieces
357        // First attempt: read only 150 bits
358        packet_data_extracted = ExtractPacketData( packet+num_read_total, 150, nr, output, 1024, nw );
359        num_read_total += nr;
360        num_written_total += nw;
361        TS_ASSERT_EQUALS( packet_data_extracted, false );
362        TS_ASSERT_EQUALS( num_written_total, 150 );
363        TS_ASSERT_EQUALS( nr, 150 );
364        TS_ASSERT_EQUALS( nw, 150 );
365
366        // Second attempt: read remaining 250 bits
367        packet_data_extracted = ExtractPacketData( packet+num_read_total, 500, nr, output+150, 1024-150, nw );
368        num_read_total += nr;
369        num_written_total += nw;
370        TS_ASSERT_EQUALS( packet_data_extracted, true );
371        TS_ASSERT_EQUALS( num_read_total, 512 );
372        TS_ASSERT_EQUALS( num_written_total, 400 );
373        TS_ASSERT_EQUALS( nr, 250 );
374        TS_ASSERT_EQUALS( nw, 250 );
375
376        //
377        // MODE 6: DECODE_PACKET_DATA
378        //
379
380        // DecodePacketData(...)
381        // NOTE: no need to decode for PACKET_RAW_400
382
383        // output data should be the same, even though the packet was decoded with bit inversions;
384        // the P/N sync correlator should have detected this and corrected for it appropriately
385        TS_ASSERT_SAME_DATA( input, output, 400 );
386       
387        free(input);
388        free(buf);
389        free(packet);
390        free(output);
391    }
392
393};
394
395#endif
396
Note: See TracBrowser for help on using the browser.