PrrtBlock_tests.cpp 4.24 KB
Newer Older
1 2
#include <gtest/gtest.h>

3

4
extern "C" {
5 6 7 8 9 10
#include "prrt/proto/block.h"
#include "prrt/proto/packet.h"
#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 18 19 20
        PrrtCodingParams *cpar = PrrtCodingParams_create();
        encBlock = PrrtBlock_create(cpar, 1);
        decBlock = PrrtBlock_create(cpar, 1);
        PrrtCodingParams_destroy(cpar);
Andreas Schmidt's avatar
Andreas Schmidt committed
21 22 23 24 25
    }

    virtual void TearDown() {
        PrrtBlock_destroy(encBlock);
        PrrtBlock_destroy(decBlock);
26
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
27

28 29 30 31
    PrrtBlock *encBlock;
    PrrtBlock *decBlock;
};

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
TEST_F(PrrtBlockTest, VDMCode)
{
    int i, j;
    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);

    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;

    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);
    }

Andreas Schmidt's avatar
Andreas Schmidt committed
60
    gf **decFec = (gf **) calloc(1, sizeof(gf *) * k);
61 62
    int *idx_p = (int *) calloc(k, sizeof(int));

Andreas Schmidt's avatar
Andreas Schmidt committed
63
    for(i = 0; i < k; i++) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        decFec[i] = (gf *) calloc(length, sizeof(gf));
    }

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

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

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

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

    PrrtCoder_decode(coder, decFec, idx_p, length);

Andreas Schmidt's avatar
Andreas Schmidt committed
81 82 83 84 85 86 87 88 89 90 91
    ASSERT_EQ(1, decFec[0][0]);
    ASSERT_EQ(0, decFec[0][1]);
    ASSERT_EQ(3, decFec[0][2]);

    ASSERT_EQ(2, decFec[1][0]);
    ASSERT_EQ(0, decFec[1][1]);
    ASSERT_EQ(7, decFec[1][2]);

    ASSERT_EQ(3, decFec[2][0]);
    ASSERT_EQ(0, decFec[2][1]);
    ASSERT_EQ(6, decFec[2][2]);
92

Andreas Schmidt's avatar
Andreas Schmidt committed
93 94 95
    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
96 97

    PrrtCoder_destroy(coder);
98
}
99

100
TEST_F(PrrtBlockTest, EncodeDecode)
Andreas Schmidt's avatar
Andreas Schmidt committed
101
{
102
    prrtSequenceNumber_t base = 1;
103

104 105
    PrrtPacket *packets[4];
    PrrtPacket *refPackets[4];
Andreas Schmidt's avatar
Andreas Schmidt committed
106

107
    PrrtPacket *redPackets = (PrrtPacket *) calloc(4, sizeof(PrrtPacket));
108

Andreas Schmidt's avatar
Andreas Schmidt committed
109
    for(uint32_t i = 0; i < 4; i++) {
110 111
        char data[3];
        sprintf(data, "%d", i);
112
        packets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
113
        packets[i]->index = (uint8_t) i;
114
        refPackets[i] = PrrtPacket_create_data_packet(0, data, (prrtPacketLength_t) strlen(data), i + 1, 0);
115
        ASSERT_TRUE(PrrtBlock_insert_data_packet(encBlock, packets[i]));
116 117 118
    }

    // ENCODING
119
    ASSERT_TRUE(PrrtBlock_encode_ready(encBlock));
Andreas Schmidt's avatar
Andreas Schmidt committed
120
    uint32_t j = 0;
121
    PrrtBlock_encode(encBlock, &base);
122

Andreas Schmidt's avatar
Andreas Schmidt committed
123
    uint32_t pkt_count = List_count(encBlock->dataPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
124
    for(j = 0; j < pkt_count; j++) {
125
        PrrtPacket *data_pkt = PrrtBlock_get_first_data(encBlock);
126
        ASSERT_EQ(refPackets[j]->sequenceNumber, data_pkt->sequenceNumber);
127
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
128
    uint32_t red_count = List_count(encBlock->redundancyPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
129
    for(j = 0; j < red_count; j++) {
130 131
        PrrtPacket *red_pkt = PrrtBlock_get_first_red_data(encBlock);
        redPackets[j] = *red_pkt;
132 133
    }

134 135
    printf("------------------------\n");

136
    // DECODING
137
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
138
    PrrtBlock_insert_data_packet(decBlock, packets[0]);
139 140
    PrrtBlock_insert_data_packet(decBlock, packets[1]);
    PrrtBlock_insert_data_packet(decBlock, packets[2]);
141

142
    ASSERT_TRUE(PrrtBlock_decode_ready(decBlock));
143

144
    PrrtBlock_decode(decBlock);
145

146
    for(int k = 0; k < 4; ++k) {
147
        PrrtPacket *ptr = PrrtBlock_get_first_data(decBlock);
148

Andreas Schmidt's avatar
Andreas Schmidt committed
149 150
        const char *s1 = (const char *) ((char *) refPackets[k]->payload + PRRT_PACKET_DATA_HEADER_SIZE);
        const char *s2 = (const char *) ((char *) ptr->payload + PRRT_PACKET_DATA_HEADER_SIZE);
151
        ASSERT_STREQ(s1, s2);
152
    }
153
}