2022-12-06 17:03:31 +08:00
|
|
|
#include "util/FFstrbuf.h"
|
|
|
|
|
#include "util/textModifier.h"
|
2022-01-12 01:02:14 +01:00
|
|
|
|
|
|
|
|
#include <string.h>
|
2022-07-25 19:54:33 +02:00
|
|
|
#include <stdlib.h>
|
2022-12-06 17:03:31 +08:00
|
|
|
#include <stdio.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) {
|
2022-12-06 17:03:31 +08:00
|
|
|
(void)c;
|
2022-01-12 01:02:14 +01:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 17:03:31 +08:00
|
|
|
int main(void)
|
2022-01-12 01:02:14 +01:00
|
|
|
{
|
|
|
|
|
FFstrbuf strbuf;
|
2022-09-04 12:43:30 +02:00
|
|
|
|
2022-09-23 15:10:50 +08:00
|
|
|
//destroy 0
|
2023-04-15 15:29:54 +08:00
|
|
|
ffStrbufInit(&strbuf);
|
2022-09-23 15:10:50 +08:00
|
|
|
ffStrbufDestroy(&strbuf);
|
|
|
|
|
|
2022-09-04 12:43:30 +02:00
|
|
|
//initA
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
ffStrbufInit(&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);
|
|
|
|
|
|
|
|
|
|
VERIFY(!ffStrbufStartsWithC(&strbuf, '0'));
|
|
|
|
|
VERIFY(!ffStrbufEndsWithC(&strbuf, '0'));
|
|
|
|
|
VERIFY(!ffStrbufStartsWithS(&strbuf, "0"));
|
|
|
|
|
VERIFY(!ffStrbufEndsWithS(&strbuf, "0"));
|
|
|
|
|
VERIFY(ffStrbufCountC(&strbuf, '0') == 0);
|
2022-09-20 21:50:50 +08:00
|
|
|
|
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
|
|
|
|
|
|
2022-09-20 21:50:50 +08:00
|
|
|
ffStrbufAppendS(&strbuf, "12345");
|
2022-09-24 19:22:33 +08:00
|
|
|
ffStrbufAppendS(&strbuf, NULL);
|
2022-09-20 21:50:50 +08:00
|
|
|
|
2022-09-24 19:22:33 +08:00
|
|
|
VERIFY(strbuf.length == 5);
|
|
|
|
|
VERIFY(strbuf.allocated >= 6);
|
2022-12-02 22:20:01 +08:00
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "12345"));
|
2022-09-20 21:50:50 +08:00
|
|
|
|
|
|
|
|
//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);
|
2022-12-02 22:20:01 +08:00
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "123456789"));
|
2022-01-12 01:02:14 +01:00
|
|
|
|
2022-09-20 21:50:50 +08: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-20 21:50:50 +08:00
|
|
|
|
2022-09-24 19:22:33 +08:00
|
|
|
//substr
|
2022-09-20 21:50:50 +08:00
|
|
|
|
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);
|
2022-12-02 22:20:01 +08:00
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "123456789"));
|
2022-09-20 21:50:50 +08:00
|
|
|
|
2022-09-24 19:22:33 +08:00
|
|
|
//startsWithC
|
2022-09-20 21:50:50 +08:00
|
|
|
|
2022-09-24 19:22:33 +08:00
|
|
|
VERIFY(ffStrbufStartsWithC(&strbuf, '1'));
|
|
|
|
|
VERIFY(!ffStrbufStartsWithC(&strbuf, '2'));
|
2022-09-20 21:50:50 +08:00
|
|
|
|
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
|
|
|
|
|
|
2023-07-25 14:53:57 +08:00
|
|
|
ffStrbufRemoveStrings(&strbuf, 3, (const char*[]) { "23", "45", "9" });
|
2022-01-12 01:02:14 +01:00
|
|
|
|
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
|
|
|
|
2022-09-24 23:17:38 +08:00
|
|
|
//ignCase
|
|
|
|
|
ffStrbufSetS(&strbuf, "AbCdEfG");
|
|
|
|
|
|
|
|
|
|
VERIFY(ffStrbufIgnCaseCompS(&strbuf, "aBcDeFg") == 0);
|
|
|
|
|
VERIFY(ffStrbufStartsWithIgnCaseS(&strbuf, "ABCD"));
|
|
|
|
|
VERIFY(ffStrbufEndsWithIgnCaseS(&strbuf, "defg"));
|
|
|
|
|
VERIFY(!ffStrbufStartsWithIgnCaseS(&strbuf, "aBcDeFgH"));
|
|
|
|
|
VERIFY(!ffStrbufEndsWithIgnCaseS(&strbuf, "0aBcDeFg"));
|
|
|
|
|
|
|
|
|
|
//ensure
|
|
|
|
|
ffStrbufEnsureEndsWithC(&strbuf, '$');
|
2022-12-02 22:20:01 +08:00
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "AbCdEfG$"));
|
|
|
|
|
ffStrbufEnsureEndsWithC(&strbuf, '$');
|
|
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "AbCdEfG$"));
|
|
|
|
|
|
|
|
|
|
//clear
|
|
|
|
|
ffStrbufClear(&strbuf);
|
|
|
|
|
VERIFY(strbuf.allocated > 0);
|
|
|
|
|
VERIFY(strbuf.length == 0);
|
|
|
|
|
VERIFY(strbuf.chars && strbuf.chars[0] == 0);
|
2022-09-24 23:17:38 +08:00
|
|
|
|
2022-09-20 21:50:50 +08:00
|
|
|
//Destroy
|
|
|
|
|
|
|
|
|
|
ffStrbufDestroy(&strbuf);
|
|
|
|
|
|
2022-09-24 19:22:33 +08:00
|
|
|
VERIFY(strbuf.allocated == 0);
|
|
|
|
|
VERIFY(strbuf.length == 0);
|
2022-12-02 22:20:01 +08:00
|
|
|
VERIFY(strbuf.chars && strbuf.chars[0] == 0);
|
|
|
|
|
|
|
|
|
|
//initA
|
|
|
|
|
ffStrbufInitA(&strbuf, 32);
|
|
|
|
|
|
|
|
|
|
VERIFY(strbuf.allocated == 32);
|
|
|
|
|
VERIFY(strbuf.length == 0);
|
|
|
|
|
VERIFY(strbuf.chars && strbuf.chars[0] == 0);
|
|
|
|
|
|
|
|
|
|
//appendF
|
|
|
|
|
ffStrbufAppendF(&strbuf, "%s", "1234567890123456789012345678901");
|
|
|
|
|
VERIFY(strbuf.allocated == 32);
|
|
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "1234567890123456789012345678901"));
|
|
|
|
|
|
|
|
|
|
ffStrbufDestroy(&strbuf);
|
2022-09-20 21:50:50 +08:00
|
|
|
|
2022-12-06 16:26:51 +08:00
|
|
|
//initF
|
|
|
|
|
ffStrbufInitF(&strbuf, "%s", "1234567890123456789012345678901");
|
|
|
|
|
VERIFY(strbuf.allocated == 32);
|
|
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "1234567890123456789012345678901"));
|
|
|
|
|
|
2022-12-29 11:02:50 +08:00
|
|
|
//containC
|
|
|
|
|
VERIFY(ffStrbufContainC(&strbuf, '1'));
|
|
|
|
|
VERIFY(!ffStrbufContainC(&strbuf, '-'));
|
|
|
|
|
|
2023-01-02 11:49:41 +08:00
|
|
|
//replaceAllC
|
|
|
|
|
ffStrbufReplaceAllC(&strbuf, '1', '-');
|
|
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "-234567890-234567890-234567890-"));
|
|
|
|
|
ffStrbufReplaceAllC(&strbuf, '1', '-');
|
|
|
|
|
VERIFY(ffStrbufEqualS(&strbuf, "-234567890-234567890-234567890-"));
|
|
|
|
|
|
2022-12-06 16:26:51 +08:00
|
|
|
ffStrbufDestroy(&strbuf);
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
{
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY testCreate = ffStrbufCreateS("TEST");
|
|
|
|
|
VERIFY(ffStrbufEqualS(&testCreate, "TEST"));
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|