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

3

4
extern "C" {
Andreas Schmidt's avatar
Andreas Schmidt committed
5 6
#include "prrt/proto/types/block.h"
#include "prrt/proto/types/codingParams.h"
7
#include "prrt/proto/types/packet.h"
8 9 10
#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()
    {
Andreas Schmidt's avatar
Andreas Schmidt committed
17 18 19
    }

    virtual void TearDown() {
20
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
21

22 23
};

24 25
TEST_F(PrrtBlockTest, VDMCode)
{
26
    int i, j, q;
27 28 29 30 31 32
    uint8_t k = 4;
    uint8_t n = 7;
    uint8_t r = n - k;
    uint8_t length = 3;


33
    PrrtCodingConfiguration* params = PrrtCodingConfiguration_create(k, n, 1, &r);
34
    PrrtCoder *coder = PrrtCoder_create(params);
35

36 37 38 39 40 41 42 43 44 45
    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;
46

47 48 49 50 51
        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);
        }
52

53
        gf **decFec = (gf **) calloc(1, sizeof(gf *) * k);
54
        int16_t *idx_p = (int16_t *) calloc(k, sizeof(int16_t));
55

56 57 58
        for(i = 0; i < k; i++) {
            decFec[i] = (gf *) calloc(length, sizeof(gf));
        }
59

60 61
        memcpy(decFec[0], src[0], length);
        idx_p[0] = 0;
62

63 64
        memcpy(decFec[1], src[1], length);
        idx_p[1] = 1;
65

66 67
        memcpy(decFec[2], encFec[0], length);
        idx_p[2] = 4;
68

69 70
        memcpy(decFec[3], src[3], length);
        idx_p[3] = 3;
71

72
        PrrtCoder_decode(coder, decFec, idx_p, length);
73

74 75 76
        ASSERT_EQ(1, decFec[0][0]);
        ASSERT_EQ(0, decFec[0][1]);
        ASSERT_EQ(3, decFec[0][2]);
77

78 79 80
        ASSERT_EQ(2, decFec[1][0]);
        ASSERT_EQ(0, decFec[1][1]);
        ASSERT_EQ(7, decFec[1][2]);
81

82 83 84 85 86 87 88
        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]);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

        // free allocated memory

        for (i = 0; i < k; ++i) {
            free(decFec[i]);
        }
        free(idx_p);
        free(decFec);

        for (j = 0; j < r; ++j) {
            free(encFec[j]);
        }
        free(encFec);

        free(src[3]);
        free(src[2]);
        free(src[1]);
        free(src[0]);
        free(src);
108
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
109 110

    PrrtCoder_destroy(coder);
111
    PrrtCodingConfiguration_destroy(params);
112
}
113

114
TEST_F(PrrtBlockTest, EncodeDecode)
Andreas Schmidt's avatar
Andreas Schmidt committed
115
{
116 117
    uint8_t r = 3;
    PrrtCodingConfiguration *cpar = PrrtCodingConfiguration_create(4, 7, 1, &r);
118 119 120 121 122
    PrrtCoder *coder = PrrtCoder_create(cpar);

    PrrtBlock *encBlock = PrrtBlock_create(PrrtCodingConfiguration_copy(cpar), PrrtCoder_copy(coder), 1);
    PrrtBlock *decBlock = PrrtBlock_create(PrrtCodingConfiguration_copy(cpar), PrrtCoder_copy(coder), 1);

123
    prrtSequenceNumber_t base = 1;
124

125 126
    PrrtPacket *packets[4];
    PrrtPacket *refPackets[4];
Andreas Schmidt's avatar
Andreas Schmidt committed
127

128
    PrrtPacket *redPackets[4];
129

Andreas Schmidt's avatar
Andreas Schmidt committed
130
    for(uint32_t i = 0; i < 4; i++) {
131 132
        char data[3];
        sprintf(data, "%d", i);
133 134 135 136
        if(i == 3) {
            // Last packet has a different size.
            sprintf(data, "D:%d", i);
        }
137
        packets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
138
        packets[i]->index = (uint8_t) i;
139
        refPackets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
140
        ASSERT_TRUE(PrrtBlock_insert_data_packet(encBlock, packets[i]));
141 142 143
    }

    // ENCODING
144
    ASSERT_TRUE(PrrtBlock_encode_ready(encBlock));
Andreas Schmidt's avatar
Andreas Schmidt committed
145
    uint32_t j = 0;
146
    PrrtBlock_encode(encBlock, &base);
147

Andreas Schmidt's avatar
Andreas Schmidt committed
148
    uint32_t pkt_count = List_count(encBlock->dataPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
149
    for(j = 0; j < pkt_count; j++) {
150
        PrrtPacket *data_pkt = PrrtBlock_get_first_data(encBlock);
151
        ASSERT_EQ(refPackets[j]->sequenceNumber, data_pkt->sequenceNumber);
152
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
153
    uint32_t red_count = List_count(encBlock->redundancyPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
154
    for(j = 0; j < red_count; j++) {
155
        PrrtPacket *red_pkt = PrrtBlock_get_first_red_data(encBlock);
156
        redPackets[j] = red_pkt;
157 158
    }

159 160
    printf("------------------------\n");

161
    // DECODING
162 163 164
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets[0]);
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets[1]);
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets[2]);
165
    PrrtBlock_insert_data_packet(decBlock, PrrtPacket_copy(packets[3]));
166

167
    ASSERT_TRUE(PrrtBlock_decode_ready(decBlock));
168

169
    PrrtBlock_decode(decBlock);
170

171
    for(int k = 0; k < 4; ++k) {
172
        PrrtPacket *ptr = PrrtBlock_get_first_data(decBlock);
173
        prrtIndex_t idx = static_cast<prrtIndex_t>(ptr->sequenceNumber - 1);
174

175
        const char *s1 = (const char *) ((char *) refPackets[idx]->payload + PRRT_PACKET_DATA_HEADER_SIZE);
Andreas Schmidt's avatar
Andreas Schmidt committed
176
        const char *s2 = (const char *) ((char *) ptr->payload + PRRT_PACKET_DATA_HEADER_SIZE);
177 178 179 180

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

181
        ASSERT_STREQ(s1, s2);
182
        ASSERT_EQ(refLength, ptrLength);
183 184

        PrrtPacket_destroy(ptr);
185
    }
186 187 188 189 190

    PrrtBlock_destroy(encBlock);
    PrrtBlock_destroy(decBlock);
    PrrtCoder_destroy(coder);
    PrrtCodingConfiguration_destroy(cpar);
191 192 193 194 195 196 197 198

    for (auto packet: packets) {
        PrrtPacket_destroy(packet);
    }

    for (auto packet: refPackets) {
        PrrtPacket_destroy(packet);
    }
199
}
200 201 202

TEST_F(PrrtBlockTest, NoCodingConfigured)
{
203
    PrrtCodingConfiguration *cpar = PrrtCodingConfiguration_create(1, 1, 0, NULL);
204 205
    PrrtCoder* coder = PrrtCoder_create(cpar);
    PrrtBlock *encBlock = PrrtBlock_create(PrrtCodingConfiguration_copy(cpar), PrrtCoder_copy(coder), 1);
206 207 208 209 210 211 212 213 214

    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);
215
        packets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data) + 1, i + 1, 0);
216
        packets[i]->index = (uint8_t) i;
217
        refPackets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data) + 1, i + 1, 0);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        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);

234 235 236 237 238
    for (int i = 0; i < 1; ++i) {
        PrrtPacket_destroy(packets[i]);
        PrrtPacket_destroy(refPackets[i]);
    }

239
    PrrtBlock_destroy(encBlock);
240 241
    PrrtCoder_destroy(coder);
    PrrtCodingConfiguration_destroy(cpar);
242
}