Added further optimizations and unit tests.

This commit is contained in:
IrgendwerA8
2017-03-05 02:09:12 +01:00
parent 09de875330
commit 81115aa826
19 changed files with 327 additions and 177 deletions

View File

@@ -1,56 +0,0 @@
#include <string.h>
#include "unittest.h"
#define SourceStringSize 257 // test correct page passing (>256)
static char SourceString[SourceStringSize+1]; // +1 room for terminating null
static char DestinationString[2*SourceStringSize+1]; // will contain two times the source buffer
TEST
{
unsigned i,j;
char* p;
for (i=0; i < SourceStringSize; ++i)
SourceString[i] = (i%128)+1;
SourceString[i] = 0;
ASSERT_AreEqual(SourceStringSize, strlen(SourceString), "%u", "Source string initialization or 'strlen()' problem!");
/* Ensure empty destination string */
DestinationString[0] = 0;
ASSERT_AreEqual(0, strlen(DestinationString), "%u", "Destination string initialization or 'strlen()' problem!");
/* Test concatenation to empty buffer */
strcat(DestinationString, SourceString);
ASSERT_AreEqual(SourceStringSize, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to empty buffer!");
/* Test concatenation to non empty buffer */
p = strcat(DestinationString, SourceString);
ASSERT_AreEqual(2*SourceStringSize, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to non-empty buffer!");
/* Test return value */
ASSERT_IsTrue(p == DestinationString,"Invalid return value!");
/* Test contents */
for(j=0; j <2; ++j)
for(i=0; i < SourceStringSize; ++i)
{
unsigned position = j*SourceStringSize+i;
unsigned current = DestinationString[position];
unsigned expected = (i%128)+1;
ASSERT_AreEqual(expected, current, "%u", "Unexpected destination buffer contents at position %u!\n" COMMA position);
}
}
ENDTEST

View File

@@ -1,59 +0,0 @@
#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;
}

View File

@@ -1,38 +0,0 @@
#include <string.h>
#include "unittest.h"
static char TestString[] = "01234567890123456789"; // two times the same string
static char Found[256];
TEST
{
unsigned len;
unsigned i;
char* p;
len = strlen(TestString)/2; // test only one half of the string, to find last appearance
/* Search for all characters in the string, including the terminator */
for (i = 0; i < len; ++i)
{
/* Search for this char */
p = strrchr (TestString, TestString[i]);
ASSERT_AreEqual(i+len, p-TestString, "%u", "Unexpected location of character '%c' found!" COMMA TestString[i]);
/* Mark the char as checked */
Found[TestString[i]] = 1;
}
/* Search for all other characters and make sure they aren't found */
for (i = 0; i < 256; ++i)
{
if (!Found[i])
{
p = strrchr (TestString, i);
ASSERT_IsFalse(p, "Unexpected location of character '%c' found!" COMMA TestString[i]);
}
}
}
ENDTEST

View File

@@ -1,89 +0,0 @@
/*****************************************************************************/
/* */
/* unittest.h */
/* */
/* Unit test helper macros */
/* */
/* */
/* */
/* (C) 2017 Christian Krueger */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* warranty. In no event will the authors be held liable for any damages */
/* arising from the use of this software. */
/* */
/* Permission is granted to anyone to use this software for any purpose, */
/* including commercial applications, and to alter it and redistribute it */
/* freely, subject to the following restrictions: */
/* */
/* 1. The origin of this software must not be misrepresented; you must not */
/* claim that you wrote the original software. If you use this software */
/* in a product, an acknowledgment in the product documentation would be */
/* appreciated but is not required. */
/* 2. Altered source versions must be plainly marked as such, and must not */
/* be misrepresented as being the original software. */
/* 3. This notice may not be removed or altered from any source */
/* distribution. */
/* */
/*****************************************************************************/
#ifndef _UNITTEST_H
#define _UNITTEST_H
#include <stdio.h>
#include <stdlib.h>
#ifndef COMMA
#define COMMA ,
#endif
#define TEST int main(void) \
{\
printf("%s: ",__FILE__);
#define ENDTEST printf("Passed\n"); \
return EXIT_SUCCESS; \
}
#define ASSERT_IsTrue(a,b) if (!(a)) \
{\
printf("Fail at line %d:\n",__LINE__);\
printf(b);\
printf("\n");\
printf("Expected status should be true but wasn't!\n");\
exit(EXIT_FAILURE);\
}
#define ASSERT_IsFalse(a,b) if ((a)) \
{\
printf("Fail at line %d:\n",__LINE__);\
printf(b);\
printf("\n");\
printf("Expected status should be false but wasn't!\n");\
exit(EXIT_FAILURE);\
}
#define ASSERT_AreEqual(a,b,c,d) if ((a) != (b)) \
{\
printf("Fail at line %d:\n",__LINE__);\
printf(d);\
printf("\n");\
printf("Expected value: "c", but is "c"!\n", (a), (b));\
exit(EXIT_FAILURE);\
}
#define ASSERT_AreNotEqual(a,b,c,d) if ((a) == (b)) \
{\
printf("Fail at line %d:\n",__LINE__);\
printf(d);\
printf("\n");\
printf("Expected value not: "c", but is "c"!\n", (a), (b));\
exit(EXIT_FAILURE);\
}
/* End of unittest.h */
#endif