122#define NO_REQUIRE_DOC
131#define assert(description, condition)
151#define assert(description, condition) \
153 fprintf(stderr, "%s, line %d: assertion \"%s\" (%s) violated\n", __FILE__, __LINE__, description, #condition);\
159#define assert_x(description, condition, ...)
179#define assert_x(description, condition, ...) \
181 fprintf(stderr, "%s, line %d: assertion \"%s\" violated: ", __FILE__, __LINE__, description);\
182 fprintf(stderr, __VA_ARGS__);\
183 fprintf(stderr, "\n");\
189#define assert_not_null(pointer)
207#define assert_not_null(pointer) \
208if (pointer == NULL) {\
209 fprintf(stderr, "%s, line %d: assertion \"not null\" (" #pointer ") violated\n", __FILE__, __LINE__);\
225#define assert_code(x)
227#define assert_code(x) x
233#define require(description, condition)
254#define require(description, condition) \
256 fprintf(stderr, "%s, line %d: %s's precondition \"%s\" (%s) violated\n", __FILE__, __LINE__, __func__, description, #condition);\
262#define require_x(description, condition, ...)
283#define require_x(description, condition, ...) \
285 fprintf(stderr, "%s, line %d: %s's precondition \"%s\" violated: ", __FILE__, __LINE__, __func__, description);\
286 fprintf(stderr, __VA_ARGS__);\
287 fprintf(stderr, "\n");\
297#define require_x(description, condition, ...) \
299 fprintf(stderr, "%s, line %d: %s's precondition \"%s\" violated: ", __FILE__, __LINE__, __func__, description);\
300 fprintf(stderr, __VA_ARGS__);\
301 fprintf(stderr, "\n");\
303 int size = backtrace(buffer, 10);\
304 char** strings = backtrace_symbols(buffer, size);\
305 if (strings != NULL) {\
306 for (int i = 1; i < size - 1; i++) {\
307 String s = strings[i] + 59;\
308 String t = strchr(s, ' ');\
309 int n = t ? t - s : strlen(s);\
310 printf (" is called from %.*s\n", n, s);\
318#define require_not_null(argument)
337#define require_not_null(argument) \
338if (argument == NULL) {\
339 fprintf(stderr, "%s, line %d: %s's precondition \"not null\" (" #argument ") violated\n", __FILE__, __LINE__, __func__);\
347#define ensure(description, condition)
370#define ensure(description, condition) \
372 fprintf(stderr, "%s, line %d: %s's postcondition \"%s\" (%s) violated\n", __FILE__, __LINE__, __func__, description, #condition);\
378#define ensure_x(description, condition, ...)
402#define ensure_x(description, condition, ...) \
404 fprintf(stderr, "%s, line %d: %s's postcondition \"%s\" violated: ", __FILE__, __LINE__, __func__, description);\
405 fprintf(stderr, __VA_ARGS__);\
406 fprintf(stderr, "\n");\
425#define ensure_code(x)
427#define ensure_code(x) x
431#define ensure_not_null(pointer)
449#define ensure_not_null(pointer) \
450if (pointer == NULL) {\
451 fprintf(stderr, "%s, line %d: %s's postcondition \"not null\" (" #pointer ") violated\n", __FILE__, __LINE__, __func__);\
468#define forall(i, length, condition) ({\
469 bool _forall_result = true;\
470 for (int i = 0; i < length; i++) { if (!(condition)) { _forall_result = false; break; } }\
487#define forall_x(init, has_more_steps, do_step, condition) ({\
488 bool _forall_result = true;\
489 for (init; has_more_steps; do_step) { if (!(condition)) { _forall_result = false; break; } }\
505#define exists(i, length, condition) ({\
506 bool _exists_result = false;\
507 for (int i = 0; i < length; i++) { if (condition) { _exists_result = true; break; } }\
524#define exists_x(init, has_more_steps, do_step, condition) ({\
525 bool _exists_result = false;\
526 for (init; has_more_steps; do_step) { if (condition) { _exists_result = true; break; } }\
543#define count_if(init, has_more_steps, do_step, condition) ({\
544 int _count_if_result = 0;\
545 for (init; has_more_steps; do_step) { if (condition) { _count_if_result++; } }\
556#define exit_if(condition, ...) \
559 fprintf(stderr, __VA_ARGS__);\
560 fprintf(stderr, "\n");\
561 funlockfile(stderr);\
571 fprintf(stderr, "%s:%d, %s: ", __FILE__, __LINE__, __func__);\
572 fprintf(stderr, __VA_ARGS__);\
573 fprintf(stderr, "\n");\
574 funlockfile(stderr);\
583#define panic_if(condition, ...) \
586 fprintf(stderr, "%s:%d, %s: ", __FILE__, __LINE__, __func__);\
587 fprintf(stderr, __VA_ARGS__);\
588 fprintf(stderr, "\n");\
589 funlockfile(stderr);\
598typedef struct timespec timespec;
601timespec time_now(void);
604double time_ms_since(timespec t);
617 #define PL printf("%s:%d\n", __func__, __LINE__)
619 #define PLi(i) printf("%s:%d: %d\n", __func__, __LINE__, i)
622 fprintf(stderr, "%s:%d: ", __func__, __LINE__);\
623 fprintf(stderr, __VA_ARGS__);\
624 fprintf(stderr, "\n");\
644Any base_malloc(
const char *file,
const char *function,
int line,
size_t size);
652#define xmalloc(size) base_malloc(__FILE__, __func__, __LINE__, size)
665Any base_realloc(
const char *file,
const char *function,
int line,
Any ptr,
size_t size);
674#define xrealloc(ptr, size) base_realloc(__FILE__, __func__, __LINE__, ptr, size)
689Any base_calloc(
const char *file,
const char *function,
int line,
size_t num,
size_t size);
698#define xcalloc(num, size) base_calloc(__FILE__, __func__, __LINE__, num, size)
705void base_free(
Any p);
711#define free base_free
718void __attribute__((noreturn)) base_exit(
int status);
724#define exit base_exit
956#define EPSILON 0.00000001
983#define test_equal_b(a, e) base_test_equal_b(__FILE__, __LINE__, a, e)
986#define test_equal_i(a, e) base_test_equal_i(__FILE__, __LINE__, a, e)
989#define test_within_d(a, e, epsilon) base_test_within_d(__FILE__, __LINE__, a, e, epsilon)
992#define test_within_i(a, e, epsilon) base_test_within_i(__FILE__, __LINE__, a, e, epsilon)
995#define test_equal_c(a, e) base_test_equal_c(__FILE__, __LINE__, a, e)
998#define test_equal_s(a, e) base_test_equal_s(__FILE__, __LINE__, a, e)
1003#define test_equal_ca(a, e, ne) base_test_equal_ca(__FILE__, __LINE__, a, e, ne)
1006#define test_equal_boa(a, e, ne) base_test_equal_ba(__FILE__, __LINE__, a, e, ne)
1009#define test_equal_struct(a, e, p) base_test_equal_struct(__FILE__, __LINE__, a, e, p)
1017void base_count_check(
void);
1023void base_count_success(
void);
void println()
Prints a line break.
bool base_test_equal_c(const char *file, int line, char a, char e)
Checks whether the actual value a is equal to the expected value e.
String s_copy(String s)
Creates a copy of the given string.
bool base_test_within_i(const char *file, int line, int a, int e, int epsilon)
Checks whether the actual value a is within +/-epsilon of the expected value e.
double d_input(void)
Reads a double from standard input.
double d_rnd(double i)
Returns a random double between in the interval [0,i).
void printiln(int i)
Prints an integer followed by a line break.
void printdaln(double *a, int n)
Prints a C-array of n doubles followed by a line break.
void printsa(String *a, int n)
Prints a C-array of n Strings.
void write_file_data(String name, Byte *data, int n)
Writes a memory block to a file.
void s_set(String s, int i, char c)
Sets s element at index i to value v.
void printsaln(String *a, int n)
Prints a C-array of n Strings followed by a line break.
int i_of_s(String s)
Converts a String to an integer.
void printboa(bool *a, int n)
Prints a C-array of n booleans.
void printsln(String s)
Prints a String followed by a line break.
bool base_test_within_d(const char *file, int line, double a, double e, double epsilon)
Checks whether the actual value a is within +/-epsilon of the expected value e.
void printca(char *a, int n)
Prints a C-array of n characters.
void printcln(char c)
Prints a character followed by a line break.
int s_length(String s)
Returns the length of the string (number of characters).
void s_write_file(String name, String data)
Writes a String to a file.
void printc(char c)
Prints a character.
void printb(bool b)
Prints a boolean value.
void printd(double d)
Prints a double.
void printbln(bool b)
Prints a boolean value followed by a line break.
String s_input(int n)
Reads at most n-1 characters into a newly allocated string.
void printba(Byte *a, int n)
Prints a C-array of n Bytes.
bool base_test_equal_b(const char *file, int line, bool a, bool e)
Checks whether the actual value a is equal to the expected value e.
bool base_test_equal_s(const char *file, int line, String a, String e)
Checks whether the actual value a is equal to the expected value e.
void printdln(double d)
Prints a double followed by a line break.
bool b_rnd(void)
Returns a random Boolean between (true or false) with 50% probability.
void get_line(char *line, int n)
Reads at most n-1 characters into an existing buffer.
void printialn(int *a, int n)
Prints a C-array of n integers followed by a line break.
void printcaln(char *a, int n)
Prints a C-array of n characters followed by a line break.
int i_rnd(int i)
Returns a random int between in the interval [0,i).
int i_input(void)
Reads an integer from standard input.
char s_get(String s, int i)
Returns character at index i.
void prints(String s)
Prints a String.
bool base_test_equal_struct(const char *file, int line, Any a, Any e, AnyFn predicate)
Checks whether the members of struct a are equal to the members of struct e.
double d_of_s(String s)
Converts a String to a double.
void printbaln(Byte *a, int n)
Prints a C-array of n Bytes followed by a line break.
String s_read_file(String name)
Reads the contents of a file into a String.
bool base_test_equal_i(const char *file, int line, int a, int e)
Checks whether the actual value a is equal to the expected value e.
void printi(int i)
Prints an integer.
void report_memory_leaks(bool do_check)
Switches memory checking on or off.
bool s_contains(String s, String part)
Returns true iff s contains part.
void printboaln(bool *a, int n)
Prints a C-array of n booleans followed by a line break.
void printia(int *a, int n)
Prints a C-array of n integers.
bool s_equals(String s, String t)
Returns true iff s and t are equal.
void printda(double *a, int n)
Prints a C-array of n doubles.
Type definitions for Programming I Library.
char * String
A String is a sequence of characters.
Definition basedefs.h:24
void * Any
Represents a pointer to an unspecified type.
Definition basedefs.h:27
void * AnyFn
Represents a pointer to a function of an unspecified type.
Definition basedefs.h:30
unsigned char Byte
A Byte represents a single byte of memory.
Definition basedefs.h:21