From b22903e498095bab90549a49a94bde8c1ef3e4a8 Mon Sep 17 00:00:00 2001 From: Mike Long Date: Tue, 21 Dec 2010 23:26:48 +0100 Subject: [PATCH] Unit tests for the framework --- src/test/Makefile | 36 ++++++++++- src/test/fff_test.cpp | 140 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 src/test/fff_test.cpp diff --git a/src/test/Makefile b/src/test/Makefile index 1757704..6821269 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -1,5 +1,35 @@ -all: - @echo "all" +CPP_SRCS += \ +fff_test.cpp + +OBJS += \ +../../build/fff_test.o + +LIBS := -lgtest_main -lgtest -lpthread + +# All Target +all: ../../build/fff_test + + +# Each subdirectory must supply rules for building sources it contributes +../../build/%.o: %.cpp + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -I/home/mlong/tools/gtest/gtest-1.5.0/include -O0 -g3 -Wall -c -o"$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +# Tool invocations +../../build/fff_test: $(OBJS) $(USER_OBJS) + @echo 'Building target: $@' + @echo 'Invoking: GCC C++ Linker' + g++ -L/home/mlong/tools/gtest -o"../../build/fff_test" $(OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets clean: - @echo "clean" + -$(RM) $(OBJS) fff_test + -@echo ' ' + diff --git a/src/test/fff_test.cpp b/src/test/fff_test.cpp new file mode 100644 index 0000000..c0162ac --- /dev/null +++ b/src/test/fff_test.cpp @@ -0,0 +1,140 @@ +/* + * fff_test.cpp + * + * Created on: Dec 20, 2010 + * Author: mlong + */ + +#include "../fff.h" +#include + +FAKE_VOID_FUNC1(voidfunc1, int); +FAKE_VOID_FUNC2(voidfunc2, char, char); +FAKE_VALUE_FUNC0(long, longfunc0); + +class FFFTestSuite : public testing::Test +{ +public: + void SetUp() + { + RESET_FAKES(); + RESET_HISTORY(); + } +}; + +// Call counting +TEST_F(FFFTestSuite, when_void_func_never_called_then_callcount_is_zero) +{ + ASSERT_EQ(voidfunc1_call_count, 0); +} + +TEST_F(FFFTestSuite, when_void_func_called_once_then_callcount_is_one) +{ + voidfunc1(66); + ASSERT_EQ(voidfunc1_call_count, 1); +} + +TEST_F(FFFTestSuite, when_void_func_called_once_and_reset_then_callcount_is_zero) +{ + voidfunc1(66); + RESET_FAKE(voidfunc1); + ASSERT_EQ(voidfunc1_call_count, 0); +} + +// Single Argument +TEST_F(FFFTestSuite, when_void_func_with_1_integer_arg_called_then_last_arg_captured) +{ + voidfunc1(77); + ASSERT_EQ(voidfunc1_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_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_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_arg0_val, 'a'); + ASSERT_EQ(voidfunc2_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_arg0_val, 'c'); + ASSERT_EQ(voidfunc2_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_arg0_val, 0); + ASSERT_EQ(voidfunc2_arg1_val, 0); +} + +// 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_return_val = 99l; + ASSERT_EQ(99l, longfunc0()); +} + +TEST_F(FFFTestSuite, value_func_will_return_zero_after_reset) +{ + longfunc0_return_val = 99l; + RESET_FAKE(longfunc0); + ASSERT_EQ(0l, longfunc0()); +} + + +TEST_F(FFFTestSuite, register_call_macro_registers_one_call) +{ + REGISTER_CALL(longfunc0); + ASSERT_EQ(call_history[0], (void *)longfunc0); +} + +TEST_F(FFFTestSuite, register_call_macro_registers_two_calls) +{ + REGISTER_CALL(longfunc0); + REGISTER_CALL(voidfunc2); + + ASSERT_EQ(call_history[0], (void *)longfunc0); + ASSERT_EQ(call_history[1], (void *)voidfunc2); +} + +TEST_F(FFFTestSuite, reset_call_history_resets_call_history) +{ + REGISTER_CALL(longfunc0); + RESET_HISTORY(); + REGISTER_CALL(voidfunc2); + + ASSERT_EQ(1u, call_history_idx); + ASSERT_EQ(call_history[0], (void *)voidfunc2); +} + +TEST_F(FFFTestSuite, call_history_will_not_write_past_array_bounds) +{ + for(unsigned int i = 0; i