aboutsummaryrefslogtreecommitdiff
path: root/tests/main.cpp
blob: e495b3a561b0a7edd05d3b52720b981a30fb7159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdio>
#include <cstdlib>
#include <sibs/SafeSerializer.hpp>
#include <sibs/SafeDeserializer.hpp>
#include <cstdint>

#define REQUIRE(statement) do { if(!(statement)) { fprintf(stderr, "Assertion failed:\n%s\n", #statement); exit(1); } } while(0)
#define FAIL(str) do { fprintf(stderr, "Failed:\n%s\n", (str)); exit(1); } while(0)

int main()
{
    sibs::SafeSerializer serializer;
    serializer.add((uint32_t)3563634);
    serializer.add((uint64_t)204232532533423632);
    serializer.add((uint8_t)2);
    serializer.add((uint8_t*)"hello", 5);
    REQUIRE(serializer.getBuffer().size() == 18);

    sibs::SafeDeserializer deserializer(serializer.getBuffer().data(), serializer.getBuffer().size());
    REQUIRE(deserializer.extract<uint32_t>() == 3563634);
    REQUIRE(deserializer.extract<uint64_t>() == 204232532533423632);
    REQUIRE(deserializer.extract<uint8_t>() == 2);
    char str[6];
    str[5] = '\0';
    deserializer.extract((uint8_t*)str, 5);
    REQUIRE(strcmp(str, "hello") == 0);
    REQUIRE(deserializer.empty());

    try
    {
        deserializer.extract<uint8_t>();
        FAIL("Expected extract to fail since we have extracted all data");
    }
    catch (sibs::DeserializeException &e)
    {

    }

    return 0;
}