(******************************************************************* This file was generated automatically by the Mathematica front end. It contains Initialization cells from a Notebook file, which typically will have the same name as this file except ending in ".nb" instead of ".m". This file is intended to be loaded into the Mathematica kernel using the package loading commands Get or Needs. Doing so is equivalent to using the Evaluate Initialization Cells menu command in the front end. DO NOT EDIT THIS FILE. This entire file is regenerated automatically each time the parent Notebook file is saved in the Mathematica front end. Any changes you make to this file will be overwritten. ***********************************************************************) BeginPackage["assert`", "Exceptions`"] assert::usage= "assert[condition_, errmsg_String, context_:0] contidition is tested. If condition is false, throw an exception and print an error message defined by errmsg. Optionally, the user can provide a undefined symbol in the current context for the third argument to track what packages the error occured in. For example, assert[x>5, \"x is too small\", c] where c is undefined in the current context. Optionally, assert will attempt to log error message to a file when it fails if the Global`logStream variable is set to a stream." assert::err2 = "assert[`1`, ...] failed. Error message is \"`2`\". "; assert::err3 = "assert[`1`, ...] failed. Error message is \"`2`\". The error occured in the package `3`."; SetAttributes[assert, HoldFirst] ; Begin["`Private`"] assert[condition_, errmsg_String,context_:0]:= Module[{msg}, If[!ReleaseHold[condition], msg = {HoldForm[condition],errmsg}; (*Trick to get the package name the assert statement was called \ from. Pass an undefined symbol and find the symbol's context*) If[Head[context]===Symbol["Symbol"], If[Context[context]=!= "Global`", AppendTo[msg, Context[context]]; ]; ]; logMessage[msg]; Raise[assert]; Print["Should not get here."]; ]; ]; logMessage[msg_List]:=Module[{msgString}, If[Length[msg]===2, Message[assert::err2, Sequence@@msg]; msgString=StringForm[assert::err2, Sequence@@msg]//ToString; , Message[assert::err3, Sequence@@msg]; msgString=StringForm[assert::err3, Sequence@@msg]//ToString; ]; If[ValueQ[Global`logStream], WriteString[Global`logStream,msgString <>"\n"]]; ] End[] Protect[logMessage, assert] EndPackage[]