xtd 0.2.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
54
71 static void fail(const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current());
72
89
106
107 protected:
109
117 static void error();
124 static void error(const xtd::string& message);
131 static void 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());
132
139 static void 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()) {
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<class value_t>
153 static xtd::string to_string(const value_t& value) {return __tunit_to_string(value);}
157 template<class value_t>
158 static xtd::string to_string(const value_t* value) {return __tunit_to_string(value);}
159
163 template<class collection_t>
164 static xtd::string join_items(const collection_t& collection) {return __tunit_join__items(collection);}
170
172
178 template<class value_a_t, class value_b_t>
179 static bool equals(const value_a_t& value_a, const value_b_t& value_b) {
180 return value_a == value_b;
181 }
182
186 template<class char_t>
187 static bool equals(const char_t* value_a, const string& value_b) {
188 return xtd::string {value_a} == value_b;
189 }
190
194 template<class char_t>
195 static bool equals(const string& value_a, const char_t* value_b) {
196 return value_a == xtd::string {value_b};
197 }
198
202 static bool equals(long double value_a, long double value_b) {
203 return equals(value_a, value_b, 0.0001l);
204 }
205
209 static bool equals(double value_a, double value_b) {
210 return equals(value_a, value_b, 0.0001);
211 }
212
216 static bool equals(float value_a, float value_b) {
217 return equals(value_a, value_b, 0.0001f);
218 }
219
224 static bool equals(double value_a, double value_b, double tolerance) {
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 static bool equals(long double value_a, long double value_b, long double tolerance) {
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 static bool equals(float value_a, float value_b, float tolerance) {
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 bool test_fail(const xtd::string& expected, const xtd::string& actual, const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame);
247 };
248 }
249}
Contains std::basic_ostream<char_t, char_traits_t>& operator <<(std::basic_ostream<char_t,...
Contains__tunit_join__items functions.
Contains xtd::tunit::abort_error exception.
Contains xtd::tunit::assert_error exception.
static bool is_attached()
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:48
static stack_frame current(const xtd::diagnostics::source_location &value=xtd::diagnostics::source_location::current()) noexcept
Crates a new xtd::diagnostics::stack_frame object corresponding to the location of the call site.
static xtd::target_type target_type() noexcept
Gets an xtd::target_type object that contains the current target identifier.
Definition environment.hpp:474
static xtd::compiler compiler_version() noexcept
Gets an xtd::compiler object that contains the current compiler identifier and version number.
Definition environment.hpp:360
static decimal abs(decimal value)
Returns the absolute value of a decimal number.
static xtd::byte max(xtd::byte a, xtd::byte b) noexcept
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 xtd::string join_items(const collection_t &collection)
Join specified collection into xtd::string.
Definition base_assert.hpp:164
static xtd::string join_items(const xtd::string &str)
Join specified string into xtd::string.
static void ignore(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Ignore current test. This is used by the other assert functions.
static void succeed(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Generates a success with a generic message. This is used by the other assert functions.
static bool equals(double value_a, double value_b, double tolerance)
Determines if specified values are equal with tolerance.
Definition base_assert.hpp:224
static void 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())
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
Definition base_assert.hpp:139
static bool equals(float value_a, float value_b, float tolerance)
Determines if specified values are equal with tolerance.
Definition base_assert.hpp:240
static void ignore(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Ignore current test. This is used by the other assert functions.
static xtd::string to_string(const value_t *value)
Convert specified value to xtd::string.
Definition base_assert.hpp:158
static void error()
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static void error(const xtd::string &message)
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static void 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())
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static void fail(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static void abort(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Abort current test. This is used by the other assert functions.
static void fail(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Throws an xtd::tunit::assert_error exception. This is used by the other assert functions.
static void succeed(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Generates a success with a generic message. This is used by the other assert functions.
static bool equals(const string &value_a, const char_t *value_b)
Determines if specified values are equal.
Definition base_assert.hpp:195
static bool equals(const value_a_t &value_a, const value_b_t &value_b)
Determines if specified values are equal.
Definition base_assert.hpp:179
static bool equals(double value_a, double value_b)
Determines if specified values are equal.
Definition base_assert.hpp:209
static xtd::string to_string(const value_t &value)
Convert specified value to xtd::string.
Definition base_assert.hpp:153
static void abort(const xtd::string &message, const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Abort current test. This is used by the other assert functions.
static bool equals(const char_t *value_a, const string &value_b)
Determines if specified values are equal.
Definition base_assert.hpp:187
static bool equals(float value_a, float value_b)
Determines if specified values are equal.
Definition base_assert.hpp:216
static bool equals(long double value_a, long double value_b, long double tolerance)
Determines if specified values are equal with tolerance.
Definition base_assert.hpp:232
static bool equals(long double value_a, long double value_b)
Determines if specified values are equal.
Definition base_assert.hpp:202
static xtd::tunit::settings & default_settings() noexcept
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:37
#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.