/*********************************************************************
* Cmockery macro demo hacking
* 说明:
* 本文记录对Cmockery的宏使用的示例进行测试、跟踪。
*
* 2016-5-7 深圳 南山平山村 曾剑锋
********************************************************************/ 一、cat src/example/assert_macro.c
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h> static const char* status_code_strings[] = {
"Address not found",
"Connection dropped",
"Connection timed out",
}; const char* get_status_code_string(const unsigned int status_code) {
return status_code_strings[status_code];
}; unsigned int string_to_status_code(const char* const status_code_string) {
unsigned int i;
for (i = ; i < sizeof(status_code_strings) /
sizeof(status_code_strings[]); i++) {
if (strcmp(status_code_strings[i], status_code_string) == ) {
return i;
}
}
return ~0U;
} 二、cat src/example/assert_macro_test.c
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmockery.h> extern const char* get_status_code_string(const unsigned int status_code);
extern unsigned int string_to_status_code(
const char* const status_code_string); /* This test will fail since the string returned by get_status_code_string(0)
* doesn't match "Connection timed out". */
void get_status_code_string_test(void **state) {
assert_string_equal(get_status_code_string(), "Address not found"); --+
assert_string_equal(get_status_code_string(), "Connection timed out"); |
} |
|
// This test will fail since the status code of "Connection timed out" isn't 1 |
void string_to_status_code_test(void **state) { |
assert_int_equal(string_to_status_code("Address not found"), ); --*-+
assert_int_equal(string_to_status_code("Connection timed out"), ); | |
} | |
| |
int main(int argc, char *argv[]) { | |
const UnitTest tests[] = { | |
unit_test(get_status_code_string_test), | |
unit_test(string_to_status_code_test), | |
}; | |
return run_tests(tests); | |
} | |
| |
三、macro hacking | |
| |
// Assert that the two given strings are equal, otherwise fail. | |
#define assert_string_equal(a, b) \ <-----+ |
_assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \ ------+ |
__LINE__) | |
| |
void _assert_string_equal(const char * const a, const char * const b, <-----+ |
const char * const file, const int line) { |
if (!string_equal_display_error(a, b)) { ------+ |
_fail(file, line); | |
} | |
} | |
| |
/* Determine whether the specified strings are equal. If the strings are equal | |
* 1 is returned. If they're not equal an error is displayed and 0 is | |
* returned. */ | |
static int string_equal_display_error( <-----+ |
const char * const left, const char * const right) { ------+ |
if (strcmp(left, right) == ) { | |
return ; | |
} | |
print_error("\"%s\" != \"%s\"\n", left, right); | |
return ; | |
} | |
| |
// Assert that the two given integers are equal, otherwise fail. | |
#define assert_int_equal(a, b) \ <---------*-+
_assert_int_equal(cast_to_largest_integral_type(a), \ ------+ |
cast_to_largest_integral_type(b), \ | |
__FILE__, __LINE__) | |
| |
void _assert_int_equal( <-----+ |
const LargestIntegralType a, const LargestIntegralType b, |
const char * const file, const int line) { |
if (!values_equal_display_error(a, b)) { ----------+
_fail(file, line); ----------*-+
} | |
} | |
| |
/* Returns 1 if the specified values are equal. If the values are not equal | |
* an error is displayed and 0 is returned. */ | |
static int values_equal_display_error(const LargestIntegralType left, | |
const LargestIntegralType right) { | |
const int equal = left == right; | |
if (!equal) { | |
print_error(LargestIntegralTypePrintfFormat " != " | |
LargestIntegralTypePrintfFormat "\n", left, right); | |
} | |
return equal; | |
} | |
| |
void print_error(const char* const format, ...) { <-------+ |
va_list args; |
va_start(args, format); |
vprint_error(format, args); --------+ |
va_end(args); | |
} | |
| |
void vprint_error(const char* const format, va_list args) { <-------+ |
char buffer[]; |
vsnprintf(buffer, sizeof(buffer), format, args); |
fprintf(stderr, buffer); |
#ifdef _WIN32 |
OutputDebugString(buffer); |
#endif // _WIN32 |
} |
|
void _fail(const char * const file, const int line) { <--------+
print_error("ERROR: " SOURCE_LOCATION_FORMAT " Failure!\n", file, line);
exit_test(); --------+
} |
|
// Exit the currently executing test. |
static void exit_test(const int quit_application) { <------+
if (global_running_test) {
longjmp(global_run_test_env, );
} else if (quit_application) {
exit(-);
}
} 四、运行结果:
myzr@myzr:~/c_program/cmockery-master/src/example$ gcc assert_macro* -lcmockery
myzr@myzr:~/c_program/cmockery-master/src/example$ ./a.out
get_status_code_string_test: Starting test
"Connection dropped" != "Connection timed out"
ERROR: assert_macro_test.c: Failure!
get_status_code_string_test: Test failed.
string_to_status_code_test: Starting test
!=
ERROR: assert_macro_test.c: Failure!
string_to_status_code_test: Test failed.
out of tests failed!
get_status_code_string_test
string_to_status_code_test
myzr@myzr:~/c_program/cmockery-master/src/example$

Cmockery macro demo hacking的更多相关文章

  1. Qt QML referenceexamples attached Demo hacking

    /********************************************************************************************* * Qt ...

  2. linux watchdog demo hacking

    /********************************************************************** * linux watchdog demo hackin ...

  3. linux SPI bus demo hacking

    /********************************************************************** * linux SPI bus demo hacking ...

  4. Linux SocketCan client server demo hacking

    /*********************************************************************** * Linux SocketCan client se ...

  5. am335x Qt SocketCAN Demo hacking

    /*********************************************************************************** * am335x Qt Soc ...

  6. Linux watchdog

    使用 watchdog 构建高可用性的 Linux 系统及应用https://www.ibm.com/developerworks/cn/linux/l-cn-watchdog/index.html ...

  7. I.MX6 PWM buzzer driver hacking with Demo test

    /***************************************************************************** * I.MX6 PWM buzzer dr ...

  8. Android Mokoid Open Source Project hacking

    /***************************************************************************** * Android Mokoid Open ...

  9. 黑客讲述渗透Hacking Team全过程(详细解说)

    近期,黑客Phineas Fisher在pastebin.com上讲述了入侵Hacking Team的过程,以下为其讲述的原文情况,文中附带有相关文档.工具及网站的链接,请在安全环境下进行打开,并合理 ...

随机推荐

  1. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  2. (转)STL中set的用法

    转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记 ...

  3. hdu 3404 Switch lights 博弈论

    主要是求NIM积!!! 代码如下: #include<iostream> #include<cstdio> #include<stack> #include< ...

  4. (转)两分钟彻底让你明白Android Activity生命周期(图文)!

    转自: http://blog.csdn.net/android_tutor/article/details/5772285 大家好,今天给大家详解一下Android中Activity的生命周期,我在 ...

  5. C语言标准

    1,K&R C 1978年, Dennis Ritchie和Brian Wilson Kernighan合作出版了<The C Programming Language>的第一版. ...

  6. 一站式学习Wireshark(五):TCP窗口与拥塞处理

    https://community.emc.com/message/821593#821593 介绍 TCP通过滑动窗口机制检测丢包,并在丢包发生时调整数据传输速率.滑动窗口机制利用数据接收端的接收窗 ...

  7. MSChart 控件

    微软发布了.NET 3.5框架下的图表控件,功能很强劲,基本上能想到的图表都可以使用它绘制出来,给图形统计和报表图形显示提供了很好的解决办法,同时支持Web和WinForm两种方式,不过缺点也比较明显 ...

  8. CodeForces485B——Valuable Resources(水题)

    Valuable Resources Many computer strategy games require building cities, recruiting army, conquering ...

  9. eclipse运行mapreduce报错Permission denied

    今天用在eclipse-hadoop平台上运行map reduce(word count)出错了,错误信息为 org.apache.hadoop.security.AccessControlExcep ...

  10. java post 请求

    新公司的分词为post调用方式,以前还没用过post,这次上网查了下,比较简单,但还是写篇博客记录下,代码为网上找的,非原创. package com.chuntent.tool; import ja ...