TEST_ASSERT_EQUAL_MEMORY_ARRAY gives false negative
Ubuntu jammy
test_notAwesome.c
#include <unity.h>
void setUp(void){}
void tearDown(void){}
void test_HelloWorld_ArrayCmpMemWay(void){
uint8_t u8VirtualScreen[]={'H','e','l','l','o',0};
uint8_t u8ReferenceScreen[]={'H','e','l','l','o',0};
/* Array compare option 4 */
for (uint32_t u32Ix=0; u32Ix < sizeof(u8VirtualScreen); u32Ix++){
printf("[%d][0x%x][0x%x],", u32Ix, u8VirtualScreen[u32Ix], u8VirtualScreen[u32Ix]);
}
printf("\n");
printf("This TEST produces a false negative, see above\n");
TEST_ASSERT_EQUAL_MEMORY_ARRAY(&u8VirtualScreen[0], &u8ReferenceScreen[0], sizeof(u8VirtualScreen), sizeof(u8VirtualScreen));
}
ceedling test:all
Test 'test_notAwesome.c'
------------------------
Generating runner for test_notAwesome.c...
Compiling test_notAwesome_runner.c...
Compiling test_notAwesome.c...
Linking test_notAwesome.out...
Running test_notAwesome.out...
-----------
TEST OUTPUT
-----------
[test_notAwesome.c]
- "[0][0x48][0x48],[1][0x65][0x65],[2][0x6c][0x6c],[3][0x6c][0x6c],[4][0x6f][0x6f],[5][0x0][0x0],"
- "This TEST produces a false negative, see above"
-------------------
FAILED TEST SUMMARY
-------------------
[test_notAwesome.c]
Test: test_HelloWorld_ArrayCmpMemWay
At line (18): "Memory Mismatch. Element 1 Byte 0 Expected 0x48 Was 0x00"
--------------------
OVERALL TEST SUMMARY
--------------------
TESTED: 1
PASSED: 0
FAILED: 1
IGNORED: 0
---------------------
BUILD FAILURE SUMMARY
---------------------
Unit test failures.
There is not much in terms of documentation for the types of parameters to tests. Is this really a false negative, or am I doing something wrong?
The problem is the two sizeof operators. You have the length of each array... but what you want is the number of elements and the length of each ELEMENT of the array.
Because you're working with just bytes here, it could be even easier to use TEST_ASSERT_EQUAL_HEX8_ARRAY, which only takes the length to compare.
Got it, so it should be:
TEST_ASSERT_EQUAL_MEMORY_ARRAY(&u8VirtualScreen[0], &u8ReferenceScreen[0], sizeof(u8VirtualScreen), sizeof(uint8_t));
You can either change to this:
TEST_ASSERT_EQUAL_MEMORY_ARRAY(&u8VirtualScreen[0], &u8ReferenceScreen[0], sizeof(u8VirtualScreen[0]), sizeof(u8VirtualScreen)/sizeof(u8VirtualScreen[0]));
Notice that the first sizeof is taking the size of ONE ELEMENT. The second is the number of elements in the arrays.
because you're working with an array of bytes, this could be more simply worded:
TEST_ASSERT_EQUAL_MEMORY_ARRAY(&u8VirtualScreen[0], &u8ReferenceScreen[0], 1, sizeof(u8VirtualScreen));
or even more simply:
TEST_ASSERT_EQUAL_HEX8_ARRAY(&u8VirtualScreen[0], &u8ReferenceScreen[0], sizeof(u8VirtualScreen));
I believe either any of the above should work better.
And here are the docs you requested: https://github.com/ThrowTheSwitch/Unity/blob/master/docs/UnityAssertionsReference.md#test_assert_equal_memory_array-expected-actual-len-num_elements
Thanks for the link, I've seen those docs before, but again, there isn't any reference to what types the parameters should be, so it's a bit of guesswork. I would expect a macro to be something like this:
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY( ptrExpected, ptrActual, sizeOfArray, bytesPerElement)
This would make it obvious what the parameters are.
Thanks.