diff options
author | johannst <johannes.stoelp@gmail.com> | 2020-11-24 21:23:08 +0100 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2020-11-24 21:23:08 +0100 |
commit | f9f2b6bb2d685556bc3346ca3f7e55f4c865fc16 (patch) | |
tree | 3ed8ac66323b6985713427d147826c5845212c13 /test | |
parent | f9e7e2003266e70c0d018f5712c431d187159e65 (diff) | |
download | dynld-f9f2b6bb2d685556bc3346ca3f7e55f4c865fc16.tar.gz dynld-f9f2b6bb2d685556bc3346ca3f7e55f4c865fc16.zip |
add process init chapter
Add chapter on process initialization.
Add program to visualize data provided by the Linux Kernel as
specified in the SysV ABI.
Add utils for syscalls and printing + tests.
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 10 | ||||
-rw-r--r-- | test/checker.cc | 68 | ||||
-rw-r--r-- | test/test_helper.h | 73 |
3 files changed, 151 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..0efa3fc --- /dev/null +++ b/test/Makefile @@ -0,0 +1,10 @@ +# Copyright (c) 2020 Johannes Stoelp + +check: build + ./checker + +build: checker.cc + g++ -o cHecker -g -O2 -I ../include -Wall -Wextra $^ + +clean: + rm -f checker diff --git a/test/checker.cc b/test/checker.cc new file mode 100644 index 0000000..2b5bc8a --- /dev/null +++ b/test/checker.cc @@ -0,0 +1,68 @@ +// Copyright (c) 2020 Johannes Stoelp + +#include "test_helper.h" + +#include <cstdio> +#include <fmt.h> + +void check_dec() { + char have[16]; + int len = dynld_snprintf(have, sizeof(have), "%d %d", 12345, -54321); + + ASSERT_EQ("12345 -54321", have); + ASSERT_EQ(12, len); + ASSERT_EQ('\0', have[len]); +} + +void check_hex() { + char have[16]; + int len = dynld_snprintf(have, sizeof(have), "%x %x", 0xdeadbeef, 0xcafe); + + ASSERT_EQ("deadbeef cafe", have); + ASSERT_EQ(13, len); + ASSERT_EQ('\0', have[len]); +} + +void check_ptr() { + char have[16]; + int len = dynld_snprintf(have, sizeof(have), "%p %p", (void*)0xabcd, (void*)0x0); + + ASSERT_EQ("0xabcd 0x0", have); + ASSERT_EQ(10, len); + ASSERT_EQ('\0', have[len]); +} + +void check_null() { + int len = dynld_snprintf(0, 0, "%s", "abcd1234efgh5678"); + + ASSERT_EQ(16, len); +} + +void check_exact_len() { + char have[8]; + int len = dynld_snprintf(have, sizeof(have), "%s", "12345678"); + + ASSERT_EQ("1234567", have); + ASSERT_EQ(8, len); + ASSERT_EQ('\0', have[7]); +} + +void check_exceed_len() { + char have[8]; + int len = dynld_snprintf(have, sizeof(have), "%s", "123456789abcedf"); + + ASSERT_EQ("1234567", have); + ASSERT_EQ(15, len); + ASSERT_EQ('\0', have[7]); +} + +int main() { + TEST_INIT; + TEST(check_dec); + TEST(check_hex); + TEST(check_ptr); + TEST(check_null); + TEST(check_exact_len); + TEST(check_exceed_len); + return TEST_FAIL_CNT; +} diff --git a/test/test_helper.h b/test/test_helper.h new file mode 100644 index 0000000..43cfce9 --- /dev/null +++ b/test/test_helper.h @@ -0,0 +1,73 @@ +// Copyright (c) 2020 Johannes Stoelp + +#pragma once +#include <cstring> +#include <exception> +#include <iostream> + +/* Extremely trivial helper, just to get some tests out. */ + +struct TestFailed : std::exception {}; + +/* Requirements + * T1: comparable + printable (stream operator) + * T2: comparable + printable (stream operator) + */ + +template<typename T1, typename T2> +void ASSERT_EQ(T1 expected, T2 have) { + if (expected != have) { + std::cerr << "ASSERT_EQ failed:\n" + << " expected: " << expected << "\n" + << " have : " << have << "\n" + << std::flush; + throw TestFailed{}; + } +} + +template<typename T1, typename T2> +void ASSERT_EQ(T1* expected, T2* have) { + ASSERT_EQ(*expected, *have); +} + +template<> +void ASSERT_EQ(const char* expected, const char* have) { + if (std::strcmp(expected, have) != 0) { + std::cerr << "ASSERT_EQ failed:\n" + << " expected: " << expected << "\n" + << " have : " << have << "\n" + << std::flush; + throw TestFailed{}; + } +} + +template<> +void ASSERT_EQ(const char* expected, char* have) { + ASSERT_EQ(expected, static_cast<const char*>(have)); +} + +template<> +void ASSERT_EQ(char* expected, const char* have) { + ASSERT_EQ(static_cast<const char*>(expected), have); +} + +void ASSERT_EQ(char* expected, char* have) { + ASSERT_EQ(static_cast<const char*>(expected), static_cast<const char*>(have)); +} + +#define TEST_INIT unsigned fail_cnt = 0; +#define TEST_FAIL_CNT fail_cnt + +#define TEST(fn) \ + { \ + try { \ + fn(); \ + std::cerr << "SUCCESS " #fn << std::endl; \ + } catch (TestFailed&) { \ + ++fail_cnt; \ + std::cerr << "FAIL " #fn << std::endl; \ + } catch (...) { \ + ++fail_cnt; \ + std::cerr << "FAIL " #fn << "(caught unspecified exception)" << std::endl; \ + } \ + } |