Skip to content

Commit

Permalink
Removed including standard libraries from mock sources.
Browse files Browse the repository at this point in the history
- Introduced CMock_memset and CMock_memcpy
- Updated tests.
  • Loading branch information
informatimago committed Aug 11, 2023
1 parent ed29ce3 commit 1b1218f
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/cmock_file_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def create_file(filename, subdir)

full_file_name_temp = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}.new"
full_file_name_done = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}"
$stderr.puts "Creating #{full_file_name_done.inspect}" unless (@config.verbosity < 2)

File.open(full_file_name_temp, 'w') do |file|
yield(file, filename)
end
Expand Down
9 changes: 3 additions & 6 deletions lib/cmock_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def create_using_statement(file, function)
def create_mock_header_file(mock_project)
if @include_inline == :include
@file_writer.create_file(mock_project[:module_name] + (mock_project[:module_ext]), mock_project[:folder]) do |file, _filename|
file << "/* Source File: #{mock_project[:source]} */\n"
file << "/* Normalized source (without inlines). */\n"
file << mock_project[:parsed_stuff][:normalized_source]
end
end
Expand Down Expand Up @@ -209,11 +211,6 @@ def create_mock_header_footer(header)
def create_source_header_section(file, filename, mock_project)
header_file = (mock_project[:folder] || '') + filename.gsub('.c', mock_project[:module_ext])
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless mock_project[:parsed_stuff][:functions].empty?
file << "#include <string.h>\n"
file << "#include <stdlib.h>\n"
unless @exclude_setjmp_h
file << "#include <setjmp.h>\n"
end
file << "#include \"cmock.h\"\n"
@includes_c_pre_header.each { |inc| file << "#include #{inc}\n" }
file << "#include \"#{header_file}\"\n"
Expand Down Expand Up @@ -283,7 +280,7 @@ def create_mock_init_function(file, mock_project)
def create_mock_destroy_function(file, mock_project)
file << "void #{mock_project[:clean_name]}_Destroy(void)\n{\n"
file << " CMock_Guts_MemFreeAll();\n"
file << " memset(&Mock, 0, sizeof(Mock));\n"
file << " CMock_memset(&Mock, 0, sizeof(Mock));\n"
file << mock_project[:parsed_stuff][:functions].collect { |function| @plugins.run(:mock_destroy, function) }.join

unless @fail_on_unexpected_calls
Expand Down
2 changes: 1 addition & 1 deletion lib/cmock_generator_plugin_return_thru_ptr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def mock_implementation(function)
lines << " if (cmock_call_instance->ReturnThruPtr_#{arg_name}_Used)\n"
lines << " {\n"
lines << " UNITY_TEST_ASSERT_NOT_NULL(#{arg_name}, cmock_line, CMockStringPtrIsNULL);\n"
lines << " memcpy((void*)#{arg_name}, (void*)cmock_call_instance->ReturnThruPtr_#{arg_name}_Val,\n"
lines << " CMock_memcpy((void*)#{arg_name}, (void*)cmock_call_instance->ReturnThruPtr_#{arg_name}_Val,\n"
lines << " cmock_call_instance->ReturnThruPtr_#{arg_name}_Size);\n"
lines << " }\n"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cmock_generator_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def code_add_base_expectation(func_name, global_ordering_supported = true)
lines = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_#{func_name}_CALL_INSTANCE));\n"
lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = (CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n"
lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n"
lines << " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n"
lines << " CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n"
lines << " Mock.#{func_name}_CallInstance = CMock_Guts_MemChain(Mock.#{func_name}_CallInstance, cmock_guts_index);\n"
lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if @ignore || @ignore_stateless
lines << " cmock_call_instance->LineNumber = cmock_line;\n"
Expand All @@ -75,7 +75,7 @@ def code_assign_argument_quickly(dest, arg)
else
assert_expr = "sizeof(#{arg[:name]}) == sizeof(#{arg[:type]}) ? 1 : -1"
comment = "/* add #{arg[:type]} to :treat_as_array if this causes an error */"
" memcpy((void*)(&#{dest}), (void*)(&#{arg[:name]}),\n" \
" CMock_memcpy((void*)(&#{dest}), (void*)(&#{arg[:name]}),\n" \
" sizeof(#{arg[:type]}[#{assert_expr}])); #{comment}\n"
end
end
Expand Down
17 changes: 17 additions & 0 deletions src/cmock.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,20 @@ void CMock_Guts_MemFreeFinal(void)
#endif
}

void CMock_memset(void* ptr, int value, size_t num)
{
size_t i;
for (i = 0; i < num; i++)
{
((char*)ptr)[i] = (char)value;
}
}

void CMock_memcpy(void* ptr, const void* src, size_t num)
{
size_t i;
for (i = 0; i < num; i++)
{
((char*)ptr)[i] = ((char*)src)[i];
}
}
2 changes: 2 additions & 0 deletions src/cmock.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesFree(void) CMOCK_FUNCTION_ATTR(pure);
CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesUsed(void) CMOCK_FUNCTION_ATTR(pure);
void CMock_Guts_MemFreeAll(void);
void CMock_Guts_MemFreeFinal(void);
void CMock_memset(void* ptr, int value, size_t num);
void CMock_memcpy(void* ptr, const void* src, size_t num);

#endif /* end of CMOCK_FRAMEWORK_H */
7 changes: 2 additions & 5 deletions test/unit/cmock_generator_main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,6 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext)
{ :name => "tres", :args => [] }
]
expected = [ "/* AUTOGENERATED FILE. DO NOT EDIT. */\n",
"#include <string.h>\n",
"#include <stdlib.h>\n",
"#include <setjmp.h>\n",
"#include \"cmock.h\"\n",
"#include \"MockPoutPoutFish.h\"\n",
"\n",
Expand Down Expand Up @@ -486,7 +483,7 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext)
output = []
expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n",
" CMock_Guts_MemFreeAll();\n",
" memset(&Mock, 0, sizeof(Mock));\n",
" CMock_memset(&Mock, 0, sizeof(Mock));\n",
"}\n\n"
]

Expand All @@ -503,7 +500,7 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext)
output = []
expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n",
" CMock_Guts_MemFreeAll();\n",
" memset(&Mock, 0, sizeof(Mock));\n",
" CMock_memset(&Mock, 0, sizeof(Mock));\n",
" uno",
" GlobalExpectCount = 0;\n",
" GlobalVerifyOrder = 0;\n",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cmock_generator_plugin_return_thru_ptr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def complex_func_expect
" if (cmock_call_instance->ReturnThruPtr_tofu_Used)\n" +
" {\n" +
" UNITY_TEST_ASSERT_NOT_NULL(tofu, cmock_line, CMockStringPtrIsNULL);\n" +
" memcpy((void*)tofu, (void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" +
" CMock_memcpy((void*)tofu, (void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" +
" cmock_call_instance->ReturnThruPtr_tofu_Size);\n" +
" }\n"

Expand Down
14 changes: 7 additions & 7 deletions test/unit/cmock_generator_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
" CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" +
" CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" +
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" +
" memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
" CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
" Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" +
" cmock_call_instance->LineNumber = cmock_line;\n"
output = @cmock_generator_utils_simple.code_add_base_expectation("Apple")
Expand All @@ -73,7 +73,7 @@
" CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" +
" CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" +
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" +
" memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
" CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
" Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" +
" Mock.Apple_IgnoreBool = (char)0;\n" +
" cmock_call_instance->LineNumber = cmock_line;\n" +
Expand All @@ -88,7 +88,7 @@
" CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" +
" CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" +
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" +
" memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
" CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
" Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" +
" Mock.Apple_IgnoreBool = (char)0;\n" +
" cmock_call_instance->LineNumber = cmock_line;\n" +
Expand All @@ -108,7 +108,7 @@
expected3 = " cmock_call_instance->Expected_Kiwi = Kiwi;\n"

arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false }
expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
expected4 = " CMock_memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
" sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n"

assert_equal(expected1, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg1))
Expand All @@ -134,7 +134,7 @@
" cmock_call_instance->ReturnThruPtr_Kiwi_Used = 0;\n"

arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false }
expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
expected4 = " CMock_memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
" sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n" +
" cmock_call_instance->IgnoreArg_Lime = 0;\n"

Expand All @@ -158,7 +158,7 @@
expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff);\n" +
"void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff)\n{\n" +
" cmock_call_instance->Expected_MyIntPtr = MyIntPtr;\n" +
" memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
" CMock_memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
" sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" +
" cmock_call_instance->Expected_MyStr = MyStr;\n" +
"}\n\n"
Expand All @@ -176,7 +176,7 @@
" cmock_call_instance->Expected_MyIntPtr_Depth = MyIntPtr_Depth;\n" +
" cmock_call_instance->IgnoreArg_MyIntPtr = 0;\n" +
" cmock_call_instance->ReturnThruPtr_MyIntPtr_Used = 0;\n" +
" memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
" CMock_memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
" sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" +
" cmock_call_instance->IgnoreArg_MyMyType = 0;\n" +
" cmock_call_instance->Expected_MyStr = MyStr;\n" +
Expand Down

0 comments on commit 1b1218f

Please sign in to comment.