Programming I version 1.5.3
Programming I C Library
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1/*
2base +
3*/
4
77#ifndef __BASE_H__
78#define __BASE_H__
79
80#include <stdio.h>
81#include <stdlib.h>
82#include <stddef.h>
83#include <string.h>
84#include <ctype.h>
85#include <math.h>
86#include <time.h>
87#include <stdbool.h>
88// #include <execinfo.h> // stack traces
89#include "basedefs.h"
90
91
93// Initialization
94
99void base_init(void);
100
105void report_memory_leaks(bool do_check);
106
107
108
110// Assertions
111
116#define NO_ASSERT_DOC
117
122#define NO_REQUIRE_DOC
123
128#define NO_ENSURE_DOC
129
130#ifdef NO_ASSERT
131#define assert(description, condition)
132#else
151#define assert(description, condition) \
152if (!(condition)) {\
153 fprintf(stderr, "%s, line %d: assertion \"%s\" (%s) violated\n", __FILE__, __LINE__, description, #condition);\
154 exit(EXIT_FAILURE);\
155}
156#endif
157
158#ifdef NO_ASSERT
159#define assert_x(description, condition, ...)
160#else
179#define assert_x(description, condition, ...) \
180if (!(condition)) {\
181 fprintf(stderr, "%s, line %d: assertion \"%s\" violated: ", __FILE__, __LINE__, description);\
182 fprintf(stderr, __VA_ARGS__);\
183 fprintf(stderr, "\n");\
184 exit(EXIT_FAILURE);\
185}
186#endif
187
188#ifdef NO_ASSERT
189#define assert_not_null(pointer)
190#else
207#define assert_not_null(pointer) \
208if (pointer == NULL) {\
209 fprintf(stderr, "%s, line %d: assertion \"not null\" (" #pointer ") violated\n", __FILE__, __LINE__);\
210 exit(EXIT_FAILURE);\
211}
212#endif
213
224#ifdef NO_ASSERT
225#define assert_code(x)
226#else
227#define assert_code(x) x
228#endif
229
230
231
232#ifdef NO_REQUIRE
233#define require(description, condition)
234#else
254#define require(description, condition) \
255if (!(condition)) {\
256 fprintf(stderr, "%s, line %d: %s's precondition \"%s\" (%s) violated\n", __FILE__, __LINE__, __func__, description, #condition);\
257 exit(EXIT_FAILURE);\
258}
259#endif
260
261#ifdef NO_REQUIRE
262#define require_x(description, condition, ...)
263#else
283#define require_x(description, condition, ...) \
284if (!(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");\
288 exit(EXIT_FAILURE);\
289}
290#endif
291
292#if 0
293// print stack trace using gcc, requires
294// #include <execinfo.h>
295
296#include <execinfo.h>
297#define require_x(description, condition, ...) \
298if (!(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");\
302 void *buffer[10];\
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);\
311 }\
312 }\
313 exit(EXIT_FAILURE);\
314}
315#endif
316
317#ifdef NO_REQUIRE
318#define require_not_null(argument)
319#else
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__);\
340 exit(EXIT_FAILURE);\
341}
342#endif
343
344
345
346#ifdef NO_ENSURE
347#define ensure(description, condition)
348#else
370#define ensure(description, condition) \
371if (!(condition)) {\
372 fprintf(stderr, "%s, line %d: %s's postcondition \"%s\" (%s) violated\n", __FILE__, __LINE__, __func__, description, #condition);\
373 exit(EXIT_FAILURE);\
374}
375#endif
376
377#ifdef NO_ENSURE
378#define ensure_x(description, condition, ...)
379#else
402#define ensure_x(description, condition, ...) \
403if (!(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");\
407 exit(EXIT_FAILURE);\
408}
409#endif
410
424#ifdef NO_ENSURE
425#define ensure_code(x)
426#else
427#define ensure_code(x) x
428#endif
429
430#ifdef NO_ENSURE
431#define ensure_not_null(pointer)
432#else
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__);\
452 exit(EXIT_FAILURE);\
453}
454#endif
455
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; } }\
471 _forall_result;\
472})
473
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; } }\
490 _forall_result;\
491})
492
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; } }\
508 _exists_result;\
509})
510
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; } }\
527 _exists_result;\
528})
529
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++; } }\
546 _count_if_result;\
547})
548
550// Exit on Failure
551
556#define exit_if(condition, ...) \
557if (condition) {\
558 flockfile(stderr);\
559 fprintf(stderr, __VA_ARGS__);\
560 fprintf(stderr, "\n");\
561 funlockfile(stderr);\
562 exit(EXIT_FAILURE);\
563}
564
569#define panic(...) {\
570 flockfile(stderr);\
571 fprintf(stderr, "%s:%d, %s: ", __FILE__, __LINE__, __func__);\
572 fprintf(stderr, __VA_ARGS__);\
573 fprintf(stderr, "\n");\
574 funlockfile(stderr);\
575 exit(EXIT_FAILURE);\
576}
577
583#define panic_if(condition, ...) \
584if (condition) {\
585 flockfile(stderr);\
586 fprintf(stderr, "%s:%d, %s: ", __FILE__, __LINE__, __func__);\
587 fprintf(stderr, __VA_ARGS__);\
588 fprintf(stderr, "\n");\
589 funlockfile(stderr);\
590 exit(EXIT_FAILURE);\
591}
592
594// Timing
595
596/*
598typedef struct timespec timespec;
599
601timespec time_now(void);
602
604double time_ms_since(timespec t);
605*/
606
608// Debugging
609
610#ifdef NO_DEBUG
611 #define PL
612 #define PLi(i)
613 #define PLs(s)
614 #define PLf(...)
615#else
617 #define PL printf("%s:%d\n", __func__, __LINE__)
619 #define PLi(i) printf("%s:%d: %d\n", __func__, __LINE__, i)
621 #define PLf(...) {\
622 fprintf(stderr, "%s:%d: ", __func__, __LINE__);\
623 fprintf(stderr, __VA_ARGS__);\
624 fprintf(stderr, "\n");\
625 }
626#endif
627
629// Memory
630
631// http://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html
632
644Any base_malloc(const char *file, const char *function, int line, size_t size);
645
652#define xmalloc(size) base_malloc(__FILE__, __func__, __LINE__, size)
653
665Any base_realloc(const char *file, const char *function, int line, Any ptr, size_t size);
666
674#define xrealloc(ptr, size) base_realloc(__FILE__, __func__, __LINE__, ptr, size)
675
676
689Any base_calloc(const char *file, const char *function, int line, size_t num, size_t size);
690
698#define xcalloc(num, size) base_calloc(__FILE__, __func__, __LINE__, num, size)
699
705void base_free(Any p);
706
711#define free base_free
712
718void __attribute__((noreturn)) base_exit(int status);
719
724#define exit base_exit
725
727// Strings
728
736
744char s_get(String s, int i);
745
753void s_set(String s, int i, char c);
754
761
769
777
779// Conversion
780
783
785double d_of_s(String s);
786
788// Output
789
791void printi(int i);
792
794void printiln(int i);
795
797void printd(double d);
798
800void printdln(double d);
801
803void printc(char c);
804
806void printcln(char c);
807
810
813
815void printb(bool b);
816
818void printbln(bool b);
819
821void println();
822
824void printia(int *a, int n);
825
827void printialn(int *a, int n);
828
830void printda(double *a, int n);
831
833void printdaln(double *a, int n);
834
836void printsa(String *a, int n);
837
839void printsaln(String *a, int n);
840
842void printca(char *a, int n);
843
845void printcaln(char *a, int n);
846
848void printba(Byte *a, int n);
849
851void printbaln(Byte *a, int n);
852
854void printboa(bool *a, int n);
855
857void printboaln(bool *a, int n);
858
859
860
862// Input
863
871void get_line(char *line, int n);
872
880
884int i_input(void);
885
889double d_input(void);
890
891
893// Files
894
902
909void s_write_file(String name, String data);
910
919void write_file_data(String name, Byte *data, int n);
920
921
922
924// Random numbers
925
933int i_rnd(int i);
934
942double d_rnd(double i);
943
948bool b_rnd(void);
949
950
951
953// Testing
954
956#define EPSILON 0.00000001
957
959bool base_test_equal_b(const char *file, int line, bool a, bool e);
960
962bool base_test_equal_i(const char *file, int line, int a, int e);
963
965bool base_test_within_d(const char *file, int line, double a, double e, double epsilon);
966
968bool base_test_within_i(const char *file, int line, int a, int e, int epsilon);
969
971bool base_test_equal_c(const char *file, int line, char a, char e);
972
974bool base_test_equal_s(const char *file, int line, String a, String e);
975
977bool base_test_equal_struct(const char *file, int line,
978 Any a, Any e, AnyFn predicate);
979
980
981
983#define test_equal_b(a, e) base_test_equal_b(__FILE__, __LINE__, a, e)
984
986#define test_equal_i(a, e) base_test_equal_i(__FILE__, __LINE__, a, e)
987
989#define test_within_d(a, e, epsilon) base_test_within_d(__FILE__, __LINE__, a, e, epsilon)
990
992#define test_within_i(a, e, epsilon) base_test_within_i(__FILE__, __LINE__, a, e, epsilon)
993
995#define test_equal_c(a, e) base_test_equal_c(__FILE__, __LINE__, a, e)
996
998#define test_equal_s(a, e) base_test_equal_s(__FILE__, __LINE__, a, e)
999
1000
1001
1003#define test_equal_ca(a, e, ne) base_test_equal_ca(__FILE__, __LINE__, a, e, ne)
1004
1006#define test_equal_boa(a, e, ne) base_test_equal_ba(__FILE__, __LINE__, a, e, ne)
1007
1009#define test_equal_struct(a, e, p) base_test_equal_struct(__FILE__, __LINE__, a, e, p)
1010
1011
1012
1017void base_count_check(void);
1018
1023void base_count_success(void);
1024
1025#endif
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