Questions tagged [assert]

An assertion is a statement, which aborts a program when it evaluates to false. Assert is typically used for debugging and situations which should never happen.

It is normally bad practice to use asserts in deployed software, because it typically provides information that is useful only to programmers; exceptions are preferred in this case. Also, assertions are not for use in validating input or in other situations where exceptions are preferred.

However, asserts can be used frequently when designing, to make sure that design requirements are met (for example, when designing-by-contract), and in debugging to make sure that code which is incorrect fails as quickly as possible. Assertions often give line numbers and file names, which makes tracking down where code failed easier than with other methods like core dumps.

C and C++ have assert in "assert.h". Most other languages have assert as a built-in (Python, Ruby, Java, and others).

2706 questions
30
votes
4 answers

What is the proper way to break on failed asserts in gdb?

I am attempting to capture failed asserts in my program. I’m using a library that makes direct calls to assert(), rather than a custom function or macro, and it is within this library I am currently trying to trace several porting-related bugs.…
user3995702
30
votes
3 answers

Asserting column(s) data type in Pandas

I'm trying to find a better way to assert the column data type in Python/Pandas of a given dataframe. For example: import pandas as pd t = pd.DataFrame({'a':[1,2,3], 'b':[2,6,0.75], 'c':['foo','bar','beer']}) I would like to assert that specific…
nfmcclure
  • 3,011
  • 3
  • 24
  • 40
30
votes
3 answers

How to prevent Debug.Assert(...) to show a modal dialog

I have a couple of libraries which use Debug.Assert(...). I think that the Debug.Assert(...) are fine and I still want them to execute, but I don't want them to block the execution of my application. Ideally, I would only like them to be logged…
Martin
  • 39,309
  • 62
  • 192
  • 278
30
votes
6 answers

junit assert in thread throws exception

What am I doing wrong that an exception is thrown instead of showing a failure, or should I not have assertions inside threads? @Test public void testComplex() throws InterruptedException { int loops = 10; for (int i = 0; i < loops; i++) { …
antony.trupe
  • 10,640
  • 10
  • 57
  • 84
29
votes
7 answers

How to put assert into release builds in C/C++

I need to only run ship build and I need to assert on certain condition in release build to see if the problem is fixed. How do I do it?
pra che
29
votes
3 answers

Custom C++ assert macro

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…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
28
votes
3 answers

Issues in Xunit.Assert.Collection - C#

I have a Class Library, it contains the following Model and Method Model: public class Employee { public int EmpId { get; set; } public string Name { get; set; } } Method: public class EmployeeService { public List
user7784919
28
votes
4 answers

When assert() fails, what is the program exit code?

When an assert() call fails, what is the exit code used, and where is it documented?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
28
votes
2 answers

Most idiomatic way in NUnit to assert equal length on collections

What's the most idiomatic way with NUnit 2.6 to assert that two collections are of equal length regardless of their elements' values? I can see several ways of expressing that assertion. Which one is preferred, or are there disadvantages/advantages…
knittl
  • 246,190
  • 53
  • 318
  • 364
28
votes
4 answers

In C#, is a Debug.Assert test run in release mode?

Take the following example: public void Foo() { //Code... Debug.Assert(ExpensiveTest()); //Code... } What happens to the the Debug.Assert method when I compile in release mode? Would ExpensiveTest() still run? If not, then how does it…
Matt
  • 21,026
  • 18
  • 63
  • 115
27
votes
6 answers

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…
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
27
votes
5 answers

Usage of Assert.Inconclusive

I'm wondering how someone should use Assert.Inconclusive(). I'm using it if my unit test would be about to fail for a reason other than what the test is for. For example, I have a method on a class that calculates the sum of an array of ints. On the…
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
26
votes
3 answers

How does C# compiler remove Debug.Assert's in release builds?

I was recently going through some code and considering whether I need to be careful with the expressions placed inside Debug.Assert statements, such as expensive operations or those with side effects. However, it appears the compiler is pretty…
Scott Wegner
  • 7,263
  • 2
  • 39
  • 55
26
votes
2 answers

What happened to std::assert

This answer and it's multitude of duplicates indicate that I should be using #include for the C headers that I pull from in C++ code, and that I should be calling them with std::*. I have been doing that but I notice an exception. std::assert…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
26
votes
4 answers

Why is assert used in the Integer.valueOf method of the Integer class?

I was digging into how the Integer class actually uses cached objects, and I found the below code in the Integer.valueOf method: public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <=…
Vishrant
  • 15,456
  • 11
  • 71
  • 120