プログラミングメモ - すべてのコードパスを通すということ


『Writing Solid Code』よりちとメモ。

Writing Solid Code: Microsoft's Techniques for Developing Bug-Free C Programs (Microsoft Programming Series)
Writing Solid Code
posted with amazlet at 09.10.01
Steve Maguire
Microsoft Pr
売り上げランキング: 93397
おすすめ度の平均: 5.0
5 C言語開発のノウハウ


Either you can create test cases that force the error conditions to occur, or you can simulate failures while you're stepping through the code. Simulating failures usually takes much less time. Take a look at this code extract, for example:

pbBlock = (byte *)malloc(32);
if (pbBlock == NULL)
    handle the error condition:

Normally, when you step through this code, malloc will allocate a 32 byte block of memory and return a non-NULL pointer, which causes the code to bypass the error handling code. But instead of leaving the error code untested, step through the code a second time and use the debugger to set pbBlock to the NULL pointer right after you execute this line:

pbBlock = (byte *)malloc(32);

malloc may allocate the block, but if you set pbBlock to the NULL pointer, it will look to your code as if malloc failed, allowing you to step through the error handler. (For you detail-oriented readers: Yes, malloc's block will be lost when you change pbBlock, but this is only a test.)


Step through every code path.

『Writing Solid Code』(Steve Maguire,Microsoft Pr,1993年,p79)

xUnit が流行しても,とりあえず基本は泥臭いけどホワイトボックステストということで。実際,エラーハンドリングのブロックまでテストする人ってあまりいない気がします。あたしゃこれでもかってほどテストするので,ちと信じられないところがあるんですけど。全部のパスが通ることを確認すれば,それだけソースに対する自信も深まるから,精神衛生上いいんですけどね。

Trackback URL:
Search This Site
Recent Entries
Log Archive
Syndicate This Site
Movable Type 3.36
Valid XHTML 1.1!
Valid CSS!

© 2003-2012 AIAN