chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
# Copyright 2022 Google LLC
# SPDX-License-Identifier: Apache-2.0
# Create a list of common files needed for tests
set(
COMMON_FILE_LIST
include/c_test_framework.h
src/test_cases.include
)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.12.0
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# Create the C test executable
add_executable(c_test src/fff_test_c.c ${COMMON_FILE_LIST})
target_include_directories(c_test PRIVATE include)
target_link_libraries(c_test PRIVATE fff)
# Create the C++ test executable
add_executable(cpp_test src/fff_test_cpp.cpp ${COMMON_FILE_LIST})
target_include_directories(cpp_test PRIVATE include)
target_link_libraries(cpp_test PRIVATE GTest::gtest_main fff)
# Create the C global test executable
add_executable(c_global_test
src/fff_test_global_c.c
src/global_fakes.c
include/global_fakes.h
${COMMON_FILE_LIST}
)
target_include_directories(c_global_test PRIVATE include)
target_link_libraries(c_global_test PRIVATE fff)
# Create the C++ global test executable
add_executable(cpp_global_test
src/fff_test_global_cpp.cpp
src/global_fakes.c
include/global_fakes.h
${COMMON_FILE_LIST}
)
target_include_directories(cpp_global_test PRIVATE include)
target_link_libraries(cpp_global_test PRIVATE GTest::gtest_main fff)
# Create the C++ custom function signature executable
add_executable(cpp_custom_fn_signature_test
src/fff_test_custom_function_template.cpp
${COMMON_FILE_LIST}
)
target_include_directories(cpp_custom_fn_signature_test PRIVATE include)
target_link_libraries(cpp_custom_fn_signature_test PRIVATE GTest::gtest_main fff)
# Due to a bug in WinLibs for Windows it's not currently possible to use:
# target_precompile_headers(cpp_custom_fn_signature_test PUBLIC include/custom_function.hpp)
# See more info at target_precompile_headers(cpp_custom_fn_signature_test PUBLIC include/custom_function.hpp)
add_test(
NAME cpp_custom_fn_signature_test
COMMAND $<TARGET_FILE:cpp_custom_fn_signature_test>
)
# Add the tests for ctest
add_test(
NAME c_test
COMMAND $<TARGET_FILE:c_test>
)
add_test(
NAME cpp_test
COMMAND $<TARGET_FILE:cpp_test>
)
add_test(
NAME c_global_test
COMMAND $<TARGET_FILE:c_global_test>
)
add_test(
NAME cpp_global_test
COMMAND $<TARGET_FILE:cpp_global_test>
)

View File

@@ -0,0 +1,15 @@
#ifndef C_TEST_FRAMEWORK_H_
#define C_TEST_FRAMEWORK_H_
#include <assert.h>
#include <stdio.h>
#include <string.h>
/* Test Framework :-) */
void setup();
#define TEST_F(SUITE, NAME) void NAME()
#define RUN_TEST(SUITE, TESTNAME) do { printf(" Running %s.%s: \n", #SUITE, #TESTNAME); setup(); TESTNAME(); printf(" SUCCESS\n"); } while (0)
#define ASSERT_EQ(A, B) assert((A) == (B))
#define ASSERT_TRUE(A) assert((A))
#endif /* C_TEST_FRAMEWORK_H_ */

View File

@@ -0,0 +1,10 @@
/* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <functional>
#define CUSTOM_FFF_FUNCTION_TEMPLATE(RETURN, FUNCNAME, ...) \
std::function<RETURN (__VA_ARGS__)> FUNCNAME

View File

@@ -0,0 +1,11 @@
/* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <functional>
#include "custom_function.hpp"
#include <fff.h>

View File

@@ -0,0 +1,49 @@
#ifndef GLOBAL_FAKES_H_
#define GLOBAL_FAKES_H_
#include <fff.h>
#include <string.h>
//// Imaginary production code header file ///
enum MYBOOL { FALSE = 899, TRUE };
struct MyStruct {
int x;
int y;
};
enum MYBOOL enumfunc();
struct MyStruct structfunc();
//// End Imaginary production code header file ///
#ifndef TEST_WITH_CALLING_CONVENTIONS
DECLARE_FAKE_VOID_FUNC(voidfunc1, int);
DECLARE_FAKE_VOID_FUNC(voidfunc2, char, char);
DECLARE_FAKE_VOID_FUNC(voidfunc1outparam, char *);
DECLARE_FAKE_VALUE_FUNC(long, longfunc0);
DECLARE_FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0);
DECLARE_FAKE_VALUE_FUNC(struct MyStruct, structfunc0);
DECLARE_FAKE_VOID_FUNC_VARARG(voidfunc3var, const char *, int, ...);
DECLARE_FAKE_VALUE_FUNC_VARARG(int, valuefunc3var, const char *, int, ...);
DECLARE_FAKE_VOID_FUNC(voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
DECLARE_FAKE_VALUE_FUNC(int, valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#else
DECLARE_FAKE_VOID_FUNC(__cdecl, voidfunc1, int);
DECLARE_FAKE_VOID_FUNC(__cdecl, voidfunc2, char, char);
DECLARE_FAKE_VOID_FUNC(__cdecl, voidfunc1outparam, char *);
DECLARE_FAKE_VALUE_FUNC(long, __cdecl, longfunc0);
DECLARE_FAKE_VALUE_FUNC(enum MYBOOL, __cdecl, enumfunc0);
DECLARE_FAKE_VALUE_FUNC(struct MyStruct, __cdecl, structfunc0);
DECLARE_FAKE_VOID_FUNC_VARARG(__cdecl, voidfunc3var, const char *, int, ...);
DECLARE_FAKE_VALUE_FUNC_VARARG(int, __cdecl, valuefunc3var, const char *, int, ...);
DECLARE_FAKE_VOID_FUNC(__cdecl, voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
DECLARE_FAKE_VALUE_FUNC(int, __cdecl, valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#endif
#ifndef __cplusplus
#ifndef TEST_WITH_CALLING_CONVENTIONS
DECLARE_FAKE_VALUE_FUNC(int, strlcpy3, char* const, const char* const, const size_t);
#else
DECLARE_FAKE_VALUE_FUNC(int, __cdecl, strlcpy3, char* const, const char* const, const size_t);
#endif
#endif /* __cplusplus */
#endif /* GLOBAL_FAKES_H_ */

View File

@@ -0,0 +1,132 @@
// Want to keep the argument history for 13 calls
#define OVERRIDE_ARG_HIST_LEN 13u
#define FFF_ARG_HISTORY_LEN OVERRIDE_ARG_HIST_LEN
// Want to keep the call sequence history for 17 function calls
#define OVERRIDE_CALL_HIST_LEN 17u
#define FFF_CALL_HISTORY_LEN OVERRIDE_CALL_HIST_LEN
#include "fff.h"
#include "c_test_framework.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
enum MYBOOL { FALSE = 899, TRUE };
struct MyStruct {
int x;
int y;
};
#ifndef TEST_WITH_CALLING_CONVENTIONS
FAKE_VOID_FUNC(voidfunc1, int);
FAKE_VOID_FUNC(voidfunc2, char, char);
FAKE_VOID_FUNC(voidfunc1outparam, char *);
FAKE_VALUE_FUNC(long, longfunc0);
FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0);
FAKE_VALUE_FUNC(struct MyStruct, structfunc0);
FAKE_VOID_FUNC_VARARG(voidfunc3var, const char *, int, ...);
FAKE_VALUE_FUNC_VARARG(int, valuefunc3var, const char *, int, ...);
FAKE_VALUE_FUNC(int, strlcpy3, char* const, const char* const, const size_t);
FAKE_VOID_FUNC(voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
FAKE_VALUE_FUNC(int, valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#else
FAKE_VOID_FUNC(__cdecl, voidfunc1, int);
FAKE_VOID_FUNC(__cdecl, voidfunc2, char, char);
FAKE_VOID_FUNC(__cdecl, voidfunc1outparam, char *);
FAKE_VALUE_FUNC(long, __cdecl, longfunc0);
FAKE_VALUE_FUNC(enum MYBOOL, __cdecl, enumfunc0);
FAKE_VALUE_FUNC(struct MyStruct, __cdecl, structfunc0);
FAKE_VOID_FUNC_VARARG(__cdecl, voidfunc3var, char *, int, ...);
FAKE_VALUE_FUNC_VARARG(int, __cdecl, valuefunc3var, char *, int, ...);
FAKE_VALUE_FUNC(int, __cdecl, strlcpy3, char* const, const char* const, const size_t);
FAKE_VOID_FUNC(__cdecl, voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
FAKE_VALUE_FUNC(int, __cdecl, valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#endif
void setup()
{
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(voidfunc1outparam);
RESET_FAKE(longfunc0);
RESET_FAKE(enumfunc0);
RESET_FAKE(structfunc0);
RESET_FAKE(voidfunc3var);
RESET_FAKE(valuefunc3var);
RESET_FAKE(strlcpy3);
FFF_RESET_HISTORY();
}
#include "test_cases.include"
TEST_F(FFFTestSuite, default_constants_can_be_overridden)
{
unsigned sizeCallHistory = (sizeof fff.call_history) / (sizeof fff.call_history[0]);
ASSERT_EQ(OVERRIDE_CALL_HIST_LEN, sizeCallHistory);
ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_fake.arg_history_len);
}
DEFINE_FFF_GLOBALS;
int main()
{
setbuf(stdout, NULL);
fprintf(stdout, "-------------\n");
fprintf(stdout, "Running Tests\n");
fprintf(stdout, "-------------\n\n");
fflush(0);
/* Run tests */
RUN_TEST(FFFTestSuite, when_void_func_never_called_then_callcount_is_zero);
RUN_TEST(FFFTestSuite, when_void_func_called_once_then_callcount_is_one);
RUN_TEST(FFFTestSuite, when_void_func_called_once_and_reset_then_callcount_is_zero);
RUN_TEST(FFFTestSuite, when_void_func_with_1_integer_arg_called_then_last_arg_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_1_integer_arg_called_twice_then_last_arg_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_1_integer_arg_called_and_reset_then_captured_arg_is_zero);
RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_then_last_args_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_twice_then_last_args_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captured_arg_is_zero);
RUN_TEST(FFFTestSuite, when_fake_func_called_then_const_arguments_captured);
RUN_TEST(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls);
RUN_TEST(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history);
RUN_TEST(FFFTestSuite, argument_history_is_reset_when_RESET_FAKE_called);
RUN_TEST(FFFTestSuite, when_fake_func_called_max_times_then_no_argument_histories_dropped);
RUN_TEST(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped);
RUN_TEST(FFFTestSuite, value_func_will_return_zero_by_default);
RUN_TEST(FFFTestSuite, value_func_will_return_value_given);
RUN_TEST(FFFTestSuite, value_func_will_return_zero_after_reset);
RUN_TEST(FFFTestSuite, register_call_macro_registers_one_call);
RUN_TEST(FFFTestSuite, register_call_macro_registers_two_calls);
RUN_TEST(FFFTestSuite, reset_call_history_resets_call_history);
RUN_TEST(FFFTestSuite, call_history_will_not_write_past_array_bounds);
RUN_TEST(FFFTestSuite, calling_fake_registers_one_call);
RUN_TEST(FFFTestSuite, return_value_sequences_not_exhausted);
RUN_TEST(FFFTestSuite, return_value_sequences_exhausted);
RUN_TEST(FFFTestSuite, default_constants_can_be_overridden);
RUN_TEST(FFFTestSuite, can_register_custom_fake);
RUN_TEST(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value);
RUN_TEST(FFFTestSuite, use_void_vararg_fake_with_different_number_of_arguments);
RUN_TEST(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments);
RUN_TEST(FFFTestSuite, vararg_custom_fake_sequence_not_exhausted);
RUN_TEST(FFFTestSuite, vararg_custom_fake_seq_return_values_saved_in_history);
RUN_TEST(FFFTestSuite, vararg_custom_fake_sequence_exhausted);
RUN_TEST(FFFTestSuite, vararg_custom_fake_sequence_reset);
RUN_TEST(FFFTestSuite, can_capture_upto_20_arguments_correctly);
RUN_TEST(FFFTestSuite, value_func_can_capture_upto_20_arguments_correctly);
printf("\n-------------\n");
printf("Complete\n");
printf("-------------\n\n");
return 0;
}

View File

@@ -0,0 +1,101 @@
/*
* fff_test.cpp
*
* Created on: Dec 20, 2010
* Author: mlong
*/
// Want to keep the argument history for 13 calls
#define OVERRIDE_ARG_HIST_LEN 13u
#define FFF_ARG_HISTORY_LEN OVERRIDE_ARG_HIST_LEN
// Want to keep the call sequence history for 17 function calls
#define OVERRIDE_CALL_HIST_LEN 17u
#define FFF_CALL_HISTORY_LEN OVERRIDE_CALL_HIST_LEN
#include "fff.h"
#include "gtest/gtest.h"
DEFINE_FFF_GLOBALS
#ifndef TEST_WITH_CALLING_CONVENTIONS
FAKE_VOID_FUNC(voidfunc1, int);
FAKE_VOID_FUNC(voidfunc2, char, char);
FAKE_VOID_FUNC(voidfunc1outparam, char *);
FAKE_VALUE_FUNC(long, longfunc0);
FAKE_VOID_FUNC_VARARG(voidfunc3var, const char *, int, ...);
FAKE_VALUE_FUNC_VARARG(int, valuefunc3var, const char *, int, ...);
FAKE_VOID_FUNC(voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
FAKE_VALUE_FUNC(int, valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#else
FAKE_VOID_FUNC(__cdecl, voidfunc1, int);
FAKE_VOID_FUNC(__cdecl, voidfunc2, char, char);
FAKE_VOID_FUNC(__cdecl, voidfunc1outparam, char *);
FAKE_VALUE_FUNC(long, __cdecl, longfunc0);
FAKE_VOID_FUNC_VARARG(__cdecl, voidfunc3var, const char *, int, ...);
FAKE_VALUE_FUNC_VARARG(int, __cdecl, valuefunc3var, const char *, int, ...);
FAKE_VOID_FUNC(__cdecl, voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
FAKE_VALUE_FUNC(int, __cdecl, valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#endif
class FFFTestSuite: public testing::Test
{
public:
void SetUp()
{
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(longfunc0);
RESET_FAKE(voidfunc1outparam);
RESET_FAKE(voidfunc3var);
RESET_FAKE(valuefunc3var);
FFF_RESET_HISTORY();
}
};
TEST_F(FFFTestSuite, default_constants_can_be_overridden)
{
unsigned sizeCallHistory = (sizeof fff.call_history) / (sizeof fff.call_history[0]);
ASSERT_EQ(OVERRIDE_CALL_HIST_LEN, sizeCallHistory);
ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_fake.arg_history_len);
}
// Fake declared in C++ context (not extern "C", using namespace)
// before the fake is declared
namespace cxx
{
typedef int int_t;
void voidfunc1(cxx::int_t);
}
// Now declare the fake. Must be in the same namespace as the
// original declaration.
namespace cxx
{
#ifndef TEST_WITH_CALLING_CONVENTIONS
FAKE_VOID_FUNC(voidfunc1, cxx::int_t);
#else
FAKE_VOID_FUNC(_cdecl, voidfunc1, cxx::int_t);
#endif
}
TEST_F(FFFTestSuite, cxx_fake_is_called)
{
cxx::voidfunc1(66);
ASSERT_EQ(cxx::voidfunc1_fake.call_count, 1u);
ASSERT_EQ(cxx::voidfunc1_fake.arg0_val, 66);
}
static int cxx_my_custom_fake_called = 0;
void cxx_my_custom_fake(cxx::int_t i)
{
cxx_my_custom_fake_called++;
}
TEST_F(FFFTestSuite, cxx_can_register_custom_fake)
{
cxx::voidfunc1_fake.custom_fake = cxx_my_custom_fake;
cxx::voidfunc1(66);
ASSERT_EQ(1, cxx_my_custom_fake_called);
}
#include "test_cases.include"

View File

@@ -0,0 +1,29 @@
/* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#include <gtest/gtest.h>
#include "fff_wrapper.hpp"
DEFINE_FFF_GLOBALS
FAKE_VOID_FUNC(do_stuff, int);
class FFFCustomFakeSuite : public ::testing::Test {
public:
void SetUp() override {
RESET_FAKE(do_stuff);
FFF_RESET_HISTORY();
}
};
TEST_F(FFFCustomFakeSuite, custom_cpp_fake_function)
{
int x = 0;
do_stuff_fake.custom_fake = [&x](int i) {
x = i;
};
do_stuff(5);
ASSERT_EQ(5, x);
}

View File

@@ -0,0 +1,84 @@
#include "global_fakes.h"
#include "c_test_framework.h"
DEFINE_FFF_GLOBALS;
void setup()
{
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(voidfunc1outparam);
RESET_FAKE(longfunc0);
RESET_FAKE(enumfunc0);
RESET_FAKE(structfunc0);
RESET_FAKE(voidfunc3var);
RESET_FAKE(valuefunc3var);
RESET_FAKE(strlcpy3);
FFF_RESET_HISTORY();
}
#include "test_cases.include"
int main()
{
setbuf(stdout, NULL);
fprintf(stdout, "-------------\n");
fprintf(stdout, "Running Tests\n");
fprintf(stdout, "-------------\n\n");
fflush(0);
/* Run tests */
RUN_TEST(FFFTestSuite, when_void_func_never_called_then_callcount_is_zero);
RUN_TEST(FFFTestSuite, when_void_func_called_once_then_callcount_is_one);
RUN_TEST(FFFTestSuite, when_void_func_called_once_and_reset_then_callcount_is_zero);
RUN_TEST(FFFTestSuite, when_void_func_with_1_integer_arg_called_then_last_arg_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_1_integer_arg_called_twice_then_last_arg_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_1_integer_arg_called_and_reset_then_captured_arg_is_zero);
RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_then_last_args_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_twice_then_last_args_captured);
RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captured_arg_is_zero);
RUN_TEST(FFFTestSuite, when_fake_func_called_then_const_arguments_captured);
RUN_TEST(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls);
RUN_TEST(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history);
RUN_TEST(FFFTestSuite, argument_history_is_reset_when_RESET_FAKE_called);
RUN_TEST(FFFTestSuite, when_fake_func_called_max_times_then_no_argument_histories_dropped);
RUN_TEST(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped);
RUN_TEST(FFFTestSuite, value_func_will_return_zero_by_default);
RUN_TEST(FFFTestSuite, value_func_will_return_value_given);
RUN_TEST(FFFTestSuite, value_func_will_return_zero_after_reset);
RUN_TEST(FFFTestSuite, register_call_macro_registers_one_call);
RUN_TEST(FFFTestSuite, register_call_macro_registers_two_calls);
RUN_TEST(FFFTestSuite, reset_call_history_resets_call_history);
RUN_TEST(FFFTestSuite, call_history_will_not_write_past_array_bounds);
RUN_TEST(FFFTestSuite, calling_fake_registers_one_call);
RUN_TEST(FFFTestSuite, return_value_sequences_not_exhausted);
RUN_TEST(FFFTestSuite, return_value_sequences_exhausted);
RUN_TEST(FFFTestSuite, can_register_custom_fake);
RUN_TEST(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value);
RUN_TEST(FFFTestSuite, use_void_vararg_fake_with_different_number_of_arguments);
RUN_TEST(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments);
RUN_TEST(FFFTestSuite, vararg_custom_fake_sequence_not_exhausted);
RUN_TEST(FFFTestSuite, vararg_custom_fake_seq_return_values_saved_in_history);
RUN_TEST(FFFTestSuite, vararg_custom_fake_sequence_exhausted);
RUN_TEST(FFFTestSuite, vararg_custom_fake_sequence_reset);
RUN_TEST(FFFTestSuite, can_capture_upto_20_arguments_correctly);
RUN_TEST(FFFTestSuite, value_func_can_capture_upto_20_arguments_correctly);
printf("\n-------------\n");
printf("Complete\n");
printf("-------------\n\n");
return 0;
}

View File

@@ -0,0 +1,26 @@
extern "C"{
#include "global_fakes.h"
}
#include "gtest/gtest.h"
DEFINE_FFF_GLOBALS;
class FFFTestSuite: public testing::Test
{
public:
void SetUp()
{
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(longfunc0);
RESET_FAKE(voidfunc1outparam);
RESET_FAKE(voidfunc3var);
RESET_FAKE(valuefunc3var);
FFF_RESET_HISTORY();
}
};
#include "test_cases.include"

View File

@@ -0,0 +1,34 @@
#include "global_fakes.h"
#include <string.h> // for memcpy
#ifndef TEST_WITH_CALLING_CONVENTIONS
DEFINE_FAKE_VOID_FUNC(voidfunc1, int);
DEFINE_FAKE_VOID_FUNC(voidfunc2, char, char);
DEFINE_FAKE_VOID_FUNC(voidfunc1outparam, char *);
DEFINE_FAKE_VALUE_FUNC(long, longfunc0);
DEFINE_FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0);
DEFINE_FAKE_VALUE_FUNC(struct MyStruct, structfunc0);
DEFINE_FAKE_VOID_FUNC_VARARG(voidfunc3var, const char *, int, ...);
DEFINE_FAKE_VALUE_FUNC_VARARG(int, valuefunc3var, const char *, int, ...);
#ifndef __cplusplus
DEFINE_FAKE_VALUE_FUNC(int, strlcpy3, char* const, const char* const, const size_t);
#endif /* __cplusplus */
DEFINE_FAKE_VOID_FUNC(voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
DEFINE_FAKE_VALUE_FUNC(int, valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#else
DEFINE_FAKE_VOID_FUNC(__cdecl, voidfunc1, int);
DEFINE_FAKE_VOID_FUNC(__cdecl, voidfunc2, char, char);
DEFINE_FAKE_VOID_FUNC(__cdecl, voidfunc1outparam, char *);
DEFINE_FAKE_VALUE_FUNC(long, __cdecl, longfunc0);
DEFINE_FAKE_VALUE_FUNC(enum MYBOOL, __cdecl, enumfunc0);
DEFINE_FAKE_VALUE_FUNC(struct MyStruct, __cdecl, structfunc0);
DEFINE_FAKE_VOID_FUNC_VARARG(__cdecl, voidfunc3var, const char *, int, ...);
DEFINE_FAKE_VALUE_FUNC_VARARG(int, __cdecl, valuefunc3var, const char *, int, ...);
#ifndef __cplusplus
DEFINE_FAKE_VALUE_FUNC(int, __cdecl, strlcpy3, char* const, const char* const, const size_t);
#endif /* __cplusplus */
DEFINE_FAKE_VOID_FUNC(__cdecl, voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
DEFINE_FAKE_VALUE_FUNC(int, __cdecl,valuefunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
#endif

View File

@@ -0,0 +1,544 @@
TEST_F(FFFTestSuite, when_void_func_never_called_then_callcount_is_zero)
{
ASSERT_EQ(voidfunc1_fake.call_count, 0u);
}
TEST_F(FFFTestSuite, when_void_func_called_once_then_callcount_is_one)
{
voidfunc1(66);
ASSERT_EQ(voidfunc1_fake.call_count, 1u);
}
TEST_F(FFFTestSuite, when_void_func_called_once_and_reset_then_callcount_is_zero)
{
voidfunc1(66);
RESET_FAKE(voidfunc1);
ASSERT_EQ(voidfunc1_fake.call_count, 0u);
}
// Single Argument
TEST_F(FFFTestSuite, when_void_func_with_1_integer_arg_called_then_last_arg_captured)
{
voidfunc1(77);
ASSERT_EQ(voidfunc1_fake.arg0_val, 77);
}
TEST_F(FFFTestSuite, when_void_func_with_1_integer_arg_called_twice_then_last_arg_captured)
{
voidfunc1(77);
voidfunc1(12);
ASSERT_EQ(voidfunc1_fake.arg0_val, 12);
}
TEST_F(FFFTestSuite, when_void_func_with_1_integer_arg_called_and_reset_then_captured_arg_is_zero)
{
voidfunc1(11);
RESET_FAKE(voidfunc1);
ASSERT_EQ(voidfunc1_fake.arg0_val, 0);
}
// Two Arguments
TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_then_last_args_captured)
{
voidfunc2('a', 'b');
ASSERT_EQ(voidfunc2_fake.arg0_val, 'a');
ASSERT_EQ(voidfunc2_fake.arg1_val, 'b');
}
TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_twice_then_last_args_captured)
{
voidfunc2('a', 'b');
voidfunc2('c', 'd');
ASSERT_EQ(voidfunc2_fake.arg0_val, 'c');
ASSERT_EQ(voidfunc2_fake.arg1_val, 'd');
}
TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captured_arg_is_zero)
{
voidfunc2('e', 'f');
RESET_FAKE(voidfunc2);
ASSERT_EQ(voidfunc2_fake.arg0_val, 0);
ASSERT_EQ(voidfunc2_fake.arg1_val, 0);
}
#ifndef __cplusplus
TEST_F(FFFTestSuite, when_fake_func_called_then_const_arguments_captured)
{
char dst[80];
strlcpy3(dst, __FUNCTION__, sizeof(__FUNCTION__));
}
#endif /* __cplusplus */
// Argument history
TEST_F(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls)
{
ASSERT_EQ(FFF_ARG_HISTORY_LEN, (sizeof voidfunc2_fake.arg0_history) / (sizeof voidfunc2_fake.arg0_history[0]));
ASSERT_EQ(FFF_ARG_HISTORY_LEN, (sizeof voidfunc2_fake.arg1_history) / (sizeof voidfunc2_fake.arg1_history[0]));
}
TEST_F(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history)
{
voidfunc2('g', 'h');
ASSERT_EQ('g', voidfunc2_fake.arg0_history[0]);
ASSERT_EQ('h', voidfunc2_fake.arg1_history[0]);
}
TEST_F(FFFTestSuite, argument_history_is_reset_when_RESET_FAKE_called)
{
//given
voidfunc2('g', 'h');
ASSERT_EQ('g', voidfunc2_fake.arg0_history[0]);
ASSERT_EQ('h', voidfunc2_fake.arg1_history[0]);
//when
RESET_FAKE(voidfunc2);
//then
ASSERT_EQ('\0', voidfunc2_fake.arg0_history[0]);
ASSERT_EQ('\0', voidfunc2_fake.arg1_history[0]);
}
TEST_F(FFFTestSuite, when_fake_func_called_max_times_then_no_argument_histories_dropped)
{
unsigned int i;
for (i = 0; i < FFF_ARG_HISTORY_LEN; i++)
{
voidfunc2('1' + i, '2' + i);
}
ASSERT_EQ(0u, voidfunc2_fake.arg_histories_dropped);
}
TEST_F(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped)
{
unsigned int i;
for (i = 0; i < FFF_ARG_HISTORY_LEN; i++)
{
voidfunc2('1' + i, '2' + i);
}
voidfunc2('1', '2');
ASSERT_EQ(1u, voidfunc2_fake.arg_histories_dropped);
// or in other words..
ASSERT_TRUE(voidfunc2_fake.call_count > voidfunc2_fake.arg_history_len);
}
// Return values
TEST_F(FFFTestSuite, value_func_will_return_zero_by_default)
{
ASSERT_EQ(0l, longfunc0());
}
TEST_F(FFFTestSuite, value_func_will_return_value_given)
{
longfunc0_fake.return_val = 99l;
ASSERT_EQ(99l, longfunc0());
}
TEST_F(FFFTestSuite, value_func_will_return_zero_after_reset)
{
longfunc0_fake.return_val = 99l;
RESET_FAKE(longfunc0);
ASSERT_EQ(0l, longfunc0());
}
TEST_F(FFFTestSuite, register_call_macro_registers_one_call)
{
REGISTER_CALL(longfunc0);
ASSERT_EQ(fff.call_history[0], (void *)longfunc0);
}
TEST_F(FFFTestSuite, register_call_macro_registers_two_calls)
{
REGISTER_CALL(longfunc0);
REGISTER_CALL(voidfunc2);
ASSERT_EQ(fff.call_history[0], (void *)longfunc0);
ASSERT_EQ(fff.call_history[1], (void *)voidfunc2);
}
TEST_F(FFFTestSuite, reset_call_history_resets_call_history)
{
REGISTER_CALL(longfunc0);
REGISTER_CALL(voidfunc1);
FFF_RESET_HISTORY();
REGISTER_CALL(voidfunc2);
ASSERT_EQ(1u, fff.call_history_idx);
ASSERT_EQ(fff.call_history[0], (void *)voidfunc2);
ASSERT_EQ(fff.call_history[1], (void *)0);
}
TEST_F(FFFTestSuite, call_history_will_not_write_past_array_bounds)
{
for (unsigned int i = 0; i < FFF_CALL_HISTORY_LEN + 1; i++)
{
REGISTER_CALL(longfunc0);
}
ASSERT_EQ(FFF_CALL_HISTORY_LEN, fff.call_history_idx);
}
TEST_F(FFFTestSuite, calling_fake_registers_one_call)
{
longfunc0();
ASSERT_EQ(fff.call_history_idx, 1u);
ASSERT_EQ(fff.call_history[0], (void *)longfunc0);
}
TEST_F(FFFTestSuite, return_value_sequences_not_exhausted)
{
long myReturnVals[3] = { 3, 7, 9 };
SET_RETURN_SEQ(longfunc0, myReturnVals, 3);
ASSERT_EQ(myReturnVals[0], longfunc0());
ASSERT_EQ(myReturnVals[1], longfunc0());
ASSERT_EQ(myReturnVals[2], longfunc0());
}
TEST_F(FFFTestSuite, return_value_sequences_exhausted)
{
long myReturnVals[3] = { 3, 7, 9 };
SET_RETURN_SEQ(longfunc0, myReturnVals, 3);
ASSERT_EQ(myReturnVals[0], longfunc0());
ASSERT_EQ(myReturnVals[1], longfunc0());
ASSERT_EQ(myReturnVals[2], longfunc0());
ASSERT_EQ(myReturnVals[2], longfunc0());
ASSERT_EQ(myReturnVals[2], longfunc0());
}
TEST_F(FFFTestSuite, return_value_sequences_reset)
{
long myReturnVals[3] = { 3, 7, 9 };
SET_RETURN_SEQ(longfunc0, myReturnVals, 3);
ASSERT_EQ(myReturnVals[0], longfunc0());
ASSERT_EQ(myReturnVals[1], longfunc0());
RESET_FAKE(longfunc0);
ASSERT_EQ(0, longfunc0());
}
static int my_custom_fake_called = 0;
void my_custom_fake(char a, char b)
{
my_custom_fake_called++;
}
TEST_F(FFFTestSuite, can_register_custom_fake)
{
voidfunc2_fake.custom_fake = my_custom_fake;
voidfunc2('a', 'b');
ASSERT_EQ(1, my_custom_fake_called);
}
void voidfunc1outparam_custom_fake1(char *a)
{
*a = 'x';
}
void voidfunc1outparam_custom_fake2(char *a)
{
*a = 'y';
}
void voidfunc1outparam_custom_fake3(char *a)
{
*a = 'z';
}
TEST_F(FFFTestSuite, custom_fake_sequence_not_exausthed)
{
CUSTOM_FFF_FUNCTION_TEMPLATE(void, custom_fakes[], char *) = {
voidfunc1outparam_custom_fake1,
voidfunc1outparam_custom_fake2,
voidfunc1outparam_custom_fake3
};
char a = 'a';
SET_CUSTOM_FAKE_SEQ(voidfunc1outparam, custom_fakes, 3);
voidfunc1outparam(&a);
ASSERT_EQ('x', a);
voidfunc1outparam(&a);
ASSERT_EQ('y', a);
voidfunc1outparam(&a);
ASSERT_EQ('z', a);
}
TEST_F(FFFTestSuite, return_value_sequence_saved_in_history)
{
long myReturnVals[3] = { 3, 7, 9 };
SET_RETURN_SEQ(longfunc0, myReturnVals, 3);
longfunc0();
longfunc0();
longfunc0();
ASSERT_EQ(myReturnVals[0], longfunc0_fake.return_val_history[0]);
ASSERT_EQ(myReturnVals[1], longfunc0_fake.return_val_history[1]);
ASSERT_EQ(myReturnVals[2], longfunc0_fake.return_val_history[2]);
}
TEST_F(FFFTestSuite, return_value_saved_in_history)
{
long i;
for (i = 0; i < FFF_ARG_HISTORY_LEN; i++)
{
longfunc0_fake.return_val = i + 1;
longfunc0();
}
for (i = 0; i < FFF_ARG_HISTORY_LEN; i++)
{
ASSERT_EQ(longfunc0_fake.return_val_history[i], i + 1);
}
}
long custom_longfunc1(void)
{
return 42;
}
long custom_longfunc2(void)
{
return 15;
}
long custom_longfunc3(void)
{
return 7;
}
TEST_F(FFFTestSuite, custom_fake_seq_return_values_saved_in_history)
{
CUSTOM_FFF_FUNCTION_TEMPLATE(long, custom_fakes[], void) = {
custom_longfunc1,
custom_longfunc2,
custom_longfunc3};
SET_CUSTOM_FAKE_SEQ(longfunc0, custom_fakes, 3);
longfunc0();
longfunc0();
longfunc0();
ASSERT_EQ(42, longfunc0_fake.return_val_history[0]);
ASSERT_EQ(15, longfunc0_fake.return_val_history[1]);
ASSERT_EQ(7, longfunc0_fake.return_val_history[2]);
}
TEST_F(FFFTestSuite, custom_fake_sequence_exhausted)
{
CUSTOM_FFF_FUNCTION_TEMPLATE(void, custom_fakes[], char *) = {
voidfunc1outparam_custom_fake1,
voidfunc1outparam_custom_fake2,
voidfunc1outparam_custom_fake3};
char a = 'a';
SET_CUSTOM_FAKE_SEQ(voidfunc1outparam, custom_fakes, 3);
voidfunc1outparam(&a);
ASSERT_EQ('x', a);
voidfunc1outparam(&a);
ASSERT_EQ('y', a);
voidfunc1outparam(&a);
ASSERT_EQ('z', a);
a = 'a';
voidfunc1outparam(&a);
ASSERT_EQ('z', a);
a = 'b';
voidfunc1outparam(&a);
ASSERT_EQ('z', a);
}
//DECLARE_FAKE_VALUE_FUNC0(long, longfunc0);
#define MEANING_OF_LIFE 42
long my_custom_value_fake(void)
{
return MEANING_OF_LIFE;
}
long my_custom_value_fake2(void)
{
static long val = 0;
return val++;
}
TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value)
{
longfunc0_fake.custom_fake = my_custom_value_fake;
long retval = longfunc0();
ASSERT_EQ(MEANING_OF_LIFE, retval);
}
TEST_F(FFFTestSuite, return_values_from_custom_fake_saved_in_history)
{
longfunc0_fake.custom_fake = my_custom_value_fake2;
longfunc0();
longfunc0();
longfunc0();
ASSERT_EQ(0, longfunc0_fake.return_val_history[0]);
ASSERT_EQ(1, longfunc0_fake.return_val_history[1]);
ASSERT_EQ(2, longfunc0_fake.return_val_history[2]);
}
int valuefunc3var_custom_fake1(const char *str, int a, va_list vl)
{
int arg;
while ((arg = va_arg(vl, int)) != 0)
a += arg;
return a;
}
int valuefunc3var_custom_fake2(const char *str, int a, va_list vl)
{
int arg;
while ((arg = va_arg(vl, int)) != 0)
a -= arg;
return a;
}
int valuefunc3var_custom_fake3(const char *str, int a, va_list vl)
{
int arg;
while ((arg = va_arg(vl, int)) != 0)
a *= arg;
return a;
}
TEST_F(FFFTestSuite, vararg_custom_fake_sequence_not_exhausted)
{
CUSTOM_FFF_FUNCTION_TEMPLATE(int, custom_fakes[], const char *, int,
va_list) = {valuefunc3var_custom_fake1,
valuefunc3var_custom_fake2,
valuefunc3var_custom_fake3};
SET_CUSTOM_FAKE_SEQ(valuefunc3var, custom_fakes, 3);
int a = 1;
ASSERT_EQ(valuefunc3var("a", a, 2, 3, 4, 0), 10);
ASSERT_EQ(valuefunc3var("aa", a, 2, 3, 4, 2, 0), -10);
ASSERT_EQ(valuefunc3var("aaa", a, 2, 3, 0), 6);
}
TEST_F(FFFTestSuite, vararg_custom_fake_seq_return_values_saved_in_history)
{
CUSTOM_FFF_FUNCTION_TEMPLATE(int, custom_fakes[], const char *, int,
va_list) = {valuefunc3var_custom_fake1,
valuefunc3var_custom_fake2,
valuefunc3var_custom_fake3};
SET_CUSTOM_FAKE_SEQ(valuefunc3var, custom_fakes, 3);
int a = 1;
valuefunc3var("a", a, 2, 3, 4, 0);
valuefunc3var("aa", a, 2, 3, 4, 2, 0);
valuefunc3var("aaa", a, 2, 3, 0);
ASSERT_EQ(10, valuefunc3var_fake.return_val_history[0]);
ASSERT_EQ(-10, valuefunc3var_fake.return_val_history[1]);
ASSERT_EQ(6, valuefunc3var_fake.return_val_history[2]);
}
TEST_F(FFFTestSuite, vararg_custom_fake_sequence_exhausted)
{
CUSTOM_FFF_FUNCTION_TEMPLATE(int, custom_fakes[], const char *, int,
va_list) = {valuefunc3var_custom_fake1,
valuefunc3var_custom_fake2,
valuefunc3var_custom_fake3};
SET_CUSTOM_FAKE_SEQ(valuefunc3var, custom_fakes, 3);
int a = 1;
ASSERT_EQ(valuefunc3var("a", a, 2, 3, 4, 0), 10);
ASSERT_EQ(valuefunc3var("aa", a, 2, 3, 4, 2, 0), -10);
ASSERT_EQ(valuefunc3var("aaa", a, 2, 3, 0), 6);
ASSERT_EQ(valuefunc3var("aaa", a, 4, 5, 1, 0), 20);
ASSERT_EQ(valuefunc3var("aaa", a, 4, 0), 4);
}
TEST_F(FFFTestSuite, vararg_custom_fake_sequence_reset)
{
CUSTOM_FFF_FUNCTION_TEMPLATE(int, custom_fakes[], const char *, int,
va_list) = {valuefunc3var_custom_fake1,
valuefunc3var_custom_fake2,
valuefunc3var_custom_fake3};
SET_CUSTOM_FAKE_SEQ(valuefunc3var, custom_fakes, 3);
int a = 1;
ASSERT_EQ(valuefunc3var("a", a, 2, 3, 4, 0), 10);
ASSERT_EQ(valuefunc3var("aa", a, 2, 3, 4, 2, 0), -10);
RESET_FAKE(valuefunc3var);
ASSERT_EQ(0, valuefunc3var("b", 7, 4, 5));
valuefunc3var_fake.return_val = 1;
ASSERT_EQ(1, valuefunc3var("c", 5, 4, 4));
valuefunc3var_fake.return_val = 2;
ASSERT_EQ(2, valuefunc3var("d", 6, 3, 5));
}
TEST_F(FFFTestSuite, use_void_vararg_fake_with_different_number_of_arguments)
{
voidfunc3var("0 parameters", 0);
voidfunc3var("1 parameter", 1, 10);
voidfunc3var("2 parameters", 2, 10, 20);
voidfunc3var("3 parameters", 3, 10, 20, 30);
ASSERT_EQ(voidfunc3var_fake.call_count, 4u);
char msg[] = "3 parameters";
ASSERT_EQ(strcmp(voidfunc3var_fake.arg0_val, msg), 0);
ASSERT_EQ(3, voidfunc3var_fake.arg1_val);
}
TEST_F(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments)
{
valuefunc3var("0 parameters", 0);
valuefunc3var("1 parameter", 1, 10);
valuefunc3var("2 parameters", 2, 10, 20);
valuefunc3var("3 parameters", 3, 10, 20, 30);
ASSERT_EQ(valuefunc3var_fake.call_count, 4u);
char msg[] = "3 parameters";
ASSERT_EQ(strcmp(valuefunc3var_fake.arg0_val, msg), 0);
ASSERT_EQ(3, valuefunc3var_fake.arg1_val);
}
TEST_F(FFFTestSuite, can_capture_upto_20_arguments_correctly)
{
voidfunc20(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
ASSERT_EQ(0, voidfunc20_fake.arg0_val);
ASSERT_EQ(1, voidfunc20_fake.arg1_val);
ASSERT_EQ(2, voidfunc20_fake.arg2_val);
ASSERT_EQ(3, voidfunc20_fake.arg3_val);
ASSERT_EQ(4, voidfunc20_fake.arg4_val);
ASSERT_EQ(5, voidfunc20_fake.arg5_val);
ASSERT_EQ(6, voidfunc20_fake.arg6_val);
ASSERT_EQ(7, voidfunc20_fake.arg7_val);
ASSERT_EQ(8, voidfunc20_fake.arg8_val);
ASSERT_EQ(9, voidfunc20_fake.arg9_val);
ASSERT_EQ(10, voidfunc20_fake.arg10_val);
ASSERT_EQ(11, voidfunc20_fake.arg11_val);
ASSERT_EQ(12, voidfunc20_fake.arg12_val);
ASSERT_EQ(13, voidfunc20_fake.arg13_val);
ASSERT_EQ(14, voidfunc20_fake.arg14_val);
ASSERT_EQ(15, voidfunc20_fake.arg15_val);
ASSERT_EQ(16, voidfunc20_fake.arg16_val);
ASSERT_EQ(17, voidfunc20_fake.arg17_val);
ASSERT_EQ(18, voidfunc20_fake.arg18_val);
ASSERT_EQ(19, voidfunc20_fake.arg19_val);
}
TEST_F(FFFTestSuite, value_func_can_capture_upto_20_arguments_correctly)
{
valuefunc20_fake.return_val = 42;
int return_val = valuefunc20(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
ASSERT_EQ(42, return_val);
ASSERT_EQ(0, valuefunc20_fake.arg0_val);
ASSERT_EQ(1, valuefunc20_fake.arg1_val);
ASSERT_EQ(2, valuefunc20_fake.arg2_val);
ASSERT_EQ(3, valuefunc20_fake.arg3_val);
ASSERT_EQ(4, valuefunc20_fake.arg4_val);
ASSERT_EQ(5, valuefunc20_fake.arg5_val);
ASSERT_EQ(6, valuefunc20_fake.arg6_val);
ASSERT_EQ(7, valuefunc20_fake.arg7_val);
ASSERT_EQ(8, valuefunc20_fake.arg8_val);
ASSERT_EQ(9, valuefunc20_fake.arg9_val);
ASSERT_EQ(10, valuefunc20_fake.arg10_val);
ASSERT_EQ(11, valuefunc20_fake.arg11_val);
ASSERT_EQ(12, valuefunc20_fake.arg12_val);
ASSERT_EQ(13, valuefunc20_fake.arg13_val);
ASSERT_EQ(14, valuefunc20_fake.arg14_val);
ASSERT_EQ(15, valuefunc20_fake.arg15_val);
ASSERT_EQ(16, valuefunc20_fake.arg16_val);
ASSERT_EQ(17, valuefunc20_fake.arg17_val);
ASSERT_EQ(18, valuefunc20_fake.arg18_val);
ASSERT_EQ(19, valuefunc20_fake.arg19_val);
}