読者です 読者をやめる 読者になる 読者になる

日曜技術者のメモ

趣味でやった事のメモ書きです。

SystemC+GoogleTestを試してみた

SystemC

Twitter上でSystemCでテストフレームワークが使えるかの話をしている時に
以下ページを教えてもらったので試してみました。

Using existing unit test frameworks with SystemC - Stack Overflow

ソースコード

まずはページに載っているソースコードをファイルにします。

  • exor2.hxx
  • main.cxx
  • main_1.cxx
  • main_2.cxx
  • stim.hxx
  • mon.hxx

これだけでは足りないので以下サイトに載っているNANDのコードをファイルにする。

Modules and Processes

ファイル名は「nand2.hxx」

googletest

googletestは以下サイトにあるので最新の1.7をダウンロード
解凍後「./configure」「make」

googletest - Google C++ Testing Framework - Google Project Hosting

cmake

googletestはcmakeを使ってビルド環境を作るっぽいのでCMakeLists.txtを作成する。
cmakeを使った事がないので見よう見まねで作成したのが↓

cmake_minimum_required(VERSION 2.6)
project(sc_unit_test)

#SystemC Path
include_directories(/home/ginnyu-tei/work/google_test/_lib/systemc-2.3.0/include)
LINK_DIRECTORIES(/home/ginnyu-tei/work/google_test/_lib/systemc-2.3.0/lib-linux64)

#googletest Path
include_directories(/home/ginnyu-tei/work/google_test/_lib/gtest-1.7.0/include)
LINK_DIRECTORIES(/home/ginnyu-tei/work/google_test/_lib/gtest-1.7.0/lib/.libs)

#main build
add_executable(main main_1.cxx main_2.cxx main.cxx)
target_link_libraries(main pthread systemc gtest)

googletestのライブラリパスは「libgtest.a」があるディレクトリを指定すれば良いですが
ビルド環境によっては「gtest-1.7.0/build」に入っている時があります。

ビルド

ソースコードとCMakeLists.txtを同じフォルダに入れて以下実行

[ginnyu-tei@localhost google_test]$ mkdir build
[ginnyu-tei@localhost google_test]$ cd build/
[ginnyu-tei@localhost build]$ cmake ..
-- The C compiler identification is GNU 4.7.2
-- The CXX compiler identification is GNU 4.7.2
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ginnyu-tei/work/google_test/build
[ginnyu-tei@localhost build]$ make
Scanning dependencies of target main
[ 33%] Building CXX object CMakeFiles/main.dir/main_1.cxx.o
[ 66%] Building CXX object CMakeFiles/main.dir/main_2.cxx.o
[100%] Building CXX object CMakeFiles/main.dir/main.cxx.o
/home/ginnyu-tei/work/google_test/main.cxx: 関数 ‘int sc_main(int, char**)’ 内:
/home/ginnyu-tei/work/google_test/main.cxx:25:18: 警告: warn_unused_result 属性付きで宣言されている ‘int RUN_ALL_TESTS()’ の戻り値を無視しています [-Wunused-result]
Linking CXX executable main
[100%] Built target main

Warningが出ているがビルドはできた。

実行ログ

[ginnyu-tei@localhost build]$ ./main 

             SystemC 2.3.0-ASI --- Jan  9 2014 22:05:39
        Copyright (c) 1996-2012 by all Contributors,
        ALL RIGHTS RESERVED

Running main() from gtest_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from systemc_test
[ RUN      ] systemc_test.test1
      Time A B F
       0 s 0 0 0
       0 s 0 0 1
     10 ns 0 1 1
     20 ns 1 0 1
     30 ns 1 1 0

Info: /OSCI/SystemC: Simulation stopped by user.
[       OK ] systemc_test.test1 (1 ms)
[ RUN      ] systemc_test.test2
unknown file: Failure
C++ exception with description "Error: (E113) insert primitive channel failed: elaboration done
In file: sc_prim_channel.cpp:216" thrown in the test body.
[  FAILED  ] systemc_test.test2 (1 ms)
[----------] 2 tests from systemc_test (2 ms total)

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

 1 FAILED TEST
[       OK ] systemc_test.test2 (2 ms)
[----------] 2 tests from systemc_test (3 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (3 ms total)
[  PASSED  ] 2 tests.
[       OK ] systemc_test.test1 (4 ms)
[ RUN      ] systemc_test.test2
      Time A B F
       0 s 0 0 0
       0 s 0 0 1
     10 ns 0 1 1
     20 ns 1 0 1
     30 ns 1 1 0

Info: /OSCI/SystemC: Simulation stopped by user.
[       OK ] systemc_test.test2 (1 ms)
[----------] 2 tests from systemc_test (5 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (5 ms total)
[  PASSED  ] 2 tests.
[       OK ] systemc_test.test2 (2 ms)
[----------] 2 tests from systemc_test (6 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (6 ms total)
[  PASSED  ] 2 tests.
[ginnyu-tei@localhost build]$ 

なんかfailしてる・・・
SystemC2.2に変えてみた。

SystemC2.2で実行する際はmain_1.cxxとmain_2.cxxに以下の
includeを追加しないとエラーになった。 (別の環境では追加しなくてもビルドできたので環境依存かも)

#include <unistd.h>

SC2.3と同じ様にビルドして実行

[ginnyu-tei@localhost build]$ ./main 

             SystemC 2.2.0 --- Jan  9 2014 23:02:22
        Copyright (c) 1996-2006 by all Contributors
                    ALL RIGHTS RESERVED
Running main() from gtest_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from systemc_test
[ RUN      ] systemc_test.test1
      Time A B F
       0 s 0 0 0
       0 s 0 0 1
     10 ns 0 1 1
     20 ns 1 0 1
     30 ns 1 1 0
SystemC: simulation stopped by user.
[       OK ] systemc_test.test1 (1 ms)
[ RUN      ] systemc_test.test2
unknown file: Failure
C++ exception with description "Error: (E546) sc_start called after sc_stop has been called
In file: sc_simcontext.cpp:1315" thrown in the test body.
[  FAILED  ] systemc_test.test2 (1 ms)
[----------] 2 tests from systemc_test (2 ms total)

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

 1 FAILED TEST
[       OK ] systemc_test.test2 (2 ms)
[----------] 2 tests from systemc_test (3 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (3 ms total)
[  PASSED  ] 2 tests.
[       OK ] systemc_test.test1 (3 ms)
[ RUN      ] systemc_test.test2
      Time A B F
       0 s 0 0 0
       0 s 0 0 1
     10 ns 0 1 1
     20 ns 1 0 1
     30 ns 1 1 0
SystemC: simulation stopped by user.
[       OK ] systemc_test.test2 (1 ms)
[----------] 2 tests from systemc_test (4 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (4 ms total)
[  PASSED  ] 2 tests.
[       OK ] systemc_test.test2 (1 ms)
[----------] 2 tests from systemc_test (5 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (5 ms total)
[  PASSED  ] 2 tests.

うーん・・・分からん・・・