Initial commit, yay

This commit is contained in:
VegOwOtenks 2024-06-13 15:28:21 +02:00
commit 25e26756cd
85 changed files with 7077 additions and 0 deletions

16
tests/.cproject Normal file
View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="org.eclipse.cdt.core.default.config.1524259863">
<storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.1524259863" moduleId="org.eclipse.cdt.core.settings" name="Configuration">
<externalSettings/>
<extensions/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.pathentry">
<pathentry excluding="**/CMakeFiles/**" kind="out" path="build"/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
</cproject>

20
tests/.project Normal file
View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tests</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.core.cBuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.cmake.core.cmakeNature</nature>
</natures>
</projectDescription>

38
tests/CMakeLists.txt Normal file
View file

@ -0,0 +1,38 @@
add_executable(threadpool-test ThreadPool.test.c)
target_link_libraries(threadpool-test
ThreadPool
allocator-interface
FreeList
dynamicarray
threading
pointers)
add_executable(argumentc-test argumentc.test.c)
target_link_libraries(argumentc-test
argumentc
dynamicarray
allocator-interface
pointers
StringView)
add_executable(StringView-test StringView.test.c)
target_link_libraries(StringView-test
StringView)
add_executable(Scratchpad-test Scratchpad.test.c)
target_link_libraries(Scratchpad-test
Scratchpad
allocator-interface
pointers)
add_executable(regex-test regex.test.c)
target_link_libraries(regex-test
regex
Scratchpad
dynamicarray
allocator-interface
pointers
StringView
utf8
)

24
tests/Scratchpad.test.c Normal file
View file

@ -0,0 +1,24 @@
#include "../src/Scratchpad/Scratchpad.h"
#include <assert.h>
#include <stdlib.h>
void test_reserve(void)
{
Scratchpad pad;
assert(Scratchpad_Create(&pad, 16, NULL) == EXIT_SUCCESS);
assert(Scratchpad_Reserve(&pad, 16) != NULL);
assert(Scratchpad_Reserve(&pad, 16) != NULL);
Scratchpad_Reset(&pad);
Scratchpad_Destroy(&pad);
return;
}
int main()
{
test_reserve();
return 0;
}

36
tests/StringView.test.c Normal file
View file

@ -0,0 +1,36 @@
#include "../src/StringView/StringView.h"
#include <assert.h>
void test_split(void)
{
const char* source = "this,is,a,csv,header";
StringView sourceSV = StringView_FromString(source);
StringView delim = StringView_FromString(",");
StringView split;
assert(StringView_NextSplit(&split, &sourceSV, delim));
assert(StringView_Equal(split, StringView_FromString("this")));
assert(StringView_NextSplit(&split, &sourceSV, delim));
assert(StringView_Equal(split, StringView_FromString("is")));
assert(StringView_NextSplit(&split, &sourceSV, delim));
assert(StringView_Equal(split, StringView_FromString("a")));
assert(StringView_NextSplit(&split, &sourceSV, delim));
assert(StringView_Equal(split, StringView_FromString("csv")));
assert(StringView_NextSplit(&split, &sourceSV, delim));
assert(StringView_Equal(split, StringView_FromString("header")));
assert(! StringView_NextSplit(&split, &sourceSV, delim));
return;
}
int main()
{
test_split();
return 0;
}

88
tests/ThreadPool.test.c Normal file
View file

@ -0,0 +1,88 @@
#include "../src/ThreadPool/ThreadPool.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
void testLifetime(void)
{
allocator_t allocator;
assert(EXIT_SUCCESS == Allocator_CreateSystemAllocator(&allocator));
ThreadPool pool;
assert(EXIT_SUCCESS == ThreadPool_Create(&pool, 4, 0x00, &allocator));
ThreadPool_Destroy(&pool);
assert(allocator.reserved == 0);
Allocator_DestroySystemAllocator(&allocator);
}
void* testJobFunction(void* argument)
{
assert(argument == NULL);
return NULL;
}
void* lastJobFunction(os_semaphore_t* semaphore)
{
OSSemaphore_Release(semaphore);
return NULL;
}
void testGeneric(size_t n_workers, size_t n_jobs, bool discard_result)
{
allocator_t allocator;
assert(EXIT_SUCCESS == Allocator_CreateSystemAllocator(&allocator));
ThreadPool pool;
assert(EXIT_SUCCESS == ThreadPool_Create(&pool, n_workers, 0x00, &allocator));
os_semaphore_t semaphore;
ThreadPoolJob job;
assert(OSSemaphore_Create(&semaphore, 0) == EXIT_SUCCESS);
for (size_t i = 0; i < n_jobs - 1; i++) {
job.flags |= discard_result ? THREADPOOLJOB_DISCARD_RESULT : 0x00;
job.arg = NULL;
job.job = testJobFunction;
assert(ThreadPool_QueueJob(&pool, &job, NULL) == EXIT_SUCCESS);
}
job.arg = &semaphore;
job.job = (ThreadFunction) lastJobFunction;
assert(ThreadPool_QueueJob(&pool, &job, NULL) == EXIT_SUCCESS);
// Wait until all the jobs are done
OSSemaphore_Wait(&semaphore);
OSSemaphore_Destroy(&semaphore);
ThreadPool_Destroy(&pool);
assert(allocator.reserved == 0);
Allocator_DestroySystemAllocator(&allocator);
}
size_t power2(size_t n)
{
size_t t = 1;
for (size_t i = 0; i < n; i++) {
t *= 2;
}
return t;
}
int main()
{
testGeneric(1, power2(6), false);
/*
for (size_t worker_count = 1; worker_count < 32; worker_count++) {
for (size_t job_count = 0; job_count < 16; job_count++) {
testGeneric(worker_count, power2(job_count), true);
testGeneric(worker_count, power2(job_count), false);
}
}
*/
return 0;
}

139
tests/argumentc.test.c Normal file
View file

@ -0,0 +1,139 @@
#include "../src/argumentc/argumentc.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void test_long()
{
int argc = 3;
const char* argv[] = {"--hello", "--world", "--!"};
Argumentc argumentc;
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
assert(Argumentc_PopLongOption(&argumentc, StringView_FromString("hello")).type != OPTIONTYPE_NONE);
assert(Argumentc_PopLongOption(&argumentc, StringView_FromString("!")).type != OPTIONTYPE_NONE);
assert(Argumentc_PopLongOption(&argumentc, StringView_FromString("world")).type != OPTIONTYPE_NONE);
assert(Argumentc_HaveNextOption(&argumentc) == false);
Argumentc_Destroy(&argumentc);
return;
}
void test_short()
{
int argc = 2;
const char* argv[] = {"-abv", "-vv"};
Argumentc argumentc;
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("a")).type != OPTIONTYPE_NONE);
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("b")).type != OPTIONTYPE_NONE);
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("v")).type != OPTIONTYPE_NONE);
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("v")).type != OPTIONTYPE_NONE);
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("v")).type != OPTIONTYPE_NONE);
assert(Argumentc_HaveNextOption(&argumentc) == false);
Argumentc_Destroy(&argumentc);
return;
}
void test_long_arg()
{
int argc = 4;
const char* argv[] = {"--long", "argument", "--longer", "argumenter"};
Argumentc argumentc;
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
assert(
Argumentc_PopLongArgument(
&argumentc,
StringView_FromString("longer")
).argument.type
!=
OPTIONTYPE_NONE
);
assert(
Argumentc_PopLongArgument(
&argumentc,
StringView_FromString("long")
).argument.type
!=
OPTIONTYPE_NONE
);
assert(Argumentc_HaveNextOption(&argumentc) == false);
Argumentc_Destroy(&argumentc);
return;
}
void test_short_arg()
{
int argc = 4;
const char* argv[] = {"-ab", "argument", "-s", "15"};
Argumentc argumentc;
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
assert(
Argumentc_PopShortArgument(
&argumentc,
StringView_FromString("b")
).argument.type
!=
OPTIONTYPE_NONE
);
assert(
Argumentc_PopShortArgument(
&argumentc,
StringView_FromString("s")
).argument.type
!=
OPTIONTYPE_NONE
);
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("a")).type != OPTIONTYPE_NONE);
assert(Argumentc_HaveNextOption(&argumentc) == false);
Argumentc_Destroy(&argumentc);
return;
}
int main(int argc, char const *argv[]) {
Argumentc argumentc;
if (Argumentc_Create(&argumentc, argc, argv)) {
fprintf(stderr, "Failed to parse options for memory reasons");
return EXIT_FAILURE;
}
for (size_t i = 0; i < DynamicArray_GetLength(&argumentc.array); i++) {
Option* option = DynamicArray_GetPointer(&argumentc.array, i);
char c[option->content.length + 1];
memset(c, 0, option->content.length + 1);
memcpy(c, option->content.source, option->content.length);
printf("%-20s: %s\n",
OptionType_ToString(option->type),
c);
}
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("long")).type != OPTIONTYPE_NONE) {
test_long();
}
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("short")).type != OPTIONTYPE_NONE) {
test_short();
}
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("long-arg")).type != OPTIONTYPE_NONE) {
test_long_arg();
}
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("short-arg")).type != OPTIONTYPE_NONE) {
test_short_arg();
}
Argumentc_Destroy(&argumentc);
}

119
tests/regex.test.c Normal file
View file

@ -0,0 +1,119 @@
#include "../src/regex/regex.h"
#include <assert.h>
#include <stdlib.h>
void testLifetime(void)
{
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
Regex regex;
Regex_Create(&regex, &allocator);
Regex_Destroy(&regex);
assert(allocator.reserved == 0);
Allocator_DestroySystemAllocator(&allocator);
return;
}
void testLiteral(void)
{
StringView test_string_0 = StringView_FromString("Hello World");
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
Regex regex;
assert(Regex_Create(&regex, &allocator) == EXIT_SUCCESS);
assert(Regex_Compile(&regex, test_string_0) == EXIT_SUCCESS);
RegexMatch match;
assert(Regex_FirstMatch(&regex, test_string_0, &match) == EXIT_SUCCESS);
assert(StringView_Equal(match.match, test_string_0));
Regex_Destroy(&regex);
assert(allocator.reserved == 0);
Allocator_DestroySystemAllocator(&allocator);
return;
}
void testBackslash(void)
{
StringView regex_string = StringView_FromString("Hello World\\\\");
StringView match_string = StringView_FromString("Hello World\\");
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
Regex regex;
assert(Regex_Create(&regex, &allocator) == EXIT_SUCCESS);
assert(Regex_Compile(&regex, regex_string) == EXIT_SUCCESS);
RegexMatch match;
assert(Regex_FirstMatch(&regex, match_string, &match) == EXIT_SUCCESS);
assert(StringView_Equal(match.match, match_string));
Regex_Destroy(&regex);
assert(allocator.reserved == 0);
Allocator_DestroySystemAllocator(&allocator);
return;
}
void testGroup(void)
{
StringView regex_string = StringView_FromString("(Hello) (World)");
StringView match_string = StringView_FromString("Hello World");
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
Regex regex;
assert(Regex_Create(&regex, &allocator) == EXIT_SUCCESS);
assert(Regex_Compile(&regex, regex_string) == EXIT_SUCCESS);
RegexMatch match;
assert(Regex_FirstMatch(&regex, match_string, &match) == EXIT_SUCCESS);
assert(StringView_Equal(match.match, match_string));
assert(RegexMatch_HaveNumberedCapture(&match, 1));
assert(RegexMatch_HaveNumberedCapture(&match, 2));
Regex_Destroy(&regex);
assert(allocator.reserved == 0);
Allocator_DestroySystemAllocator(&allocator);
return;
}
void testQuantifier(void)
{
StringView regex_string = StringView_FromString("\\??");
StringView match_string_0 = StringView_FromString("?");
StringView match_string_1 = StringView_FromString("");
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
Regex regex;
assert(Regex_Create(&regex, &allocator) == EXIT_SUCCESS);
assert(Regex_Compile(&regex, regex_string) == EXIT_SUCCESS);
RegexMatch match;
assert(Regex_FirstMatch(&regex, match_string_0, &match) == EXIT_SUCCESS);
assert(StringView_Equal(match.match, match_string_0));
assert(Regex_FirstMatch(&regex, match_string_1, &match) == EXIT_SUCCESS);
assert(StringView_Equal(match.match, match_string_1));
Regex_Destroy(&regex);
assert(allocator.reserved == 0);
Allocator_DestroySystemAllocator(&allocator);
return;
}
int main()
{
testLifetime();
testLiteral();
testBackslash();
testGroup();
testQuantifier();
return 0;
}