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

Revision 5373, 7.4 KB (checked in by jgaeddert, 6 years ago)

modifying subheader extraction methods; initialization of external buffer pointers

  • 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 PacketizerDSP
15{
16public:
17
18    void test_CorrelateSequence() {
19        int r(0);
20       
21        // Test for positive P/N sync code correlation
22        for (unsigned int i=0; i<63; i++)
23            binary_sequence_push( pn_sync_buffer,  pnSyncCode[i] );
24
25        r = binary_sequence_correlate( pn_sync_buffer, pn_sync_code );
26        TS_ASSERT_EQUALS( r, 63 );
27       
28        // Test for negative P/N sync code correlation
29        for (unsigned int i=0; i<63; i++)
30            binary_sequence_push( pn_sync_buffer,  1-pnSyncCode[i] );
31
32        r = binary_sequence_correlate( pn_sync_buffer, pn_sync_code );
33        TS_ASSERT_EQUALS( r, -63 );
34
35        // Test for positive P/N control code correlation
36        for (unsigned int i=0; i<7; i++)
37            binary_sequence_push( pn_control_buffer,  pnControlCode[i] );
38
39        r = binary_sequence_correlate( pn_control_buffer, pn_control_code );
40        TS_ASSERT_EQUALS( r, 7 );
41       
42        // Test for positive P/N control code correlation
43        for (unsigned int i=0; i<7; i++)
44            binary_sequence_push( pn_control_buffer,  1-pnControlCode[i] );
45
46        r = binary_sequence_correlate( pn_control_buffer, pn_control_code );
47        TS_ASSERT_EQUALS( r, -7 );
48       
49    }
50
51    //
52    // Basic test for creating and extracting a packet of type PACKET_RAW_400
53    //
54    void test_ExtractPacketHeader_01() {
55        // generate input buffer
56        char * input;
57        input = (char*) calloc( 400, sizeof(char) );
58        input[0] = 1;
59        input[1] = 0;
60        input[2] = 1;
61
62        // generate packet buffer
63        char * packet;
64        packet = (char*) calloc( 1024, sizeof(char) );
65        memset( packet, 0x01, 1024*sizeof(char) );
66
67        // generate output buffer
68        char * output;
69        output = (char*) calloc( 1024, sizeof(char) );
70        memset( output, 0x01, 1024*sizeof(char) );
71
72        bool found_header;
73        bool packet_type_extracted;
74        bool packet_subheader_extracted;
75        bool packet_data_extracted;
76        unsigned int n;
77
78        // try to extract packet header before packet has been assembled
79        found_header = ExtractPacketHeader( packet, 1024, n );
80        TS_ASSERT_EQUALS(found_header, false);  // should not have found header
81        TS_ASSERT_EQUALS(n, 1024);              // should have read entire buffer
82
83        // assemble packet
84        unsigned int packet_length(1024);
85        AssemblePacket( input, 400, packet, packet_length, PACKET_RAW_400 );
86
87        // try to extract packet header
88        unsigned int N(0);                      // total bits read from input
89        found_header = ExtractPacketHeader( packet+N, 1024, n );
90        N += n;
91        TS_ASSERT_EQUALS(found_header, true);       // should have found header
92        TS_ASSERT_EQUALS(n, PN_SYNC_CODE_LENGTH);   // should have only read 63 values
93
94        // try to extract packet type
95        packet_type_extracted = ExtractPacketType( packet+N, PN_CONTROL_CODE_LENGTH, n );
96        N += n;
97        // should not have extracted packet type because buffer was said to only be
98        // 7 samples long
99        TS_ASSERT_EQUALS(packet_type_extracted, false);
100        TS_ASSERT_EQUALS(n, PN_CONTROL_CODE_LENGTH);    // should have only read 1 control code
101
102        packet_type_extracted = ExtractPacketType( packet+N, 500, n );
103        N += n;
104        TS_ASSERT_EQUALS(packet_type_extracted, true);
105        TS_ASSERT_EQUALS(n, (NUM_CONTROL_CODES-1)*PN_CONTROL_CODE_LENGTH);    // should have read remaining control codes
106        TS_ASSERT_EQUALS( N, PN_SYNC_CODE_LENGTH + NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH);
107
108        TS_ASSERT_EQUALS(new_packet_type, 0);
109
110        // try to extract the subheader
111        packet_subheader_extracted = ExtractPacketSubheader( packet+N, 500, output, n );
112        N += n;
113        TS_ASSERT_EQUALS(packet_subheader_extracted, true);
114        TS_ASSERT_EQUALS(n, 21);
115        TS_ASSERT_EQUALS(N, 112);
116
117        // try to extract packet data
118        n = 1024;
119        packet_data_extracted = ExtractPacketData( packet+N, 400, output, n );
120        N += n;
121        TS_ASSERT_EQUALS( packet_data_extracted, true );
122        TS_ASSERT_EQUALS( n, 400 );
123        TS_ASSERT_SAME_DATA( input, output, 400 );
124       
125        free(input);
126        free(packet);
127        free(output);
128    }
129
130    //
131    // Test extracting a packet with bit inversion
132    //
133    void test_ExtractPacketHeader_02() {
134        // generate input buffer
135        char * input;
136        input = (char*) calloc( 400, sizeof(char) );
137        input[0] = 1;
138        input[1] = 0;
139        input[2] = 1;
140
141        // generate packet buffer
142        char * packet;
143        packet = (char*) calloc( 1024, sizeof(char) );
144        memset( packet, 0x01, 1024*sizeof(char) );
145
146        // generate output buffer
147        char * output;
148        output = (char*) calloc( 1024, sizeof(char) );
149        memset( output, 0x01, 1024*sizeof(char) );
150
151        bool found_header;
152        bool packet_type_extracted;
153        bool packet_subheader_extracted;
154        bool packet_data_extracted;
155        unsigned int n;
156
157        // assemble packet
158        unsigned int packet_length(1024);
159        AssemblePacket( input, 400, packet, packet_length, PACKET_RAW_400 );
160
161        // flip all the bits
162        for (unsigned int i=0; i<1024; i++)
163            packet[i] = 1-packet[i];
164
165        // try to extract packet header
166        unsigned int N(0);                      // total bits read from input
167        found_header = ExtractPacketHeader( packet+N, 1024, n );
168        N += n;
169        TS_ASSERT_EQUALS(found_header, true);       // should have found header
170        TS_ASSERT_EQUALS(n, PN_SYNC_CODE_LENGTH);   // should have only read 63 values
171        TS_ASSERT_EQUALS(rxy, -PN_SYNC_CODE_LENGTH);// correlator should be negative
172
173        // try to extract packet type
174        packet_type_extracted = ExtractPacketType( packet+N, 500, n );
175        N += n;
176        TS_ASSERT_EQUALS(packet_type_extracted, true);
177        TS_ASSERT_EQUALS(n, NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH);    // should have read control codes
178        TS_ASSERT_EQUALS( N, PN_SYNC_CODE_LENGTH + NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH);
179
180        TS_ASSERT_EQUALS(new_packet_type, 0);
181
182        // try to extract the subheader
183        packet_subheader_extracted = ExtractPacketSubheader( packet+N, 500, output, n );
184        N += n;
185        TS_ASSERT_EQUALS(packet_subheader_extracted, true);
186        TS_ASSERT_EQUALS(n, 21);
187        TS_ASSERT_EQUALS(N, 112);
188
189        // try to extract packet data
190        n = 1024;
191        packet_data_extracted = ExtractPacketData( packet+N, 400, output, n );
192        N += n;
193        TS_ASSERT_EQUALS( packet_data_extracted, true );
194        TS_ASSERT_EQUALS( n, 400 );
195
196        // output data should be the same, even though the packet was decoded with bit inversions;
197        // the P/N sync correlator should have detected this and corrected for it appropriately
198        TS_ASSERT_SAME_DATA( input, output, 400 );
199       
200        free(input);
201        free(packet);
202        free(output);
203    }
204
205};
206
207#endif
208
Note: See TracBrowser for help on using the browser.