stdex Additional custom or not Standard C++ covered algorithms

Loading...
Searching...
No Matches
compat.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#pragma once
7
8#ifdef _WIN32
9#include <CppUnitTest.h>
10#else
11#include <stdexcept>
12
13#define TEST_CLASS(name) class name
14#define TEST_METHOD(name) static void name()
15
16 namespace Assert
17{
18 inline void IsTrue(bool c)
19 {
20 if (!c)
21 throw std::runtime_error("not true");
22 }
23
24 inline void IsFalse(bool c)
25 {
26 if (c)
27 throw std::runtime_error("not false");
28 }
29
30 template <class T>
31 void AreEqual(const T& a, const T& b)
32 {
33 if (!(a == b))
34 throw std::runtime_error("not equal");
35 }
36
37 template <class T, size_t N>
38 void AreEqual(const T (&a)[N], const T (&b)[N])
39 {
40 for (size_t i = 0; i < N; ++i)
41 if (!(a[i] == b[i]))
42 throw std::runtime_error("not equal");
43 }
44
45 inline void AreEqual(const char* a, const char* b)
46 {
47 if (strcmp(a, b) != 0)
48 throw std::runtime_error("not equal");
49 }
50
51 inline void AreEqual(const wchar_t* a, const wchar_t* b)
52 {
53 if (wcscmp(a, b) != 0)
54 throw std::runtime_error("not equal");
55 }
56
57 inline void AreEqual(const char32_t* a, const char32_t* b)
58 {
59#ifdef _WIN32
60 if (stdex::strcmp(a, b) != 0)
61 throw std::runtime_error("not equal");
62#else
63 if (wcscmp(reinterpret_cast<const wchar_t*>(a), reinterpret_cast<const wchar_t*>(b)) != 0)
64 throw std::runtime_error("not equal");
65#endif
66 }
67
68 template <class T>
69 void AreNotEqual(const T& a, const T& b)
70 {
71 if (a == b)
72 throw std::runtime_error("equal");
73 }
74
75 inline void AreNotEqual(const char* a, const char* b)
76 {
77 if (strcmp(a, b) == 0)
78 throw std::runtime_error("equal");
79 }
80
81 inline void AreNotEqual(const wchar_t* a, const wchar_t* b)
82 {
83 if (wcscmp(a, b) == 0)
84 throw std::runtime_error("equal");
85 }
86
87 template <class E, typename F>
88 void ExpectException(F functor)
89 {
90 try { functor(); }
91 catch (const E&) { return; }
92 catch (...) { throw std::runtime_error("unexpected exception"); }
93 throw std::runtime_error("exception not thrown");
94 }
95}
96#endif