Permalink
Browse files

More tests

  • Loading branch information...
petya2164 committed Oct 2, 2018
1 parent 6006064 commit 08f821947224168e1f2895b1e5bc96fd89a98a5d
@@ -0,0 +1,99 @@
// Copyright Steinwurf ApS 2018.
// Distributed under the "STEINWURF EVALUATION LICENSE 1.0".
// See accompanying file LICENSE.rst or
// http://www.steinwurf.com/licensing
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <kodo_rlnc_c/encoder.h>
#include <kodo_rlnc_c/decoder.h>
/// @example encode_decode_using_coefficients.c
///
/// Example showing how to use the Symbol API with direct coefficient
/// access. Using this approach we have full control over the coding vector
/// (we can even rewrite coefficients if desired). However, we also have to
/// manage extra functionality such as systematic encoding ourselves.
int main()
{
// Seed the random number generator to produce different data every time
srand(time(NULL));
// Set the number of symbols (i.e. the generation size in RLNC
// terminology) and the size of a symbol in bytes
uint32_t symbols = 10;
uint32_t symbol_size = 100;
// Here we select the finite field to use
int32_t finite_field = krlnc_binary8;
// First, we create an encoder & decoder factory.
// The factories are used to build actual encoders/decoders
krlnc_encoder_factory_t encoder_factory = krlnc_new_encoder_factory(
finite_field, symbols, symbol_size);
krlnc_decoder_factory_t decoder_factory = krlnc_new_decoder_factory(
finite_field, symbols, symbol_size);
krlnc_encoder_t encoder = krlnc_encoder_factory_build(encoder_factory);
krlnc_decoder_t decoder = krlnc_decoder_factory_build(decoder_factory);
// Allocate some storage for a coded symbol
uint8_t* symbol = (uint8_t*) malloc(symbol_size);
// Allocate some storage for the coding vector coefficients
uint32_t vector_size = krlnc_encoder_coefficient_vector_size(encoder);
uint8_t* coefficients = (uint8_t*) malloc(vector_size);
uint32_t block_size = krlnc_encoder_block_size(encoder);
uint8_t* data_in = (uint8_t*) malloc(block_size);
uint8_t* data_out = (uint8_t*) malloc(block_size);
uint32_t i = 0;
for (; i < block_size; ++i)
data_in[i] = rand() % 256;
krlnc_encoder_set_const_symbols(encoder, data_in, block_size);
krlnc_decoder_set_mutable_symbols(decoder, data_out, block_size);
while (!krlnc_decoder_is_complete(decoder))
{
// Generate an encoding vector
krlnc_encoder_generate(encoder, coefficients);
// Write a coded symbol to the symbol buffer
krlnc_encoder_write_symbol(encoder, symbol, coefficients);
// Pass that symbol and the corresponding coefficients to the decoder
krlnc_decoder_read_symbol(decoder, symbol, coefficients);
printf("Symbol processed by decoder, current rank = %d\n",
krlnc_decoder_rank(decoder));
}
if (memcmp(data_in, data_out, block_size) == 0)
{
printf("Data decoded correctly\n");
}
else
{
printf("Unexpected failure to decode, please file a bug report :)\n");
}
free(data_in);
free(data_out);
free(symbol);
free(coefficients);
krlnc_delete_encoder(encoder);
krlnc_delete_decoder(decoder);
krlnc_delete_encoder_factory(encoder_factory);
krlnc_delete_decoder_factory(decoder_factory);
return 0;
}
@@ -0,0 +1,7 @@
#! /usr/bin/env python
# encoding: utf-8
bld.program(features='cxx limit_includes',
source='encode_decode_using_coefficients.c',
target='encode_decode_using_coefficients',
use=['kodo_rlnc_c_static'])
@@ -67,7 +67,7 @@ int main()
// Note: the density can be adjusted at any time. This feature can be used
// to adapt to changing network conditions.
printf("The density defaults to: %0.2f\n", krlnc_encoder_density(encoder));
krlnc_encoder_set_density(encoder, 0.4);
krlnc_encoder_set_density(encoder, 0.4f);
printf("The density was set to: %0.2f\n", krlnc_encoder_density(encoder));
// A low density setting can lead to a large number of redundant symbols.
// In practice, the value should be tuned to the specific scenario.
@@ -116,7 +116,7 @@ int main(int argc, char* argv[])
// Create the encoder factory
decoder_factory = krlnc_new_decoder_factory(
finite_field, symbols, symbol_size);
finite_field, symbols, symbol_size);
decoder = krlnc_decoder_factory_build(decoder_factory);
// Create the buffer needed for the payload
@@ -22,7 +22,7 @@ int ends_with(const char* str, const char* suffix)
if (!str || !suffix) return 0;
size_t len_str = strlen(str);
size_t len_suffix = strlen(suffix);
if (len_suffix > len_str) return 0;
if (len_suffix > len_str) return 0;
return strncmp(str + len_str - len_suffix, suffix, len_suffix) == 0;
}
@@ -11,15 +11,27 @@
#include <gtest/gtest.h>
void trace_callback(const char* zone, const char* data, void* context)
{
(void) context;
if (strcmp(zone, "decoder_state") == 0)
{
printf("%s:\n", zone);
printf("%s\n", data);
}
}
TEST(test_coders, factory_api)
{
uint32_t symbols = 50;
uint32_t symbol_size = 750;
auto decoder_factory = krlnc_new_decoder_factory(
krlnc_binary8, symbols, symbol_size);
krlnc_binary, symbols, symbol_size);
auto encoder_factory = krlnc_new_encoder_factory(
krlnc_binary8, symbols, symbol_size);
krlnc_binary, symbols, symbol_size);
EXPECT_EQ(symbols, krlnc_decoder_factory_symbols(decoder_factory));
EXPECT_EQ(symbols, krlnc_encoder_factory_symbols(encoder_factory));
@@ -46,24 +58,29 @@ TEST(test_coders, factory_api)
new_symbol_size, krlnc_decoder_factory_symbol_size(decoder_factory));
krlnc_encoder_factory_set_coding_vector_format(
encoder_factory, krlnc_sparse_seed);
encoder_factory, krlnc_seed);
krlnc_decoder_factory_set_coding_vector_format(
decoder_factory, krlnc_full_vector);
decoder_factory, krlnc_sparse_seed);
krlnc_delete_decoder_factory(decoder_factory);
krlnc_delete_encoder_factory(encoder_factory);
}
TEST(test_coders, api)
TEST(test_coders, basic_api)
{
uint32_t symbols = 50;
uint32_t symbol_size = 750;
auto encoder_factory = krlnc_new_encoder_factory(
krlnc_binary8, symbols, symbol_size);
krlnc_binary4, symbols, symbol_size);
auto decoder_factory = krlnc_new_decoder_factory(
krlnc_binary8, symbols, symbol_size);
krlnc_binary4, symbols, symbol_size);
krlnc_encoder_factory_set_coding_vector_format(
encoder_factory, krlnc_full_vector);
krlnc_decoder_factory_set_coding_vector_format(
decoder_factory, krlnc_full_vector);
auto encoder = krlnc_encoder_factory_build(encoder_factory);
auto decoder1 = krlnc_decoder_factory_build(decoder_factory);
@@ -128,6 +145,9 @@ TEST(test_coders, api)
EXPECT_FALSE(krlnc_decoder_is_symbol_uncoded(decoder1, i));
}
krlnc_encoder_set_density(encoder, 0.4f);
EXPECT_EQ(0.4f, krlnc_encoder_density(encoder));
while (!krlnc_decoder_is_complete(decoder2))
{
krlnc_encoder_write_payload(encoder, payload1.data());
@@ -142,10 +162,68 @@ TEST(test_coders, api)
EXPECT_EQ(data_in, data_out1);
EXPECT_EQ(data_in, data_out2);
std::vector<uint8_t> coefficients(
krlnc_encoder_coefficient_vector_size(encoder));
krlnc_encoder_set_seed(encoder, 42U);
krlnc_encoder_generate(encoder, coefficients.data());
krlnc_encoder_generate_partial(encoder, coefficients.data());
krlnc_decoder_set_seed(decoder1, 42U);
krlnc_decoder_generate(decoder1, coefficients.data());
krlnc_decoder_generate_partial(decoder1, coefficients.data());
krlnc_encoder_set_zone_prefix(encoder, "encoder");
krlnc_encoder_set_trace_stdout(encoder);
krlnc_encoder_set_trace_callback(encoder, trace_callback, NULL);
krlnc_encoder_set_trace_off(encoder);
krlnc_decoder_set_zone_prefix(decoder1, "decoder");
krlnc_decoder_set_trace_stdout(decoder1);
krlnc_decoder_set_trace_callback(decoder1, trace_callback, NULL);
krlnc_decoder_set_trace_off(decoder1);
krlnc_delete_encoder(encoder);
krlnc_delete_decoder(decoder1);
krlnc_delete_decoder(decoder2);
krlnc_delete_encoder_factory(encoder_factory);
krlnc_delete_decoder_factory(decoder_factory);
}
TEST(test_coders, symbol_status_api)
{
uint32_t symbols = 4;
uint32_t symbol_size = 40;
krlnc_decoder_factory_t decoder_factory = krlnc_new_decoder_factory(
krlnc_binary8, symbols, symbol_size);
krlnc_decoder_t decoder = krlnc_decoder_factory_build(decoder_factory);
std::vector<uint8_t> data_out(krlnc_decoder_block_size(decoder), '\0');
krlnc_decoder_set_mutable_symbols(
decoder, data_out.data(), data_out.size());
std::vector<uint8_t> symbol(krlnc_decoder_symbol_size(decoder));
std::vector<uint8_t> coefficients(
krlnc_decoder_coefficient_vector_size(decoder));
coefficients = {1, 0, 0, 0};
krlnc_decoder_read_symbol(decoder, symbol.data(), coefficients.data());
EXPECT_EQ(0U, krlnc_decoder_symbols_uncoded(decoder));
krlnc_decoder_update_symbol_status(decoder);
EXPECT_EQ(1U, krlnc_decoder_symbols_uncoded(decoder));
krlnc_decoder_set_status_updater_on(decoder);
coefficients = {0, 1, 0, 0};
krlnc_decoder_read_symbol(decoder, symbol.data(), coefficients.data());
EXPECT_EQ(2U, krlnc_decoder_symbols_uncoded(decoder));
krlnc_decoder_set_status_updater_off(decoder);
krlnc_delete_decoder(decoder);
krlnc_delete_decoder_factory(decoder_factory);
}
@@ -44,6 +44,7 @@ def build(bld):
bld.recurse('test')
bld.recurse('examples/encode_decode_on_the_fly')
bld.recurse('examples/encode_decode_simple')
bld.recurse('examples/encode_decode_using_coefficients')
bld.recurse('examples/sparse_seed')
bld.recurse('examples/switch_systematic_on_off')
bld.recurse('examples/symbol_status_updater')

0 comments on commit 08f8219

Please sign in to comment.