1
0
mirror of https://github.com/meekrosoft/fff synced 2026-01-23 08:25:59 +01:00

Added tests from C context

This commit is contained in:
Mike Long
2011-01-15 21:35:17 +01:00
parent d43e9c9243
commit 2043a11291
3 changed files with 206 additions and 10 deletions

View File

@@ -1,18 +1,18 @@
BUILD_DIR = ../build
CPP_SRCS += \
fff_test.cpp
CPP_OBJS += \
$(BUILD_DIR)/fff_test_cpp.o
OBJS += \
$(BUILD_DIR)/fff_test.o
C_OBJS += \
$(BUILD_DIR)/fff_test_c.o
LIBS := -lgtest_main -lgtest -lpthread
PROGNAME = $(BUILD_DIR)/fff_test
CPPNAME = $(BUILD_DIR)/fff_test_cpp
CNAME = $(BUILD_DIR)/fff_test_c
# All Target
all: $(BUILD_DIR)/fff_test
all: $(CPPNAME) $(CNAME)
# Each subdirectory must supply rules for building sources it contributes
@@ -23,17 +23,31 @@ $(BUILD_DIR)/%.o: %.cpp
@echo 'Finished building: $<'
@echo ' '
$(BUILD_DIR)/%.o: %.c
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
gcc -I/home/mlong/tools/gtest/gtest-1.5.0/include -O0 -g3 -Wall -std=c99 -c -o"$@" "$<"
@echo 'Finished building: $<'
@echo ' '
# Tool invocations
$(PROGNAME): $(OBJS) $(USER_OBJS)
$(CPPNAME): $(CPP_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C++ Linker'
g++ -L/home/mlong/tools/gtest -o"$(PROGNAME)" $(OBJS) $(LIBS)
g++ -L/home/mlong/tools/gtest -o"$(CPPNAME)" $(CPP_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
$(CNAME): $(C_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C++ Linker'
g++ -L/home/mlong/tools/gtest -o"$(CNAME)" $(C_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS) $(PROGNAME)
-$(RM) $(CPP_OBJS) $(C_OBJS) $(CPPNAME) $(CNAME)
-@echo ' '

182
test/fff_test_c.c Normal file
View File

@@ -0,0 +1,182 @@
#include "../fff.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) printf(" Running %s.%s: \n", #SUITE, #TESTNAME); setup(); TESTNAME(); printf(" SUCCESS\n");
#define ASSERT_EQ(A, B) assert((A) == (B))
FAKE_VOID_FUNC1(voidfunc1, int);
FAKE_VOID_FUNC2(voidfunc2, char, char);
FAKE_VALUE_FUNC0(long, longfunc0);
void setup()
{
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(longfunc0);
RESET_HISTORY();
}
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<MAX_CALL_HISTORY+1; i++){
REGISTER_CALL(longfunc0);
}
ASSERT_EQ(MAX_CALL_HISTORY, call_history_idx);
}
TEST_F(FFFTestSuite, calling_fake_registers_one_call)
{
longfunc0();
ASSERT_EQ(call_history_idx, 1u);
ASSERT_EQ(call_history[0], (void *)longfunc0);
}
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, 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);
printf("\n-------------\n");
printf("Complete\n");
printf("-------------\n\n");
return 0;
}