www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the difference between a static assert and a unit test?

reply Alain De Vos <devosalain ymail.com> writes:
I don't know when to use a static assert and when to use a unit 
test ?
Apr 21 2022
next sibling parent frame <frame86 live.com> writes:
On Thursday, 21 April 2022 at 22:26:57 UTC, Alain De Vos wrote:
 I don't know when to use a static assert and when to use a unit 
 test ?
There is `assert()`, `static assert()` and `unittest`. `static assert()` is used while compiling, to find errors or circumstances that can lead to errors in runtime or just show type incompatibilities. `assert()` is used at runtime to allow some checks before processing data. It is evaluated at runtime and removed from code in release mode. Except `assert(0)` which will always emit an `AssertError` and will be never removed. `unittest` is a just block that will be only compiled/executed when the `-unittest` switch will be applied to compiler command line. That's it.
Apr 21 2022
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/21/22 6:26 PM, Alain De Vos wrote:
 I don't know when to use a static assert and when to use a unit test ?
An assert in general is something that must be true for the program to be valid. A normal assert is some runtime condition that must be true or the program will be terminated. A static assert is some compile-time condition that must be true or the compilation will be terminated. A unittest is a completely different thing -- this is testing generally a piece of your program (like a function), to ensure that given known inputs it returns known results. A unittest uses asserts to test that the results are valid. -Steve
Apr 21 2022