assert

What does “#define assert(exp) ((void) 0)” do?

泄露秘密 提交于 2019-12-10 02:21:35
问题 I came across this preprocessor definition while reading the source code in Windows Research Kernel (WRK) 1.2: #define assert(exp) ((void) 0) What does this code do? Why is it defined? 回答1: It defines the expression assert( anything ) to do nothing. Presumably, the environment being used does not support the ANSI C assert statement, or the programmer was unaware of the fact that it could be disabled by defining NDEBUG. 回答2: To expand on what bdonlan says, the reason the macro does not expand

JavaScript anti-silent techniques to indicate failure

末鹿安然 提交于 2019-12-10 02:09:40
问题 What would be a good way to report errors in JavaScript instead of relying on nulls, and undefineds when errors do occur and a function is unable to proceed forward. I can think of three approaches: do nothing throw an exception assert Here's a simple example scenario - a function that credits a user account with the amount passed in. The function credit is part of an Account object. Here's the naive solution. function credit(amount) { this.balance += amount; } A major problem with this

Why does Assert.AreEqual(1.0, double.NaN, 1.0) pass?

被刻印的时光 ゝ 提交于 2019-12-10 01:10:02
问题 Short question, why does Assert.AreEqual(1.0, double.NaN, 1.0) pass? Whereas Assert.AreEqual(1.0, double.NaN) fails. Is it a bug in MSTest (Microsoft.VisualStudio.QualityTools.UnitTestFramework) or am I missing something here? Best regards, Egil. Update: Should probably add, that the reason behind my question is, that I have a bunch of unit tests that unfortunately passed due to the result of some linear algebraic matrix operation being NaN or (+/-)Infinity. The unit tests are fine, but since

Node.js实战11:fs模块初探。

左心房为你撑大大i 提交于 2019-12-09 15:06:56
fs模块封装了对文件操作的各种方法,比如同步和异步读写、批量操作、流、监听。 我们还是通常例程学习, 获取目录下的文件清单: var fs =require("fs"); fs.readdir("./",function(err,files){ console.log(files); }) 输出如下: 再来一例: 向文件同步写入内容,再同步读出: var fs = require("fs"); var assert = require("assert"); //同步写入 var fd = fs.openSync("./test.txt","w+"); var write_buf = new Buffer("something to write"); fs.writeSync(fd,write_buf,0,write_buf.length,0); //同步读取 var read_buf = new Buffer(write_buf.length); fs.readSync(fd,read_buf,0,write_buf.length,0); console.log(read_buf.toString()); //用断言asset比较写入和读取的内容是否一至 assert.equal(write_buf.toString(),read_buf.toString()); fs

run code when unit test assert fails [closed]

99封情书 提交于 2019-12-09 13:33:08
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I'm using assertEquals() from unittest.TestCase . What I want to do now is to call a function and do something there when the assertion fails, I wonder if there's a way of doing this? 回答1: In general you shouldn't do it, but if you really want to, here is a simple example: import unittest def testFailed(): print

Asserts are hit in production build causing crashes

☆樱花仙子☆ 提交于 2019-12-09 11:49:50
问题 I have several assert(condition, "message") statements in my project. They are used to check invariant conditions during development . I thought they would be ignored in production/release build (as stated in this answer). They are not. Instead they cause crashes during TestFlight testing. When I comment asserts the app does not crash. Something usually gets wrong a bit but it does not crash. Can it be something with my build settings? All my archive schemes use release configuration: The

Is using assert() for production not favored over if..else.. blocks?

依然范特西╮ 提交于 2019-12-09 11:17:36
问题 I am finding that using assert(...) makes my code shorter and easier to read, as opposed to lengthy if..else.. blocks. However, are there good technical reasons not to use assert(...) in shipping code, when it does the same thing as testing a return value while using less code? 回答1: Having read this article I will share my beliefs about assert : Yes it's fine to use assert when something absolutely should meet the condition you are asserting. Many languages allow you to raise custom errors

How to format a python assert statement that complies with PEP8?

杀马特。学长 韩版系。学妹 提交于 2019-12-09 07:28:33
问题 How does one format a long assert statement that complies with PEP8? Please ignore the contrived nature of my example. def afunc(some_param_name): assert isinstance(some_param_name, SomeClassName), 'some_param_name must be an instance of SomeClassName, silly goose!' One cannot wrap it in parenthesis, because that changes the behavior of the assert statement since it is a keyword, not a builtin function. 回答1: It's important to remember that PEP8 is only a guideline and even states that there

Node.js assert.throws with async functions (Promises)

旧城冷巷雨未停 提交于 2019-12-09 05:09:36
问题 I want to check if an async function throws using assert.throws from the native assert module. I tried with const test = async () => await aPromise(); assert.throws(test); // AssertionError: Missing expected exception.. It (obvioulsy?) doesn't work because the function exits before the Promise is resolved. Yet I found this question where the same things is attained using callbacks. Any suggestion? (I'm transpiling to Node.js native generators using Babel) 回答1: node 10 and newer Since Node.js

AssertionFailedError: null on boolean method

拈花ヽ惹草 提交于 2019-12-08 20:41:17
问题 I am testing a method that takes two objects as parameters and returns a boolean . When I use and assertTrue or assertFalse on the method in question I get the following test failure: junit.framework.AssertionFailedError: null . I know that I am passing invalid parameters and will likely be causing a NPE within the method but that is not what is happening, instead the test is failing. Note: I am using boolean and not Boolean. Sample Code: Class: public class MyClass{ public boolean foo