summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/tests/hpptest.hpp
blob: 0c7b4e53e0e6b0f0122f52f7b48d32032f31b4dd (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * File: tests/hpptest.hpp
 * Purpose: HPP Test wrapper for the OCL library.
 * Author: Amlal El Mahrouss (amlal@nekernel.org)
 * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License.
 */

#pragma once

#include <io/print.hpp>
#include <cstdlib>

namespace ocl::hpptest
{
	/// @brief Standard termination error handler, called when a test fails.
	struct standard_terminate final
	{
		template <bool stop_execution = true>
		static void error() noexcept
		{
			ocl::io::print("standard_terminate::error, terminating...\n");

			if (stop_execution)
				std::terminate();
		}
	};

	struct posix_terminate final
	{
		template <bool stop_execution = true, errno_t args>
		static void error(errno_t err) noexcept
		{
			ocl::io::print("posix_terminate::error: expected=", strerror(args), ", got=", strerror(err), "\n");

			if (stop_execution)
				std::terminate();
		}
	};

	typedef bool condition_type;

	template <condition_type expr = true>
	consteval inline void must_be()
	{
#ifdef OCL_HPPTEST
		OCL_HPPTEST_ASSERT(expr);
#endif // _WIN32
	}

	template <condition_type expect, typename on_fail>
	inline void must_be(condition_type cond) noexcept
	{
		if (cond != expect)
		{
			on_fail::template error<true>();
		}
	}

	template <errno_t expect = 0>
	inline void must_be(errno_t ern) noexcept
	{
		if (ern != expect)
		{
			posix_terminate::error<true, expect>(ern);
		}
	}

#ifdef _WIN32
	struct win32_terminate final
	{
		template <bool stop_execution = false>
		static void error(HRESULT err) noexcept
		{
			ocl::io::print("win32_terminate::error: expected=S_OK, got=", err, "\n");

			if (stop_execution)
				std::terminate();
		}
	};

	template <HRESULT expect = S_OK>
	inline void must_be(HRESULT hr) noexcept
	{
		if (hr != expect)
		{
			win32_terminate::error<true>(hr);
		}
	}
#endif // _WIN32
} // namespace ocl::hpptest