From 2503a31938ec3325937be5b4a22a238c5911277f Mon Sep 17 00:00:00 2001
From: cuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Date: Sat, 22 Jul 2000 11:14:00 +0000
Subject: [PATCH] Added several test programs that were lying around for some
 time

git-svn-id: svn://svn.cc65.org/cc65/trunk@188 b7a2c559-68d2-44c3-8de9-860c34a00d81
---
 testcode/lib/strchr-test.c  |  59 ++++++++++++++++++
 testcode/lib/strdup-test.c  | 115 ++++++++++++++++++++++++++++++++++++
 testcode/lib/strncmp-test.c |  24 ++++++++
 3 files changed, 198 insertions(+)
 create mode 100644 testcode/lib/strchr-test.c
 create mode 100644 testcode/lib/strdup-test.c
 create mode 100644 testcode/lib/strncmp-test.c

diff --git a/testcode/lib/strchr-test.c b/testcode/lib/strchr-test.c
new file mode 100644
index 000000000..ed5b73f88
--- /dev/null
+++ b/testcode/lib/strchr-test.c
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+							
+/* Test string. Must NOT have duplicate characters! */
+static char S[] = "Helo wrd!\n";
+
+static char Found[256];
+
+
+
+int main (void)
+{
+    unsigned Len;
+    unsigned I;
+    char*    P;
+
+    /* Print a header */
+    printf ("strchr(): ");
+
+    /* Get the length of the string */
+    Len = strlen (S);
+
+    /* Search for all characters in the string, including the terminator */
+    for (I = 0; I < Len+1; ++I) {
+
+	/* Search for this char */
+	P = strchr (S, S[I]);
+
+	/* Check if we found it */
+	if (P == 0 || (P - S) != I) {
+	    printf ("Failed for code 0x%02X, offset %u!\n", S[I], I);
+	    printf ("P = %04X offset = %04X\n", P, P-S);
+	    exit (EXIT_FAILURE);
+	}
+
+	/* Mark the char as checked */
+	Found[S[I]] = 1;
+    }
+
+    /* Search for all other characters and make sure they aren't found */
+    for (I = 0; I < 256; ++I) {
+	if (Found[I] == 0) {
+	    if (strchr (S, (char)I) != 0) {
+		printf ("Failed for code 0x%02X\n", I);
+		exit (EXIT_FAILURE);
+	    }
+	}
+    }
+
+    /* Test passed */
+    printf ("Passed\n");
+    return EXIT_SUCCESS;
+}
+
+
+
diff --git a/testcode/lib/strdup-test.c b/testcode/lib/strdup-test.c
new file mode 100644
index 000000000..535ec9caa
--- /dev/null
+++ b/testcode/lib/strdup-test.c
@@ -0,0 +1,115 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+
+/* From _heap.h */
+extern unsigned	_horg;       	/* Bottom of heap */
+extern unsigned _hptr;		/* Current top */
+extern unsigned _hend;		/* Upper limit */
+extern unsigned	_hfirst; 	/* First free block in list */
+extern unsigned _hlast;		/* Last free block in list */
+
+
+static unsigned char* V[256];
+
+
+
+static void ShowInfo (void)
+/* Show heap info */
+{
+    /* Count free blocks */
+    unsigned Count = 0;
+    unsigned** P = (unsigned**) _hfirst;
+    while (P) {
+	++Count;
+	P = P[1];
+    }
+    printf ("%04X  %04X  %04X  %04X  %04X %u\n",
+      	    _horg, _hptr, _hend, _hfirst, _hlast, Count);
+
+    if (Count) {
+	P = (unsigned**) _hfirst;
+	while (P) {
+	    printf ("%04X  %04X  %04X %04X(%u)\n",
+		    (unsigned) P, P[2], P[1], P[0], P[0]);
+	    P = P[1];
+	}
+	getchar ();
+    }
+}
+
+
+
+static const char* RandStr (void)
+/* Create a random string */
+{
+    static char S [300];
+    unsigned Len = (rand () & 0xFF) + (sizeof (S) - 0xFF - 1);
+    unsigned I;
+    char C;
+
+    for (I = 0; I < Len; ++I) {
+	do {
+	    C = rand() & 0xFF;
+	} while (C == 0);
+	S[I] = C;
+    }
+    S[Len] = '\0';
+
+    return S;
+}
+
+
+
+static void FillArray (void)
+/* Fill the string array */
+{
+    unsigned char I = 0;
+    do {
+       	V[I] = strdup (RandStr ());
+	++I;
+    } while (I != 0);
+}
+
+
+
+static void FreeArray (void)
+/* Free all strings in the array */
+{
+    unsigned char I = 0;
+    do {
+       	free (V[I]);
+	++I;
+    } while (I != 0);
+}
+
+
+
+int main (void)
+{
+    unsigned long T;
+
+    /* Show info at start */
+    ShowInfo ();
+
+    /* Remember the time */
+    T = clock ();
+
+    /* Do the tests */
+    FillArray ();
+    ShowInfo ();
+    FreeArray ();
+    ShowInfo ();
+
+    /* Calculate the time and print it */
+    T = clock () - T;
+    printf ("Time needed: %lu ticks\n", T);
+
+    /* Done */
+    return EXIT_SUCCESS;
+}
+
+
+
diff --git a/testcode/lib/strncmp-test.c b/testcode/lib/strncmp-test.c
new file mode 100644
index 000000000..453eb88fe
--- /dev/null
+++ b/testcode/lib/strncmp-test.c
@@ -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;
+}
+