From 8c95db33be58a545dd2e030428bded0bd958c4b6 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 16 Oct 2024 22:54:34 +0200 Subject: Start work on the testing framework --- asl/tests/format_tests.cpp | 106 ++++++++++++++++++++++++ asl/tests/integers_tests.cpp | 11 +++ asl/tests/maybe_uninit_tests.cpp | 10 +++ asl/tests/meta_tests.cpp | 170 +++++++++++++++++++++++++++++++++++++++ asl/tests/option_tests.cpp | 33 ++++++++ asl/tests/test_types.hpp | 25 ++++++ asl/tests/utility_tests.cpp | 1 + 7 files changed, 356 insertions(+) create mode 100644 asl/tests/format_tests.cpp create mode 100644 asl/tests/integers_tests.cpp create mode 100644 asl/tests/maybe_uninit_tests.cpp create mode 100644 asl/tests/meta_tests.cpp create mode 100644 asl/tests/option_tests.cpp create mode 100644 asl/tests/test_types.hpp create mode 100644 asl/tests/utility_tests.cpp (limited to 'asl/tests') diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp new file mode 100644 index 0000000..6e2430d --- /dev/null +++ b/asl/tests/format_tests.cpp @@ -0,0 +1,106 @@ +#include "asl/format.hpp" + +#include +#include +#include +#include + +// @Todo Improve this to use our utilities, not the C stdlib + +static_assert(asl::formattable); + +class StringSink : public asl::writer +{ + int64_t m_current_len{}; + char* m_data{}; + +public: + void write(const char* str, int64_t len) override + { + m_data = (char*)realloc(m_data, (size_t)(m_current_len + len + 1)); + memcpy(m_data + m_current_len, str, (size_t)len); + m_current_len += len; + m_data[m_current_len] = '\0'; + } + + constexpr const char* cstr() const { return m_data; } + + void reset() + { + m_current_len = 0; + free(m_data); + m_data = nullptr; + } +}; + +int main2() +{ + StringSink sink; + + // @Todo Use the testing framework + + asl::format(&sink, "Hello, world!"); + assert(strcmp(sink.cstr(), "Hello, world!") == 0); + + sink.reset(); + asl::format(&sink, ""); + assert(strcmp(sink.cstr(), "") == 0); + + sink.reset(); + asl::format(&sink, "Hello, {}!", "world"); + assert(strcmp(sink.cstr(), "Hello, world!") == 0); + + sink.reset(); + asl::format(&sink, "Hello, {}! {}", "world"); + assert(strcmp(sink.cstr(), "Hello, world! ") == 0); + + sink.reset(); + asl::format(&sink, "Hello, pup!", "world"); + assert(strcmp(sink.cstr(), "Hello, pup!") == 0); + + sink.reset(); + asl::format(&sink, "{}", "CHEESE"); + assert(strcmp(sink.cstr(), "CHEESE") == 0); + + sink.reset(); + asl::format(&sink, "{ ", "CHEESE"); + assert(strcmp(sink.cstr(), " ") == 0); + + sink.reset(); + asl::format(&sink, "{", "CHEESE"); + assert(strcmp(sink.cstr(), "") == 0); + + sink.reset(); + asl::format(&sink, "a{{b"); + assert(strcmp(sink.cstr(), "a{b") == 0); + + sink.reset(); + asl::format(&sink, "{{{}}} }", "CHEESE"); + assert(strcmp(sink.cstr(), "{CHEESE} }") == 0); + + sink.reset(); + asl::format(&sink, "{} {} {}", 0, 1, 2); + assert(strcmp(sink.cstr(), "0 1 2") == 0); + + sink.reset(); + asl::format(&sink, "{} {} {}", 10, 11, 12); + assert(strcmp(sink.cstr(), "10 11 12") == 0); + + sink.reset(); + asl::format(&sink, "{} {} {}", 100, 101, 102); + assert(strcmp(sink.cstr(), "100 101 102") == 0); + + sink.reset(); + asl::format(&sink, "{} {} {}", 1000, 1001, 1002); + assert(strcmp(sink.cstr(), "1000 1001 1002") == 0); + + sink.reset(); + asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890); + assert(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0); + + sink.reset(); + asl::format(&sink, "{} {}", true, false); + assert(strcmp(sink.cstr(), "true false") == 0); + + return 0; +} diff --git a/asl/tests/integers_tests.cpp b/asl/tests/integers_tests.cpp new file mode 100644 index 0000000..d15168e --- /dev/null +++ b/asl/tests/integers_tests.cpp @@ -0,0 +1,11 @@ +#include "asl/integers.hpp" + +static_assert(sizeof(int8_t) == 1); +static_assert(sizeof(int16_t) == 2); +static_assert(sizeof(int32_t) == 4); +static_assert(sizeof(int64_t) == 8); + +static_assert(sizeof(uint8_t) == 1); +static_assert(sizeof(uint16_t) == 2); +static_assert(sizeof(uint32_t) == 4); +static_assert(sizeof(uint64_t) == 8); diff --git a/asl/tests/maybe_uninit_tests.cpp b/asl/tests/maybe_uninit_tests.cpp new file mode 100644 index 0000000..3f60558 --- /dev/null +++ b/asl/tests/maybe_uninit_tests.cpp @@ -0,0 +1,10 @@ +#include "asl/maybe_uninit.hpp" +#include "asl/tests/test_types.hpp" + +static_assert(asl::layout::of() == asl::layout::of>()); +static_assert(asl::size_of == asl::size_of>); +static_assert(asl::align_of == asl::align_of>); + +static_assert(asl::trivially_destructible>); +static_assert(!asl::trivially_destructible>); + diff --git a/asl/tests/meta_tests.cpp b/asl/tests/meta_tests.cpp new file mode 100644 index 0000000..5de4f8d --- /dev/null +++ b/asl/tests/meta_tests.cpp @@ -0,0 +1,170 @@ +#include "asl/meta.hpp" +#include "asl/tests/test_types.hpp" + +struct Struct {}; +union Union {}; +enum Enum { EnumVariant = 0, }; +enum class EnumClass { Variant = 0, }; + +static_assert(!asl::is_same); +static_assert(asl::is_same); + +static_assert(asl::is_same, float>); +static_assert(asl::is_same, int>); + +static_assert(asl::default_constructible); +static_assert(asl::default_constructible); +static_assert(asl::default_constructible); +static_assert(!asl::default_constructible); + +static_assert(asl::trivially_default_constructible); +static_assert(asl::trivially_default_constructible); +static_assert(!asl::trivially_default_constructible); +static_assert(!asl::trivially_default_constructible); + +static_assert(asl::copy_constructible); +static_assert(asl::copy_constructible); +static_assert(asl::copy_constructible); +static_assert(!asl::copy_constructible); + +static_assert(asl::trivially_copy_constructible); +static_assert(asl::trivially_copy_constructible); +static_assert(!asl::trivially_copy_constructible); +static_assert(!asl::trivially_copy_constructible); + +static_assert(asl::move_constructible); +static_assert(asl::move_constructible); +static_assert(asl::move_constructible); +static_assert(!asl::move_constructible); + +static_assert(asl::trivially_move_constructible); +static_assert(asl::trivially_move_constructible); +static_assert(!asl::trivially_move_constructible); +static_assert(!asl::trivially_move_constructible); + +static_assert(asl::copy_assignable); +static_assert(asl::copy_assignable); +static_assert(asl::copy_assignable); +static_assert(!asl::copy_assignable); + +static_assert(asl::trivially_copy_assignable); +static_assert(!asl::trivially_copy_assignable); +static_assert(asl::trivially_copy_assignable); +static_assert(!asl::trivially_copy_assignable); + +static_assert(asl::move_assignable); +static_assert(asl::move_assignable); +static_assert(asl::move_assignable); +static_assert(!asl::move_assignable); + +static_assert(asl::trivially_move_assignable); +static_assert(!asl::trivially_move_assignable); +static_assert(asl::trivially_move_assignable); +static_assert(!asl::trivially_move_assignable); + +static_assert(asl::trivially_destructible); +static_assert(asl::trivially_destructible); +static_assert(!asl::trivially_destructible); + +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); + +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); + +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); + +static_assert(asl::is_void); +static_assert(asl::is_void); +static_assert(asl::is_void); +static_assert(asl::is_void); +static_assert(!asl::is_void); +static_assert(!asl::is_void); +static_assert(!asl::is_void); +static_assert(!asl::is_void); +static_assert(!asl::is_void); +static_assert(!asl::is_void); + +static_assert(asl::is_ref); +static_assert(asl::is_ref); +static_assert(asl::is_ref); +static_assert(asl::is_ref); +static_assert(!asl::is_ref); +static_assert(!asl::is_ref); +static_assert(!asl::is_ref); +static_assert(!asl::is_ref); + +static_assert(asl::is_ptr); +static_assert(asl::is_ptr); +static_assert(asl::is_ptr); +static_assert(!asl::is_ptr); +static_assert(!asl::is_ptr); +static_assert(!asl::is_ptr); +static_assert(!asl::is_ptr); + +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); + +static_assert(asl::is_func); +static_assert(asl::is_func); +static_assert(asl::is_func); +static_assert(asl::is_func); +static_assert(asl::is_func); +static_assert(asl::is_func); +static_assert(!asl::is_func); +static_assert(!asl::is_func); +static_assert(!asl::is_func); +static_assert(!asl::is_func); + +static_assert(asl::is_object); +static_assert(asl::is_object); +static_assert(asl::is_object); +static_assert(asl::is_object); +static_assert(asl::is_object); +static_assert(asl::is_object); +static_assert(asl::is_object); +static_assert(asl::is_object); +static_assert(!asl::is_object); +static_assert(!asl::is_object); +static_assert(!asl::is_object); +static_assert(!asl::is_object); + +static_assert(!asl::is_array); +static_assert(!asl::is_array); +static_assert(!asl::is_array); +static_assert(!asl::is_array); +static_assert(!asl::is_array); +static_assert(asl::is_array); +static_assert(asl::is_array); +static_assert(!asl::is_array); +static_assert(!asl::is_array); +static_assert(!asl::is_array); +static_assert(!asl::is_array); +static_assert(!asl::is_array); + +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); +static_assert(asl::is_same>); + +static_assert(asl::types_count == 2); +static_assert(asl::types_count == 2); +static_assert(asl::types_count == 1); +static_assert(asl::types_count<> == 0); + +static_assert(asl::trivially_copyable); +static_assert(!asl::trivially_copyable); +static_assert(!asl::trivially_copyable); +static_assert(asl::trivially_copyable); +static_assert(asl::trivially_copyable); diff --git a/asl/tests/option_tests.cpp b/asl/tests/option_tests.cpp new file mode 100644 index 0000000..7de1f9c --- /dev/null +++ b/asl/tests/option_tests.cpp @@ -0,0 +1,33 @@ +#include "asl/option.hpp" +#include "asl/tests/test_types.hpp" +#include + +static_assert(asl::trivially_destructible>); +static_assert(!asl::trivially_destructible>); + +static_assert(asl::copy_constructible>); +static_assert(asl::copy_constructible>); +static_assert(!asl::copy_constructible>); +static_assert(!asl::copy_constructible>); + +static_assert(asl::move_constructible>); +static_assert(asl::move_constructible>); +static_assert(asl::move_constructible>); +static_assert(!asl::move_constructible>); + +static_assert(asl::copy_assignable>); +static_assert(asl::copy_assignable>); +static_assert(!asl::copy_assignable>); +static_assert(!asl::copy_assignable>); + +static_assert(asl::move_assignable>); +static_assert(asl::move_assignable>); +static_assert(!asl::move_assignable>); + +ASL_TEST(Option, cheese) +{ + asl::option a; + asl::option b; + + a = ASL_MOVE(b); +} diff --git a/asl/tests/test_types.hpp b/asl/tests/test_types.hpp new file mode 100644 index 0000000..da1faa6 --- /dev/null +++ b/asl/tests/test_types.hpp @@ -0,0 +1,25 @@ +#pragma once + +struct DefaultConstructible { DefaultConstructible() {} }; +struct TriviallyDefaultConstructible { TriviallyDefaultConstructible() = default; }; +struct NonDefaultConstructible { NonDefaultConstructible() = delete; }; + +struct CopyConstructible { CopyConstructible(const CopyConstructible&) {} }; +struct TriviallyCopyConstructible { TriviallyCopyConstructible(const TriviallyCopyConstructible&) = default; }; +struct NonCopyConstructible { NonCopyConstructible(const NonCopyConstructible&) = delete; }; + +struct MoveConstructible { MoveConstructible(MoveConstructible&&) {} }; +struct TriviallyMoveConstructible { TriviallyMoveConstructible(TriviallyMoveConstructible&&) = default; }; +struct NonMoveConstructible { NonMoveConstructible(NonMoveConstructible&&) = delete; }; + +struct CopyAssignable { CopyAssignable& operator=(const CopyAssignable&) { return *this; } }; +struct TriviallyCopyAssignable { TriviallyCopyAssignable& operator=(const TriviallyCopyAssignable&) = default; }; +struct NonCopyAssignable { NonCopyAssignable& operator=(const NonCopyAssignable&) = delete; }; + +struct MoveAssignable { MoveAssignable& operator=(MoveAssignable&&) { return *this; } }; +struct TriviallyMoveAssignable { TriviallyMoveAssignable& operator=(TriviallyMoveAssignable&&) = default; }; +struct NonMoveAssignable { NonMoveAssignable& operator=(NonMoveAssignable&&) = delete; }; + +struct TriviallyDestructible { ~TriviallyDestructible() = default; }; +struct HasDestructor { ~HasDestructor() {} }; + diff --git a/asl/tests/utility_tests.cpp b/asl/tests/utility_tests.cpp new file mode 100644 index 0000000..4acded7 --- /dev/null +++ b/asl/tests/utility_tests.cpp @@ -0,0 +1 @@ +#include "asl/utility.hpp" -- cgit