I tried using google test and CMake in C

This is a sample to build with cmake using googletest for C.

An example is the book Test-Driven Development for Embedded C / [Embedded Programming with Test-Driven Development](https: / It can be used as a skeleton for the LED Driver at /www.oreilly.co.jp/books/9784873116143/).

Directory structure

build/Directory for build
ext/
ext/googletest googletest source code
inc/header
src/Source
test/test

google test preparation

Initialize git and get google test.

% git init
% mkdir build ext inc src test
% cd ext
% git submodule add https://github.com/google/googletest.git

Create a cmake file for googletest.

CMakeLists.txt


project("led driver")
cmake_minimum_required(VERSION 2.8)
add_subdirectory(ext)

ext/CMakeLists.txt


option(BUILD_GTEST "Builds the googletest subproject" ON)
option(BUILD_GMOCK "Builds the googlemock subproject" OFF)
add_subdirectory(googletest)

Build in the build directory.

$ cd build
$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found PythonInterp: /usr/bin/python (found version "2.7.6")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sekine/github/leddriver2/build
$ make
Scanning dependencies of target gtest
[ 50%] Building CXX object ext/googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
Linking CXX static library libgtest.a
[ 50%] Built target gtest
Scanning dependencies of target gtest_main
[100%] Building CXX object ext/googletest/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
Linking CXX static library libgtest_main.a
[100%] Built target gtest_main

I will git commit the work so far.

$ cd ..
$ git add CMakeLists.txt ext/CMakeLists.txt
$ git commit -m "setup googletest."

Source code example

inc/LedDriver.h


#ifndef LEDDRIVER_H
#define LEDDRIVER_H

void LedDriver_Create(uint16_t *address);
void LedDriver_Destroy(void);
void LedDriver_TurnOn(int ledNumber);
void LedDriver_TurnOff(int ledNumber);

#endif

src/LedDriver.c


#include <stdint.h>
#include "LedDriver.h"

static uint16_t *ledsAddress;

void LedDriver_Create(uint16_t *address)
{
    ledsAddress = address;
    *ledsAddress = 0;
}

void LedDriver_Destroy(void)
{
}

void LedDriver_TurnOn(int ledNumber)
{
    *ledsAddress = 1;
}

void LedDriver_TurnOff(int ledNumber)
{
    *ledsAddress = 0;
}

test/LedDriverTest.cpp


#include <stdint.h>
#include "gtest/gtest.h"
extern "C"
{
#include "LedDriver.h"
}

uint16_t virtualleds;

class LedDriver : public ::testing::Test
{
protected:
    virtual void SetUp()
    {
        LedDriver_Create(&virtualleds);
    }
    virtual void TearDown()
    {
        LedDriver_Destroy();
    }
};

TEST_F(LedDriver, LedsOffAfterCreate)
{
    uint16_t virtualleds1 = 0xffff;
    LedDriver_Create(&virtualleds1);
    ASSERT_EQ(0, virtualleds1);
}

TEST_F(LedDriver, TurnOnLedOne)
{
    LedDriver_TurnOn(1);
    ASSERT_EQ(1, virtualleds);
}

TEST_F(LedDriver, TurnOffLedOne)
{
    LedDriver_TurnOn(1);
    LedDriver_TurnOff(1);
    ASSERT_EQ(0, virtualleds);
}

CMake settings.

CMakeLists.txt


project("led driver")

cmake_minimum_required(VERSION 2.8)

add_subdirectory(ext)
add_subdirectory(src)
add_subdirectory(test)

ADD_CUSTOM_TARGET(check test/led_driver_test_app)

src/CMakeLists.txt


add_library(LedDriver LedDriver.c)
target_include_directories(LedDriver PUBLIC ../inc)

test/CMakeLists.txt


add_executable(led_driver_test_app
    LedDriverTest.cpp)
target_link_libraries(led_driver_test_app
    gtest
    gtest_main
    LedDriver)

This is an execution example.

$ cd build
$ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sekine/github/leddriver2/build
$ make
[ 25%] Built target gtest
[ 50%] Built target gtest_main
[ 75%] Built target LedDriver
[100%] Built target led_driver_test_app
$ make check
Running main() from gtest_main.cc
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from LedDriver
[ RUN      ] LedDriver.LedsOffAfterCreate
[       OK ] LedDriver.LedsOffAfterCreate (0 ms)
[ RUN      ] LedDriver.TurnOnLedOne
[       OK ] LedDriver.TurnOnLedOne (0 ms)
[ RUN      ] LedDriver.TurnOffLedOne
[       OK ] LedDriver.TurnOffLedOne (0 ms)
[----------] 3 tests from LedDriver (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 3 tests.
Built target check
$

Recommended Posts

I tried using google test and CMake in C
I tried using docomo speech recognition API and Google Speech API in Java
I tried to illustrate the time and time in C language
I tried programming the chi-square test in Python and Java.
How to use Google Test in C
I tried using PyEZ and JSNAPy. Part 2: I tried using PyEZ
I tried using Bayesian Optimization in Python
I tried updating Google Calendar with CSV appointments using Python and Google APIs
I tried using PyEZ and JSNAPy. Part 1: Overview
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV
I tried using the Google Cloud Vision API
I tried adding a Python3 module in C
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried using Google Translate from Python and it was just too easy
I tried using TradeWave (BitCoin system trading in Python)
I tried to access Google Spread Sheets using Python
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried using Amazon Glacier
I tried using git inspector
I tried Python C extension
[Python] I tried using OpenPose
I tried using magenta / TensorFlow
I tried using AWS Chalice
I tried using Slack emojinator
I tried [scraping] fashion images and text sentences in Python.
I tried to get Web information using "Requests" and "lxml"
[Python scraping] I tried google search top10 using Beautifulsoup & selenium
I created a class in Python and tried duck typing
I tried to make a stopwatch using tkinter in python
I tried using Rotrics Dex Arm # 2
Multi-instance module test in C language