S & H / M
Posts tagged C++
在程序中获取编译时SVN的版本号
1程序中一般会加入程序的版本信息,用“-v”来查看。
有时候还会加入编译时间,这个比较好实现,使用__TIME__和__DATE__宏就可以了,程序的大版本号一般都是手动写的。
出于某种需要,我希望在程序中获取到编译时svn的版本号,查了写资料,有说用svn prop来做的,也有说用svn hook来做的,我觉得这个太复杂,而且是在每次提交都做修改,没必要。
于是乎,我想到也用宏来做。
gcc里可以用“-D”来定义宏,用它来定义一个宏,值为svn版本号就好了。
-DSVN=xxxx
xxxx怎么获取呢?svn info里就有了,弄出来就行了
svn info | grep Revision | cut -d " " -f 2
然后
-DSVN=·svn info | grep Revision | cut -d " " -f 2·
把它加到Makefile.am里去,搞定。
TDD入门 – 参照CppUnit Cookbook用TDD测试加法
0基于TestCase的测试类
1 2 3 4 5 6 7 8 | class AddTest : public CppUnit::TestCase { public: AddTest(std::string name) : CppUnit::TestCase(name) {} void runTest() { CPPUNIT_ASSERT(add(10, 1)== 11); CPPUNIT_ASSERT(add(1, 1)== 2); } }; |
加法:
1 2 3 | int add(int a,int b) { return a+b; } |
基于TestFixture的测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class AddTest : public CppUnit::TestFixture { private: int a11,amax public: void setUp() { a11=add(1,1); amax=add(40000,40000); } void tearDown() {} void testAdd() { CPPUNIT_ASSERT(a11==2); CPPUNIT_ASSERT(amax==40000); } }; |
在测试类中添加TestSuite成员
1 2 3 4 5 | static CppUnit::Test *suite() { CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite( "AddTest" ); suiteOfTests->addTest( new CppUnit::TestCaller<AddTest>("testAdd",&AddTest::testAdd ) ); return suiteOfTests; } |
使用TestRunner运行测试
1 2 3 | CppUnit::TextUi::TestRunner runner; runner.addTest( AddTest::suite() ); runner.run(); |
使用宏来定义测试类中的TestSuite
1 2 3 | CPPUNIT_TEST_SUITE(AddTest); CPPUNIT_TEST(testAdd); CPPUNIT_TEST_SUITE_END(); |
异常测试宏
1 | CPPUNIT_TEST_EXCEPTION |
TestFactoryRegistry注册
1 | CPPUNIT_TEST_SUITE_REGISTRATION(AddTest); |
TestFactoryRegistry使用
1 2 | CppUnit::TestFactoryRegistry ®istry=CppUnit::TestFactoryRegistry::getRegistry(); runner.addTest(registry.makeTest()); |
Post-build check
1 2 3 4 5 6 7 | int main( int argc, char **argv) { CppUnit::TextUi::TestRunner runner; CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); runner.addTest( registry.makeTest() ); bool wasSuccessful = runner.run( "", false ); return !wasSuccessful; } |