Assert
Assert is a small set of test-assertion helpers. xcc’s own test fixtures use
it, and user code can too. Each assertion increments a test counter. A failure
also increments a failure counter and prints FAIL T<n>, which the fixture
runner scans for. summary prints the DONE <count> line at the end
of a run. Every method is static.
#import <Assert.xc>Overview
Section titled “Overview”Assert lives under support/generic/lib/, not an architecture directory, so it
works the same on every target. It imports Stdio for
its FAIL / DONE output. Every assertion funnels through
isTrue so the counter bookkeeping and the FAIL T<n> print live in
one place.
Topics
Section titled “Topics”Equality · isEqual · isNotEqual
Range & ordering · isInRange · isLess · isGreater
Summary & reset · summary · reset
Accessors · testCount · failCount
Lifecycle · init
Every other assertion routes through isTrue.
isTrue
Section titled “isTrue”static void isTrue(bool ok)Records one test. If ok is false, increments the failure counter and prints
FAIL T<n> with the 1-based test index.
isFalse
Section titled “isFalse”static void isFalse(bool ok)isTrue(!ok): records a test that passes when ok is false.
Assert.isTrue(1 + 1 == 2);Assert.isFalse(needle == 0);Equality
Section titled “Equality”Overloaded across the common scalar widths.
isEqual
Section titled “isEqual”static void isEqual(u16 a, u16 b)static void isEqual(u32 a, u32 b)static void isEqual(i16 a, i16 b)static void isEqual(i32 a, i32 b)Asserts a == b.
isNotEqual
Section titled “isNotEqual”static void isNotEqual(u16 a, u16 b)static void isNotEqual(u32 a, u32 b)Asserts a != b.
Assert.isEqual(counter, (u16)42);Assert.isEqual(timestamp, (u32)1234567);Assert.isNotEqual(scoreA, scoreB);Pointers
Section titled “Pointers”isNull
Section titled “isNull”static void isNull(pointer p)Asserts p is the null pointer.
isNotNull
Section titled “isNotNull”static void isNotNull(pointer p)Asserts p is non-null.
Foo* f = lookup(name);Assert.isNotNull(f);Range & ordering
Section titled “Range & ordering”isInRange
Section titled “isInRange”static void isInRange(u16 v, u16 lo, u16 hi)Asserts lo <= v && v <= hi.
isLess
Section titled “isLess”static void isLess(u16 a, u16 b)Asserts a < b.
isGreater
Section titled “isGreater”static void isGreater(u16 a, u16 b)Asserts a > b.
Assert.isInRange(angle, (u16)0, (u16)360);Assert.isLess(elapsed, (u16)budget);Summary & reset
Section titled “Summary & reset”summary
Section titled “summary”static void summary(void)Prints DONE <count> and, if any assertions failed, a FAIL <m> tests line.
Like every method here it is a no-op in release builds.
static void reset(void)Zeroes both the test and failure counters.
void main(void) { Assert.isEqual(1 + 2, (u16)3); Assert.isInRange((u16)50, (u16)0, (u16)100); Assert.isNotNull("abc"); Assert.summary(); // DONE 3}Accessors
Section titled “Accessors”testCount
Section titled “testCount”static u16 testCount(void)The number of assertions run so far (returns 0 in release builds).
failCount
Section titled “failCount”static u16 failCount(void)The number of failed assertions so far (returns 0 in release builds). Use it to decide on post-test cleanup based on whether anything failed. In a release build, either base that decision on the build flavour or do not read these accessors.
Lifecycle
Section titled “Lifecycle”void init(void)Zeroes the counters. The class is used statically, so you rarely call it: the
counters start at zero, and reset clears them during a run.