Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEST_ASSERT_EQUAL_MEMORY_ARRAY gives false negative #817

Open
parrotrueper opened this issue Sep 27, 2023 · 5 comments
Open

TEST_ASSERT_EQUAL_MEMORY_ARRAY gives false negative #817

parrotrueper opened this issue Sep 27, 2023 · 5 comments

Comments

@parrotrueper
Copy link

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?

@mvandervoord
Copy link
Member

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.

@parrotrueper
Copy link
Author

parrotrueper commented Sep 27, 2023

Got it, so it should be:

TEST_ASSERT_EQUAL_MEMORY_ARRAY(&u8VirtualScreen[0], &u8ReferenceScreen[0], sizeof(u8VirtualScreen), sizeof(uint8_t));

@mvandervoord
Copy link
Member

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.

@parrotrueper
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants