move a bunch of tests from testcode/lib to test/val (and a failing one to test/todo)
This commit is contained in:
45
test/val/atoi-test.c
Normal file
45
test/val/atoi-test.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/* A small test for atoi. Assumes twos complement */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
|
||||
#define outfile stderr
|
||||
|
||||
|
||||
|
||||
static unsigned int Failures = 0;
|
||||
|
||||
|
||||
|
||||
static void CheckAtoi (const char* Str, int Val)
|
||||
{
|
||||
int Res = atoi (Str);
|
||||
if (Res != Val) {
|
||||
fprintf (outfile, "atoi error in \"%s\":\n"
|
||||
" result = %d, should be %d\n", Str, Res, Val);
|
||||
++Failures;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
CheckAtoi ("\t +0A", 0);
|
||||
CheckAtoi ("\t -0.123", 0);
|
||||
CheckAtoi (" -32 ", -32);
|
||||
CheckAtoi (" +32 ", 32);
|
||||
CheckAtoi ("0377", 377);
|
||||
CheckAtoi (" 0377 ", 377);
|
||||
CheckAtoi (" +0377 ", 377);
|
||||
CheckAtoi (" -0377 ", -377);
|
||||
CheckAtoi ("0x7FFF", 0);
|
||||
CheckAtoi (" +0x7FFF", 0);
|
||||
CheckAtoi (" -0x7FFF", 0);
|
||||
fprintf (outfile, "Failures: %u\n", Failures);
|
||||
return (Failures != 0);
|
||||
}
|
||||
136
test/val/shift-test.c
Normal file
136
test/val/shift-test.c
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
|
||||
static unsigned UnsignedShiftLeft1 (unsigned Val)
|
||||
/* Shift an unsigned left by 1 */
|
||||
{
|
||||
__AX__ = Val;
|
||||
asm ("stx tmp1");
|
||||
asm ("asl a");
|
||||
asm ("rol tmp1");
|
||||
asm ("ldx tmp1");
|
||||
return __AX__;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static unsigned UnsignedShiftRight1 (unsigned Val)
|
||||
/* Shift an unsigned right by 1 */
|
||||
{
|
||||
__AX__ = Val;
|
||||
asm ("stx tmp1");
|
||||
asm ("lsr tmp1");
|
||||
asm ("ror a");
|
||||
asm ("ldx tmp1");
|
||||
return __AX__;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int SignedShiftRight1 (int Val)
|
||||
/* Shift a signed right by 1 */
|
||||
{
|
||||
__AX__ = Val;
|
||||
asm ("stx tmp1");
|
||||
asm ("cpx #$80");
|
||||
asm ("ror tmp1");
|
||||
asm ("ror a");
|
||||
asm ("ldx tmp1");
|
||||
return __AX__;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void TestUnsignedLeftShift (void)
|
||||
/* Test left shift. This is identical for signed and unsigned ints */
|
||||
{
|
||||
unsigned L, R, V;
|
||||
printf ("Testing unsigned left shift:\n");
|
||||
L = 0;
|
||||
do {
|
||||
V = L;
|
||||
for (R = 0; R < 16; ++R) {
|
||||
/* Check it */
|
||||
if ((L << R) != V) {
|
||||
fprintf (stderr,
|
||||
"Failed: %u << %u != %u (%u)\n",
|
||||
L, R, V, L << R);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
V = UnsignedShiftLeft1 (V);
|
||||
}
|
||||
if ((L & 0xFF) == 0) {
|
||||
printf ("%04X ", L);
|
||||
}
|
||||
} while (++L != 0);
|
||||
printf ("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void TestUnsignedRightShift (void)
|
||||
/* Test unsigned right shift. */
|
||||
{
|
||||
unsigned L, R, V;
|
||||
printf ("Testing unsigned right shift:\n");
|
||||
L = 0;
|
||||
do {
|
||||
V = L;
|
||||
for (R = 0; R < 16; ++R) {
|
||||
/* Check it */
|
||||
if ((L >> R) != V) {
|
||||
fprintf (stderr,
|
||||
"Failed: %u >> %u != %u (%u)\n",
|
||||
L, R, V, L >> R);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
V = UnsignedShiftRight1 (V);
|
||||
}
|
||||
if ((L & 0xFF) == 0) {
|
||||
printf ("%04X ", L);
|
||||
}
|
||||
} while (++L != 0);
|
||||
printf ("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void TestSignedRightShift (void)
|
||||
/* Test signed right shift. */
|
||||
{
|
||||
int L, R, V;
|
||||
printf ("Testing signed right shift:\n");
|
||||
L = 0;
|
||||
do {
|
||||
V = L;
|
||||
for (R = 0; R < 16; ++R) {
|
||||
/* Check it */
|
||||
if ((L >> R) != V) {
|
||||
fprintf (stderr,
|
||||
"Failed: %d >> %d != %d (%d)\n",
|
||||
L, R, V, L >> R);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
V = SignedShiftRight1 (V);
|
||||
}
|
||||
if ((L & 0xFF) == 0) {
|
||||
printf ("%04X ", L);
|
||||
}
|
||||
} while (++L != 0);
|
||||
printf ("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
TestUnsignedLeftShift ();
|
||||
TestUnsignedRightShift ();
|
||||
TestSignedRightShift ();
|
||||
printf ("\nOk!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
29
test/val/signal-test.c
Normal file
29
test/val/signal-test.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
|
||||
|
||||
void __fastcall__ sighandler (int sig)
|
||||
{
|
||||
printf ("Got signal #%d\n", sig);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
if (signal (SIGSEGV, sighandler) == SIG_ERR) {
|
||||
printf ("signal failure %d: %s\n", errno, strerror (errno));
|
||||
return 1;
|
||||
}
|
||||
printf ("About to raise SIGSEGV...\n");
|
||||
raise (SIGSEGV);
|
||||
printf ("Back from signal handler\n");
|
||||
printf ("About to raise SIGILL...\n");
|
||||
raise (SIGILL);
|
||||
printf ("Back from signal handler\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
144
test/val/snprintf-test.c
Normal file
144
test/val/snprintf-test.c
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
** Test a function that formats and writes characters into a string buffer.
|
||||
** This program does not test formatting. It tests some behaviors that are
|
||||
** specific to the buffer. It tests that certain conditions are handled
|
||||
** properly.
|
||||
**
|
||||
** 2015-07-17, Greg King
|
||||
*/
|
||||
|
||||
#include <conio.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char format[] = "1234567890\nabcdefghijklmnopqrstuvwxyz\n%u\n%s\n\n";
|
||||
#define FORMAT_SIZE (sizeof format - 2u - 2u - 1u)
|
||||
|
||||
#define arg1 12345u
|
||||
#define ARG1_SIZE (5u)
|
||||
|
||||
static const char arg2[] = "!@#$%^&*()-+";
|
||||
#define ARG2_SIZE (sizeof arg2 - 1u)
|
||||
|
||||
#define STRING_SIZE (FORMAT_SIZE + ARG1_SIZE + ARG2_SIZE)
|
||||
|
||||
static char buf[256];
|
||||
static int size;
|
||||
|
||||
|
||||
static void fillbuf(void)
|
||||
{
|
||||
memset(buf, 0xFF, sizeof buf - 1u);
|
||||
buf[sizeof buf - 1u] = '\0';
|
||||
}
|
||||
|
||||
|
||||
unsigned char main(void)
|
||||
{
|
||||
static unsigned char failures = 0;
|
||||
|
||||
/* Show what sprintf() should create. */
|
||||
|
||||
if ((size = printf(format, arg1, arg2)) != STRING_SIZE) {
|
||||
++failures;
|
||||
printf("printf() gave the wrong size: %d.\n", size);
|
||||
}
|
||||
|
||||
/* Test the normal behavior of sprintf(). */
|
||||
|
||||
fillbuf();
|
||||
size = sprintf(buf, format, arg1, arg2);
|
||||
fputs(buf, stdout);
|
||||
if (size != STRING_SIZE) {
|
||||
++failures;
|
||||
printf("sprintf() gave the wrong size: %d.\n", size);
|
||||
}
|
||||
|
||||
/* Test the normal behavior of snprintf(). */
|
||||
|
||||
fillbuf();
|
||||
size = snprintf(buf, sizeof buf, format, arg1, arg2);
|
||||
fputs(buf, stdout);
|
||||
if (size != STRING_SIZE) {
|
||||
++failures;
|
||||
printf("snprintf(sizeof buf) gave the wrong size:\n %d.\n", size);
|
||||
}
|
||||
|
||||
/* Does snprintf() return the full-formatted size even when the buffer
|
||||
** is short? Does it write beyond the end of that buffer?
|
||||
*/
|
||||
|
||||
fillbuf();
|
||||
size = snprintf(buf, STRING_SIZE - 5u, format, arg1, arg2);
|
||||
if (size != STRING_SIZE) {
|
||||
++failures;
|
||||
printf("snprintf(STRING_SIZE-5) gave the wrong size:\n %d.\n", size);
|
||||
}
|
||||
if (buf[STRING_SIZE - 5u - 1u] != '\0' || buf[STRING_SIZE - 5u] != 0xFF) {
|
||||
++failures;
|
||||
printf("snprintf(STRING_SIZE-5) wrote beyond\n the end of the buffer.\n");
|
||||
}
|
||||
|
||||
/* Does snprintf() detect a buffer size that is too big? */
|
||||
|
||||
fillbuf();
|
||||
errno = 0;
|
||||
size = snprintf(buf, 0x8000, format, arg1, arg2);
|
||||
if (size >= 0) {
|
||||
++failures;
|
||||
printf("snprintf(0x8000) didn't give an error:\n %d; errno=%d.\n", size, errno);
|
||||
} else {
|
||||
printf("snprintf(0x8000) did give an error:\n errno=%d.\n", errno);
|
||||
}
|
||||
if (buf[0] != 0xFF) {
|
||||
++failures;
|
||||
printf("snprintf(0x8000) wrote into the buffer.\n");
|
||||
}
|
||||
|
||||
/* snprintf() must measure the length of the formatted output even when the
|
||||
** buffer size is zero. But, it must not touch the buffer.
|
||||
*/
|
||||
|
||||
fillbuf();
|
||||
size = snprintf(buf, 0, format, arg1, arg2);
|
||||
if (size != STRING_SIZE) {
|
||||
++failures;
|
||||
printf("snprintf(0) gave the wrong size:\n %d.\n", size);
|
||||
}
|
||||
if (buf[0] != 0xFF) {
|
||||
++failures;
|
||||
printf("snprintf(0) wrote into the buffer.\n");
|
||||
}
|
||||
|
||||
/* Does sprintf() detect a zero buffer-pointer? */
|
||||
|
||||
errno = 0;
|
||||
size = sprintf(NULL, format, arg1, arg2);
|
||||
if (size >= 0) {
|
||||
++failures;
|
||||
printf("sprintf(NULL) didn't give an error:\n %d; errno=%d.\n", size, errno);
|
||||
} else {
|
||||
printf("sprintf(NULL) did give an error:\n errno=%d.\n", errno);
|
||||
}
|
||||
|
||||
/* snprintf() must measure the length of the formatted output even when the
|
||||
** buffer size is zero. A zero pointer is not an error, in that case.
|
||||
*/
|
||||
|
||||
size = snprintf(NULL, 0, format, arg1, arg2);
|
||||
if (size != STRING_SIZE) {
|
||||
++failures;
|
||||
printf("snprintf(NULL,0) gave the wrong size:\n %d.\n", size);
|
||||
}
|
||||
|
||||
if (failures != 0) {
|
||||
printf("There were %u", failures);
|
||||
} else {
|
||||
printf("There were no");
|
||||
}
|
||||
printf(" failures.\nTap a key. ");
|
||||
cgetc();
|
||||
|
||||
return failures;
|
||||
}
|
||||
24
test/val/strncmp-test.c
Normal file
24
test/val/strncmp-test.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char S1[] = {
|
||||
'h', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0', 'A'
|
||||
};
|
||||
|
||||
static const char S2[] = {
|
||||
'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0', 'B'
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
char I;
|
||||
for (I = 0; I < 20; ++I) {
|
||||
printf ("%02d: %d\n", I, strncmp (S1, S2, I));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
72
test/val/strnicmp-test.c
Normal file
72
test/val/strnicmp-test.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <conio.h>
|
||||
|
||||
static int do_test(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
printf("strnicmp(\"%s\", \"%s\", %d): ", s1, s2, (int)n);
|
||||
return strncasecmp(s1, s2, n);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = do_test("Wurzl", "wURZL", 5);
|
||||
if (ret)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("Wurzl", "wURZL", 6);
|
||||
if (ret)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("Wurzl", "wURZL", 10);
|
||||
if (ret)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("Wurzla", "wURZLB", 10);
|
||||
if (ret >= 0)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("Wurzla", "wURZLb", 5);
|
||||
if (ret)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("BLI", "bla", 5);
|
||||
if (ret <= 0)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("", "bla", 5);
|
||||
if (ret >= 0)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("BLI", "", 5);
|
||||
if (ret <= 0)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
ret = do_test("", "", 5);
|
||||
if (ret)
|
||||
printf("fail (%d)\n", ret);
|
||||
else
|
||||
printf("OK (%d)\n", ret);
|
||||
|
||||
cgetc();
|
||||
return 0;
|
||||
}
|
||||
26
test/val/strpbrk-test.c
Normal file
26
test/val/strpbrk-test.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char fox[] = "The quick brown fox jumped over the lazy dogs.";
|
||||
|
||||
void main (void)
|
||||
{
|
||||
printf ("Testing strpbrk():\n");
|
||||
if (strpbrk (fox, "qwerty") != &fox[2]) {
|
||||
printf ("\nThe first 'e' wasn't found.\n");
|
||||
}
|
||||
if (strpbrk (fox, "QWERTY") != &fox[0]) {
|
||||
printf ("The 'T' wasn't found.\n");
|
||||
}
|
||||
if (strpbrk (fox, "asdfg") != &fox[16]) {
|
||||
printf ("The 'f' wasn't found.\n");
|
||||
}
|
||||
if (strpbrk (fox, "nxv,zmb") != &fox[10]) {
|
||||
printf ("The 'b' wasn't found.\n");
|
||||
}
|
||||
if (strpbrk (fox, "!@#$%^&*()-+=[];:',/?<>.") != &fox[45]) {
|
||||
printf ("The '.' wasn't found.\n");
|
||||
}
|
||||
|
||||
printf ("\nFinished.\n");
|
||||
}
|
||||
145
test/val/strtol-test.c
Normal file
145
test/val/strtol-test.c
Normal file
@@ -0,0 +1,145 @@
|
||||
/* A small test for strtol. Assumes twos complement */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
|
||||
#define outfile stderr
|
||||
|
||||
|
||||
|
||||
#define ERROR 0
|
||||
#define OK 1
|
||||
|
||||
|
||||
|
||||
static unsigned int Failures = 0;
|
||||
|
||||
|
||||
|
||||
static void IncStr (char* Buf)
|
||||
/* Increment a number represented as a string by one. The string MUST not
|
||||
** start with a '9', we cannot handle overflow in this case.
|
||||
*/
|
||||
{
|
||||
int Len = strlen (Buf);
|
||||
|
||||
while (--Len >= 0) {
|
||||
switch (Buf[Len]) {
|
||||
case '9':
|
||||
Buf[Len] = '0';
|
||||
break;
|
||||
|
||||
default:
|
||||
++(Buf[Len]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void CheckStrToL (const char* Str, int Base, long Val, unsigned char Ok)
|
||||
{
|
||||
char* EndPtr;
|
||||
long Res = strtol (Str, &EndPtr, Base);
|
||||
if (Ok) {
|
||||
if (Res != Val) {
|
||||
fprintf (outfile,
|
||||
"strtol error in \"%s\":\n"
|
||||
" result = %ld, should be %ld, chars = %d\n",
|
||||
Str, Res, Val, EndPtr - Str);
|
||||
++Failures;
|
||||
}
|
||||
} else {
|
||||
if (errno != ERANGE) {
|
||||
fprintf (outfile,
|
||||
"strtol error in \"%s\":\n"
|
||||
" should not convert, but errno = %d\n",
|
||||
Str, errno);
|
||||
++Failures;
|
||||
}
|
||||
if (Res != Val) {
|
||||
fprintf (outfile,
|
||||
"strtol error in \"%s\":\n"
|
||||
" result = %ld, should be %ld, chars = %d\n",
|
||||
Str, Res, Val, EndPtr - Str);
|
||||
++Failures;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
char Buf[80];
|
||||
|
||||
/* Prefixed allowed if base = 0 */
|
||||
CheckStrToL ("\t 0x10G ", 0, 16L, OK);
|
||||
CheckStrToL ("\t 0X10G ", 0, 16L, OK);
|
||||
CheckStrToL (" \t0377\t", 0, 255L, OK);
|
||||
CheckStrToL (" 377", 0, 377L, OK);
|
||||
|
||||
CheckStrToL ("\t -0x10G ", 0, -16L, OK);
|
||||
CheckStrToL ("\t -0X10G ", 0, -16L, OK);
|
||||
CheckStrToL (" \t-0377\t", 0, -255L, OK);
|
||||
CheckStrToL (" -377", 0, -377L, OK);
|
||||
|
||||
/* No prefixes if base = 10 */
|
||||
CheckStrToL ("\t 1234 ", 10, 1234L, OK);
|
||||
CheckStrToL ("\t -1234 ", 10, -1234L, OK);
|
||||
CheckStrToL ("\t -0x10G ", 10, 0L, OK);
|
||||
CheckStrToL ("\t -0X10G ", 10, 0L, OK);
|
||||
CheckStrToL (" \t-0377\t", 10, -377L, OK);
|
||||
CheckStrToL (" 0377", 10, 377L, OK);
|
||||
|
||||
/* 0x prefix is allowed if base = 16 */
|
||||
CheckStrToL ("\t 0x1234 ", 16, 0x1234L, OK);
|
||||
CheckStrToL ("\t -0x1234 ", 16, -0x1234L, OK);
|
||||
CheckStrToL ("\t -010G ", 16, -16L, OK);
|
||||
CheckStrToL ("\t 10G ", 16, 16L, OK);
|
||||
|
||||
/* Check LONG_MIN and LONG_MAX */
|
||||
sprintf (Buf, "%ld", LONG_MIN);
|
||||
CheckStrToL (Buf, 0, LONG_MIN, OK);
|
||||
sprintf (Buf, "%ld", LONG_MAX);
|
||||
CheckStrToL (Buf, 0, LONG_MAX, OK);
|
||||
|
||||
/* Check value one smaller */
|
||||
sprintf (Buf+1, "%ld", LONG_MIN);
|
||||
Buf[1] = '0'; /* Overwrite '-' */
|
||||
IncStr (Buf+1);
|
||||
if (Buf[1] == '0') {
|
||||
Buf[1] = '-';
|
||||
Buf[0] = ' ';
|
||||
} else {
|
||||
Buf[0] = '-';
|
||||
}
|
||||
CheckStrToL (Buf, 0, LONG_MIN, ERROR);
|
||||
|
||||
/* Check value one larger */
|
||||
sprintf (Buf+1, "%ld", LONG_MAX);
|
||||
Buf[0] = '0';
|
||||
IncStr (Buf);
|
||||
if (Buf[0] == '0') {
|
||||
Buf[0] = ' ';
|
||||
}
|
||||
CheckStrToL (Buf, 0, LONG_MAX, ERROR);
|
||||
|
||||
/* Check numbers that are much too large or small */
|
||||
CheckStrToL ("-999999999999999999999999999999999999999999999999999999999", 0, LONG_MIN, ERROR);
|
||||
CheckStrToL ("+999999999999999999999999999999999999999999999999999999999", 0, LONG_MAX, ERROR);
|
||||
CheckStrToL (" 999999999999999999999999999999999999999999999999999999999", 0, LONG_MAX, ERROR);
|
||||
|
||||
/* Check a few other bases */
|
||||
CheckStrToL ("aBcD", 36, 481261L, OK);
|
||||
CheckStrToL ("zyaB", 35, 0L, ERROR);
|
||||
CheckStrToL ("zyaB", 36, 1677395L, ERROR);
|
||||
|
||||
fprintf (outfile, "Failures: %u\n", Failures);
|
||||
return (Failures != 0);
|
||||
}
|
||||
132
test/val/strtoul-test.c
Normal file
132
test/val/strtoul-test.c
Normal file
@@ -0,0 +1,132 @@
|
||||
/* A small test for strtuol. Assumes twos complement */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
|
||||
#define outfile stderr
|
||||
|
||||
|
||||
|
||||
#define ERROR 0
|
||||
#define OK 1
|
||||
|
||||
|
||||
|
||||
static unsigned int Failures = 0;
|
||||
|
||||
|
||||
|
||||
static void IncStr (char* Buf)
|
||||
/* Increment a number represented as a string by one. The string MUST not
|
||||
** start with a '9', we cannot handle overflow in this case.
|
||||
*/
|
||||
{
|
||||
int Len = strlen (Buf);
|
||||
|
||||
while (--Len >= 0) {
|
||||
switch (Buf[Len]) {
|
||||
case '9':
|
||||
Buf[Len] = '0';
|
||||
break;
|
||||
|
||||
default:
|
||||
++(Buf[Len]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void CheckStrToUL (const char* Str, int Base, unsigned long Val, unsigned char Ok)
|
||||
{
|
||||
char* EndPtr;
|
||||
unsigned long Res = strtoul (Str, &EndPtr, Base);
|
||||
if (Ok) {
|
||||
if (Res != Val) {
|
||||
fprintf (outfile,
|
||||
"strtol error in \"%s\":\n"
|
||||
" result = %lu, should be %lu, chars = %d\n",
|
||||
Str, Res, Val, EndPtr - Str);
|
||||
++Failures;
|
||||
}
|
||||
} else {
|
||||
if (errno != ERANGE) {
|
||||
fprintf (outfile,
|
||||
"strtol error in \"%s\":\n"
|
||||
" should not convert, but errno = %d\n",
|
||||
Str, errno);
|
||||
++Failures;
|
||||
}
|
||||
if (Res != Val) {
|
||||
fprintf (outfile,
|
||||
"strtol error in \"%s\":\n"
|
||||
" result = %lu, should be %lu, chars = %d\n",
|
||||
Str, Res, Val, EndPtr - Str);
|
||||
++Failures;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
char Buf[80];
|
||||
|
||||
/* Prefixed allowed if base = 0 */
|
||||
CheckStrToUL ("\t 0x10G ", 0, 16UL, OK);
|
||||
CheckStrToUL ("\t 0X10G ", 0, 16UL, OK);
|
||||
CheckStrToUL (" \t0377\t", 0, 255UL, OK);
|
||||
CheckStrToUL (" 377", 0, 377UL, OK);
|
||||
|
||||
CheckStrToUL ("\t -0x10G ", 0, (unsigned long) -16L, OK);
|
||||
CheckStrToUL ("\t -0X10G ", 0, (unsigned long) -16L, OK);
|
||||
CheckStrToUL (" \t-0377\t", 0, (unsigned long) -255L, OK);
|
||||
CheckStrToUL (" -377", 0, (unsigned long) -377L, OK);
|
||||
|
||||
/* No prefixes if base = 10 */
|
||||
CheckStrToUL ("\t 1234 ", 10, 1234UL, OK);
|
||||
CheckStrToUL ("\t -1234 ", 10, (unsigned long) -1234L, OK);
|
||||
CheckStrToUL ("\t -0x10G ", 10, 0UL, OK);
|
||||
CheckStrToUL ("\t -0X10G ", 10, 0UL, OK);
|
||||
CheckStrToUL (" \t-0377\t", 10, (unsigned long) -377L, OK);
|
||||
CheckStrToUL (" 0377", 10, 377UL, OK);
|
||||
|
||||
/* 0x prefix is allowed if base = 16 */
|
||||
CheckStrToUL ("\t 0x1234 ", 16, 0x1234UL, OK);
|
||||
CheckStrToUL ("\t -0x1234 ", 16, (unsigned long) -0x1234L, OK);
|
||||
CheckStrToUL ("\t -010G ", 16, (unsigned long) -16L, OK);
|
||||
CheckStrToUL ("\t 10G ", 16, 16UL, OK);
|
||||
|
||||
/* Check ULONG_MAX */
|
||||
sprintf (Buf, "%lu", ULONG_MAX);
|
||||
CheckStrToUL (Buf, 0, ULONG_MAX, OK);
|
||||
|
||||
/* Check value one larger */
|
||||
sprintf (Buf+1, "%lu", ULONG_MAX);
|
||||
Buf[0] = '0';
|
||||
IncStr (Buf);
|
||||
if (Buf[0] == '0') {
|
||||
Buf[0] = ' ';
|
||||
}
|
||||
CheckStrToUL (Buf, 0, ULONG_MAX, ERROR);
|
||||
|
||||
/* Check numbers that are much too large or small */
|
||||
CheckStrToUL ("-999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
|
||||
CheckStrToUL ("+999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
|
||||
CheckStrToUL (" 999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
|
||||
|
||||
/* Check a few other bases */
|
||||
CheckStrToUL ("aBcD", 36, 481261UL, OK);
|
||||
CheckStrToUL ("zyaB", 35, 0UL, ERROR);
|
||||
CheckStrToUL ("zyaB", 36, 1677395UL, ERROR);
|
||||
|
||||
fprintf (outfile, "Failures: %u\n", Failures);
|
||||
return (Failures != 0);
|
||||
}
|
||||
|
||||
38
test/val/time-test.c
Normal file
38
test/val/time-test.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
struct tm tm;
|
||||
time_t t;
|
||||
char buf[64];
|
||||
|
||||
|
||||
tm.tm_sec = 9;
|
||||
tm.tm_min = 34;
|
||||
tm.tm_hour = 21;
|
||||
tm.tm_mday = 12;
|
||||
tm.tm_mon = 10; /* 0..11, so this is november */
|
||||
tm.tm_year = 102; /* year - 1900, so this is 2002 */
|
||||
tm.tm_wday = 2; /* Tuesday */
|
||||
tm.tm_isdst = 0;
|
||||
|
||||
/* Convert this broken down time into a time_t and back */
|
||||
t = mktime (&tm);
|
||||
printf ("Test passes if the following lines are\n"
|
||||
"all identical:\n");
|
||||
printf ("3DD173D1 - Tue Nov 12 21:34:09 2002\n");
|
||||
printf ("%08lX - %s", t, asctime (&tm));
|
||||
printf ("%08lX - %s", t, asctime (gmtime (&t)));
|
||||
strftime (buf, sizeof (buf), "%c", &tm);
|
||||
printf ("%08lX - %s\n", t, buf);
|
||||
strftime (buf, sizeof (buf), "%a %b %d %H:%M:%S %Y", &tm);
|
||||
printf ("%08lX - %s\n", t, buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user