投稿

10月, 2020の投稿を表示しています

Cっぽいコードでgtestとgmockを使ってみる その2

Cっぽいコードでgtestとgmockを使ってみる その2 前回 Cっぽいコードのフリー関数に対してgtest, gmockを使った試験ができそうだ、というところまで進めることができましたが、やはり既存のソースコードに手が入ってしまうことが気になっていました。 今回は、そこを改良できないか試してみます。 参考にした情報 以下の情報を参考にさせていただきました。ありがとうございます。 (自分で思いつくのは無理です・・・) ブログ C言語のテストでスタブ関数を使うためのアイデア googlemockでグローバル変数のモックオブジェクトを作れるか 方針を検討する 前回のコード 前回の試験対象関数が書かれたコードはこんな感じでした。 関数名 説明 publicMethod 試験対象の関数。内部で面倒な処理を行う privateMethod01 を呼び出している。 privateMethod01 面倒な処理をしている関数。 publicMethod と結合して試験はしたくない。 # include "capsule.h" # ifdef GTEST // add "_impl" suffix to private function # define FUNC(func_name) func_name ## _impl # else // not to change # define FUNC(func_name) func_name # endif // TEST // Prototype for private function static void FUNC ( privateMethod01 ) ( const uint8_t input , uint8_t & output ) ; # ifdef GTEST // To use mock, replace function call to pointer void ( * privateMethod01 ) ( const uint8_t input , uint8_t & output ) = FUNC ( privateMetho...

Cっぽいコードでgoogle testとgoogle mockを使ってみる

Cっぽいコードでgtestとgmockを使ってみる クラスとか使っていないコードでgtest, ついでにgmockを使うことができないか試してみたのでメモ。 参考にした情報 以下の情報を参考にさせていただきました。ありがとうございます。 ブログ sioaji2012のブログ - 組込C言語でUnitTest 5 GoogleMock 書籍 モダンC言語プログラミング 元のコード 我ながらCだかC++だか分からないコードですが・・・ capsule.h # include <iostream> # include <stdint.h> # ifndef CAPSULE_H_ # define CAPSULE_H_ // Prototype for public function void publicMethod ( const uint8_t input , uint8_t & output ) ; # endif // CAPSULE_H_ capsule.cpp # include "capsule.h" // Prototype for private function static void privateMethod01 ( const uint8_t input , uint8_t & output ) ; // Implement for public function void publicMethod ( const uint8_t input , uint8_t & output ) { std :: cout << "publicMethod called." << std :: endl ; // call private function privateMethod01 ( input , output ) ; return ; } // Implement for private function static void privateMethod01 ( con...

WSLにgoogle testを入れてみる

WSLにgoogle testを入れてみる ふと思い立って試してみたので、メモとして残しておきます。 インストール ソースコードをDL&展開 wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz tar zxvf release-1.8.0.tar.gz cd googletest ビルド cmake が必要なので、インストールします。 sudo apt install cmake mkdir build cd build cmake .. sudo make && sudo make install 成功していれば、 ls /usr/local/lib とした際に libgtest.a libgtest_main.a が表示されるはずです。 使ってみる 適当なコードを用意 サンプルとして超適当なコードを用意します。 gtest_sample.cpp # include <iostream> # include "gtest_sample.h" bool sample_function ( const uint8_t input , uint8_t & output ) { std :: cout << "called sample_function" << std :: endl ; output = input ; return true ; } int main ( ) { uint8_t input = 16 ; uint8_t output = 0 ; bool ret = sample_function ( input , output ) ; std :: cout << "output: " << static_cast < uint16_t > ( output ) << std :: endl ; std ::...