boost::test

#include <boost/test/minimal.hpp>

bool
is_leap_year(int year){
    return year%4 == 0 && year%100 != 0 || year%400 == 0;
}

template<typename T, typename U>
struct is_same{
    static const bool value = false;
};
template<typename T>
struct is_same<T, T>{
    static const bool value = true;
};

int
test_main(int argc, char* argv[]){
    
    BOOST_CHECK( is_leap_year(2004) == true );
    BOOST_CHECK( is_leap_year(2010) == false );
    
    BOOST_CHECK(( is_same<int, int>::value == true ));
    BOOST_CHECK(( is_same<int, char>::value == false ));

    return 0;
}


[出力]

 **** no errors detected


[エラー時の出力]

main.cpp(24): test ( is_same::value == true )
 failed in function: 'int __cdecl test_main(int,char *[])'

 **** 1 error detected


通常の main 関数ではなくて、test_main を定義する事で、テストを行ってくれます。
unit_test.hpp とは違い、ライブラリをリンクさせる必要が無いので、簡単なテストを行いたい場合はこちらの方がいいのかな?
(やれることは unit_test.hpp の方が多いみたいですが。)
ユニットテストを行うこと自体あまり習慣が無いので、これから使っていかないとなー。


[参照]
http://www.kmonos.net/alang/boost/classes/test.html
http://d.hatena.ne.jp/faith_and_brave/20101020/1287559991