Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to integrate googletest as unit-test. #34

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
SUBDIR= common mbbsd util
SUBDIR= common mbbsd util tests

.include <bsd.subdir.mk>

.ORDER: all-common all-mbbsd
.ORDER: all-common all-util
.ORDER: all-mbbsd all-tests
.ORDER: all-util all-tests
22 changes: 22 additions & 0 deletions docs/unittest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Unit-Test
===========

這個 unit-test 是 based on googletest. 在 ubuntu 裡使用 libgtest-dev.

Setup (在 ubuntu 16.04 裡)
-----
1. `apt-get install -y libgtest-dev valgrind`

2. 在 pttbbs 做 `make GTEST_DIR=/usr/src/gtest`

3. mkdir -p data_test

4. 執行相對應的 test (ex: tests/test_mbbsd/test_dummy)

5. 可執行 `scripts/test_all.sh` 執行所有的 tests

6. 可執行 `scripts/test_list.sh` list 所有的 tests

7. 可執行 `scripts/test.sh [test]` 執行 specific 的 test (ex: ./scripts/test.sh test_dummy)

8. 可執行 scripts/test_mem_leak_all.sh 來利用 valgrind check mem-leak
21 changes: 21 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

DIRS=( tests/test_common/test_bbs tests/test_common/test_osdep tests/test_common/test_sys tests/test_mbbsd )

if [ "${BASH_ARGC}" != "1" ]
then
echo "usage: test [test-name]"
exit 255
fi

test_name=${BASH_ARGV[0]}

for p in ${DIRS[*]}
do
if [ -x "${p}/${test_name}" ]
then
echo -e "\e[33m${p}/${test_name}:\e[m"
"${p}/${test_name}"
echo ""
fi
done
24 changes: 24 additions & 0 deletions scripts/test_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

DIRS=( tests/test_common/test_bbs tests/test_common/test_osdep tests/test_common/test_sys tests/test_mbbsd )

for p in ${DIRS[*]}
do
for j in `ls ${p}/*`
do
if [ -x ${j} ]
then
echo -e "\e[33m${j}:\e[m"
"${j}"
ret="$?"
if [ "${ret}" != "0" ]
then
echo "ERROR: ${ret}"
exit 255
fi
echo ""
fi
done
done

exit 0
17 changes: 17 additions & 0 deletions scripts/test_list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

DIRS=( tests/test_common/test_bbs tests/test_common/test_osdep tests/test_common/test_sys tests/test_mbbsd )

for p in ${DIRS[*]}
do
echo -e "\e[33m${p}:\e[m"
for j in `ls ${p}/*`
do
if [ -x ${j} ]
then
k=`echo "${j}"|sed 's/.*\///g'`
echo "${k}"
fi
done
echo ""
done
34 changes: 34 additions & 0 deletions scripts/test_mem_leak_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

DIRS=( tests/test_common/test_bbs tests/test_common/test_osdep tests/test_common/test_sys tests/test_mbbsd )

for p in ${DIRS[*]}
do
for j in `ls ${p}/*`
do
if [ -x ${j} ]
then
echo "${j}"
k=`valgrind --leak-check=yes ${j} 2>&1`
d_lost_bytes=`echo "${k}"|grep "definitely lost"|awk '{print $4}'`
d_lost_blocks=`echo "${k}"|grep "definitely lost"|awk '{print $7}'`
id_lost_bytes=`echo "${k}"|grep "indirectly lost"|awk '{print $4}'`
id_lost_blocks=`echo "${k}"|grep "indirectly lost"|awk '{print $7}'`
p_lost_bytes=`echo "${k}"|grep "possibly lost"|awk '{print $4}'`
p_lost_blocks=`echo "${k}"|grep "possibly lost"|awk '{print $7}'`
t_lost_bytes=`expr "${d_lost_bytes}" + "${id_lost_bytes}" + "${p_lost_bytes}"`
t_lost_blocks=`expr "${d_lost_blocks}" + "${id_lost_blocks}" + "${p_lost_blocks}"`

echo "lost bytes: ${t_lost_bytes} lost blocks: ${t_lost_blocks}"

if [ "${t_lost_bytes}" != "0" ]
then
echo "${k}"
echo -e "\e[41mERROR!\e[m"
exit 255
fi
fi
done
done

echo -e "\e[32mAll Pass!\e[m"
3 changes: 3 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUBDIR:= test_mbbsd test_common

.include <bsd.subdir.mk>
10 changes: 10 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Unit-Test
===========

這個 unit-test 是 based on googletest. 在 ubuntu 裡使用 libgtest-dev.

Setup (在 ubuntu 16.04 裡)
-----
1. `apt-get install -y libgtest-dev`

2. 在 pttbbs 做 `make GTEST_DIR=/usr/src/gtest`
3 changes: 3 additions & 0 deletions tests/test_common/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUBDIR:= test_bbs test_osdep test_sys

.include <bsd.subdir.mk>
81 changes: 81 additions & 0 deletions tests/test_common/test_bbs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# $Id$

##########
# use googletest as test-framework
##########

SRCROOT:= ../../../
.include "$(SRCROOT)/pttbbs.mk"

##########
# gtest
##########

CPPFLAGS+= -isystem $(GTEST_DIR)/include
CXXFLAGS+= -g -Wall -Wextra -pthread

GTEST_HEADERS= /usr/include/gtest/*.h \
/usr/include/gtest/internal/*.h

GTEST_SRCS_= $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

AR:= ar
ARFLAGS:= rv

#######################################################################
# lib
#######################################################################

LDFLAGS+= -L$(SRCROOT)/common/bbs
LDLIBS:= -lcmbbs

##########
# test
##########

TEST_PROGS= test_dummy

##########
# TARGETS
##########

.if defined(GTEST_DIR)
##########
# gtest
##########
gtest-all.o: $(GTEST_SRCS_)
$(CXX) -I$(GTEST_DIR) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o: $(GTEST_SRCS_)
$(CXX) -I$(GTEST_DIR) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest_main.cc

gtest.a: gtest-all.o
$(AR) $(ARFLAGS) $@ $>

gtest_main.a: gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $>

##########
# all
##########

.SUFFIXES: .cc .o

.cc.o: $(SRCROOT)/include/var.h
$(CXX) $(CXXFLAGS) -c $*.cc

.for fn in ${TEST_PROGS}
${fn}: ${fn}.o gtest.a
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $> $(LDLIBS)
.endfor

all: $(TEST_PROGS)

.else # GTEST_DIR

all:

.endif # GTEST_DIR

clean:
rm -f *.o gtest.a gtest_main.a $(TEST_PROGS)
27 changes: 27 additions & 0 deletions tests/test_common/test_bbs/test_dummy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "gtest/gtest.h"

TEST(dummy, dummy) {
EXPECT_EQ(0, 0);
}

/**********
* MAIN
*/
class MyEnvironment: public ::testing::Environment {
public:
void SetUp();
void TearDown();
};

void MyEnvironment::SetUp() {
}

void MyEnvironment::TearDown() {
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
::testing::AddGlobalTestEnvironment(new MyEnvironment);

return RUN_ALL_TESTS();
}
81 changes: 81 additions & 0 deletions tests/test_common/test_osdep/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# $Id$

##########
# use googletest as test-framework
##########

SRCROOT:= ../../../
.include "$(SRCROOT)/pttbbs.mk"

##########
# gtest
##########

CPPFLAGS+= -isystem $(GTEST_DIR)/include
CXXFLAGS+= -g -Wall -Wextra -pthread

GTEST_HEADERS= /usr/include/gtest/*.h \
/usr/include/gtest/internal/*.h

GTEST_SRCS_= $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

AR:= ar
ARFLAGS:= rv

#######################################################################
# lib
#######################################################################

LDFLAGS+= -L$(SRCROOT)/common/osdep
LDLIBS:= -losdep

##########
# test
##########

TEST_PROGS= test_dummy

##########
# TARGETS
##########

.if defined(GTEST_DIR)
##########
# gtest
##########
gtest-all.o: $(GTEST_SRCS_)
$(CXX) -I$(GTEST_DIR) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o: $(GTEST_SRCS_)
$(CXX) -I$(GTEST_DIR) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest_main.cc

gtest.a: gtest-all.o
$(AR) $(ARFLAGS) $@ $>

gtest_main.a: gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $>

##########
# all
##########

.SUFFIXES: .cc .o

.cc.o: $(SRCROOT)/include/var.h
$(CXX) $(CXXFLAGS) -c $*.cc

.for fn in ${TEST_PROGS}
${fn}: ${fn}.o gtest.a
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $> $(LDLIBS)
.endfor

all: $(TEST_PROGS)

.else # GTEST_DIR

all:

.endif # GTEST_DIR

clean:
rm -f *.o gtest.a gtest_main.a $(TEST_PROGS)
27 changes: 27 additions & 0 deletions tests/test_common/test_osdep/test_dummy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "gtest/gtest.h"

TEST(dummy, dummy) {
EXPECT_EQ(0, 0);
}

/**********
* MAIN
*/
class MyEnvironment: public ::testing::Environment {
public:
void SetUp();
void TearDown();
};

void MyEnvironment::SetUp() {
}

void MyEnvironment::TearDown() {
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
::testing::AddGlobalTestEnvironment(new MyEnvironment);

return RUN_ALL_TESTS();
}
Loading