assert

When should assertions stay in production code?

Hi all, There's a discussion going on over at comp.lang.c++.moderated about whether or not assertions, which in C++ only exist in debug builds by default, should be kept in production code or not. Obviously, each project is unique, so my question here is not so much whether assertions should be kept, but in which cases this is recommen...

Passing null to a method

I am in the middle of reading the excellent Clean Code One discussion is regarding passing nulls into a method. public class MetricsCalculator { public double xProjection(Point p1, Point p2) { return (p2.x - p1.x) * 1.5; } } ... calculator.xProjection(null, new Point(12,13)); It represents different ways of handling t...

How can I assert() without using abort()?

If I use assert() and the assertion fails then assert() will call abort(), ending the running program abruptly. I can't afford that in my production code. Is there a way to assert in runtime yet be able to catch failed assertions so I have the chance to handle them gracefully? ...

Debug.Assert vs. Specific Thrown Exceptions

Hi, I've just started skimming 'Debugging MS .Net 2.0 Applications' by John Robbins, and have become confused by his evangelism for Debug.Assert(...). He points out that well-implemented Asserts store the state, somewhat, of an error condition, e.g.: Debug.Assert(i > 3, "i > 3", "This means I got a bad parameter"); Now, personally, ...

How to generate a stacktrace when my gcc C++ app crashes

When my c++ app crashes I would like to generate a stacktrace. I already asked this but I guess I needed to clarify my needs. My app is being run by many different users and it also runs on Linux, Windows and Macintosh ( all versions are compiled using gcc ). I would like my program to be able to generate a stack trace when it crashes...

How to disable a programmatical breakpoint / assert?

I am using Visual Studio, developing a native application, I have a programmatical breakpoint (assert) in my code placed using __asm int 3 or __debugbreak. Sometimes when I hit it, I would like to disable it so that successive hits in the same debugging session no longer break into the debugger. How can I do this? ...

design by contract tests by assert or by exception?

When programming by contract a function or method first checks whether its preconditions are fulfilled, before starting to work on its responsibilities, right? The two most prominent ways to do these checks are by assert and by exception. assert fails only in debug mode. To make sure it is crucial to (unit) test all separate contract ...

Unit Testing without Assertions

Occasionally I come accross a unit test that doesn't Assert anything. The particular example I came across this morning was testing that a log file got written to when a condition was met. The assumption was that if no error was thrown the test passed. I personally don't have a problem with this, however it seems to be a bit of a "code ...

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

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 framew...

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

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? ...

Ways to ASSERT expressions at build time in C

I'm tidying up some older code that uses 'magic numbers' all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values used to document the registers). However, I'm concerned that with the volume of changes I m...

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

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: Load...

visual studio enable assert

I am trying to add assert statements to a project but they keep being skiped is there an option I need to enable somewhere. The assert statement is: Debug.Assert(false, "Deserialization failed", "Deserialization failed"); and I am running in debug mode. I could be doing something silly not sure. ...

What are assertions? and why would you use them?

How are assertions done in c++? Example code is appreciated. ...

ASSERT vs. ATLASSERT vs. assert

I am refactoring some MFC code that is littered with ASSERT statements, and in preparation for a future Linux port I want to replace them with the standard assert. Are there any significant differences between the two implementations that people know of that could bite me on the backside? Similarly, I have also come across some code th...

Testing for assert in the Boost Test framework

I use the Boost Test framework to unit test my C++ code and wondered if it is possible to test if a function will assert? Yes, sounds a bit strange but bear with me! Many of my functions check the input parameters upon entry, asserting if they are invalid, and it would be useful to test for this. For example: void MyFunction(int para...

Why doesn't java's assert statement allow you to specify a message?

Seems likes it might be useful to have the assert display a message when an assertion fails. Currently an AssertionError gets thrown, but I don't think you have the power to specify a custom message for it. Am I wrong? If I am please correct me, and if I'm correct can you provide a mechanism for doing this (other than creating your ow...

differences between 2 JUnit Assert classes

Hi, I've noticed that the JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is? The classes I'm referring to are: junit.framework.Assert and org.junit.Assert. Cheers, Don ...

Can I set Visual Stdio 2005 to ignore assertions in a specific region of code while debugging....

Aaargh! OK, here's the scenario. I'm debugging my own app (C/C++) which is using some library developed by another team in the company. An assertion fails when my code generates some edge case. Its a pain because the assertion is not formulated correctly so the library function is working OK but I get all these interruptions where I jus...

Some (anti-)patterns on using assert (Java, and others)

Finally, I have a question to ask on Stack Overflow! :-) The main target is for Java but I believe it is mostly language agnostic: if you don't have native assert, you can always simulate it. I work for a company selling a suite of softwares written in Java. The code is old, dating back to Java 1.3 at least, and at some places, it show...