<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>竹枝 &#187; C++</title>
	<atom:link href="http://blog.anyshpm.com/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.anyshpm.com</link>
	<description>S &#38; H / M</description>
	<lastBuildDate>Wed, 21 Dec 2011 14:14:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<atom:link rel='hub' href='http://blog.anyshpm.com/?pushpress=hub'/>
		<item>
		<title>在程序中获取编译时SVN的版本号</title>
		<link>http://blog.anyshpm.com/2010/10/05/get-subversion-revision-in-code.html</link>
		<comments>http://blog.anyshpm.com/2010/10/05/get-subversion-revision-in-code.html#comments</comments>
		<pubDate>Tue, 05 Oct 2010 09:17:47 +0000</pubDate>
		<dc:creator>anyshpm</dc:creator>
				<category><![CDATA[旅程]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[代码]]></category>
		<category><![CDATA[程序]]></category>

		<guid isPermaLink="false">http://blog.anyshpm.com/?p=832</guid>
		<description><![CDATA[程序中一般会加入程序的版本信息，用“-v”来查看。 有时候还会加入编译时间，这个比较好实现，使用__TIME__和__DATE__宏就可以了，程序的大版本号一般都是手动写的。 出于某种需要，我希望在程序中获取到编译时svn的版本号，查了写资料，有说用svn prop来做的，也有说用svn hook来做的，我觉得这个太复杂，而且是在每次提交都做修改，没必要。 于是乎，我想到也用宏来做。 gcc里可以用“-D”来定义宏，用它来定义一个宏，值为svn版本号就好了。 -DSVN=xxxx xxxx怎么获取呢？svn info里就有了，弄出来就行了 svn info &#124; grep Revision &#124; cut -d " " -f 2 然后 -DSVN=·svn info &#124; grep Revision &#124; cut -d " " -f 2· 把它加到Makefile.am里去，搞定。 相关文章:TDD入门 &#8211; 参照CppUnit Cookbook用TDD测试加法 (0)]]></description>
			<content:encoded><![CDATA[<p>程序中一般会加入程序的版本信息，用“-v”来查看。</p>
<p>有时候还会加入编译时间，这个比较好实现，使用__TIME__和__DATE__宏就可以了，程序的大版本号一般都是手动写的。</p>
<p>出于某种需要，我希望在程序中获取到编译时svn的版本号，查了写资料，有说用svn prop来做的，也有说用svn hook来做的，我觉得这个太复杂，而且是在每次提交都做修改，没必要。</p>
<p>于是乎，我想到也用宏来做。</p>
<p>gcc里可以用“-D”来定义宏，用它来定义一个宏，值为svn版本号就好了。</p>
<p><code>-DSVN=xxxx</code></p>
<p>xxxx怎么获取呢？svn info里就有了，弄出来就行了</p>
<p><code>svn info | grep Revision | cut -d " " -f 2</code></p>
<p>然后</p>
<p><code>-DSVN=·svn info | grep Revision | cut -d " " -f 2·</code></p>
<p>把它加到Makefile.am里去，搞定。</p>
<h3  class="related_post_title">相关文章:</h3><ul class="related_post"><li><a href="http://blog.anyshpm.com/2010/02/08/tdd-getting-start-cppunit-cookbook.html" title="TDD入门 &#8211; 参照CppUnit Cookbook用TDD测试加法">TDD入门 &#8211; 参照CppUnit Cookbook用TDD测试加法</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.anyshpm.com/2010/10/05/get-subversion-revision-in-code.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TDD入门 &#8211; 参照CppUnit Cookbook用TDD测试加法</title>
		<link>http://blog.anyshpm.com/2010/02/08/tdd-getting-start-cppunit-cookbook.html</link>
		<comments>http://blog.anyshpm.com/2010/02/08/tdd-getting-start-cppunit-cookbook.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 05:30:29 +0000</pubDate>
		<dc:creator>anyshpm</dc:creator>
				<category><![CDATA[旅程]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cppunit]]></category>
		<category><![CDATA[cppunit cookbook]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[加法]]></category>

		<guid isPermaLink="false">http://blog.anyshpm.com/?p=706</guid>
		<description><![CDATA[基于TestCase的测试类 1 2 3 4 5 6 7 8 class AddTest : public CppUnit::TestCase &#123; public: AddTest&#40;std::string name&#41; : CppUnit::TestCase&#40;name&#41; &#123;&#125; void runTest&#40;&#41; &#123; CPPUNIT_ASSERT&#40;add&#40;10, 1&#41;== 11&#41;; CPPUNIT_ASSERT&#40;add&#40;1, 1&#41;== 2&#41;; &#125; &#125;; 加法： 1 2 3 int add&#40;int a,int b&#41; &#123; return a+b; &#125; 基于TestFixture的测试类 1 2 3 4 5 6 7 8 9 10 [...]]]></description>
			<content:encoded><![CDATA[<div>基于TestCase的测试类</div>

<div class="wp_codebox"><table><tr id="p70611"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p706code11"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> AddTest <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestCase</span> <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    AddTest<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestCase</span><span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">void</span> runTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        CPPUNIT_ASSERT<span style="color: #008000;">&#40;</span>add<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">==</span> <span style="color: #0000dd;">11</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        CPPUNIT_ASSERT<span style="color: #008000;">&#40;</span>add<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">==</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></p>
<div>加法：</div>

<div class="wp_codebox"><table><tr id="p70612"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p706code12"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> add<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a,<span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> a<span style="color: #000040;">+</span>b<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></p>
<div>基于TestFixture的测试类</div>

<div class="wp_codebox"><table><tr id="p70613"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p706code13"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> AddTest <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestFixture</span> <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">int</span> a11,amax
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">void</span> setUp<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        a11<span style="color: #000080;">=</span>add<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        amax<span style="color: #000080;">=</span>add<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">40000</span>,<span style="color: #0000dd;">40000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">void</span> tearDown<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">void</span> testAdd<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        CPPUNIT_ASSERT<span style="color: #008000;">&#40;</span>a11<span style="color: #000080;">==</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        CPPUNIT_ASSERT<span style="color: #008000;">&#40;</span>amax<span style="color: #000080;">==</span><span style="color: #0000dd;">40000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></p>
<div>在测试类中添加TestSuite成员</div>

<div class="wp_codebox"><table><tr id="p70614"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p706code14"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">Test</span> <span style="color: #000040;">*</span>suite<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestSuite</span> <span style="color: #000040;">*</span>suiteOfTests <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestSuite</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">&quot;AddTest&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    suiteOfTests<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>addTest<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">new</span> CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestCaller</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>AddTest<span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;testAdd&quot;</span>,<span style="color: #000040;">&amp;</span>AddTest<span style="color: #008080;">::</span><span style="color: #007788;">testAdd</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> suiteOfTests<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></p>
<div>使用TestRunner运行测试</div>

<div class="wp_codebox"><table><tr id="p70615"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p706code15"><pre class="cpp" style="font-family:monospace;">CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TextUi</span><span style="color: #008080;">::</span><span style="color: #007788;">TestRunner</span> runner<span style="color: #008080;">;</span>
runner.<span style="color: #007788;">addTest</span><span style="color: #008000;">&#40;</span> AddTest<span style="color: #008080;">::</span><span style="color: #007788;">suite</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
runner.<span style="color: #007788;">run</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></p>
<div>使用宏来定义测试类中的TestSuite</div>

<div class="wp_codebox"><table><tr id="p70616"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p706code16"><pre class="cpp" style="font-family:monospace;">CPPUNIT_TEST_SUITE<span style="color: #008000;">&#40;</span>AddTest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
CPPUNIT_TEST<span style="color: #008000;">&#40;</span>testAdd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
CPPUNIT_TEST_SUITE_END<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></p>
<div>异常测试宏</div>

<div class="wp_codebox"><table><tr id="p70617"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p706code17"><pre class="cpp" style="font-family:monospace;">CPPUNIT_TEST_EXCEPTION</pre></td></tr></table></div>

<p></p>
<div>TestFactoryRegistry注册</div>

<div class="wp_codebox"><table><tr id="p70618"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p706code18"><pre class="cpp" style="font-family:monospace;">CPPUNIT_TEST_SUITE_REGISTRATION<span style="color: #008000;">&#40;</span>AddTest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></p>
<div>TestFactoryRegistry使用</div>

<div class="wp_codebox"><table><tr id="p70619"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p706code19"><pre class="cpp" style="font-family:monospace;">CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestFactoryRegistry</span> <span style="color: #000040;">&amp;</span>registry<span style="color: #000080;">=</span>CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestFactoryRegistry</span><span style="color: #008080;">::</span><span style="color: #007788;">getRegistry</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
runner.<span style="color: #007788;">addTest</span><span style="color: #008000;">&#40;</span>registry.<span style="color: #007788;">makeTest</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></p>
<div>Post-build check</div>

<div class="wp_codebox"><table><tr id="p70620"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p706code20"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>argv<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TextUi</span><span style="color: #008080;">::</span><span style="color: #007788;">TestRunner</span> runner<span style="color: #008080;">;</span>
    CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestFactoryRegistry</span> <span style="color: #000040;">&amp;</span>registry <span style="color: #000080;">=</span> CppUnit<span style="color: #008080;">::</span><span style="color: #007788;">TestFactoryRegistry</span><span style="color: #008080;">::</span><span style="color: #007788;">getRegistry</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    runner.<span style="color: #007788;">addTest</span><span style="color: #008000;">&#40;</span> registry.<span style="color: #007788;">makeTest</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">bool</span> wasSuccessful <span style="color: #000080;">=</span> runner.<span style="color: #007788;">run</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">&quot;&quot;</span>, <span style="color: #0000ff;">false</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000040;">!</span>wasSuccessful<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3  class="related_post_title">相关文章:</h3><ul class="related_post"><li><a href="http://blog.anyshpm.com/2010/10/05/get-subversion-revision-in-code.html" title="在程序中获取编译时SVN的版本号">在程序中获取编译时SVN的版本号</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.anyshpm.com/2010/02/08/tdd-getting-start-cppunit-cookbook.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

