-
打桩之后如何调用原函数
a) 通过Cpptest API去调整,我们可以通过如下的API去使得我门的桩函数去走原函数。例如我们的原函数为:
int foo(int param1, int param2);
。
如果我们要实现桩函数去调用原函数就可直接这么写:[C++] CPPTEST_ON_CALL("foo").Arg("__callOrig").Assign(1); [C] CPPTEST_ON_CALL("foo")->Arg("__callOrig")->Assign()->Int(1);
b) 通过直接在桩函数中修改代码。
代码中我们可以直接添加函数的调用,可以添加头文件即可。
上面是针对整形的简单变量创建的。如果我们的返回值是结构体。如下:struct Point { int _x; int _y; };
原函数如下:
Point movePoint(Point line, int xoff, int yoff);
这种情况的话必须要对桩函数做修改。要对每一个变量进行复制,然后返回给原函数。
详细的打桩代码如下:Point CppTest_Stub_movePoint (Point point) { Point __return = Point(); int __callOrig = 0; int __point_x = __return._x; /* [1] helper variable for complex type member */ int __point_y = __return._y; /* [1] helper variable for complex type member */ CPPTEST_ACTUAL_CALL("movePoint").WithArg("point", &point). WithArg("__return", &__return)).WithArg("__callOrig", ByRef(__callOrig)). /* [2] expose variable for external modification */ WithArg("__point_x", ByRef(__point_x)). /* [b] expose variable for external modification */ WithArg("__point_y", ByRef(__point_y)). End(); __return._x = __point_x; /* [3] update return value with modified value */ __return._y = __point_y; /* [3] update return value with modified value */ if (__callOrig) { Point __return = movePoint(point); CPPTEST_ACTUAL_AFTER_CALL("movePoint"). WithArg("point", &point).WithArg("__return", &__return)). WithArg("__callOrig", ByRef(__callOrig)).End(); return __return; } return __return; }
-
调用原函数,修改原函数的返回值,如果将1变为0,那么函数的返回值就是5.否则就启用的默认返回值
[C++] CPPTEST_ON_CALL("foo").Arg("__callOrig").Assign(1).Arg("__return").Assign(5); [C] CPPTEST_ON_CALL("foo")->Arg("__callOrig")->Assign()->Int(1)->Arg("__return")->Assign()->Int(5);
-
桩函数多次调用
a) 三次调用CPPTEST_Expect_NCALLS(“foo”,3)
;
b) 超过三次调用CPPTEST_EXPECT_NCALLS_MORE_THAN("foo",3)
;
c) 少于三次调用CPPTEST_EXPECT_NCALLS_LESS_THAN("foo",3)
;
d) 3---7次调用CPPTEST_EXPECT_NCALLS_IN_RANGE("foo",3, 7)
;
Want to see Parasoft in action? Sign up for our Monthly Demos! See Demos & Events >>