Merge pull request #389 from IrgendwerA8/stringimprovements
Optimization of string functions (size & speed).
This commit is contained in:
65
test/val/lib_common_memmove.c
Normal file
65
test/val/lib_common_memmove.c
Normal file
@@ -0,0 +1,65 @@
|
||||
// temporarily disable optimizations altogether until a fine grain control
|
||||
// is implemented on Makefile level only disabling the compiler option -Os
|
||||
#pragma optimize (off)
|
||||
|
||||
#include <string.h>
|
||||
#include "unittest.h"
|
||||
|
||||
#define BufferSize 384 // test correct page passing (>256, multiple of 128 here)
|
||||
|
||||
static char Buffer[BufferSize+3]; // +1 to move up (and down)
|
||||
|
||||
|
||||
|
||||
TEST
|
||||
{
|
||||
unsigned i, v;
|
||||
char* p;
|
||||
|
||||
for (i=0; i < BufferSize; ++i)
|
||||
Buffer[i+1] = (i%128);
|
||||
|
||||
Buffer[0] = 255; // to check if start position is untouched
|
||||
Buffer[BufferSize+2] = 255; // to check if end position is untouched
|
||||
|
||||
// copy upwards
|
||||
p = memmove(Buffer+2, Buffer+1, BufferSize);
|
||||
|
||||
// check buffer consistency before target
|
||||
ASSERT_AreEqual(255, (unsigned)Buffer[0], "%u", "Unexpected value before range!");
|
||||
|
||||
// check buffer consistency at starting point
|
||||
ASSERT_AreEqual(0, (unsigned)Buffer[1], "%u", "Unexpected value at range start!");
|
||||
|
||||
// check buffer consistency after range
|
||||
ASSERT_AreEqual(255, (unsigned)Buffer[BufferSize+2], "%u", "Unexpected value after range!");
|
||||
|
||||
// check buffer values
|
||||
for (i=0; i < BufferSize; ++i)
|
||||
{
|
||||
ASSERT_AreEqual(i%128, (unsigned)Buffer[i+2], "%u", "Unexpected value in buffer at position %u!" COMMA i+2);
|
||||
}
|
||||
|
||||
v = Buffer[BufferSize+1]; // rember value of first untouched end-byte
|
||||
|
||||
// copy downwards
|
||||
p = memmove(Buffer+1, Buffer+2, BufferSize);
|
||||
|
||||
// check buffer consistency before target
|
||||
ASSERT_AreEqual(255, (unsigned)Buffer[0], "%u", "Unexpected value before range!");
|
||||
|
||||
// check buffer consistency at end point
|
||||
ASSERT_AreEqual(v, (unsigned)Buffer[BufferSize+1], "%u", "Unexpected value at range end!");
|
||||
|
||||
// check buffer consistency after range
|
||||
ASSERT_AreEqual(255, (unsigned)Buffer[BufferSize+2], "%u", "Unexpected value after range!");
|
||||
|
||||
// check buffer values
|
||||
for (i=0; i < BufferSize; ++i)
|
||||
{
|
||||
ASSERT_AreEqual(i%128, (unsigned)Buffer[i+1], "%u", "Unexpected value in buffer at position %u!" COMMA i+1);
|
||||
}
|
||||
}
|
||||
ENDTEST
|
||||
|
||||
|
||||
58
test/val/lib_common_strcat.c
Normal file
58
test/val/lib_common_strcat.c
Normal file
@@ -0,0 +1,58 @@
|
||||
// temporarily disable optimizations altogether until a fine grain control
|
||||
// is implemented on Makefile level only disabling the compiler option -Os
|
||||
#pragma optimize (off)
|
||||
|
||||
#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
|
||||
42
test/val/lib_common_strchr.c
Normal file
42
test/val/lib_common_strchr.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <string.h>
|
||||
#include "unittest.h"
|
||||
|
||||
|
||||
/* Test string. Must NOT have duplicate characters! */
|
||||
static char S[] = "Helo wrd!\n";
|
||||
|
||||
static char Found[256];
|
||||
|
||||
|
||||
|
||||
TEST
|
||||
{
|
||||
unsigned Len;
|
||||
unsigned I;
|
||||
char* P;
|
||||
|
||||
/* 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 */
|
||||
ASSERT_IsFalse(P == 0 || (P - S) != I, "For code 0x%02X, offset %u!\nP = %04X offset = %04X\n" COMMA S[I] COMMA I COMMA P COMMA P-S);
|
||||
/* 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)
|
||||
{
|
||||
ASSERT_IsFalse(strchr (S, (char)I), "Failed for code 0x%02X\n" COMMA I);
|
||||
}
|
||||
}
|
||||
}
|
||||
ENDTEST
|
||||
30
test/val/lib_common_strcspn.c
Normal file
30
test/val/lib_common_strcspn.c
Normal file
@@ -0,0 +1,30 @@
|
||||
// temporarily disable optimizations altogether until a fine grain control
|
||||
// is implemented on Makefile level only disabling the compiler option -Os
|
||||
#pragma optimize (off)
|
||||
|
||||
#include <string.h>
|
||||
#include "unittest.h"
|
||||
|
||||
#define EstimatedStringSize 384 // test correct page passing (>256)
|
||||
|
||||
static char EstimatedString[EstimatedStringSize+1]; // +1 room for terminating null
|
||||
static char* EmptyTestChars=""; // strlen equivalent...
|
||||
static char* TestChars="1234567890"; // we like to find numbers
|
||||
|
||||
TEST
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i=0; i < EstimatedStringSize; ++i)
|
||||
EstimatedString[i] = (i%26)+'A'; // put ABCD... into the string to be estimated
|
||||
|
||||
ASSERT_AreEqual(strlen(EstimatedString), strcspn(EstimatedString, TestChars), "%u", "Unxpected position returned for non-participant case!");
|
||||
|
||||
EstimatedString[EstimatedStringSize/2] = TestChars[strlen(TestChars-1)];
|
||||
ASSERT_AreEqual(EstimatedStringSize/2, strcspn(EstimatedString, TestChars), "%u", "Unxpected position returned for participant case!");
|
||||
|
||||
ASSERT_AreEqual(strlen(EstimatedString), strcspn(EstimatedString, EmptyTestChars), "%u", "Unxpected position returned for empty test case!");
|
||||
}
|
||||
ENDTEST
|
||||
|
||||
|
||||
58
test/val/lib_common_strncat.c
Normal file
58
test/val/lib_common_strncat.c
Normal file
@@ -0,0 +1,58 @@
|
||||
// temporarily disable optimizations altogether until a fine grain control
|
||||
// is implemented on Makefile level only disabling the compiler option -Os
|
||||
#pragma optimize (off)
|
||||
|
||||
#include <string.h>
|
||||
#include "unittest.h"
|
||||
|
||||
#define SourceStringSize 384 // test correct page passing (>256, multiple of 128 here)
|
||||
|
||||
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;
|
||||
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 "unlimted" concatenation to empty buffer */
|
||||
|
||||
strncat(DestinationString, SourceString, 1024);
|
||||
|
||||
ASSERT_AreEqual(SourceStringSize, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to empty buffer!");
|
||||
|
||||
/* Test limited concatenation to non empty buffer */
|
||||
|
||||
p = strncat(DestinationString, SourceString, 128);
|
||||
|
||||
ASSERT_AreEqual(SourceStringSize+128, 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(i=0; i < strlen(DestinationString); ++i)
|
||||
{
|
||||
unsigned current = DestinationString[i];
|
||||
unsigned expected = (i%128)+1;
|
||||
ASSERT_AreEqual(expected, current, "%u", "Unexpected destination buffer contents at position %u!\n" COMMA i);
|
||||
}
|
||||
}
|
||||
ENDTEST
|
||||
|
||||
|
||||
38
test/val/lib_common_strrchr.c
Normal file
38
test/val/lib_common_strrchr.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#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
|
||||
|
||||
|
||||
28
test/val/lib_common_strspn.c
Normal file
28
test/val/lib_common_strspn.c
Normal file
@@ -0,0 +1,28 @@
|
||||
// temporarily disable optimizations altogether until a fine grain control
|
||||
// is implemented on Makefile level only disabling the compiler option -Os
|
||||
#pragma optimize (off)
|
||||
|
||||
#include <string.h>
|
||||
#include "unittest.h"
|
||||
|
||||
#define EstimatedStringSize 384 // test correct page passing (>256)
|
||||
|
||||
static char EstimatedString[EstimatedStringSize+1]; // +1 room for terminating null
|
||||
static char* EmptyTestChars=""; // empty test case...
|
||||
static char* TestChars="1234567890"; // we like to find numbers
|
||||
|
||||
TEST
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i=0; i < EstimatedStringSize; ++i)
|
||||
EstimatedString[i] = (i%10)+'0'; // put 0123... into the string to be estimated
|
||||
|
||||
ASSERT_AreEqual(strlen(EstimatedString), strspn(EstimatedString, TestChars), "%u", "Unxpected position returned for all participant case!");
|
||||
|
||||
EstimatedString[EstimatedStringSize/2] = 'X';
|
||||
ASSERT_AreEqual(EstimatedStringSize/2, strspn(EstimatedString, TestChars), "%u", "Unxpected position returned for breaking case!");
|
||||
|
||||
ASSERT_AreEqual(0, strspn(EstimatedString, EmptyTestChars), "%u", "Unxpected position returned for empty test case!");
|
||||
}
|
||||
ENDTEST
|
||||
89
test/val/unittest.h
Normal file
89
test/val/unittest.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/*****************************************************************************/
|
||||
/* */
|
||||
/* 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
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user