Fixed array comparison assert samples
This commit is contained in:
parent
d5518956cb
commit
24f90ea817
3 changed files with 30 additions and 10 deletions
|
@ -65,8 +65,8 @@ Test(asserts, array) {
|
|||
int arr1[] = {1, 2, 3, 4};
|
||||
int arr2[] = {4, 3, 2, 1};
|
||||
|
||||
cr_assert_arr_eq(arr1, arr1, 4);
|
||||
cr_assert_arr_neq(arr1, arr2, 4);
|
||||
cr_assert_arr_eq(arr1, arr1, 4 * sizeof (int));
|
||||
cr_assert_arr_neq(arr1, arr2, 4 * sizeof (int));
|
||||
|
||||
#ifdef __GNUC__
|
||||
struct dummy_struct s1[] = {{4, 2}, {2, 4}};
|
||||
|
|
|
@ -57,20 +57,40 @@ Test(asserts, float) {
|
|||
struct dummy_struct {
|
||||
char a;
|
||||
size_t b;
|
||||
|
||||
bool operator==(const struct dummy_struct& rhs) const {
|
||||
return this->a == rhs.a && this->b == rhs.b;
|
||||
}
|
||||
|
||||
bool operator<(const struct dummy_struct& rhs) const {
|
||||
return this->a < rhs.a;
|
||||
}
|
||||
};
|
||||
|
||||
int eq_dummy(struct dummy_struct *a, struct dummy_struct *b) {
|
||||
return a->a != b->a || a->b != b->b;
|
||||
return *a == *b ? 0 : (*a < *b ? -1 : 1);
|
||||
}
|
||||
|
||||
Test(asserts, array) {
|
||||
// 1. (recommended): use std::array and cr_assert_eq
|
||||
std::array<dummy_struct, 2> cpparr1 = {{{4, 2}, {2, 4}}};
|
||||
std::array<dummy_struct, 2> cpparr2;
|
||||
memset(&cpparr2[0], 0xFF, 2 * sizeof(struct dummy_struct));
|
||||
cpparr2[0].a = 4;
|
||||
cpparr2[0].b = 2;
|
||||
cpparr2[1].a = 2;
|
||||
cpparr2[1].b = 4;
|
||||
|
||||
cr_assert_eq(cpparr1, cpparr2);
|
||||
|
||||
// 2. Compare arrays byte-to-byte
|
||||
int arr1[] = {1, 2, 3, 4};
|
||||
int arr2[] = {4, 3, 2, 1};
|
||||
|
||||
cr_assert_arr_eq(arr1, arr1, 4);
|
||||
cr_assert_arr_neq(arr1, arr2, 4);
|
||||
cr_assert_arr_eq(arr1, arr1, 4 * sizeof (int));
|
||||
cr_assert_arr_neq(arr1, arr2, 4 * sizeof (int));
|
||||
|
||||
#ifdef __GNUC__
|
||||
// 3. Compare arrays with a comparison function
|
||||
struct dummy_struct s1[] = {{4, 2}, {2, 4}};
|
||||
struct dummy_struct s2[2];
|
||||
memset(s2, 0xFF, sizeof(s2));
|
||||
|
@ -79,9 +99,9 @@ Test(asserts, array) {
|
|||
s2[1].a = 2;
|
||||
s2[1].b = 4;
|
||||
|
||||
// cr_assert_arrays_eq(&s1, &s2, 2); not guaranteed to work on structs.
|
||||
// cr_assert_arr_eq(&s1, &s2, 2 * sizeof (struct dummy_struct));
|
||||
// isn't guaranteed to work on structs.
|
||||
cr_assert_arr_eq_cmp(&s1, &s2, 2, eq_dummy);
|
||||
#endif
|
||||
}
|
||||
|
||||
Test(asserts, exception) {
|
||||
|
|
|
@ -12,12 +12,12 @@ Test C assertions:
|
|||
Test C++ assertions:
|
||||
|
||||
$ asserts.cc.bin
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m83\x1b[0m: Assertion failed: The expression (&s1)[0 .. 2] == (&s2)[0 .. 2] is false. (esc)
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m104\x1b[0m: Assertion failed: The expression (&s1)[0 .. 2] == (&s2)[0 .. 2] is false. (esc)
|
||||
[\x1b[0;31mFAIL\x1b[0m] asserts::array (esc)
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m13\x1b[0m: Assertion failed: assert is fatal, expect isn't (esc)
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m14\x1b[0m: Assertion failed: This assert runs (esc)
|
||||
[\x1b[0;31mFAIL\x1b[0m] asserts::base (esc)
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m89\x1b[0m: Assertion failed: The statement `throw std::exception()` did not throw an instance of the `std::bad_alloc` exception. (esc)
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m109\x1b[0m: Assertion failed: The statement `throw std::exception()` did not throw an instance of the `std::bad_alloc` exception. (esc)
|
||||
[\x1b[0;31mFAIL\x1b[0m] asserts::exception (esc)
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m19\x1b[0m: Assertion failed: You can fail an assertion with a message from anywhere (esc)
|
||||
[\x1b[0;34m----\x1b[0m] \x1b[0;1masserts.cc\x1b[0m:\x1b[0;31m20\x1b[0m: Assertion failed: The conditions for this assertion were not met. (esc)
|
||||
|
|
Loading…
Add table
Reference in a new issue