Skip to content

Commit 77f0080

Browse files
committed
Fix: Warnings in YT_EQ_MEM for buffer of complex types
Also includes: * Test case for checking this case
1 parent 5733f4e commit 77f0080

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

tests/basic_failures.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,21 @@ YT_TEST (basic, memory_tests)
4444
#define ARRAY_LEN(b) (sizeof (b) / sizeof (b[0]))
4545

4646
char buffer1[] = { 0, 1, 2, 3 };
47-
// char buffer2[] = { 0, 1, 2, 3 };
4847
char buffer3[] = { 0, 1, 2, 4 };
4948

5049
YT_EQ_MEM (buffer1, buffer3, ARRAY_LEN (buffer1)); // Fails because buffer1 != buffer3.
51-
// YT_NEQ_MEM (buffer1, buffer2, ARRAY_LEN (buffer1)); // Fails because buffer1 == buffer2.
50+
51+
YT_END();
52+
}
53+
54+
YT_TEST (basic, memory_tests_complex_type)
55+
{
56+
typedef struct Person { int id; } Person;
57+
58+
Person buffer1[] = { (Person) {.id = 0}, (Person) {.id = 1}, (Person) {.id = 2}, (Person) {.id = 3 }};
59+
Person buffer3[] = { (Person) {.id = 0}, (Person) {.id = 1}, (Person) {.id = 2}, (Person) {.id = 4 }};
60+
61+
YT_EQ_MEM (buffer1, buffer3, sizeof(Person) * ARRAY_LEN (buffer1)); // Fails because buffer1 != buffer3.
5262

5363
YT_END();
5464
}
@@ -64,5 +74,6 @@ int main (void)
6474
scaler_tests_floats();
6575
string_tests();
6676
memory_tests();
77+
memory_tests_complex_type();
6778
YT_RETURN_WITH_REPORT();
6879
}

tests/basic_passing.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#define YUKTI_TEST_IMPLEMENTATION
22
#include "../yukti.h"
33

4+
#define ARRAY_LEN(b) sizeof (b) / sizeof (b[0])
5+
46
YT_TEST (basic, scaler_tests_floats)
57
{
68
YT_NEQ_DOUBLE_ABS (1.0, 2.0, 0.01); // Passed because 1.0 != 2.0
@@ -44,8 +46,6 @@ YT_TEST (basic, string_tests)
4446

4547
YT_TEST (basic, memory_tests)
4648
{
47-
#define ARRAY_LEN(b) sizeof (b) / sizeof (b[0])
48-
4949
char buffer1[] = { 0, 1, 2, 3 };
5050
char buffer2[] = { 0, 1, 2, 3 };
5151
char buffer3[] = { 0, 1, 2, 4 };
@@ -56,6 +56,20 @@ YT_TEST (basic, memory_tests)
5656
YT_END();
5757
}
5858

59+
YT_TEST (basic, memory_tests_complex_type)
60+
{
61+
typedef struct Person { int id; } Person;
62+
63+
Person buffer1[] = { (Person) {.id = 0}, (Person) {.id = 1}, (Person) {.id = 2}, (Person) {.id = 3 }};
64+
Person buffer2[] = { (Person) {.id = 0}, (Person) {.id = 1}, (Person) {.id = 2}, (Person) {.id = 3 }};
65+
Person buffer3[] = { (Person) {.id = 0}, (Person) {.id = 1}, (Person) {.id = 2}, (Person) {.id = 4 }};
66+
67+
YT_EQ_MEM (buffer1, buffer2, sizeof(Person) * ARRAY_LEN (buffer1)); // Passes because buffer1 == buffer2.
68+
YT_NEQ_MEM (buffer1, buffer3, sizeof(Person) * ARRAY_LEN (buffer1)); // Passes because buffer1 != buffer3.
69+
70+
YT_END();
71+
}
72+
5973
void yt_reset()
6074
{
6175
}
@@ -67,5 +81,6 @@ int main (void)
6781
scaler_tests_floats();
6882
string_tests();
6983
memory_tests();
84+
memory_tests_complex_type();
7085
YT_RETURN_WITH_REPORT();
7186
}

tests/exps/basic_failures.exp

265 Bytes
Binary file not shown.

tests/exps/basic_passing.exp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
  basic:scaler_tests_floats  OK [0 of 5 failed]
33
  basic:string_tests  OK [0 of 3 failed]
44
  basic:memory_tests  OK [0 of 2 failed]
5+
  basic:memory_tests_complex_type  OK [0 of 2 failed]
56

6-
All tests passed [0 of 4 failed]
7+
All tests passed [0 of 5 failed]
78

yukti.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ static bool yt__approxeq (bool is_abs, double a, double b, double epsilon)
913913

914914
#define YT__TEST_MEM(a, o, b, sz) \
915915
do { \
916-
AUTOTYPE ut_a = (a); \
917-
AUTOTYPE ut_b = (b); \
916+
uint8_t* ut_a = (uint8_t*)(a); \
917+
uint8_t* ut_b = (uint8_t*)(b); \
918918
YT__current_testrecord->total_exp_count++; \
919919
int i; \
920920
if (!(YT__equal_mem (ut_a, ut_b, sz, &i) o 1)) \

0 commit comments

Comments
 (0)