根据我的理解断言是C中的一个宏,并且如果你在编译时使用它但是禁用它,那么就不会有开销(这可能不正确我不知道).对我来说问题是,我想要做的是将所有变量传递给我的函数并打印输出,但仅限于我想要启用调试.这是我到目前为止:
int exampleFunction (int a, int b)
{
#ifdef debugmode
printf("a = %i, b = %i", a, b);
#endif
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更容易(并且不那么难看)的方法来做这样的事情.xdebug for php有这个功能,我发现在调试时节省了大量的时间,所以我想为每个功能做这件事.
谢谢
这样做assertNotEmpty("0")PHPUnit中失败.它不应该失败,因为它"0"是一个长度为1的字符串.
以下测试成功.
$this->assertEquals(1, strlen("0"));
$this->assertInternalType('string', "0");
Run Code Online (Sandbox Code Playgroud)
然后,为什么它说"0"是空的?assert语句是否在内部将其转换为整数以检查空虚?
一个Bookhas_many Reviews和一个Reviewbelongs_to Book.我使用的是rails 4.0和ruby 2.0.0p247
我正在运行测试如下:
test "DELETE destroy should decrement review count by 1" do
book = Book.create({
:isbn=>"1234567890",
:title=>"Dummies Guide to Rails",
:author=>"KP",
:description=>"lorem ipsum"
})
# assert_not_nil Book.find_by_isbn('1234567890')
review = Review.create!({
:book_id => book.id,
:name => "KP",
:email => "KP@KP.com",
:description => "Good book.",
# :book => book
})
assert_difference 'Review.count', 1 , "a review should be deleted" do
puts Review.count # => 3
delete :destroy, :book_id => book.id, :id => …Run Code Online (Sandbox Code Playgroud) 牛顿拉夫森方法的失效分析表明,"对于某些函数,一些起点可能进入无限循环,阻止收敛".我想在程序中检查它是否进入无限循环或不使用assert语句.如果它进入,那么程序将终止说使用这个初始猜测无法收敛.如何在程序中检测此循环?码:
int user_power, i=0, cnt=0, flag=0;
int coef[10]={0};
float x1=0, x2=0, t=0;
float fx1=0, fdx1=0;
void main()
{
printf("\n\n\t\t\t PROGRAM FOR NEWTON RAPHSON GENERAL");
printf("\n\n\n\tENTER THE MAXIMUM POWER:");
scanf("%d",&user_power);
for(i=0;i<=user_power;i++)
{
printf("\n\t x^%d:",i);
scanf("%d",&coef[i]);
}
printf("\n");
printf("\n\tINTIAL X1---->");
scanf("%f",&x1);
printf("\n ******************************************************");
printf("\n ITERATION X1 FX1 F'X1 ");
printf("\n **********************************************************");
do
{
cnt++;
fx1=fdx1=0;
for(i=user_power;i>=1;i--)
{
fx1+=coef[i] * (pow(x1,i)) ; //calculating f(x1)
}
fx1+=coef[0];
for(i=user_power;i>=0;i--)
{
fdx1+=coef[i]* (i*pow(x1,(i-1))); //calculating f'(x1)
}
t=x2;
assert(fdx1!=0);
x2=(x1-(fx1/fdx1));
x1=x2;
printf("\n %d %.3f %.3f %.3f ",cnt,x2,fx1,fdx1); …Run Code Online (Sandbox Code Playgroud) 当一个变量有另一个值而不是定义时,我试图让脚本断言失败.我的目标:如果TestStep失败,将TestStep标记为红色.请注意,我正在使用TestStep的脚本断言 - 而不是单独的Groovy-Script TestStep.我的脚本看起来像这样:
httpResponseHeader = messageExchange.responseHeaders
contentType = httpResponseHeader["Content-Type"]
log.info("Content-Type: " + contentType)
if (contentType != "[image/jpeg]"){
log.info("ERROR! Response is not an image.")
//Script Assertion should fail.
//TestStep should be marked red.
} else {
log.info("OK! ResponseType is an image.")
}
Run Code Online (Sandbox Code Playgroud)
有什么办法,如何让脚本断言取决于属性?我试过使用getStatus()方法,但这只适用于testrunner对象.不幸的是,testRunner-object不能在关于这篇文章的脚本断言中使用:http://forum.soapui.org/viewtopic.php? f = 2&t = 2494#p9107
在我的Python包中,我有一个函数,我用它来创建我的类的验证实例,类似于
@staticmethod
def config_enigma(rotor_names, window_letters, plugs, rings):
comps = (rotor_names + '-' + plugs).split('-')[::-1]
winds = [num_A0(c) for c in 'A' + window_letters + 'A'][::-1]
rngs = [int(x) for x in ('01.' + rings + '.01').split('.')][::-1]
assert all(name in rotors for name in comps[1:-1])
assert comps[-1] in reflectors
assert len(rngs) == len(winds) == len(comps)
assert all(1 <= rng <= 26 for rng in rngs)
assert all(chr_A0(wind) in LETTERS for wind in winds)
#...
Run Code Online (Sandbox Code Playgroud)
我想在Haskell中强制执行相同的行为.但是以相同的方式执行此操作 - 使用断言 - 不起作用,因为Haskell断言通常被禁用( …
我有一个列表,其中包含最近 5 个订单日期
List<String> recentFiveOrdersDates = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
列表中的值为:
["20/07/2018", "19/07/2018", "18/07/2018", "18/07/2018", "18/07/2018"]
Run Code Online (Sandbox Code Playgroud)
我有另一个列表,其中包含所有订单日期:
List<String> AllOrderDates = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
此列表中的值为:
["20/07/2018", "19/07/2018", "18/07/2018", "18/07/2018", "18/07/2018", "17/07/2018", "17/07/2018", "16/07/2018", "16/07/2018", "12/07/2018", "12/07/2018", "17/05/2018"]
Run Code Online (Sandbox Code Playgroud)
现在我想在所有订单列表中断言最近的五个订单列表。是否可以验证它们的顺序相同?
我已经使用了hamcrestassertThat()方法,但我不确定它是否以同样的方式工作
assertThat(AllOrderDates, contains(recentFiveOrdersDates.toArray()));
Run Code Online (Sandbox Code Playgroud) 我在C++中尝试一个简单的代码,但是Debug Assertion Failed _BLOCK_TYPE_IS_VALID当我删除指针时出现错误.我不知道我错了什么.这是我的代码.
hash_map<string,string> m_hashDetails;
m_hashDetails.insert(hash_map<string,string>::value_type("test",*(new string("test123"))));
hash_map<string,string>::iterator myIterator;
myIterator = m_hashDetails.find("test");
if(myIterator == m_hashDetails.end())
{
printf("not found");
}
else
{
printf(myIterator->second.c_str());
//this is where I get Debug Assertion Failed _BLOCK_TYPE_IS_VALID
delete &(myIterator->second);
}
Run Code Online (Sandbox Code Playgroud)
当我删除我得到错误的second字段.我错了什么?我用运营商分配了这个字段?有一件事我注意到,如果我将hash_map定义更改为
并插入类似的值hash_mapDebug Assertion Failed _BLOCK_TYPE_IS_VALIDsecondnewhash_map<string,string *> m_hashDetails;
m_hashDetails.insert(hash_map<string,string>::value_type("test",new string("test123")));
Run Code Online (Sandbox Code Playgroud)
然后delete不给出错误..并且工作正常?这个错误的实际原因是什么?
以下代码我有分段错误,有人可以帮我理解原因吗?
typedef struct ClientData {
int _clientId;
char _msg[200];
} ClientData_t;
// in a function
char *id = malloc(50);
char *msg = malloc(sizeof(MESSAGE_LENGTH));
memset(id, 0, 50);
memset(msg, 0, MESSAGE_LENGTH);
strcpy(id, &(buffer[1]));
strcpy(msg, &(buffer[50]));
free(id);
printf("this message can be printed\n");
ClientData_t *newData = malloc(sizeof(ClientData_t));
// I got segmentation fault for this malloc here
Run Code Online (Sandbox Code Playgroud)
第二次,我free(id);从上面删除了调用,并保留了其余的,一旦调用了最后一个malloc,我得到了以下错误:
mainClient1: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned …Run Code Online (Sandbox Code Playgroud) 我在Java(Eclipse)中发现了一个奇怪的断言行为.简单的例子:如果我执行这个......
public static void main (String[] args) {
assert(getA() == "a") : "Invalid";
System.out.println("Assertion successful!");
}
private static String getA()
{
return "a";
}
Run Code Online (Sandbox Code Playgroud)
......它会告诉我"断言成功!" 正如它应该.但是如果我试试这个......
public static void main (String[] args) {
assert(getA() + "b" == "ab") : "Invalid";
System.out.println("Assertion successful!");
}
private static String getA()
{
return "a";
}
Run Code Online (Sandbox Code Playgroud)
...我得到一个AssertionError.为什么这个断言不会回归真实?
注意:
assertions ×10
c ×3
java ×2
assert ×1
behavior ×1
c++ ×1
debugging ×1
eclipse ×1
groovy ×1
hamcrest ×1
haskell ×1
http-headers ×1
malloc ×1
minitest ×1
new-operator ×1
php ×1
phpunit ×1
python-2.7 ×1
soapui ×1
unit-testing ×1
validation ×1