ThrowTheSwitch / CMock

CMock - Mock/stub generator for C
http://throwtheswitch.org
MIT License
652 stars 269 forks source link

_Expect does not work as expected - with structures #459

Closed gorogbalazs closed 3 months ago

gorogbalazs commented 9 months ago

_Expect does not work as expected when the parameter is a non-packed structure

Minimum reproducible example here:

test_header.h

#ifndef TEST_HEADER_H
#define TEST_HEADER_H

#include <stdint.h>

typedef struct
{
    uint8_t param1;
    uint16_t param2;
    uint16_t param3;
    uint32_t param4;
} my_struct_t;

void function_to_be_mocked(my_struct_t param);

#endif

test.h

#ifndef TEST_H
#define TEST_H 1

#include <stdint.h>

void my_function(uint8_t param1, uint16_t param2, uint16_t param3, uint32_t param4);

#endif  //TEST_H

test.c

#include "test.h"

#include "test_header.h"
#include <stdio.h>

void my_function(uint8_t param1, uint16_t param2, uint16_t param3, uint32_t param4)
{
    my_struct_t temp;
    temp.param1 = param1;
    temp.param2 = param2;
    temp.param3 = param3;
    temp.param4 = param4;

    uint32_t *ptr = (uint32_t *)&temp;
    printf("passed:\n");
    printf("x%08X, x%08X, x%08X\n", *ptr, *(ptr + 1), *(ptr + 2));

    function_to_be_mocked(temp);
}

test_my_function.c

#include "unity.h"
#include <stdio.h>
#include <string.h>

#include "test.h"

#include "mock_test_header.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_my_function(void)
{
    printf("--------------------------------------------------\n");
    my_struct_t temp;
    temp.param1 = 0x11;
    temp.param2 = 0x2222;
    temp.param3 = 0x3333;
    temp.param4 = 0x44444444;

    uint32_t *ptr = (uint32_t *)&temp;
    printf("expected:\n");
    printf("x%08X, x%08X, x%08X\n", *ptr, *(ptr + 1), *(ptr + 2));

    function_to_be_mocked_Expect(temp);
    my_function(temp.param1, temp.param2, temp.param3, temp.param4);
}

output:

./test_my_function.c: "--------------------------------------------------"
./test_my_function.c: "expected:"
./test_my_function.c: "x22222011, x00003333, x44444444"
./test_my_function.c: "passed:"
./test_my_function.c: "x2222FE11, x00FB3333, x44444444"

./test_my_function.c:31:test_my_function: "Memory Mismatch. Byte 1 Expected 0x20 Was 0xFE. Function function_to_be_mocked Argument param. Function called with unexpected argument value."

My expectation:

mvandervoord commented 3 months ago

Documentation has been greatly expanded in this area. :)