Computer Science Canada

assert command

Author:  marz [ Thu Jun 12, 2003 4:23 pm ]
Post subject:  assert command

What does the assert command do?
as in:
var text : int
text := Font.New ("Tomaha:14")
assert text > 0

Author:  Mazer [ Thu Jun 12, 2003 4:38 pm ]
Post subject: 

assert checks the statement and terminates the program if it's false.
although, it wouldn't work with fonts. when you make a font, the font is made no matter what. if the font you chose doesn't exist, it is replaced with another one, arial i guess. so the font variable will always be greater than 0 (except before you initialize it).

assert is usually used with opening files and loading pictures. so:

code:

var pic : int
pic := Pic.FileNew ("blah.bmp")

assert pic > 0

if the picture "blah.bmp" doesn't exist in the same folder as this program is running in, the picture isn't created and pic would not be greater than 0.
or with files:
code:

var fs : int
open : fs, "blah.txt", get
assert fs > 0

again, if "blah.txt" doesn't exist in that directory, fs will not be greater than 0 and the program will automatically terminate.

i don't really know why people like to use assert. maybe it'd be good for testing your program, but if you have other people using it having your program terminate itself is just as bad as it crashing.

Author:  Catalyst [ Thu Jun 12, 2003 4:42 pm ]
Post subject: 

something like this might be good in a program to stop it from harming your computer or files, but turing isnt really powerful enough to any thing major so there really is no point


: