PrrtBlock_tests.cpp 6.28 KB
Newer Older
Andreas Schmidt's avatar
Andreas Schmidt committed
1
#include "common.h"
2

3

4
extern "C" {
5
#include "prrt/proto/block.h"
6
#include "prrt/proto/types/packet.h"
7
8
9
10
#include "prrt/proto/codingParams.h"
#include "prrt/util/dbg.h"
#include "prrt/proto/vdmcode/block_code.h"
#include "prrt/util/common.h"
11
12
13
14
}

class PrrtBlockTest : public ::testing::Test {
protected:
Andreas Schmidt's avatar
Andreas Schmidt committed
15
16
    virtual void SetUp()
    {
17
        cpar = PrrtCodingParams_create();
Andreas Schmidt's avatar
Andreas Schmidt committed
18
19
        // TODO: Should be [3] instead of of NULL
        PrrtCodingParams_update(cpar, 4, 7, 1, NULL);
20
21
        encBlock = PrrtBlock_create(cpar, 1);
        decBlock = PrrtBlock_create(cpar, 1);
Andreas Schmidt's avatar
Andreas Schmidt committed
22
23
24
25
26
    }

    virtual void TearDown() {
        PrrtBlock_destroy(encBlock);
        PrrtBlock_destroy(decBlock);
27
        PrrtCodingParams_destroy(cpar);
28
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
29

30
    PrrtCodingParams *cpar;
31
32
33
34
    PrrtBlock *encBlock;
    PrrtBlock *decBlock;
};

35
36
TEST_F(PrrtBlockTest, VDMCode)
{
37
    int i, j, q;
38
39
40
41
42
43
44
45
46
    uint8_t k = 4;
    uint8_t n = 7;
    uint8_t r = n - k;
    uint8_t length = 3;

    PrrtCoder *coder = NULL;

    PrrtCoder_get_coder(&coder, n, k);

47
48
49
50
51
52
53
54
55
56
    for(q = 0; q < 4; q++) {
        gf **src = (gf **) calloc(k, sizeof(gf *));
        src[0] = (gf *) calloc(length, sizeof(gf));
        src[0][0] = 1; src[0][2] = 3;
        src[1] = (gf *) calloc(length, sizeof(gf));
        src[1][0] = 2; src[1][2] = 7;
        src[2] = (gf *) calloc(length, sizeof(gf));
        src[2][0] = 3; src[2][2] = 6;
        src[3] = (gf *) calloc(length, sizeof(gf));
        src[3][0] = 4; src[3][1]= 9; src[3][2] = 5;
57

58
59
60
61
62
        gf **encFec = (gf **) calloc(1, sizeof(gf *) * r);
        for(j = 0; j < r; j++) {
            encFec[j] = (gf *) calloc(length, sizeof(gf));
            PrrtCoder_encode(coder, src, encFec[j], j + k, length);
        }
63

64
        gf **decFec = (gf **) calloc(1, sizeof(gf *) * k);
rna's avatar
rna committed
65
        int16_t *idx_p = (int16_t *) calloc(k, sizeof(int16_t));
66

67
68
69
        for(i = 0; i < k; i++) {
            decFec[i] = (gf *) calloc(length, sizeof(gf));
        }
70

71
72
        memcpy(decFec[0], src[0], length);
        idx_p[0] = 0;
73

74
75
        memcpy(decFec[1], src[1], length);
        idx_p[1] = 1;
76

77
78
        memcpy(decFec[2], encFec[0], length);
        idx_p[2] = 4;
79

80
81
        memcpy(decFec[3], src[3], length);
        idx_p[3] = 3;
82

83
        PrrtCoder_decode(coder, decFec, idx_p, length);
Andreas Schmidt's avatar
Andreas Schmidt committed
84

85
86
87
        ASSERT_EQ(1, decFec[0][0]);
        ASSERT_EQ(0, decFec[0][1]);
        ASSERT_EQ(3, decFec[0][2]);
Andreas Schmidt's avatar
Andreas Schmidt committed
88

89
90
91
        ASSERT_EQ(2, decFec[1][0]);
        ASSERT_EQ(0, decFec[1][1]);
        ASSERT_EQ(7, decFec[1][2]);
92

93
94
95
96
97
98
99
100
        ASSERT_EQ(3, decFec[2][0]);
        ASSERT_EQ(0, decFec[2][1]);
        ASSERT_EQ(6, decFec[2][2]);

        ASSERT_EQ(4, decFec[3][0]);
        ASSERT_EQ(9, decFec[3][1]);
        ASSERT_EQ(5, decFec[3][2]);
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
101
102

    PrrtCoder_destroy(coder);
103
}
104

105
TEST_F(PrrtBlockTest, EncodeDecode)
Andreas Schmidt's avatar
Andreas Schmidt committed
106
{
107
    prrtSequenceNumber_t base = 1;
108

109
110
    PrrtPacket *packets[4];
    PrrtPacket *refPackets[4];
Andreas Schmidt's avatar
Andreas Schmidt committed
111

rna's avatar
rna committed
112
    PrrtPacket *redPackets[4];
113

Andreas Schmidt's avatar
Andreas Schmidt committed
114
    for(uint32_t i = 0; i < 4; i++) {
115
116
        char data[3];
        sprintf(data, "%d", i);
rna's avatar
rna committed
117
118
119
120
        if(i == 3) {
            // Last packet has a different size.
            sprintf(data, "D:%d", i);
        }
121
        packets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
122
        packets[i]->index = (uint8_t) i;
123
        refPackets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
124
        ASSERT_TRUE(PrrtBlock_insert_data_packet(encBlock, packets[i]));
125
126
127
    }

    // ENCODING
128
    ASSERT_TRUE(PrrtBlock_encode_ready(encBlock));
Andreas Schmidt's avatar
Andreas Schmidt committed
129
    uint32_t j = 0;
130
    PrrtBlock_encode(encBlock, &base);
131

Andreas Schmidt's avatar
Andreas Schmidt committed
132
    uint32_t pkt_count = List_count(encBlock->dataPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
133
    for(j = 0; j < pkt_count; j++) {
134
        PrrtPacket *data_pkt = PrrtBlock_get_first_data(encBlock);
135
        ASSERT_EQ(refPackets[j]->sequenceNumber, data_pkt->sequenceNumber);
136
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
137
    uint32_t red_count = List_count(encBlock->redundancyPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
138
    for(j = 0; j < red_count; j++) {
139
        PrrtPacket *red_pkt = PrrtBlock_get_first_red_data(encBlock);
rna's avatar
rna committed
140
        redPackets[j] = red_pkt;
141
142
    }

143
144
    printf("------------------------\n");

145
    // DECODING
rna's avatar
rna committed
146
147
148
149
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets[0]);
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets[1]);
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets[2]);
    PrrtBlock_insert_data_packet(decBlock, packets[3]);
150

151
    ASSERT_TRUE(PrrtBlock_decode_ready(decBlock));
152

153
    PrrtBlock_decode(decBlock);
154

155
    for(int k = 0; k < 4; ++k) {
156
        PrrtPacket *ptr = PrrtBlock_get_first_data(decBlock);
rna's avatar
rna committed
157
        prrtIndex_t idx = static_cast<prrtIndex_t>(ptr->sequenceNumber - 1);
158

rna's avatar
rna committed
159
        const char *s1 = (const char *) ((char *) refPackets[idx]->payload + PRRT_PACKET_DATA_HEADER_SIZE);
Andreas Schmidt's avatar
Andreas Schmidt committed
160
        const char *s2 = (const char *) ((char *) ptr->payload + PRRT_PACKET_DATA_HEADER_SIZE);
rna's avatar
rna committed
161
162
163
164

        prrtPacketLength_t refLength = ((PrrtPacketDataPayload*) refPackets[idx]->payload)->dataLength;
        prrtPacketLength_t ptrLength = ((PrrtPacketDataPayload*) ptr->payload)->dataLength;

165
        ASSERT_STREQ(s1, s2);
rna's avatar
rna committed
166
        ASSERT_EQ(refLength, ptrLength);
167
    }
168
}
Andreas Schmidt's avatar
Andreas Schmidt committed
169
170
171
172

TEST_F(PrrtBlockTest, NoCodingConfigured)
{
    PrrtCodingParams *cpar = PrrtCodingParams_create();
Andreas Schmidt's avatar
Andreas Schmidt committed
173
    PrrtCodingParams_update(cpar, 1, 1, 1, NULL);
Andreas Schmidt's avatar
Andreas Schmidt committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    PrrtBlock *encBlock = PrrtBlock_create(cpar, 1);
    PrrtBlock *decBlock = PrrtBlock_create(cpar, 1);

    prrtSequenceNumber_t base = 1;

    PrrtPacket *packets[1];
    PrrtPacket *refPackets[1];

    for(uint32_t i = 0; i < 1; i++) {
        char data[3];
        sprintf(data, "%d", i);
        packets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
        packets[i]->index = (uint8_t) i;
        refPackets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
        ASSERT_TRUE(PrrtBlock_insert_data_packet(encBlock, packets[i]));
    }

    // ENCODING
    ASSERT_TRUE(PrrtBlock_encode_ready(encBlock));
    uint32_t j = 0;
    PrrtBlock_encode(encBlock, &base);

    uint32_t pkt_count = List_count(encBlock->dataPackets);
    for(j = 0; j < pkt_count; j++) {
        PrrtPacket *data_pkt = PrrtBlock_get_first_data(encBlock);
        ASSERT_EQ(refPackets[j]->sequenceNumber, data_pkt->sequenceNumber);
    }
    uint32_t red_count = List_count(encBlock->redundancyPackets);
    ASSERT_EQ(red_count, 0);

    PrrtBlock_destroy(encBlock);
    PrrtBlock_destroy(decBlock);
Andreas Schmidt's avatar
Andreas Schmidt committed
206
    PrrtCodingParams_destroy(cpar);
Andreas Schmidt's avatar
Andreas Schmidt committed
207
}