aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/main.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/main.cpp b/tests/main.cpp
new file mode 100644
index 0000000..e495b3a
--- /dev/null
+++ b/tests/main.cpp
@@ -0,0 +1,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;
+} \ No newline at end of file