GoogleTestのお試し実装

GoogleTestの導入[https://www.emb-se.com/?page_id=953] で静的ライブラリ(libgtest.a)を生成したので、お試しテストコードを作り実行します。

makefile

# クロスコンパイラでWarningがでるため、make実行前に環境変数 STAGING_DIRを空セットすること
# export STAGING_DIR=
#
# file structure
# ├-- Makefile
# ├-- build
# │   ├-- target file.
# │   └-- object files.
# ├-- header files.
# └-- source files.

## Directory defines
BUILDDIR = ./build
OBJDIR = ./build
SRCDIR = ./
INCDIRS = ~/pi_GoogleTest/googletest/include
LIBDIRS = -L ~/pi_GoogleTest/build/lib

## Target name define
TARGET = $(BUILDDIR)/GTestSample

## Compiler & Compiler option defines
CC = ~/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-gcc
CFLAGS = -O2 -Wall
CXX = ~/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-g++
CXXFLAGS = -O2 -Wall
LDFLAGS =

## Commands
MKDIR = mkdir -p

## Objects
SRCS := $(shell find $(SRCDIR) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(OBJDIR)/%.o)
DEPS := $(OBJS:.o=.d)
LIBS = -lpthread -lgtest

INCLUDE := $(shell find $(INCDIRS) -type d)
INCLUDE := $(addprefix -I,$(INCLUDE))

CPPFLAGS := $(INCLUDE) -MMD -MP
LDFLAGS += $(LIBDIRS) $(LIBS)

# Target build
default:
    make all

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CXX) -o $@ $^ $(LDFLAGS)

# Assembly
$(OBJDIR)/%.s.o: %.s
    $(MKDIR) $(dir $@)
    $(AS) $(ASFLAGS) -c $< -o $@

# C Compile
$(OBJDIR)/%.c.o: %.c
    $(MKDIR) $(dir $@)
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

# C++ Compile
$(OBJDIR)/%.cpp.o: %.cpp
    $(MKDIR) $(dir $@)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

.PHONY: all clean rebuild

clean:
    $(RM) -r $(BUILDDIR)

rebuild:
    make clean && make

func.h

#ifndef _FUNC_H_
#define _FUNC_H_
#include <stdint.h>
#include <stdbool.h>

extern bool function( int param );

#endif //_FUNC_H_

func.c

#include "func.h"

bool function( int param )
{
    if( param ){
        return true;
    }else{
        return false;
    }
}

GTestSample.cpp

#include "gtest/gtest.h"

extern "C" {
#include "func.h"
};

class TestCase01 : public ::testing::Test
{
protected:
    TestCase01(){
    }
    ~TestCase01(){
    }
    virtual void SetUp() {
    }
    virtual void TearDown() {
    }
};

TEST_F( TestCase01, Test0101 )
{
    EXPECT_EQ( false, function(0) );
    EXPECT_EQ( true, function(1) );
}

TEST_F( TestCase01, Test0102 )
{
    EXPECT_EQ( false, function(1) );
}

int main( int argc, char *argv[] )
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

実行結果

パラメータなしで実行した結果は以下の通りです。
※GoogleTest本体でlibstdc++.soを使用しているようです。/libに用意しましょう。私の場合は、OpenWrtのmake menuconfigでチェックを付けて /lib/libstdc++.so.6 が生成されるようにしました。

$ ./GTestSample
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from TestCase01
[ RUN      ] TestCase01.Test0101
[       OK ] TestCase01.Test0101 (0 ms)
[ RUN      ] TestCase01.Test0102
GTestSample.cpp:28: Failure
Expected equality of these values:
  false
  function(1)
    Which is: true
[  FAILED  ] TestCase01.Test0102 (1 ms)
[----------] 2 tests from TestCase01 (1 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (1 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] TestCase01.Test0102

 1 FAILED TEST

OpenWrtのmake menuconfigでチェックを付けた箇所は以下の場所です。

Global build settings
    Preferred standard C++ library (libstdc++)  --->

Base System
    <*> libstdcpp