void* handling within the array plugin defaults to bytes.

This commit is contained in:
Mark VanderVoord
2026-06-16 21:41:07 -04:00
parent bad95c5f54
commit a0207f3800
3 changed files with 129 additions and 0 deletions
+5
View File
@@ -160,6 +160,11 @@ class CMockGeneratorUtils
# void* cannot be safely dereferenced; without the array plugin there's no way
# to specify depth, so fall back to comparing the pointer value itself
['UNITY_TEST_ASSERT_EQUAL_PTR', '']
elsif arg[:ptr?] && @arrays && void_pointer_type?(c_type)
# With the array plugin, treat_as_void aliases not in treat_as would fall back to
# MEMORY_ARRAY with sizeof(void), which is illegal. Use HEX8_ARRAY (byte comparison)
# to match what literal void* gets from the default treat_as entry.
['UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY', '']
else
@helpers.nil? || @helpers[:unity_helper].nil? ? ['UNITY_TEST_ASSERT_EQUAL', ''] : @helpers[:unity_helper].get_helper(c_type)
end
@@ -0,0 +1,102 @@
# =========================================================================
# CMock - Automatic Mock Generation for C
# ThrowTheSwitch.org
# Copyright (c) 2007-26 Mike Karlesky, Mark VanderVoord, & Greg Williams
# SPDX-License-Identifier: MIT
# =========================================================================
---
#The purpose of this test is to verify that pointers to treat_as_void alias types
#(e.g. MY_VOID*) use HEX8_ARRAY byte comparison when the array plugin is active,
#rather than falling through to MEMORY_ARRAY which would emit sizeof(void) — illegal C.
:cmock:
:plugins:
- :array
:when_ptr: :smart
:treat_as_void:
- MY_VOID
:systest:
:types: |
typedef void MY_VOID;
:mockable: |
void get_my_void_ptr(MY_VOID* ptr);
:source:
:header: |
void function_a(MY_VOID* arg);
:code: |
void function_a(MY_VOID* arg) {
get_my_void_ptr(arg);
}
:tests:
:common: |
void setUp(void) {}
void tearDown(void) {}
:units:
- :pass: TRUE
:should: 'handle treat_as_void pointer with same pointer using standard expect'
:code: |
test()
{
char* a = "Hello";
get_my_void_ptr_Expect(a);
function_a(a);
}
- :pass: TRUE
:should: 'handle treat_as_void pointer with same content using ExpectWithArray'
:code: |
test()
{
char* a = "Hello";
char* b = "Hello";
get_my_void_ptr_ExpectWithArray(a, 5);
function_a(b);
}
- :pass: FALSE
:should: 'detect treat_as_void pointer content mismatch with ExpectWithArray'
:code: |
test()
{
char* a = "Hello";
char* b = "Jello";
get_my_void_ptr_ExpectWithArray(a, 5);
function_a(b);
}
- :pass: TRUE
:should: 'handle null treat_as_void pointer comparison'
:code: |
test()
{
get_my_void_ptr_Expect(NULL);
function_a(NULL);
}
- :pass: TRUE
:should: 'handle treat_as_void pointer comparison with depth 0 using smart ptr comparison'
:code: |
test()
{
char* a = "Hello";
get_my_void_ptr_ExpectWithArray(a, 0);
function_a(a);
}
- :pass: FALSE
:should: 'detect treat_as_void pointer value mismatch with depth 0 in smart mode'
:code: |
test()
{
static char a[] = "Hello";
static char b[] = "Hello";
get_my_void_ptr_ExpectWithArray(a, 0);
function_a(b);
}
...
+22
View File
@@ -446,4 +446,26 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
" }\n"
assert_equal(expected, utils.code_verify_an_arg_expectation(function, arg))
end
it 'handle treat_as_void alias pointer with array plugin using HEX8_ARRAY to avoid sizeof(void)' do
config_stub = create_stub({
when_ptr: :compare_data,
enforce_strict_ordering: false,
plugins: [:array],
treat_as: {},
treat_as_void: ['MY_VOID']
})
utils = CMockGeneratorUtils.new(config_stub, {:unity_helper => @unity_helper})
function = { :name => 'Pear' }
arg = {:type => "MY_VOID*", :name => 'MyVoidAliasPtr', :ptr? => true, :const? => false, :const_ptr? => false}
expected = " {\n" +
" UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyVoidAliasPtr);\n" +
" if (cmock_call_instance->Expected_MyVoidAliasPtr == NULL)\n" +
" { UNITY_TEST_ASSERT_NULL(MyVoidAliasPtr, cmock_line, CMockStringExpNULL); }\n" +
" else\n" +
" { UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY(cmock_call_instance->Expected_MyVoidAliasPtr, MyVoidAliasPtr, cmock_call_instance->Expected_MyVoidAliasPtr_Depth, cmock_line, CMockStringMismatch); }\n" +
" }\n"
assert_equal(expected, utils.code_verify_an_arg_expectation(function, arg))
end
end