Annotation of ircnowd/src/portab/portabtest.c, Revision 1.1
1.1 ! tomglok 1: /*
! 2: * ngIRCd -- The Next Generation IRC Daemon
! 3: * Copyright (c)2001-2014 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 <stdarg.h>
! 20: #include <stdio.h>
! 21: #include <stdlib.h>
! 22: #include <string.h>
! 23:
! 24: int allow_severity = 0, deny_severity = 0;
! 25:
! 26: static void
! 27: Panic(char *Reason)
! 28: {
! 29: /* Oops, something failed!? */
! 30: fprintf(stderr, "Oops, test for %s failed!?\n", Reason);
! 31: exit(1);
! 32: } /* Panic */
! 33:
! 34: static void
! 35: Check_snprintf(void)
! 36: {
! 37: char str[5];
! 38:
! 39: snprintf(str, sizeof(str), "%s", "1234567890");
! 40: if (str[4] != '\0')
! 41: Panic("snprintf NULL byte");
! 42: if (strlen(str) != 4)
! 43: Panic("snprintf string length");
! 44: }
! 45:
! 46: static void
! 47: Check_strdup(void)
! 48: {
! 49: char *ptr;
! 50:
! 51: ptr = strdup("1234567890");
! 52: if (!ptr)
! 53: Panic("strdup");
! 54: if (ptr[10] != '\0')
! 55: Panic("strdup NULL byte");
! 56: if (strlen(ptr) != 10)
! 57: Panic("strdup string length");
! 58: free(ptr);
! 59: }
! 60:
! 61: static void
! 62: Check_strndup(void)
! 63: {
! 64: char *ptr;
! 65:
! 66: ptr = strndup("1234567890", 5);
! 67: if (!ptr)
! 68: Panic("strndup");
! 69: if (ptr[5] != '\0')
! 70: Panic("strndup NULL byte");
! 71: if (strlen(ptr) != 5)
! 72: Panic("strndup string length");
! 73: free(ptr);
! 74: }
! 75:
! 76: static void
! 77: Check_strlcpy(void)
! 78: {
! 79: char str[5];
! 80:
! 81: if (strlcpy(str, "1234567890", sizeof(str)) != 10)
! 82: Panic("strlcpy return code");
! 83: if (str[4] != '\0')
! 84: Panic("strlcpy NULL byte");
! 85: if (strlen(str) != 4)
! 86: Panic("strlcpy string length");
! 87: }
! 88:
! 89: static void
! 90: Check_strlcat(void)
! 91: {
! 92: char str[5];
! 93:
! 94: if (strlcpy(str, "12", sizeof(str)) != 2)
! 95: Panic("strlcpy for strlcat");
! 96: if (strlcat(str, "1234567890", sizeof(str)) != 12)
! 97: Panic("strlcat return code");
! 98: if (str[4] != '\0')
! 99: Panic("strlcat NULL byte");
! 100: if (strlen(str) != 4)
! 101: Panic("strlcat string length");
! 102: }
! 103:
! 104: static void
! 105: Check_strtok_r(void)
! 106: {
! 107: char *str, *ptr, *last;
! 108:
! 109: ptr = strdup("12,abc");
! 110: str = ptr;
! 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: free(str);
! 129: }
! 130:
! 131: #ifdef PROTOTYPES
! 132: static void
! 133: Check_vsnprintf(const int Len, const char *Format, ...)
! 134: #else
! 135: static void
! 136: Check_vsnprintf(Len, Format, va_alist)
! 137: const int Len;
! 138: const char *Format;
! 139: va_dcl
! 140: #endif
! 141: {
! 142: char str[5];
! 143: va_list ap;
! 144: int r;
! 145:
! 146: #ifdef PROTOTYPES
! 147: va_start(ap, Format);
! 148: #else
! 149: va_start(ap);
! 150: #endif
! 151: r = vsnprintf(str, sizeof(str), Format, ap);
! 152: va_end(ap);
! 153: if (r != Len) {
! 154: /* C99 states that vsnprintf() "returns the number of
! 155: * characters that would have been printed if the n were
! 156: * unlimited", but according to the Linux manual page "glibc
! 157: * until 2.0.6 would return -1 when the output was truncated",
! 158: * and other implementations (libUTIL on A/UX) even return the
! 159: * number of characters processed ... so we only test our own
! 160: * implementation and warn on errors otherwise :-/ */
! 161: #ifdef HAVE_VSNPRINTF
! 162: fprintf(stderr,
! 163: "\n ** WARNING: The vsnprintf() function of this system isn't standard\n");
! 164: fprintf(stderr,
! 165: " ** conformant and returns a WRONG result: %d (should be %d)! The test\n",
! 166: r, Len);
! 167: fprintf(stderr,
! 168: " ** result has been ignored but may lead to errors during execution!\n\n");
! 169: #else
! 170: Panic("vsnprintf return code");
! 171: #endif
! 172: }
! 173: if (str[4] != '\0')
! 174: Panic("vsnprintf NULL byte");
! 175: if (strlen(str) != 4)
! 176: Panic("vsnprintf string length");
! 177: }
! 178:
! 179: GLOBAL int
! 180: main(void)
! 181: {
! 182: /* validate datatypes */
! 183: if (false != 0)
! 184: Panic("false");
! 185: if (true != 1)
! 186: Panic("true");
! 187: if (sizeof(UINT8) != 1)
! 188: Panic("UINT8");
! 189: if (sizeof(UINT16) != 2)
! 190: Panic("UINT16");
! 191: if (sizeof(UINT32) != 4)
! 192: Panic("UINT32");
! 193:
! 194: /* check functions */
! 195: Check_snprintf();
! 196: Check_strdup();
! 197: Check_strndup();
! 198: Check_strlcpy();
! 199: Check_strlcat();
! 200: Check_strtok_r();
! 201: Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
! 202:
! 203: return 0;
! 204: }
! 205:
! 206: /* -eof- */
CVSweb