xtd 1.0.0
Loading...
Searching...
No Matches
base_assert.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "../tunit_export.hpp"
6#define __XTD_TUNIT_INTERNAL__
9#define __XTD_TUNIT_INTERNAL__
10#include "abort_error.hpp"
11#include "assert_error.hpp"
12#include "ignore_error.hpp"
13#include "settings.hpp"
14#include <xtd/environment>
15#include <xtd/math>
16#include <xtd/static>
17#include <xtd/types>
18#include <xtd/typeof>
19#include <xtd/string>
20#include <xtd/diagnostics/assert>
21#include <xtd/diagnostics/stack_frame>
22
24namespace xtd {
26 namespace tunit {
34 public:
36
53 static auto abort(const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) -> void;
54
71 static auto fail(const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) -> void;
72
88 static auto ignore(const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) -> void;
89
104 static auto succeed(const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) -> void;
106
107 protected:
109
117 static auto error() -> void;
124 static auto error(const xtd::string& message) -> void;
131 static auto error(const xtd::string& actual, const xtd::string& expected, const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) -> void;
132
139 static auto fail(const xtd::string& actual, const xtd::string& expected, const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) -> void {
140 if (!test_fail(actual, expected, message, stack_frame) && xtd::environment::compiler_version().build_type() == build_type::debug) {
141 auto msg = message != xtd::string {""} ? message : xtd::string {"assertion failed!"}; // Force asyle do not remove {}
142 assert_(false, msg);
143 } else { /*if (xtd::environment::target_type().is_test_application())*/
145 throw assert_error(message != xtd::string {""} ? message : xtd::string {"assertion failed!"});
146 }
147 }
148
152 template<typename value_t>
153 static auto to_string(const value_t& value) -> xtd::string {return __tunit_to_string(value);}
157 template<typename value_t>
158 static auto to_string(const value_t* value) -> xtd::string {return __tunit_to_string(value);}
159
163 template<typename collection_t>
164 static auto join_items(const collection_t& collection) -> xtd::string {return __tunit_join__items(collection);}
168 static auto join_items(const xtd::string& str) -> xtd::string;
170
172
178 template<typename value_a_t, typename value_b_t>
179 [[nodiscard]] static auto equals(const value_a_t& value_a, const value_b_t& value_b) -> bool {
180 return value_a == value_b;
181 }
182
186 template<typename char_t>
187 [[nodiscard]] static auto equals(const char_t* value_a, const string& value_b) -> bool {
188 return xtd::string {value_a} == value_b;
189 }
190
194 template<typename char_t>
195 [[nodiscard]] static auto equals(const string& value_a, const char_t* value_b) -> bool {
196 return value_a == xtd::string {value_b};
197 }
198
202 [[nodiscard]] static auto equals(long double value_a, long double value_b) -> bool {
203 return equals(value_a, value_b, 0.0001l);
204 }
205
209 [[nodiscard]] static auto equals(double value_a, double value_b) -> bool {
210 return equals(value_a, value_b, 0.0001);
211 }
212
216 [[nodiscard]] static auto equals(float value_a, float value_b) -> bool {
217 return equals(value_a, value_b, 0.0001f);
218 }
219
224 [[nodiscard]] static auto equals(double value_a, double value_b, double tolerance) -> bool {
225 return value_a == value_b ? true : xtd::math::abs(value_a - value_b) <= (xtd::math::abs(tolerance) * xtd::math::max(xtd::math::abs(value_a), xtd::math::abs(value_b)));
226 }
227
232 [[nodiscard]] static auto equals(long double value_a, long double value_b, long double tolerance) -> bool {
233 return value_a == value_b ? true : xtd::math::abs(value_a - value_b) <= (xtd::math::abs(tolerance) * xtd::math::max(xtd::math::abs(value_a), xtd::math::abs(value_b)));
234 }
235
240 [[nodiscard]] static auto equals(float value_a, float value_b, float tolerance) -> bool {
241 return value_a == value_b ? true : xtd::math::abs(value_a - value_b) <= (xtd::math::abs(tolerance) * xtd::math::max(xtd::math::abs(value_a), xtd::math::abs(value_b)));
242 }
243
244
245 private:
246 static auto test_fail(const xtd::string& expected, const xtd::string& actual, const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame) -> bool;
247 };
248 }
249}
Contains xtd::tunit::abort_error exception.
Contains xtd::tunit::assert_error exception.
static auto is_attached() -> bool
Gets a value that indicates whether a debugger is attached to the process.
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition stack_frame.hpp:46
static auto current(const xtd::diagnostics::source_location &value=xtd::diagnostics::source_location::current()) noexcept -> xtd::diagnostics::stack_frame
Crates a new xtd::diagnostics::stack_frame object corresponding to the location of the call site.
static auto compiler_version() noexcept -> xtd::compiler
Gets an xtd::compiler object that contains the current compiler identifier and version number.
Definition environment.hpp:360
static auto abs(xtd::decimal value) -> xtd::decimal
Returns the absolute value of a decimal number.
static auto max(xtd::byte a, xtd::byte b) noexcept -> xtd::byte
Returns the larger of two 8-bit unsigned integers.
Exception thow when an assertion failed.
Definition assert_error.hpp:17
The base class for assert.
Definition base_assert.hpp:33
static auto fail(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static auto ignore(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Ignore current test. This is used by the other assert functions.
static auto error() -> void
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static auto to_string(const value_t &value) -> xtd::string
Convert specified value to xtd::string.
Definition base_assert.hpp:153
static auto equals(float value_a, float value_b, float tolerance) -> bool
Determines if specified values are equal with tolerance.
Definition base_assert.hpp:240
static auto abort(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Abort current test. This is used by the other assert functions.
static auto fail(const xtd::string &actual, const xtd::string &expected, const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
Definition base_assert.hpp:139
static auto ignore(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Ignore current test. This is used by the other assert functions.
static auto to_string(const value_t *value) -> xtd::string
Convert specified value to xtd::string.
Definition base_assert.hpp:158
static auto join_items(const collection_t &collection) -> xtd::string
Join specified collection into xtd::string.
Definition base_assert.hpp:164
static auto equals(long double value_a, long double value_b, long double tolerance) -> bool
Determines if specified values are equal with tolerance.
Definition base_assert.hpp:232
static auto equals(const string &value_a, const char_t *value_b) -> bool
Determines if specified values are equal.
Definition base_assert.hpp:195
static auto succeed(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Generates a success with a generic message. This is used by the other assert functions.
static auto join_items(const xtd::string &str) -> xtd::string
Join specified string into xtd::string.
static auto equals(double value_a, double value_b) -> bool
Determines if specified values are equal.
Definition base_assert.hpp:209
static auto error(const xtd::string &actual, const xtd::string &expected, const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static auto fail(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static auto abort(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Abort current test. This is used by the other assert functions.
static auto equals(double value_a, double value_b, double tolerance) -> bool
Determines if specified values are equal with tolerance.
Definition base_assert.hpp:224
static auto succeed(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current()) -> void
Generates a success with a generic message. This is used by the other assert functions.
static auto equals(float value_a, float value_b) -> bool
Determines if specified values are equal.
Definition base_assert.hpp:216
static auto equals(const value_a_t &value_a, const value_b_t &value_b) -> bool
Determines if specified values are equal.
Definition base_assert.hpp:179
static auto equals(const char_t *value_a, const string &value_b) -> bool
Determines if specified values are equal.
Definition base_assert.hpp:187
static auto equals(long double value_a, long double value_b) -> bool
Determines if specified values are equal.
Definition base_assert.hpp:202
static auto error(const xtd::string &message) -> void
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static auto default_settings() noexcept -> xtd::tunit::settings &
Gets default settings instance.
#define debug_break_()
Signals a breakpoint to an attached debugger.
Definition debug_break.hpp:21
#define assert_(...)
Checks for a condition; if the condition is false, displays a message box that shows the call stack.
Definition assert.hpp:25
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:38
#define tunit_export_
Define shared library export.
Definition tunit_export.hpp:13
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
build_type
Identifies the build type.
Definition build_type.hpp:20
@ debug
Build type debug.
Definition build_type.hpp:22
Contains xtd::tunit::ignore_error exception.
The tunit namespace contains a unit test library.
Definition abort_error.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains tunit_export_ keyword.
Contains xtd::tunit::settings class.