]> arthur.barton.de Git - ngircd-alex.git/blob - src/portab/portabtest.c
portabtest: Actually test functions
[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_strlcpy(void)
49 {
50         char str[5];
51
52         if (strlcpy(str, "1234567890", sizeof(str)) != 10)
53                 Panic("strlcpy return code");
54         if (str[4] != '\0')
55                 Panic("strlcpy NULL byte");
56         if (strlen(str) != 4)
57                 Panic("strlcpy string length");
58 }
59
60 static void
61 Check_strlcat(void)
62 {
63         char str[5];
64
65         if (strlcpy(str, "12", sizeof(str)) != 2)
66                 Panic("strlcpy for strlcat");
67         if (strlcat(str, "1234567890", sizeof(str)) != 12)
68                 Panic("strlcat return code");
69         if (str[4] != '\0')
70                 Panic("strlcat NULL byte");
71         if (strlen(str) != 4)
72                 Panic("strlcat string length");
73 }
74
75 #ifdef PROTOTYPES
76 static void
77 Check_vsnprintf(const int Len, const char *Format, ...)
78 #else
79 static void
80 Check_vsnprintf(Len, Format, va_alist)
81 const int Len;
82 const char *Format;
83 va_dcl
84 #endif
85 {
86         char str[5];
87         va_list ap;
88
89 #ifdef PROTOTYPES
90         va_start(ap, Format);
91 #else
92         va_start(ap);
93 #endif
94         if (vsnprintf(str, sizeof(str), Format, ap) != Len)
95                 Panic("vsnprintf return code");
96         va_end(ap);
97
98         if (str[4] != '\0')
99                 Panic("vsnprintf NULL byte");
100         if (strlen(str) != 4)
101                 Panic("vsnprintf string length");
102 }
103
104 GLOBAL int
105 main(void)
106 {
107         /* validate datatypes */
108         if (false != 0)
109                 Panic("false");
110         if (true != 1)
111                 Panic("true");
112         if (sizeof(UINT8) != 1)
113                 Panic("UINT8");
114         if (sizeof(UINT16) != 2)
115                 Panic("UINT16");
116         if (sizeof(UINT32) != 4)
117                 Panic("UINT32");
118
119         /* check functions */
120         Check_snprintf();
121         Check_strlcpy();
122         Check_strlcat();
123         Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
124         
125         return 0;
126 }
127
128 /* -eof- */