Files
fastfetch/tests/strbuf.c
T

170 lines
4.4 KiB
C
Raw Normal View History

2022-01-12 01:02:14 +01:00
#include "fastfetch.h"
#include <string.h>
2022-07-25 19:54:33 +02:00
#include <stdlib.h>
2022-01-12 01:02:14 +01:00
2022-09-24 19:22:33 +08:00
__attribute__((__noreturn__))
static void testFailed(const FFstrbuf* strbuf, const char* expression, int lineNo)
2022-01-12 01:02:14 +01:00
{
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stderr);
2022-09-24 19:22:33 +08:00
fprintf(stderr, "[%d] %s, strbuf:", lineNo, expression);
2022-01-12 01:02:14 +01:00
ffStrbufWriteTo(strbuf, stderr);
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stderr);
fputc('\n', stderr);
2022-09-24 19:22:33 +08:00
exit(1);
}
#define VERIFY(expression) if(!(expression)) testFailed(&strbuf, #expression, __LINE__)
int shouldNotBeCalled(int c) {
FF_UNUSED(c);
2022-01-12 01:02:14 +01:00
exit(1);
}
int main(int argc, char** argv)
{
FF_UNUSED(argc, argv)
FFstrbuf strbuf;
2022-09-04 12:43:30 +02:00
2022-09-23 15:10:50 +08:00
//destroy 0
ffStrbufInitA(&strbuf, 0);
ffStrbufDestroy(&strbuf);
2022-09-04 12:43:30 +02:00
//initA
ffStrbufInitA(&strbuf, 0);
2022-01-12 01:02:14 +01:00
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.chars[0] == 0);
VERIFY(strbuf.allocated == 0);
VERIFY(strbuf.length == 0);
VERIFY(!ffStrbufStartsWithC(&strbuf, '0'));
VERIFY(!ffStrbufEndsWithC(&strbuf, '0'));
VERIFY(!ffStrbufStartsWithS(&strbuf, "0"));
VERIFY(!ffStrbufEndsWithS(&strbuf, "0"));
VERIFY(ffStrbufCountC(&strbuf, '0') == 0);
2022-09-24 19:22:33 +08:00
//Ensure following functions work with non-allocated string
ffStrbufAppendS(&strbuf, "");
ffStrbufAppendF(&strbuf, "%s", "");
ffStrbufAppendTransformS(&strbuf, "", shouldNotBeCalled);
ffStrbufPrependS(&strbuf, "");
ffStrbufTrim(&strbuf, ' ');
2022-01-12 01:02:14 +01:00
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.chars[0] == 0);
VERIFY(strbuf.allocated == 0);
VERIFY(strbuf.length == 0);
2022-01-12 01:02:14 +01:00
2022-09-04 12:43:30 +02:00
//appendS
ffStrbufAppendS(&strbuf, "12345");
2022-09-24 19:22:33 +08:00
ffStrbufAppendS(&strbuf, NULL);
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.length == 5);
VERIFY(strbuf.allocated >= 6);
VERIFY(ffStrbufCompS(&strbuf, "12345") == 0);
//appendNS
ffStrbufAppendNS(&strbuf, 4, "67890");
2022-09-24 19:22:33 +08:00
ffStrbufAppendNS(&strbuf, 0, NULL);
2022-01-12 01:02:14 +01:00
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.length == 9);
VERIFY(strbuf.allocated >= 10);
VERIFY(ffStrbufCompS(&strbuf, "123456789") == 0);
2022-01-12 01:02:14 +01:00
//appendS long
ffStrbufAppendS(&strbuf, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.length == 109);
VERIFY(strbuf.allocated >= 110);
VERIFY(strbuf.chars[strbuf.length] == 0);
2022-09-24 19:22:33 +08:00
//substr
2022-09-24 19:22:33 +08:00
ffStrbufSubstrBefore(&strbuf, 9);
VERIFY(strbuf.length == 9);
VERIFY(strbuf.allocated >= 110);
VERIFY(strbuf.chars[strbuf.length] == 0);
VERIFY(ffStrbufCompS(&strbuf, "123456789") == 0);
2022-09-24 19:22:33 +08:00
//startsWithC
2022-09-24 19:22:33 +08:00
VERIFY(ffStrbufStartsWithC(&strbuf, '1'));
VERIFY(!ffStrbufStartsWithC(&strbuf, '2'));
2022-09-04 12:43:30 +02:00
//startsWithS
2022-09-24 19:22:33 +08:00
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);
2022-01-12 01:02:14 +01:00
2022-09-04 12:43:30 +02:00
//removeS
2022-01-12 01:02:14 +01:00
ffStrbufRemoveS(&strbuf, "78");
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.length == 7);
VERIFY(strcmp(strbuf.chars, "1234569") == 0);
2022-01-12 01:02:14 +01:00
2022-09-04 12:43:30 +02:00
//removeStrings
2022-01-12 01:02:14 +01:00
ffStrbufRemoveStrings(&strbuf, 3, "23", "45", "9");
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.length == 2);
VERIFY(strcmp(strbuf.chars, "16") == 0);
2022-01-12 01:02:14 +01:00
2022-09-04 12:43:30 +02:00
//PrependS
ffStrbufPrependS(&strbuf, "123");
2022-09-24 19:22:33 +08:00
ffStrbufPrependS(&strbuf, NULL);
2022-09-04 12:43:30 +02:00
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.length == 5);
VERIFY(strcmp(strbuf.chars, "12316") == 0);
2022-09-04 12:43:30 +02:00
2022-09-24 19:22:33 +08:00
//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);
2022-09-04 12:43:30 +02:00
//Destroy
ffStrbufDestroy(&strbuf);
2022-09-24 19:22:33 +08:00
VERIFY(strbuf.allocated == 0);
VERIFY(strbuf.length == 0);
VERIFY(strbuf.chars == NULL);
2022-09-04 12:43:30 +02:00
//Success
2022-01-12 01:02:14 +01:00
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
}