mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-08 14:10:11 +01:00
FFstrbuf / FFlist: add some improvements
1. make some short fns inlinable 2. simplify implementation 3. fix possible crashes with non-allocated strbuf 4. make the default init fn allocates no memory 5. add more tests
This commit is contained in:
+29
-39
@@ -4,12 +4,11 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void testFailed(const FFlist* list, const char* message, ...)
|
||||
__attribute__((__noreturn__))
|
||||
static void testFailed(const FFlist* list, const char* expression, int lineNo)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, message);
|
||||
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stderr);
|
||||
vfprintf(stderr, message, args);
|
||||
fprintf(stderr, "[%d] %s, list:", lineNo, expression);
|
||||
for (uint32_t i = 0; i < list->length; ++i)
|
||||
{
|
||||
fprintf(stderr, "%u ", *(uint32_t*)ffListGet(list, i));
|
||||
@@ -17,10 +16,16 @@ static void testFailed(const FFlist* list, const char* message, ...)
|
||||
fputc('\n', stderr);
|
||||
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stderr);
|
||||
fputc('\n', stderr);
|
||||
va_end(args);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static bool numEqualsAdapter(const void* first, const void* second)
|
||||
{
|
||||
return *(uint32_t*)first == *(uint32_t*)second;
|
||||
}
|
||||
|
||||
#define VERIFY(expression) if(!(expression)) testFailed(&list, #expression, __LINE__)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
FF_UNUSED(argc, argv)
|
||||
@@ -29,60 +34,45 @@ int main(int argc, char** argv)
|
||||
|
||||
//initA
|
||||
|
||||
ffListInitA(&list, sizeof(uint32_t), 0);
|
||||
ffListInit(&list, sizeof(uint32_t));
|
||||
|
||||
if(ffListGet(&list, 0) != NULL)
|
||||
testFailed(&list, "ffListGet(&list, 0) != NULL");
|
||||
|
||||
if(list.elementSize != sizeof(int))
|
||||
testFailed(&list, "list.elementSize != sizeof(int)");
|
||||
|
||||
if(list.capacity != 0)
|
||||
testFailed(&list, "list.capacity != 0");
|
||||
|
||||
if(list.length != 0)
|
||||
testFailed(&list, "list.length != 0");
|
||||
VERIFY(list.elementSize == sizeof(uint32_t));
|
||||
VERIFY(list.capacity == 0);
|
||||
VERIFY(list.length == 0);
|
||||
|
||||
//add
|
||||
for (uint32_t i = 1; i <= FF_LIST_DEFAULT_ALLOC + 1; ++i)
|
||||
{
|
||||
*(uint32_t*)ffListAdd(&list) = i;
|
||||
|
||||
if(list.elementSize != sizeof(uint32_t))
|
||||
testFailed(&list, "list.elementSize != sizeof(uint32_t)");
|
||||
|
||||
if(list.length != i)
|
||||
testFailed(&list, "list.length != i");
|
||||
VERIFY(list.elementSize == sizeof(uint32_t));
|
||||
VERIFY(list.length == i);
|
||||
|
||||
if(i <= FF_LIST_DEFAULT_ALLOC)
|
||||
{
|
||||
if(list.capacity != FF_LIST_DEFAULT_ALLOC)
|
||||
testFailed(&list, "list.length != FF_LIST_DEFAULT_ALLOC");
|
||||
VERIFY(list.capacity == FF_LIST_DEFAULT_ALLOC);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(list.capacity != FF_LIST_DEFAULT_ALLOC * 2)
|
||||
testFailed(&list, "list.length != FF_LIST_DEFAULT_ALLOC * 2");
|
||||
VERIFY(list.capacity == FF_LIST_DEFAULT_ALLOC * 2);
|
||||
}
|
||||
|
||||
if(*(uint32_t*)ffListGet(&list, 0) != 1)
|
||||
testFailed(&list, "*(int*)ffListGet(&list, 0) != 1");
|
||||
|
||||
if(*(uint32_t*)ffListGet(&list, i - 1) != i)
|
||||
testFailed(&list, "*(int*)ffListGet(&list, i - 1) != i");
|
||||
VERIFY(*(uint32_t*)ffListGet(&list, 0) == 1);
|
||||
VERIFY(*(uint32_t*)ffListGet(&list, i - 1) == i);
|
||||
}
|
||||
|
||||
// ffListFirstIndexComp
|
||||
uint32_t n = 10;
|
||||
VERIFY(ffListFirstIndexComp(&list, &n, numEqualsAdapter) == 9);
|
||||
n = 999;
|
||||
VERIFY(ffListFirstIndexComp(&list, &n, numEqualsAdapter) == list.length);
|
||||
|
||||
//Destroy
|
||||
ffListDestroy(&list);
|
||||
|
||||
if(list.elementSize != sizeof(uint32_t))
|
||||
testFailed(&list, "list.elementSize != sizeof(uint32_t)");
|
||||
|
||||
if(list.capacity != 0)
|
||||
testFailed(&list, "list.capacity != 0");
|
||||
|
||||
if(list.length != 0)
|
||||
testFailed(&list, "list.length != 0");
|
||||
VERIFY(list.elementSize == sizeof(uint32_t));
|
||||
VERIFY(list.capacity == 0);
|
||||
VERIFY(list.length == 0);
|
||||
|
||||
//Success
|
||||
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
|
||||
+94
-59
@@ -1,20 +1,23 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void testFailed(const FFstrbuf* strbuf, const char* message, ...)
|
||||
__attribute__((__noreturn__))
|
||||
static void testFailed(const FFstrbuf* strbuf, const char* expression, int lineNo)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, message);
|
||||
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stderr);
|
||||
vfprintf(stderr, message, args);
|
||||
fputs(", strbuf: ", stderr);
|
||||
fprintf(stderr, "[%d] %s, strbuf:", lineNo, expression);
|
||||
ffStrbufWriteTo(strbuf, stderr);
|
||||
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stderr);
|
||||
fputc('\n', stderr);
|
||||
va_end(args);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#define VERIFY(expression) if(!(expression)) testFailed(&strbuf, #expression, __LINE__)
|
||||
|
||||
int shouldNotBeCalled(int c) {
|
||||
FF_UNUSED(c);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -32,102 +35,134 @@ int main(int argc, char** argv)
|
||||
|
||||
ffStrbufInitA(&strbuf, 0);
|
||||
|
||||
if(strbuf.chars[0] != 0) //make sure chars[0] is accessable
|
||||
testFailed(&strbuf, "strbuf.chars[0] != 0");
|
||||
VERIFY(strbuf.chars[0] == 0);
|
||||
VERIFY(strbuf.allocated == 0);
|
||||
VERIFY(strbuf.length == 0);
|
||||
|
||||
if(strbuf.allocated != 0)
|
||||
testFailed(&strbuf, "strbuf.allocated != 0");
|
||||
VERIFY(!ffStrbufStartsWithC(&strbuf, '0'));
|
||||
VERIFY(!ffStrbufEndsWithC(&strbuf, '0'));
|
||||
VERIFY(!ffStrbufStartsWithS(&strbuf, "0"));
|
||||
VERIFY(!ffStrbufEndsWithS(&strbuf, "0"));
|
||||
VERIFY(ffStrbufCountC(&strbuf, '0') == 0);
|
||||
|
||||
if(strbuf.length != 0)
|
||||
testFailed(&strbuf, "strbuf.length != 0");
|
||||
//Ensure following functions work with non-allocated string
|
||||
ffStrbufAppendS(&strbuf, "");
|
||||
ffStrbufAppendF(&strbuf, "%s", "");
|
||||
ffStrbufAppendTransformS(&strbuf, "", shouldNotBeCalled);
|
||||
ffStrbufPrependS(&strbuf, "");
|
||||
ffStrbufTrim(&strbuf, ' ');
|
||||
|
||||
VERIFY(strbuf.chars[0] == 0);
|
||||
VERIFY(strbuf.allocated == 0);
|
||||
VERIFY(strbuf.length == 0);
|
||||
|
||||
//appendS
|
||||
|
||||
ffStrbufAppendS(&strbuf, "12345");
|
||||
ffStrbufAppendS(&strbuf, NULL);
|
||||
|
||||
if(strbuf.length != 5)
|
||||
testFailed(&strbuf, "strbuf.length != 5");
|
||||
|
||||
if(strbuf.allocated < 6)
|
||||
testFailed(&strbuf, "strbuf.allocated < 6");
|
||||
|
||||
if(ffStrbufCompS(&strbuf, "12345") != 0)
|
||||
testFailed(&strbuf, "strbuf.data != \"12345\"");
|
||||
VERIFY(strbuf.length == 5);
|
||||
VERIFY(strbuf.allocated >= 6);
|
||||
VERIFY(ffStrbufCompS(&strbuf, "12345") == 0);
|
||||
|
||||
//appendNS
|
||||
|
||||
ffStrbufAppendNS(&strbuf, 4, "67890");
|
||||
ffStrbufAppendNS(&strbuf, 0, NULL);
|
||||
|
||||
if(strbuf.length != 9)
|
||||
testFailed(&strbuf, "strbuf.length != 9");
|
||||
|
||||
if(strbuf.allocated < 10)
|
||||
testFailed(&strbuf, "strbuf.allocated < 10");
|
||||
|
||||
if(ffStrbufCompS(&strbuf, "123456789") != 0)
|
||||
testFailed(&strbuf, "strbuf.data != \"123456789\"");
|
||||
VERIFY(strbuf.length == 9);
|
||||
VERIFY(strbuf.allocated >= 10);
|
||||
VERIFY(ffStrbufCompS(&strbuf, "123456789") == 0);
|
||||
|
||||
//appendS long
|
||||
|
||||
ffStrbufAppendS(&strbuf, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
|
||||
VERIFY(strbuf.length == 109);
|
||||
VERIFY(strbuf.allocated >= 110);
|
||||
VERIFY(strbuf.chars[strbuf.length] == 0);
|
||||
|
||||
if(strbuf.length != 109)
|
||||
testFailed(&strbuf, "strbuf.length != 109");
|
||||
//substr
|
||||
|
||||
if(strbuf.allocated < 110)
|
||||
testFailed(&strbuf, "strbuf.allocated < 110");
|
||||
ffStrbufSubstrBefore(&strbuf, 9);
|
||||
VERIFY(strbuf.length == 9);
|
||||
VERIFY(strbuf.allocated >= 110);
|
||||
VERIFY(strbuf.chars[strbuf.length] == 0);
|
||||
VERIFY(ffStrbufCompS(&strbuf, "123456789") == 0);
|
||||
|
||||
if(strbuf.chars[strbuf.length] != 0)
|
||||
testFailed(&strbuf, "strbuf.chars[strbuf.length] != 0");
|
||||
//startsWithC
|
||||
|
||||
strbuf.length = 9;
|
||||
VERIFY(ffStrbufStartsWithC(&strbuf, '1'));
|
||||
VERIFY(!ffStrbufStartsWithC(&strbuf, '2'));
|
||||
|
||||
//startsWithS
|
||||
|
||||
if(!ffStrbufStartsWithS(&strbuf, "123"))
|
||||
testFailed(&strbuf, "!ffStrbufStartsWithS(&strbuf, \"123\")");
|
||||
VERIFY(ffStrbufStartsWithS(&strbuf, "123"));
|
||||
VERIFY(ffStrbufStartsWithS(&strbuf, "123456789"));
|
||||
VERIFY(!ffStrbufStartsWithS(&strbuf, "1234567890123"));
|
||||
|
||||
//endsWithC
|
||||
VERIFY(ffStrbufEndsWithC(&strbuf, '9'));
|
||||
VERIFY(!ffStrbufEndsWithC(&strbuf, '1'));
|
||||
|
||||
//endsWithS
|
||||
|
||||
VERIFY(ffStrbufEndsWithS(&strbuf, "789"));
|
||||
VERIFY(ffStrbufEndsWithS(&strbuf, "123456789"));
|
||||
VERIFY(!ffStrbufEndsWithS(&strbuf, "1234567890123"));
|
||||
|
||||
//toNumber
|
||||
|
||||
VERIFY(ffStrbufToDouble(&strbuf) == 123456789.0);
|
||||
VERIFY(ffStrbufToUInt16(&strbuf, 999) == 999); //overflow
|
||||
|
||||
//countC
|
||||
|
||||
VERIFY(ffStrbufCountC(&strbuf, '1') == 1);
|
||||
VERIFY(ffStrbufCountC(&strbuf, '0') == 0);
|
||||
|
||||
//removeS
|
||||
|
||||
ffStrbufRemoveS(&strbuf, "78");
|
||||
|
||||
if(strbuf.length != 7)
|
||||
testFailed(&strbuf, "strbuf.length != 7");
|
||||
|
||||
if(strcmp(strbuf.chars, "1234569") != 0)
|
||||
testFailed(&strbuf, "strcmp(strbuf.chars, \"123469\") != 0");
|
||||
VERIFY(strbuf.length == 7);
|
||||
VERIFY(strcmp(strbuf.chars, "1234569") == 0);
|
||||
|
||||
//removeStrings
|
||||
|
||||
ffStrbufRemoveStrings(&strbuf, 3, "23", "45", "9");
|
||||
|
||||
if(strbuf.length != 2)
|
||||
testFailed(&strbuf, "strbuf.length != 2");
|
||||
|
||||
if(strcmp(strbuf.chars, "16") != 0)
|
||||
testFailed(&strbuf, "strbuf.chars != \"126\"");
|
||||
VERIFY(strbuf.length == 2);
|
||||
VERIFY(strcmp(strbuf.chars, "16") == 0);
|
||||
|
||||
//PrependS
|
||||
|
||||
ffStrbufPrependS(&strbuf, "123");
|
||||
ffStrbufPrependS(&strbuf, NULL);
|
||||
|
||||
if(strbuf.length != 5)
|
||||
testFailed(&strbuf, "strbuf.length != 5");
|
||||
VERIFY(strbuf.length == 5);
|
||||
VERIFY(strcmp(strbuf.chars, "12316") == 0);
|
||||
|
||||
if(strcmp(strbuf.chars, "12316") != 0)
|
||||
testFailed(&strbuf, "strbuf.chars != \"12316\"");
|
||||
//indexC
|
||||
VERIFY(ffStrbufFirstIndexC(&strbuf, '1') == 0);
|
||||
VERIFY(ffStrbufNextIndexC(&strbuf, 1, '1') == 3);
|
||||
VERIFY(ffStrbufNextIndexC(&strbuf, 4, '1') == 5);
|
||||
VERIFY(ffStrbufLastIndexC(&strbuf, '1') == 3);
|
||||
VERIFY(ffStrbufPreviousIndexC(&strbuf, 2, '1') == 0);
|
||||
VERIFY(ffStrbufPreviousIndexC(&strbuf, 0, '1') == 0);
|
||||
VERIFY(ffStrbufPreviousIndexC(&strbuf, 0, '2') == 5);
|
||||
|
||||
//indexS
|
||||
VERIFY(ffStrbufFirstIndexS(&strbuf, "12316") == 0);
|
||||
VERIFY(ffStrbufNextIndexS(&strbuf, 1, "1") == 3);
|
||||
VERIFY(ffStrbufNextIndexS(&strbuf, 4, "1") == 5);
|
||||
|
||||
//Destroy
|
||||
|
||||
ffStrbufDestroy(&strbuf);
|
||||
if(strbuf.allocated != 0)
|
||||
testFailed(&strbuf, "strbuf.allocated != 0");
|
||||
|
||||
if(strbuf.length != 0)
|
||||
testFailed(&strbuf, "strbuf.length != 0");
|
||||
|
||||
if(strbuf.chars != NULL)
|
||||
testFailed(&strbuf, "strbuf.chars != NULL");
|
||||
VERIFY(strbuf.allocated == 0);
|
||||
VERIFY(strbuf.length == 0);
|
||||
VERIFY(strbuf.chars == NULL);
|
||||
|
||||
//Success
|
||||
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
|
||||
Reference in New Issue
Block a user