assert

Is it idiomatic Ruby to add an assert( ) method to Ruby's Kernel class?

廉价感情. 提交于 2019-11-30 01:39:44
I'm expanding my Ruby understanding by coding an equivalent of Kent Beck's xUnit in Ruby. Python (which Kent writes in) has an assert() method in the language which is used extensively. Ruby does not. I think it should be easy to add this but is Kernel the right place to put it? BTW, I know of the existence of the various Unit frameworks in Ruby - this is an exercise to learn the Ruby idioms, rather than to "get something done". Julik No it's not a best practice. The best analogy to assert() in Ruby is just raising raise "This is wrong" unless expr and you can implement your own exceptions if

design of python: why is assert a statement and not a function?

你说的曾经没有我的故事 提交于 2019-11-30 01:10:54
In Python, assert is a statement, and not a function. Was this a deliberate decision? Are there any advantages to having assert be a statement (and reserved word) instead of a function? According to the docs , assert expression1, expression2 is expanded to if __debug__: if not expression1: raise AssertionError(expression2) The docs also say that "The current code generator emits no code for an assert statement when optimization is requested at compile time." Without knowing the details, it seems like a special case was required to make this possible. But then, a special case could also be used

Am I misunderstanding assert() usage?

邮差的信 提交于 2019-11-30 00:27:49
问题 I was taking a look at the assert() reference page and I got stuck while I read the given example: /* assert example */ #include <stdio.h> #include <assert.h> int main () { FILE * datafile; datafile=fopen ("file.dat","r"); assert (datafile); fclose (datafile); return 0; } In this example, assert is used to abort the program execution if datafile compares equal to 0, which happens when the previous call to fopen was not successful. I totally agree that if fopen() fails, assert() will abort

Pthread mutex assertion error

送分小仙女□ 提交于 2019-11-29 23:43:01
I'm encountering the following error at unpredictable times in a linux-based (arm) communications application: pthread_mutex_lock.c:82: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed. Google turns up a lot of references to that error, but little information that seems relevant to my situation. I was wondering if anyone can give me some ideas about how to troubleshoot this error. Does anyone know of a common cause for this assertion? Thanks in advance. Rock solid for 4 days straight. I'm declaring victory on this one. The answer is "stupid user error" (see comments above).

What is the best way of implementing assertion checking in C++?

大憨熊 提交于 2019-11-29 23:35:37
By that I mean, what do I need to do to have useful assertions in my code? MFC is quite easy, i just use ASSERT(something). What's the non-MFC way? Edit: Is it possible to stop assert breaking in assert.c rather than than my file which called assert()? Edit: What's the difference between <assert.h> & <cassert> ? Accepted Answer: Loads of great answers in this post, I wish I could accept more than one answer (or someone would combine them all). So answer gets awarded to Ferruccio (for first answer). #include <cassert> assert(something); and for compile-time checking, Boost's static asserts are

How to use assert in android?

北城以北 提交于 2019-11-29 23:26:30
I want to use assert obj != null : "object cannot be null" on Android device. The assert doesn't seem to work, so I searched online and I found this local solution: adb shell setprop debug.assert 1 it does work on my local machine. I want to run this command using my eclipse project(so it would be in the source control). How can I do it? Thanks! Assert won't work in Android because most of the time a person isn't running in debug mode, but rather some optimized code. Thus, the proper solution is to manually throw an exception, with code like this: if (obj==null) throw new AssertionError(

Difference between Raise Try and Assert

你说的曾经没有我的故事 提交于 2019-11-29 23:06:08
I have been learning Python for a while and the raise function and assert are (what I realised is that both of them crash the app, unlike try - except) really similar and I can't see a situation where you would use raise or assert over try . So, what is the difference between Raise, Try and Assert? Assert: Used when you want to "stop" the script based on a certain condition and return something to help debug faster: list_ = ["a","b","x"] assert "x" in list_, "x is not in the list" print("passed") #>> prints passed list_ = ["a","b","c"] assert "x" in list_, "x is not in the list" print("passed"

How to find the name of the current function at runtime?

自作多情 提交于 2019-11-29 22:53:19
After years of using the big ugly MFC ASSERT macro, I have finally decided to ditch it and create the ultimate ASSERT macro. I am fine with getting the file and line number, and even the expression that failed. I can display a messagebox with these in, and Abort/Retry/Cancel buttons. And when I press Retry the VS debugger jumps to the line containing the ASSERT call (as opposed to the disassembly somewhere like some other ASSERT functions). So it's all pretty much working. But what would be really cool would be to display the name of the function that failed . Then I can decide whether to

assert() with message

此生再无相见时 提交于 2019-11-29 22:12:40
I saw somewhere assert used with a message in the following way: assert(("message", condition)); This seems to work great, except that gcc throws the following warning: warning: left-hand operand of comma expression has no effect How can I stop the warning? Use -Wno-unused-value to stop the warning; (the option -Wall includes -Wunused-value ). I think even better is to use another method, like assert(condition && "message"); bugfeeder Try: #define assert__(x) for ( ; !(x) ; assert(x) ) use as such: assert__(x) { printf("assertion will fail\n"); } Will execute the block only when assert fails.

Why is assert a macro and not a function?

泪湿孤枕 提交于 2019-11-29 22:03:12
My lecturer has asked me that in class, and I was wondering why is it a macro instead of a function? Shafik Yaghmour The simple explanation would be that the standard requires assert to be a macro, if we look at the draft C99 standard ( as far as I can tell the sections are the same in draft C11 standard as well ) section 7.2 Diagnostics paragraph 2 says: The assert macro shall be implemented as a macro, not as an actual function. If the macro definition is suppressed in order to access an actual function, the behavior is undefined. Why does it require this, the rationale given in Rationale