TeaSpeakLibrary/test/generationTest.cpp

105 lines
3.3 KiB
C++
Raw Normal View History

2020-01-26 20:21:39 -05:00
#include <src/protocol/generation.h>
#include <iostream>
#include <array>
#include <vector>
using namespace ts::protocol;
typedef std::vector<std::pair<size_t, size_t>> test_vector_t;
test_vector_t generate_test_vector(size_t size, int loss) {
test_vector_t result{};
result.reserve(size);
for(size_t i = 0; i < size; i++) {
if ((rand() % 100) < loss) continue;
result.emplace_back(i & 0xFFFFU, i >> 16U);
}
return result;
}
test_vector_t swap_elements(test_vector_t vector, int per, int max_distance) {
for(size_t index = 0; index < vector.size() - max_distance; index++) {
if ((rand() % 100) < per) {
//lets switch
auto offset = rand() % max_distance;
if(!offset) offset = 1;
std::swap(vector[index], vector[index + offset]);
}
}
return vector;
}
bool test_vector(const std::string_view& name, const test_vector_t& vector) {
generation_estimator gen{};
size_t last_value{0};
for(auto [id, exp] : vector) {
if(auto val = gen.visit_packet(id); val != exp) {
std::cout << "[" << name << "] failed for " << id << " -> " << exp << " | " << val << ". Last value: " << last_value << "\n";
return false;
}
last_value = id;
}
return true;
}
template <size_t N>
bool test_vector(generation_estimator& generator, const std::array<uint16_t, N>& packet_ids, const std::array<uint16_t, N>& expected) {
for(size_t index = 0; index < N; index++) {
auto result = generator.visit_packet(packet_ids[index]);
if(result != expected[index]) {
std::cout << "failed to packet id " << packet_ids[index] << " (" << index << "). Result: " << result << " Expected: " << expected[index] << "\n";
std::cout << "----- fail\n";
return false;
}
std::cout << "PacketID: " << packet_ids[index] << " -> " << result << "\n";
}
std::cout << "----- pass\n";
return true;
}
int main() {
generation_estimator gen{};
{
test_vector("00 loss", generate_test_vector(0x3000, 0));
test_vector("10 loss", generate_test_vector(0x3000, 10));
test_vector("25 loss", generate_test_vector(0x3000, 25));
test_vector("50 loss", generate_test_vector(0x3000, 50));
test_vector("80 loss", generate_test_vector(0x3000, 80));
}
{
auto base = generate_test_vector(0x3000, 0);
test_vector("swap 30:20", swap_elements(base, 30, 20));
test_vector("swap 30:1000", swap_elements(base, 30, 200));
test_vector("swap 80:1000", swap_elements(base, 80, 200));
}
if(false) {
test_vector("10 loss", generate_test_vector(0x3000, 10));
test_vector("25 loss", generate_test_vector(0x3000, 25));
test_vector("50 loss", generate_test_vector(0x3000, 50));
test_vector("80 loss", generate_test_vector(0x3000, 80));
}
gen.set_last_state(0, 0);
test_vector<6>(gen, {0, 1, 2, 4, 3, 5}, {0, 0, 0, 0, 0, 0});
gen.set_last_state(0xFF00, 0);
test_vector<6>(gen, {0, 1, 2, 4, 3, 5}, {1, 1, 1, 1, 1, 1});
gen.set_last_state(0xFF00, 0);
test_vector<6>(gen, {0, 1, 2, 0xFF00, 3, 5}, {1, 1, 1, 0, 1, 1});
gen.set_last_state(0xFF00, 0);
test_vector<6>(gen, {0xFFFE, 0xFFFF, 0, 1, 0xFFFC, 2}, {0, 0, 1, 1, 0, 1});
}