4 #include "test_muttng.h"
11 /* XXXXXXXXXXXXXXXXXXXXX */
12 /* the following things are only here to satisfy the linker */
14 void (*mutt_error) (const char *, ...);
15 const char * gettext(const char * a) { return a; }
16 void mutt_exit(int i) { exit(i); }
17 /* XXXXXXXXXXXXXXXXXXXX */
20 /* tests the basic functions in lib.c */
21 static char * test_lib(void) {
22 /* first check whether allocating 0 bytes behaves correctly */
23 mu_assert("safe_malloc(0)!=NULL",safe_malloc(0)==NULL);
24 mu_assert("safe_calloc(0,0)!=NULL",safe_calloc(0,0)==NULL);
25 mu_assert("safe_calloc(1,0)!=NULL",safe_calloc(1,0)==NULL);
26 mu_assert("safe_calloc(0,1)!=NULL",safe_calloc(0,1)==NULL);
28 /* check whether safe_malloc()/safe_free work correctly */
32 mu_assert("safe_malloc(1)==NULL",ptr!=NULL);
34 mu_assert("ptr!=NULL",ptr==NULL);
37 /* check whether safe_strdup works correctly */
40 mu_assert("safe_strdup(NULL)!=NULL",safe_strdup(NULL)==NULL);
41 mu_assert("safe_strdup("")!=NULL",safe_strdup("")==NULL);
42 ptr = safe_strdup("teststring");
43 mu_assert("safe_strdup(\"teststring\")!=\"teststring\"",strcmp(ptr,"teststring")==0);
48 /* check whether safe_strcat works correctly */
51 safe_strcat(buf,sizeof(buf),"asdf");
52 mu_assert("buf != \"asdf\"",strcmp(buf,"asdf")==0);
53 safe_strcat(buf,sizeof(buf),"qwer");
54 mu_assert("buf != \"asdfqwer\"",strcmp(buf,"asdfqwer")==0);
55 safe_strcat(buf,sizeof(buf),"0123456789");
56 mu_assert("buf != \"asdfqwer0123456\"",strcmp(buf,"asdfqwer0123456")==0);
57 safe_strcat(buf,sizeof(buf),"trash");
58 mu_assert("buf != \"asdfqwer0123456\" (2)",strcmp(buf,"asdfqwer0123456")==0);
61 /* check whether safe_strncat works correctly */
64 safe_strncat(buf,sizeof(buf),"asdf",3);
65 mu_assert("buf != \"asd\"",strcmp(buf,"asd")==0);
66 safe_strncat(buf,sizeof(buf),"fghj",3);
67 mu_assert("buf != \"asdfgh\"",strcmp(buf,"asdfgh")==0);
68 safe_strncat(buf,sizeof(buf),"",10);
69 mu_assert("buf != \"asdfgh\" (2)",strcmp(buf,"asdfgh")==0);
70 safe_strncat(buf,sizeof(buf),"qwertzuiopyxcvvbnm",255);
71 mu_assert("buf != \"asdfghqwertzuio\"",strcmp(buf,"asdfghqwertzuio")==0);
74 /* check whether mutt_str_replace works correctly */
77 mutt_str_replace(&ptr,"foobar");
78 mu_assert("ptr != \"foobar\"",strcmp(ptr,"foobar")==0);
79 mutt_str_replace(&ptr,"quux");
80 mu_assert("ptr != \"quux\"",strcmp(ptr,"quux")==0);
81 mutt_str_replace(&ptr,NULL);
82 mu_assert("ptr != NULL",ptr==NULL);
85 /* check whether mutt_str_adjust works correctly */
87 char * ptr = safe_strdup("some teststring");
88 mutt_str_adjust(&ptr);
89 mu_assert("ptr != \"some teststring\"",strcmp(ptr,"some teststring")==0);
93 /* check whether mutt_strlower works correctly */
95 char * supposed_result = "all you need is love";
96 char * ptr = safe_strdup("ALL YOU NEED IS LOVE");
97 mu_assert("ptr != \"all you need is love\"",strcmp(mutt_strlower(ptr),supposed_result)==0);
98 mutt_str_replace(&ptr,"All You Need Is LovE");
99 mu_assert("ptr != \"all you need is love\" (2)",strcmp(mutt_strlower(ptr),supposed_result)==0);
100 mutt_str_replace(&ptr,"all you need is love");
101 mu_assert("ptr != \"all you need is love\" (3)",strcmp(mutt_strlower(ptr),supposed_result)==0);
108 static char * all_tests(void) {
109 mu_run_test(test_lib);
113 int main(int argc, char **argv) {
114 char *result = all_tests();
116 printf("ERROR: %s\n", result);
118 printf("ALL TESTS PASSED\n");
120 printf("Tests run: %d\nAssertions checked: %d\n", tests_run,asserts_run);