Added "strrchr" optimizaion a matching unit test and tiny unit test framework. (Documentation for that will follow later)

This commit is contained in:
IrgendwerA8
2017-02-26 20:03:05 +01:00
parent 6afcc370ed
commit c240d42a9e
4 changed files with 167 additions and 83 deletions

View File

@@ -1,5 +1,4 @@
#include <stdio.h>
#include <stdlib.h>
#include <unittest.h>
#include <string.h>
#define SourceStringSize 257 // test correct page passing (>256)
@@ -8,65 +7,38 @@ static char SourceString[SourceStringSize+1]; // +1 room for terminating null
static char DestinationString[2*SourceStringSize+1]; // will contain two times the source buffer
int main (void)
TEST
{
unsigned i,j;
char* p;
/* Print a header */
printf ("strcat(): ");
for (i=0; i < SourceStringSize; ++i)
SourceString[i] = (i%128)+1;
SourceString[i] = 0;
if (strlen(SourceString) != SourceStringSize)
{
printf ("Fail: Source string initialization or 'strlen()' problem!\n");
printf ("Expected length: %u but is %u!\n", SourceStringSize, strlen(SourceString));
exit (EXIT_FAILURE);
}
ASSERT_AreEqual(SourceStringSize, strlen(SourceString), "%u", "Source string initialization or 'strlen()' problem!");
/* Ensure empty destination string */
DestinationString[0] = 0;
if (strlen(DestinationString) != 0)
{
printf ("Fail: Destination string initialization or 'strlen()' problem!\n");
printf ("Expected length: %u but is %u!\n", 0, strlen(DestinationString));
exit (EXIT_FAILURE);
}
ASSERT_AreEqual(0, strlen(DestinationString), "%u", "Destination string initialization or 'strlen()' problem!");
/* Test concatenation to empty buffer */
p = strcat(DestinationString, SourceString);
if (strlen(DestinationString) != SourceStringSize)
{
printf ("Fail: String concatenation to empty buffer!\n");
printf ("Expected length: %u but is %u!\n", SourceStringSize, strlen(DestinationString));
exit (EXIT_FAILURE);
}
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);
if (strlen(DestinationString) != 2*SourceStringSize)
{
printf ("Fail: String concatenation to non-empty buffer!\n");
printf ("Expected length: %u but is %u!\n", 2*SourceStringSize, strlen(DestinationString));
exit (EXIT_FAILURE);
}
ASSERT_AreEqual(2*SourceStringSize, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to non-empty buffer!");
/* Test return value */
if (p != DestinationString)
{
printf ("Invalid return value!\n");
exit (EXIT_FAILURE);
}
ASSERT_IsTrue(p == DestinationString,"Invalid return value!");
/* Test contents */
@@ -76,18 +48,9 @@ int main (void)
unsigned position = j*SourceStringSize+i;
unsigned current = DestinationString[position];
unsigned expected = (i%128)+1;
if (current != expected)
{
printf ("Fail: Unexpected destination buffer contents at position %u!\n", position);
printf ("Expected %u, but is %u!\n", expected, current);
exit (EXIT_FAILURE);
}
ASSERT_AreEqual(expected, current, "%u", "Unexpected destination buffer contents at position %u!\n" COMMA position);
}
/* Test passed */
printf ("Passed\n");
return EXIT_SUCCESS;
}
ENDTEST

View File

@@ -0,0 +1,38 @@
#include <unittest.h>
#include <string.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