Andreas Krennmair:
[apps/madmutt.git] / test / test_muttng.c
1 /* based on MinUnit */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "test_muttng.h"
5
6 #include <lib.h>
7
8 int tests_run = 0;
9 int asserts_run = 0;
10
11 /* XXXXXXXXXXXXXXXXXXXXX */
12 /* the following things are only here to satisfy the linker */
13 int Umask;
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 */
18
19
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);
27
28   /* check whether safe_malloc()/safe_free work correctly */
29   {
30     char * ptr;
31     ptr = safe_malloc(1);
32     mu_assert("safe_malloc(1)==NULL",ptr!=NULL);
33     safe_free(&ptr);
34     mu_assert("ptr!=NULL",ptr==NULL);
35   }
36
37   /* check whether safe_strdup works correctly */
38   {
39     char * ptr;
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);
44     safe_free(&ptr);
45
46   }
47
48   /* check whether safe_strcat works correctly */
49   {
50     char buf[16] = { 0 };
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);
59   }
60
61   /* check whether safe_strncat works correctly */
62   {
63     char buf[16] = { 0 };
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);
72   }
73
74   /* check whether mutt_str_replace works correctly */
75   {
76     char * ptr = NULL;
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);
83   }
84
85   /* check whether mutt_str_adjust works correctly */
86   {
87     char * ptr = safe_strdup("some teststring");
88     mutt_str_adjust(&ptr);
89     mu_assert("ptr != \"some teststring\"",strcmp(ptr,"some teststring")==0);
90     safe_free(&ptr);
91   }
92
93   /* check whether mutt_strlower works correctly */
94   {
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);
102      safe_free(&ptr);
103   }
104
105   return 0;
106 }
107
108 static char * all_tests(void) {
109   mu_run_test(test_lib);
110   return 0;
111 }
112
113 int main(int argc, char **argv) {
114   char *result = all_tests();
115   if (result != 0) {
116     printf("ERROR: %s\n", result);
117   } else {
118     printf("ALL TESTS PASSED\n");
119   }
120   printf("Tests run: %d\nAssertions checked: %d\n", tests_run,asserts_run);
121   return result != 0;
122 }