assert

Adding message to assert

99封情书 提交于 2019-11-28 17:12:32
Hallo! I'm looking for a way to add custom messages to assert statements. I found this questions Add custom messages in assert? but the message is static there. I want to do something like this: assert((0 < x) && (x < 10), std::string("x was ") + myToString(x)); When the assertion fails I want the normal output plus for example "x was 100". You are out of luck here. The best way is to define your own assert macro. Basically, it can look like this: #ifndef NDEBUG # define ASSERT(condition, message) \ do { \ if (! (condition)) { \ std::cerr << "Assertion `" #condition "` failed in " << __FILE__

Custom C++ assert macro

寵の児 提交于 2019-11-28 16:52:28
I stumbled upon an informative article: http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ which pointed out a great number of problems that exist in my current suite of debugging macros. The full code for the final version of the macro is given near the end of the article if you follow the link. The general form as presented is like this (somebody please correct me if i am wrong in transposing it): #ifdef DEBUG #define ASSERT(cond) \ do \ { \ if (!(cond)) \ { \ ReportFailure(#cond, __FILE__, __LINE__, 0); \ HALT(); \ } \ } while(0) #else #define ASSERT(cond) \ do { (void

PHPUnit: assert two arrays are equal, but order of elements not important

柔情痞子 提交于 2019-11-28 16:46:15
What is a good way to assert that two arrays of objects are equal, when the order of the elements in the array is unimportant, or even subject to change? The cleanest way to do this would be to extend phpunit with a new assertion method. But here's an idea for a simpler way for now. Untested code, please verify: Somewhere in your app: /** * Determine if two associative arrays are similar * * Both arrays must have the same indexes with identical values * without respect to key ordering * * @param array $a * @param array $b * @return bool */ function arrays_are_similar($a, $b) { // if the

单元测试之NUnit二

╄→гoц情女王★ 提交于 2019-11-28 15:24:59
NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考 官方文档 。初次写博客,望大家指点。 导航: 单元测试之NUnit一 单元测试之NUnit二 单元测试之NUnit三 本文介绍常用的NUnit属性特性和断言。 常用属性 更多属性参考官方文档 1. Test Attribute 标记一个方法为测试方法。 /// <summary> /// 简单标记方法为测试方法 /// </summary> [Test] public void Add() { Assert.AreEqual(4, 2 + 2); } /// <summary> /// 添加说明内容,等同于DescriptionAttribute属性 /// </summary> //[Test(Description = "这是说明内容")] [Test,Description("这是说明属性")] public void Add() { Assert.AreEqual(4, 2 + 2); } // 标记异步方法 [Test] public async Task AddAsync() { /* ... */ } // 如果测试方法有返回值,要使用ExpectedResult校验 [Test(ExpectedResult = 4)] public int TestAdd() { return 2 + 2; } //

Is assert evil? [closed]

孤街醉人 提交于 2019-11-28 15:08:29
The Go language creators write : Go doesn't provide assertions. They are undeniably convenient, but our experience has been that programmers use them as a crutch to avoid thinking about proper error handling and reporting. Proper error handling means that servers continue operation after non-fatal errors instead of crashing. Proper error reporting means that errors are direct and to the point, saving the programmer from interpreting a large crash trace. Precise errors are particularly important when the programmer seeing the errors is not familiar with the code. What is your opinion about this

Why doesn't JUnit provide assertNotEquals methods?

吃可爱长大的小学妹 提交于 2019-11-28 14:57:57
Does anybody know why JUnit 4 provides assertEquals(foo,bar) but not assertNotEqual(foo,bar) methods? It provides assertNotSame (corresponding to assertSame ) and assertFalse (corresponding to assertTrue ), so it seems strange that they didn't bother including assertNotEqual . By the way, I know that JUnit-addons provides the methods I'm looking for. I'm just asking out of curiosity. I'd suggest you use the newer assertThat() style asserts, which can easily describe all kinds of negations and automatically build a description of what you expected and what you got if the assertion fails:

TestNG shows 0 Test run

本秂侑毒 提交于 2019-11-28 14:24:36
I'm trying to execute my test scripts using testNG and trying the below code, but 0 displayed against run, fail and skip in the console. Because of that I'm unable to verify the results in my script. Java: package com.demoaut.newtours.testcases; import org.testng.Assert; import org.testng.annotations.Test; //import junit.framework.Assert; public class TC002_CheckAssert { @Test public TC002_CheckAssert() { System.out.println("ajkcbh"); try { Assert.assertEquals("Pass", "Pass"); } catch(Exception e) { System.out.println("Exception:" + e.getLocalizedMessage()); } } } I am executing the above

undefined reference to `__assert_fail'

一笑奈何 提交于 2019-11-28 14:11:20
今天同事问了我一个关于DD-WRT编译的问题,在link里面一个工具的时候出现了下面的错误: [root@localhost dhcpforwarder]# mipsel-linux-uclibc-gcc -o dhcpfwd -ffunction-sections -fdata-sections -Wl,--gc-sections src/parser.o src/main.o src/cfg.o src/recvfromflags.o src/output.o /home/ipdslam_users/yzeng/dd-wrt/toolchains/4.1.0-uclibc-0.9.28/bin/../lib/gcc/mipsel-linux-uclibc/4.1.0/../../../../mipsel-linux-uclibc/bin/ld: BFD 2.16.1 assertion fail /home/openwrt/trunk/openwrt/toolchain_build_mipsel/binutils-2.16.1/bfd/elfxx-mips.c:2562 src/main.o: In function `execRelay': main.c:(.text+0x590): undefined reference to `__assert_fail' main.c:

C# - What does the Assert() method do? Is it still useful?

回眸只為那壹抹淺笑 提交于 2019-11-28 13:43:41
问题 I am debugging with breakpoints and I realize the assert call? I thought it was only for unit tests. What does it do more than breakpoint? Since I can breakpoint, why should I use Assert? 回答1: In a debug compilation, Assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true. If you compile in Release, all Debug.Assert's are automatically left out. 回答2: From Code Complete 8

Android NDK assert.h problems

喜欢而已 提交于 2019-11-28 12:20:01
First one - is what NDEBUG somehow already defined by default, so asserts don't work until you #undef it. Second one - they do they work, but i receive no logging in DDMS. If there is some android specific one assert.h? Or i just do something wrong? If you want to compile your code with asserts then you can do it in three ways: use NDK_DEBUG=1 argument in ndk-build commandline add android:debuggable="true" to < application > tag in AndroidManifest.xml add APP_OPTIM := debug to your Application.mk file - this will also disable optimizations and will compile with debug symbols ylchung Usually