/*********************************************************************
* 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. Java异常类和自定义异常类

    自定义异常类: public class ExtendsException extends Exception { private static final long serialVersionUID ...

  2. CPLD VS FPGA

    FPGA(Field-Programmable Gate Array),即现场可编程门阵列,它是在PAL.GAL.CPLD等可编程器件的基础上进一步发展的产物.它是作为专用集成电路(ASIC)领域中的 ...

  3. mysql之游标

    游标 行,也不存在每次一行地处理所有行的简单方法(相对于成批地处理它们).有时,需要在检索出来的行中前进或后退一行或多行.这就是使用游标的原因.游标( cursor)是一个存储在MySQL服务器上的数 ...

  4. MongoDB (二) MongoDB 优点

    任何关系型数据库,具有典型的架构设计,显示表和这些表之间的关系.虽然在 MongoDB中,没有什么关系的概念. MongoDB比RDBMS的优势 架构:MongoDB是文档型数据库,其中一个集合保存不 ...

  5. 定制CentOS (Redhat AS 5.1)安装盘

    CentOS(Redhat)提供了一套完整的自动化安装机制,利用该机制,我们可以自己定制无人值守的自动安装光盘,也可以进行系统裁减,甚至可以以CentOS为基础制作自己软件系统的系统安装盘.以下全部内 ...

  6. eclipse导入maven项目后依赖jar包更新问题->update project按钮

    eclipse导入maven项目后依赖jar包更新问题 1.eclipse有专门的导入maven项目按钮,file-import-maven project,eclipse会自动查找指定路径下的pom ...

  7. IE8 浏览器自动保存文档副本,添加缓存

    若响应(response)HTTP头信息中没有关于缓存的头信息,则在IE8中第二次请求网页时,从缓存中拿取文件,而不是重新向服务器请求.而在Firefox或chrome则是重新向服务器请求. 解决方法 ...

  8. 转载网页博客:ie7bug:div容器下的img与div存在间隙

    1.代码及在浏览器上的显示 ie7: ie8+: Firefox: Chrome: 可以看出ie7上在div容器下添加img,div与img中有间隙,而在ie8+和其他浏览器上均显示正常 网页源代码如 ...

  9. iOS xcode设置

    Xcode build search paths c/c++ 头文件引用问题include <> 引用编译器的类库路径下的头文件include “” 引用工程目录的相对路径的头文件 inc ...

  10. PCA基础理解