<!-- generator=" CodePongo (Base on Kukkaisvoima version 15b3TA)" -->
<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/"
>
<channel><title>CodePongo: c</title><link>http://codepongo.com/blog</link><description></description><pubDate>Fri, 11 Nov 2016 17:30:06 +0200</pubDate><lastBuildDate>Fri, 11 Nov 2016 17:30:06 +0200</lastBuildDate><generator>http://codepongo.com/blog</generator><language>zh-cn</language><item><title>C语言中for循环的语法
</title><link>http://codepongo.com/blog/3S3Wqr</link><comments>http://codepongo.com/blog/3S3Wqr#comments</comments><pubDate>Fri, 11 Nov 2016 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>c</category><guid isPermaLink="false">http://codepongo.com/blog/3S3Wqr/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[

for (before-all; condition; after-each) {
}




]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/3S3Wqr/feed/</wfw:commentRss></item><item><title>C语言 单元测试框架 —— Cmockery
</title><link>http://codepongo.com/blog/1Ms1h2</link><comments>http://codepongo.com/blog/1Ms1h2#comments</comments><pubDate>Fri, 18 Mar 2016 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>c</category><guid isPermaLink="false">http://codepongo.com/blog/1Ms1h2/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[
Cmockery是google开源的一套C语言单元测试框架。

选择原因

正如编写这个框架的动机，选择这个测试框架的原因在于
兼容性好 、耦合度低 、不依赖外部库 。总得来说，就是简单！

功能

按照功能，可分为四个模块

test execution 测试运行模块

测试单元用例是以函数形式给出


void test_function(void** state) {
}


然后，将其添加到一个测试单元组当中，测试单元组为一个数组


const UnitTest tests = {
    unit_test(test_function),
};


运行测试


run_tests()


如果出现异常，则程序会中断测试。如果发生错误，输出相关信息后继续执行下一条单元测试用例。

assertion 断言


expect_assert_failure():类似于C语言中的assert

assert_{type}_equal(), assert_{type}_equal:用于检查运行结果。

expect_{type}()，用于函数输入参数和测试用例的检查


Dynamic Memory Allocation 内存泄漏检查

用于宏定义的方式重新定义C运行库的内存管理函数，通过在申请内存时，加入信息头记录在链表当中的方式检测内存泄漏。

Mock 模拟测试


重写待测试中需要进行模拟测试的函数
在重写模拟测试函数中调用mock()
调用函数前，先用will_return()指定，返回值



//1
int* need_to_mock() {
    //2
    return (int*)mock();
}
int* want_to_test_func() {
    return need_to_mock();
void test_function(void** state) {
    //3
    will_return(want_to_test_func, 0x2046);
    assert_true(want_to_test_func() == 1);
}
int main(int, char**) {
    const UnitTest u[] = {
        unit_test(test_function),
    }
    return run_tests(u);
}


State 环境状态

通过unit_test_setup_teardonw可以在单元测试运行之前改变（预装载）和恢复环境状态（测试后卸载）。

Problem 存在的问题


在内存检测部分，没有对realloc函数进行处理，导致在有realloc调用的时候程序崩溃
此问题在cmocka已被解决。
free(0),释放一个空指针程序将崩溃，在test_free中加入判空操作。


this is my fork that fixes those problems above

reference

什么是mock测试
用cmockery做mocking测试

]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/1Ms1h2/feed/</wfw:commentRss></item><item><title>sscanf uses the brackets to format string 
</title><link>http://codepongo.com/blog/1IHAt1</link><comments>http://codepongo.com/blog/1IHAt1#comments</comments><pubDate>Fri, 14 Mar 2014 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>c</category><guid isPermaLink="false">http://codepongo.com/blog/1IHAt1/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[
sscanf 函数使用中括号格式化字符串

在[]之中的字符串集合可以更好的格式化字符串
a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The set of characters in brackets is referred to as a control string.


[^characters]
匹配直到所有非此集合中的字符，直到此集合的字符出现为止。If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.
[characters]
匹配直到出现未在此集合中出现的字符input field is read up to the first character that does not appear in the control string



int
main(int argc, char * * a rgv)
{
    char* s = "key= value ";
    char key[1024] = {0};
    char value[1024] = {0};
    char* digits = "pi3.1415926";
    sscanf(s, "%[^=]=%[^\0]", key, value);
    printf("%s\n%s\n", key, value);
    sscanf(digits, "%[abcdefghijklmnopqrstuvwxyz]%[1234567890.]", key, value);
    printf("%s\n%s\n", key, value);
    return 0;
}


]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/1IHAt1/feed/</wfw:commentRss></item></channel></rss>