Skip to content

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>

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.

Core · isTrue · isFalse

Equality · isEqual · isNotEqual

Pointers · isNull · isNotNull

Range & ordering · isInRange · isLess · isGreater

Summary & reset · summary · reset

Accessors · testCount · failCount

Lifecycle · init


Every other assertion routes through 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.

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);

↑ Topics

Overloaded across the common scalar widths.

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.

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);

↑ Topics

static void isNull(pointer p)

Asserts p is the null pointer.

static void isNotNull(pointer p)

Asserts p is non-null.

Foo* f = lookup(name);
Assert.isNotNull(f);

↑ Topics

static void isInRange(u16 v, u16 lo, u16 hi)

Asserts lo <= v && v <= hi.

static void isLess(u16 a, u16 b)

Asserts a < b.

static void isGreater(u16 a, u16 b)

Asserts a > b.

Assert.isInRange(angle, (u16)0, (u16)360);
Assert.isLess(elapsed, (u16)budget);

↑ Topics

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
}

↑ Topics

static u16 testCount(void)

The number of assertions run so far (returns 0 in release builds).

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.

↑ Topics

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.

↑ Topics