Skip to content

Commit

Permalink
Merge pull request #12 from mihero/feature/unittest
Browse files Browse the repository at this point in the history
Adding possibility to create and run unittest
  • Loading branch information
piodabro authored Dec 29, 2017
2 parents 52bb051 + b92b400 commit 84dd456
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 0 deletions.
2 changes: 2 additions & 0 deletions PRO58/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/Debug/
/Release/
/obj/
user_env.mk
16 changes: 16 additions & 0 deletions PRO58/Inc/filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef FILTER_H
#define FILTER_H

#include <stdint.h>

typedef struct expFilter_s {
int64_t state;
int gain;
int precision;
}expFilter_t;

int32_t averageFilter(int32_t *val, int size);
void expFilterInit(expFilter_t *filter, int gain, int precision);
void expFilterUpdate(expFilter_t *filter, int64_t val);

#endif
24 changes: 24 additions & 0 deletions PRO58/Src/filter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include "filter.h"
#include <assert.h>

int32_t averageFilter(int32_t *val, int size){
int64_t res = 0;
for(int i = 0; i<size; i++){
res += val[i];
}
return res/size;
}

void expFilterInit(expFilter_t *filter, int gain, int precision){
assert(gain<= precision);
filter->gain=gain;
filter->state=0;
filter->precision=precision;
}
void expFilterUpdate(expFilter_t *filter, int64_t val){
filter->state=((filter->precision - filter->gain)*filter->state + filter->gain*val)/filter->precision;
}



205 changes: 205 additions & 0 deletions PRO58/Test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# A sample Makefile for building Google Test and using it in user
# tests. Please tweak it to suit your environment and project. You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
# make [all] - makes everything.
# make TARGET - makes the given target.
# make clean - removes all files generated by make.

# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.

# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
include ../user_env.mk

USER_DIR = ../Src
USER_INCLUDE_DIR = ../Inc
TEST_DIR = unit
OBJECT_DIR = ../obj/test

include system-id.mk

# specify which files that are included in the test in addition to the unittest file.
# variables available:
# <test_name>_SRC
# <test_name>_DEFINES
filter_test_SRC := $(USER_DIR)/filter.c

# Flags passed to the preprocessor.
# Set Google Test's header directory as a system directory, such that
# the compiler doesn't generate warnings in Google Test headers.

COMMON_FLAGS = \
-g \
-Wall \
-Wextra \
-Werror \
-ggdb3 \
-O0 \
-DUNIT_TEST \
-isystem $(GTEST_DIR)/include \
-MMD -MP

ifeq ($(shell $(CC) -v 2>&1 | grep -q "clang version" && echo "clang"),clang)
COMMON_FLAGS += -fblocks
LDFLAGS += -lBlocksRuntime
endif

ifndef MACOSX
COMMON_FLAGS += -pthread
endif

# Flags passed to the C compiler.
C_FLAGS = $(COMMON_FLAGS) \
-std=gnu99

# Flags passed to the C++ compiler.
CXX_FLAGS = $(COMMON_FLAGS) \
-std=gnu++11

# Compiler flags for coverage instrumentation
COVERAGE_FLAGS := --coverage

C_FLAGS += $(COVERAGE_FLAGS)
CXX_FLAGS += $(COVERAGE_FLAGS)


# Set up the parameter group linker flags according to OS
ifdef MACOSX
LDFLAGS += -Wl,-map,$(OBJECT_DIR)/$@.map
else
LDFLAGS += -Wl,-T,$(TEST_DIR)/parameter_group.ld -Wl,-Map,$(OBJECT_DIR)/$@.map
#LDFLAGS += -Wl,-map,$(OBJECT_DIR)/[email protected]
endif

# All tests produced by this Makefile. Remember to add new tests you
# created to the list.
# Gather up all of the tests.
TEST_SRC = $(sort $(wildcard $(TEST_DIR)/*.cc))
TESTS = $(TEST_SRC:$(TEST_DIR)/%.cc=%)
$(info $(TEST_SRC))
$(info $(TESTS))

# All Google Test headers. Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h

## V : Set verbosity level based on the V= parameter
## V=0 Low
## V=1 High
include ./verbosity.mk
# House-keeping build targets.

all : $(TESTS)

clean :
rm -rf $(OBJECT_DIR)/*

## test : Build and run the Unit Tests (default goal)
test: $(TESTS:%=test_%)

## junittest : Build and run the Unit Tests, producing Junit XML result files."
junittest: EXEC_OPTS = "--gtest_output=xml:$<_results.xml"
junittest: $(TESTS:%=test_%)

# Builds gtest.a and gtest_main.a.

# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized. This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
$(OBJECT_DIR)/gtest-all.o : $(GTEST_SRCS_)
@echo "compiling $@" "$(STDOUT)"
@mkdir -p $(dir $@)
$(CXX) $(CPP_FLAGS) -I$(GTEST_DIR) $(CXX_FLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc -o $@

$(OBJECT_DIR)/gtest_main.o : $(GTEST_SRCS_)
@echo "compiling $@" "$(STDOUT)"
@mkdir -p $(dir $@)
$(CXX) $(CPP_FLAGS) -I$(GTEST_DIR) $(CXX_FLAGS) -c \
$(GTEST_DIR)/src/gtest_main.cc -o $@

$(OBJECT_DIR)/gtest.a : $(OBJECT_DIR)/gtest-all.o
@echo "linking $@" "$(STDOUT)"
$(AR) $(ARFLAGS) $@ $^

$(OBJECT_DIR)/gtest_main.a : $(OBJECT_DIR)/gtest-all.o $(OBJECT_DIR)/gtest_main.o
@echo "linking $@" "$(STDOUT)"
$(AR) $(ARFLAGS) $@ $^

-include $(OBJECT_DIR)/gtest-all.d \
$(OBJECT_DIR)/gtest_main.d


# includes in test dir must override includes in user dir
TEST_INCLUDE_DIRS := $(TEST_DIR) \
$(USER_INCLUDE_DIR)

TEST_CFLAGS = $(addprefix -I,$(TEST_INCLUDE_DIRS))


# canned recipe for all test builds
# param $1 = testname
define test-specific-stuff

$$1_OBJS = $$(patsubst $$(TEST_DIR)%,$$(OBJECT_DIR)/$1%, $$(patsubst $$(USER_DIR)%,$$(OBJECT_DIR)/$1%,$$($1_SRC:=.o)))

$$(info $1 -v-v-------)
$$(info $1_SRC: $($1_SRC))
$$(info $1_OBJS: $$($$1_OBJS))
$$(info $1 -^-^-------)


#include generated dependencies
-include $$($$1_OBJS:.o=.d)
-include $(OBJECT_DIR)/$1/$1.d


$(OBJECT_DIR)/$1/%.c.o: $(USER_DIR)/%.c
@echo "compiling $$<" "$(STDOUT)"
$(V1) mkdir -p $$(dir $$@)
$(V1) $(CC) $(C_FLAGS) $(TEST_CFLAGS) \
$(foreach def,$($1_DEFINES),-D $(def)) \
-c $$< -o $$@

$(OBJECT_DIR)/$1/%.c.o: $(TEST_DIR)/%.c
@echo "compiling test c file: $$<" "$(STDOUT)"
$(V1) mkdir -p $$(dir $$@)
$(V1) $(CC) $(C_FLAGS) $(TEST_CFLAGS) \
$(foreach def,$($1_DEFINES),-D $(def)) \
-c $$< -o $$@

$(OBJECT_DIR)/$1/$1.o: $(TEST_DIR)/$1.cc
@echo "compiling $$<" "$(STDOUT)"
$(V1) mkdir -p $$(dir $$@)
$(V1) $(CXX) $(CXX_FLAGS) $(TEST_CFLAGS) \
$(foreach def,$($1_DEFINES),-D $(def)) \
-c $$< -o $$@


$(OBJECT_DIR)/$1/$1 : $$($$1_OBJS) \
$(OBJECT_DIR)/$1/$1.o \
$(OBJECT_DIR)/gtest_main.a

@echo "linking $$@" "$(STDOUT)"
$(V1) mkdir -p $(dir $$@)
$(V1) $(CXX) $(CXX_FLAGS) $(LDFLAGS) $$^ -o $$@

test_$1: $(OBJECT_DIR)/$1/$1
$(V1) $$< $$(EXEC_OPTS) "$(STDOUT)" && echo "running $$@: PASS"

endef

#apply the canned recipe above to all tests
$(eval $(foreach test,$(TESTS),$(call test-specific-stuff,$(test))))

50 changes: 50 additions & 0 deletions PRO58/Test/system-id.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# shared.mk
#
# environment variables common to all operating systems supported by the make system

# Make sure we know a few things about the architecture
UNAME := $(shell uname)
ARCH := $(shell uname -m)
ifneq (,$(filter $(ARCH), x86_64 amd64))
X86-64 := 1
X86_64 := 1
AMD64 := 1
ARCHFAMILY := x86_64
else
ARCHFAMILY := $(ARCH)
endif

# configure some variables dependent upon what type of system this is

# Linux
ifeq ($(UNAME), Linux)
OSFAMILY := linux
LINUX := 1
endif

# Mac OSX
ifeq ($(UNAME), Darwin)
OSFAMILY := macosx
MACOSX := 1
endif

# Windows using MinGW shell
ifeq (MINGW, $(findstring MINGW,$(UNAME)))
OSFAMILY := windows
MINGW := 1
WINDOWS := 1
endif

# Windows using Cygwin shell
ifeq (CYGWIN ,$(findstring CYGWIN,$(UNAME)))
OSFAMILY := windows
WINDOWS := 1
CYGWIN := 1
endif

# report an error if we couldn't work out what OS this is running on
ifndef OSFAMILY
$(info uname reports $(UNAME))
$(info uname -m reports $(ARCH))
$(error failed to detect operating system)
endif
10 changes: 10 additions & 0 deletions PRO58/Test/unit/dummy_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "gtest/gtest.h"
#include "filter.h"

TEST(DummyTest, Dummy1Test)
{

}
TEST(DummyTest, Dummy2Test)
{
}
49 changes: 49 additions & 0 deletions PRO58/Test/unit/filter_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "gtest/gtest.h"
extern "C" {
#include "filter.h"
}
#include <stdint.h>

TEST(FilterTest, AverageTest)
{
int vals[5]={1,1,1,1,1};
EXPECT_FLOAT_EQ(1, averageFilter(vals,5));
}
TEST(FilterTest, AverageZeroTest)
{
int vals[5]={0};
EXPECT_FLOAT_EQ(0, averageFilter(vals,5));
}
TEST(FilterTest, expInitTest)
{
expFilter_t fil;
expFilterInit(&fil, 10, 10);
EXPECT_EQ(10,fil.gain);
EXPECT_EQ(0,fil.state);
}
TEST(FilterTest, expInitFailTest)
{
expFilter_t fil;
EXPECT_DEATH( expFilterInit(&fil, 10, 1),".*");

}
TEST(FilterTest, expUpdate)
{
expFilter_t fil;
expFilterInit(&fil, 10, 10);
expFilterUpdate(&fil, 10);
EXPECT_EQ(10,fil.state);
}
TEST(FilterTest, expUpdateG0_5)
{
expFilter_t fil;
expFilterInit(&fil, 5, 10);
expFilterUpdate(&fil, 10);
EXPECT_EQ(5,fil.state);
expFilterUpdate(&fil, 10);
EXPECT_EQ(7,fil.state);
expFilterUpdate(&fil, 10);
EXPECT_EQ(8,fil.state);
expFilterUpdate(&fil, 10);
EXPECT_EQ(9,fil.state);
}
19 changes: 19 additions & 0 deletions PRO58/Test/verbosity.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## V : Set verbosity level based on the V= parameter
## V=0 Low
## V=1 High
export AT := @

ifndef V
export V0 :=
export V1 := $(AT)
export STDOUT :=
else ifeq ($(V), 0)
export V0 := $(AT)
export V1 := $(AT)
export STDOUT:= "> /dev/null"
export MAKE := $(MAKE) --no-print-directory
else ifeq ($(V), 1)
export V0 :=
export V1 :=
export STDOUT :=
endif
1 change: 1 addition & 0 deletions PRO58/user_env.mk.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GTEST_DIR = dir to googletest

0 comments on commit 84dd456

Please sign in to comment.