/** * expt.h * Utilities for conducting experiments with code. */ #ifndef __EXPT_H__ #define __EXPT_H__ // +--------+----------------------------------------------------------- // | Macros | // +--------+ /** * BLOCK(CODE) * Turn CODE into a block that you can legally nest. */ #define BLOCK(CODE) do { CODE } while (0) /** * EXPERIMENT(COMMAND) * Print out command and then execute it. */ #define EXPERIMENT(COMMAND) BLOCK (fprintf (stderr, "\t%s;\n", #COMMAND); COMMAND;); /** * VAR_PREFIX * A prefix that should be put before printing any variable. */ #define VAR_PREFIX "\t // " /** * PRINT_ARRAY (ARR, SIZE, TYPE, PATTERN) * Print an array of values of the given type, using PATTERN to * print each element. */ #define PRINT_ARRAY(ARR, SIZE, TYPE, PATTERN) \ BLOCK( \ TYPE *_stuff_ = ARR; \ fprintf (stderr, VAR_PREFIX "%s = ", #ARR); \ if (SIZE == 0) \ { \ fprintf (stderr, "[]\n"); \ } \ else \ { \ int _i_; \ fprintf (stderr, "["); \ fprintf (stderr, PATTERN, _stuff_[0]); \ for (_i_ = 1; _i_ < SIZE; _i_++) \ { \ fprintf (stderr, ", "); \ fprintf (stderr, PATTERN, _stuff_[_i_]); \ } \ fprintf (stderr, "]\n"); \ } \ ) /** * PRINT_DOUBLE(VAR) * Print the value of a double variable. */ #define PRINT_DOUBLE(VAR) fprintf (stderr, VAR_PREFIX "%s = %d\n", #VAR, VAR) /** * PRINT_INT(VAR) * Print the value of an integer variable. */ #define PRINT_INT(VAR) fprintf (stderr, VAR_PREFIX "%s = %d\n", #VAR, VAR) /** * PRINT_STRING(VAR) * Print the value of a string variable. */ #define PRINT_STRING(VAR) \ BLOCK ( \ char *result = VAR; \ fprintf (stderr, \ VAR_PREFIX "%s = %s\n", \ #VAR, \ result == NULL ? "" : result); \ ) /** * PRINT_STRING_ARRAY(ARR, SIZE) * Print an array of strings */ #define PRINT_STRING_ARRAY(ARR, SIZE) \ PRINT_ARRAY (ARR, SIZE, char *, "%s") #endif // __EXPT_H__