Cmockery macro demo hacking
/*********************************************************************
* 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的更多相关文章
- Qt QML referenceexamples attached Demo hacking
/********************************************************************************************* * Qt ...
- linux watchdog demo hacking
/********************************************************************** * linux watchdog demo hackin ...
- linux SPI bus demo hacking
/********************************************************************** * linux SPI bus demo hacking ...
- Linux SocketCan client server demo hacking
/*********************************************************************** * Linux SocketCan client se ...
- am335x Qt SocketCAN Demo hacking
/*********************************************************************************** * am335x Qt Soc ...
- Linux watchdog
使用 watchdog 构建高可用性的 Linux 系统及应用https://www.ibm.com/developerworks/cn/linux/l-cn-watchdog/index.html ...
- I.MX6 PWM buzzer driver hacking with Demo test
/***************************************************************************** * I.MX6 PWM buzzer dr ...
- Android Mokoid Open Source Project hacking
/***************************************************************************** * Android Mokoid Open ...
- 黑客讲述渗透Hacking Team全过程(详细解说)
近期,黑客Phineas Fisher在pastebin.com上讲述了入侵Hacking Team的过程,以下为其讲述的原文情况,文中附带有相关文档.工具及网站的链接,请在安全环境下进行打开,并合理 ...
随机推荐
- KMP高质量代码实现详解
KMP算法 对于KMP算法我分为两个部分说明,第一部分是算法部分,介绍KMP算法的算法思想:第二部分是实现部分,介绍一种厉害的实现代码以及代码注释.当然了由于本文主要介绍怎么实现故而先分析实现,对KM ...
- POJ 2591 1338 2545 2247(数列递归衍生问题,思路挺妙)
四道题的难度: 2591<1338<2545<2247 POJ 2591 Set Definition: 这是从discuss里看来的,写的挺好,直接copy,根据我的代码稍有改动( ...
- [STL]heap和priority_queue
一.heap 在STL中,priority_queue(优先权队列)的底层机制是最大堆,因此有必要先来了解一下heap.heap采用完全二叉树的结构,当然不是真正的binary tree,因为对于完全 ...
- .Net知识点总结(一)
1.文件上传:Jquery.uploadify 它依赖于flash 舍去起上传 功能 改用SWFupload 他是第三方的插件 2.验证码激活的时候,邮箱开始是写死的,但是为了以后更改邮箱 ...
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)
题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...
- Linux网络编程5——使用UDP协议实现群聊
引言 本文实现的功能类似于我之前所写的一篇博文(Linux之select系统调用_2),区别在于进程之间的通信方式有所不同.之前的文章中,我所使用的是管道,而本文我将会使用socket接口. 需求 客 ...
- MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-5]
测试项目 目录结构
- asp.net中Literal与label的区别
Literal 控件表示用于向页面添加内容的几个选项之一.对于静态内容,无需使用容器,可以将标记作为 HTML 直接添加到页面中.但是,如果要动态添加内容,则必须将内容添加到容器中.典型的容器有 La ...
- Sold out
When will the writer see the play? 'The play may begin at any moment,'I said. 'It may have begun alr ...
- 【Apache运维基础(4)】Apache的Rewrite攻略(1)
简述 Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式.如果要想用到rewrite模块 ...