4.14.6 Utility Functions [ToC] [Index]     [Skip Back] [Skip Fwd]     [Prev] [Up] [Next]

The first utility function is compare_ints(). This function is not used by <test.c 97> but it is included there because it is used by the test modules for all the individual tree structures.

134. <Test utility functions 134> =
/* Utility functions. */

<Comparison function for ints 3>

See also 136 and 137.

This code is included in 97.

It is prototyped in <test.h 99>:

135. <Test prototypes 101> +=
int compare_ints (const void *pa, const void *pb, void *param);

The fail() function prints a provided error message to stderr, formatting it as with printf(), and terminates the program unsuccessfully:

136. <Test utility functions 134> +=
/* Prints message on stderr, which is formatted as for printf(), 
   and terminates the program unsuccessfully. */
static void 
fail (const char *message, ...)
{ va_list args; fprintf (stderr, "%s: ", pgm_name); va_start (args, message); vfprintf (stderr, message, args); va_end (args); putchar ('\n'); exit (EXIT_FAILURE); }

Finally, the xmalloc() function is a malloc() wrapper that aborts the program if allocation fails:

137. <Test utility functions 134> +=
/* Allocates and returns a pointer to size bytes of memory.
   Aborts if allocation fails. */
static void *
xmalloc (size_t size)
{ void *block = malloc (size); if (block == NULL && size != 0) fail ("out of memory"); return block; }