]> arthur.barton.de Git - ngircd-alex.git/blob - src/portab/portabtest.c
Merge branch 'bug167-WebircIPAnoDNS' of git://arthur.barton.de/ngircd-alex
[ngircd-alex.git] / src / portab / portabtest.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Test program for portab.h and friends ;-)
17  */
18
19 #include "imp.h"
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "exp.h"
26
27 static void
28 Panic(char *Reason)
29 {
30         /* Oops, something failed!? */
31         fprintf(stderr, "Oops, test for %s failed!?\n", Reason);
32         exit(1);
33 } /* Panic */
34
35 static void
36 Check_snprintf(void)
37 {
38         char str[5];
39
40         snprintf(str, sizeof(str), "%s", "1234567890");
41         if (str[4] != '\0')
42                 Panic("snprintf NULL byte");
43         if (strlen(str) != 4)
44                 Panic("snprintf string length");
45 }
46
47 static void
48 Check_strdup(void)
49 {
50         char *ptr;
51
52         ptr = strdup("1234567890");
53         if (!ptr)
54                 Panic("strdup");
55         if (ptr[10] != '\0')
56                 Panic("strdup NULL byte");
57         if (strlen(ptr) != 10)
58                 Panic("strdup string length");
59         free(ptr);
60 }
61
62 static void
63 Check_strndup(void)
64 {
65         char *ptr;
66
67         ptr = strndup("1234567890", 5);
68         if (!ptr)
69                 Panic("strndup");
70         if (ptr[5] != '\0')
71                 Panic("strndup NULL byte");
72         if (strlen(ptr) != 5)
73                 Panic("strndup string length");
74         free(ptr);
75 }
76
77 static void
78 Check_strlcpy(void)
79 {
80         char str[5];
81
82         if (strlcpy(str, "1234567890", sizeof(str)) != 10)
83                 Panic("strlcpy return code");
84         if (str[4] != '\0')
85                 Panic("strlcpy NULL byte");
86         if (strlen(str) != 4)
87                 Panic("strlcpy string length");
88 }
89
90 static void
91 Check_strlcat(void)
92 {
93         char str[5];
94
95         if (strlcpy(str, "12", sizeof(str)) != 2)
96                 Panic("strlcpy for strlcat");
97         if (strlcat(str, "1234567890", sizeof(str)) != 12)
98                 Panic("strlcat return code");
99         if (str[4] != '\0')
100                 Panic("strlcat NULL byte");
101         if (strlen(str) != 4)
102                 Panic("strlcat string length");
103 }
104
105 static void
106 Check_strtok_r(void)
107 {
108         char *ptr, *last;
109
110         ptr = strdup("12,abc");
111
112         ptr = strtok_r(ptr, ",", &last);
113         if (!ptr)
114                 Panic("strtok_r result #1");
115         if (strcmp(ptr, "12") != 0)
116                 Panic("strtok_r token #1");
117
118         ptr = strtok_r(NULL, ",", &last);
119         if (!ptr)
120                 Panic("strtok_r result #2");
121         if (strcmp(ptr, "abc") != 0)
122                 Panic("strtok_r token #2");
123
124         ptr = strtok_r(NULL, ",", &last);
125         if (ptr)
126                 Panic("strtok_r result #3");
127 }
128
129 #ifdef PROTOTYPES
130 static void
131 Check_vsnprintf(const int Len, const char *Format, ...)
132 #else
133 static void
134 Check_vsnprintf(Len, Format, va_alist)
135 const int Len;
136 const char *Format;
137 va_dcl
138 #endif
139 {
140         char str[5];
141         va_list ap;
142
143 #ifdef PROTOTYPES
144         va_start(ap, Format);
145 #else
146         va_start(ap);
147 #endif
148         if (vsnprintf(str, sizeof(str), Format, ap) != Len)
149                 Panic("vsnprintf return code");
150         va_end(ap);
151
152         if (str[4] != '\0')
153                 Panic("vsnprintf NULL byte");
154         if (strlen(str) != 4)
155                 Panic("vsnprintf string length");
156 }
157
158 GLOBAL int
159 main(void)
160 {
161         /* validate datatypes */
162         if (false != 0)
163                 Panic("false");
164         if (true != 1)
165                 Panic("true");
166         if (sizeof(UINT8) != 1)
167                 Panic("UINT8");
168         if (sizeof(UINT16) != 2)
169                 Panic("UINT16");
170         if (sizeof(UINT32) != 4)
171                 Panic("UINT32");
172
173         /* check functions */
174         Check_snprintf();
175         Check_strdup();
176         Check_strndup();
177         Check_strlcpy();
178         Check_strlcat();
179         Check_strtok_r();
180         Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
181         
182         return 0;
183 }
184
185 /* -eof- */