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

3

4
extern "C" {
Andreas Schmidt's avatar
Andreas Schmidt committed
5
6
7
#include "src/prrt/block.h"
#include "src/prrt/packet.h"
#include "src/prrt/coding_params.h"
8
9
10
#include "src/util/dbg.h"
#include "src/prrt/vdmcode/block_code.h"
#include "src/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
        encBlock = (PrrtBlock *) calloc(1, sizeof(PrrtBlock));
        decBlock = (PrrtBlock *) calloc(1, sizeof(PrrtBlock));
19
        PrrtCodingParams *cpar = (PrrtCodingParams *) calloc(1, sizeof(PrrtCodingParams));
20
        PrrtCodingParams_init(cpar);
21
22
        PrrtBlock_create(encBlock, cpar, 1);
        PrrtBlock_create(decBlock, cpar, 1);
23
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
24

25
26
27
28
    PrrtBlock *encBlock;
    PrrtBlock *decBlock;
};

29
30
31
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;

    printf("----\n");
    for(i = 0; i < k; ++i) {
        print_gf(src[i], length);
    }
    printf("----\n");

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

    for(i = 0; i < r; ++i) {
        print_gf(encFec[i], length);
    }
    printf("----\n");

    gf **decFec = (gf **) calloc(1, sizeof(gf *) * n);
    int *idx_p = (int *) calloc(k, sizeof(int));

    for(i = 0; i < n; i++) {
        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);

    for(i = 0; i < n; ++i) {
        print_gf(decFec[i], length);
    }
    printf("----\n");

}
95

96
TEST_F(PrrtBlockTest, EncodeDecode)
Andreas Schmidt's avatar
Andreas Schmidt committed
97
{
98
    uint16_t base = 1;
99

100
101
    PrrtPacket *packets[4];
    PrrtPacket *refPackets[4];
Andreas Schmidt's avatar
Andreas Schmidt committed
102

103
    PrrtPacket *redPackets = (PrrtPacket *) calloc(4, sizeof(PrrtPacket));
104

105
    for(int i = 0; i < 4; i++) {
106
107
        char data[3];
        sprintf(data, "%d", i);
108
109
110
111
        packets[i] = PrrtPacket_create_data_packet(0, data, (uint32_t) strlen(data), i+1);
        packets[i]->index = (uint8_t) i;
        refPackets[i] = PrrtPacket_create_data_packet(0, data, (uint32_t) strlen(data), i+1);
        ASSERT_TRUE(PrrtBlock_insert_data_packet(encBlock, packets[i]) == EXIT_SUCCESS);
112
113
114
    }

    // ENCODING
115
116
117
    ASSERT_TRUE(PrrtBlock_encode_ready(encBlock));
    int j = 0;
    PrrtBlock_encode(encBlock, &base);
118

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

130
131
    printf("------------------------\n");

132
    // DECODING
133
    PrrtBlock_insert_redundancy_packet(decBlock, redPackets);
Andreas Schmidt's avatar
Andreas Schmidt committed
134
    PrrtBlock_insert_data_packet(decBlock, packets[0]);
135
136
    PrrtBlock_insert_data_packet(decBlock, packets[1]);
    PrrtBlock_insert_data_packet(decBlock, packets[2]);
137

138
    ASSERT_TRUE(PrrtBlock_decode_ready(decBlock));
139

140
    PrrtBlock_decode(decBlock);
141

142
    for(int k = 0; k < 4; ++k) {
143
        PrrtPacket *ptr = PrrtBlock_get_first_data(decBlock);
144

Andreas Schmidt's avatar
Andreas Schmidt committed
145
146
        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);
147
        ASSERT_STREQ(s1, s2);
148
    }
149
}