Skip to content

Commit

Permalink
Implement ZyanString example
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP authored and athre0z committed Jan 27, 2025
1 parent baba8da commit 7afed0e
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 80 deletions.
182 changes: 105 additions & 77 deletions examples/String.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
/* Helper functions */
/* ============================================================================================== */

static ZyanStatus PrintString(ZyanString* string) {
const char* cstring;
ZYAN_CHECK(ZyanStringGetData(string, &cstring));

printf("(*ZyanString)%p = %s\n", (void*)string, cstring);

return ZYAN_STATUS_SUCCESS;
}

/* ============================================================================================== */
/* Tests */
/* ============================================================================================== */
Expand All @@ -60,12 +69,23 @@
*
* @return A zyan status code.
*/
static ZyanStatus PerformBasicTests(ZyanString* string)
{
static ZyanStatus PerformBasicTests(ZyanString* string) {
ZYAN_ASSERT(string);
ZYAN_UNUSED(string);

ZyanUSize size;
ZYAN_CHECK(ZyanStringGetSize(string, &size));

ZyanStringView view1 = ZYAN_DEFINE_STRING_VIEW("The quick brown fox jumps over the lazy dog");
ZyanStringView view2 = ZYAN_DEFINE_STRING_VIEW("big ");

ZYAN_CHECK(ZyanStringAppend(string, &view1));
PrintString(string);

ZYAN_CHECK(ZyanStringInsert(string, 4, &view2));
PrintString(string);

ZYAN_CHECK(ZyanStringSetChar(string, 7, ','));
PrintString(string);

return ZYAN_STATUS_SUCCESS;
}
Expand All @@ -75,82 +95,86 @@ static ZyanStatus PerformBasicTests(ZyanString* string)
*
* @return A zyan status code.
*/
static ZyanStatus TestDynamic(void)
{
PerformBasicTests(ZYAN_NULL);
return ZYAN_STATUS_SUCCESS;
static ZyanStatus TestDynamic(void) {
ZyanString string;

ZYAN_CHECK(ZyanStringInit(&string, 10));

ZYAN_CHECK(PerformBasicTests(&string));

return ZyanStringDestroy(&string);
}

/**
* Performs basic tests on a string that uses a static buffer.
*
* @return A zyan status code.
*/
static ZyanStatus TestStatic(void)
{
PerformBasicTests(ZYAN_NULL);
return ZYAN_STATUS_SUCCESS;
static ZyanStatus TestStatic(void) {
static char buffer[50];
ZyanString string;

ZYAN_CHECK(ZyanStringInitCustomBuffer(&string, buffer, sizeof(buffer)));

ZYAN_CHECK(PerformBasicTests(&string));

return ZyanStringDestroy(&string);
}

/* ---------------------------------------------------------------------------------------------- */
/* Custom allocator */
/* ---------------------------------------------------------------------------------------------- */

//static ZyanStatus AllocatorAllocate(ZyanAllocator* allocator, void** p, ZyanUSize element_size,
// ZyanUSize n)
//{
// ZYAN_ASSERT(allocator);
// ZYAN_ASSERT(p);
// ZYAN_ASSERT(element_size);
// ZYAN_ASSERT(n);
//
// ZYAN_UNUSED(allocator);
//
// *p = ZYAN_MALLOC(element_size * n);
// if (!*p)
// {
// return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
// }
//
// return ZYAN_STATUS_SUCCESS;
//}
//
//static ZyanStatus AllocatorReallocate(ZyanAllocator* allocator, void** p, ZyanUSize element_size,
// ZyanUSize n)
//{
// ZYAN_ASSERT(allocator);
// ZYAN_ASSERT(p);
// ZYAN_ASSERT(element_size);
// ZYAN_ASSERT(n);
//
// ZYAN_UNUSED(allocator);
//
// void* const x = ZYAN_REALLOC(*p, element_size * n);
// if (!x)
// {
// return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
// }
// *p = x;
//
// return ZYAN_STATUS_SUCCESS;
//}
//
//static ZyanStatus AllocatorDeallocate(ZyanAllocator* allocator, void* p, ZyanUSize element_size,
// ZyanUSize n)
//{
// ZYAN_ASSERT(allocator);
// ZYAN_ASSERT(p);
// ZYAN_ASSERT(element_size);
// ZYAN_ASSERT(n);
//
// ZYAN_UNUSED(allocator);
// ZYAN_UNUSED(element_size);
// ZYAN_UNUSED(n);
//
// ZYAN_FREE(p);
//
// return ZYAN_STATUS_SUCCESS;
//}
static ZyanStatus AllocatorAllocate(
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
ZYAN_ASSERT(allocator);
ZYAN_ASSERT(p);
ZYAN_ASSERT(element_size);
ZYAN_ASSERT(n);

ZYAN_UNUSED(allocator);

*p = ZYAN_MALLOC(element_size * n);
if (!*p) {
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
}

return ZYAN_STATUS_SUCCESS;
}

static ZyanStatus AllocatorReallocate(
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
ZYAN_ASSERT(allocator);
ZYAN_ASSERT(p);
ZYAN_ASSERT(element_size);
ZYAN_ASSERT(n);

ZYAN_UNUSED(allocator);

void* const x = ZYAN_REALLOC(*p, element_size * n);
if (!x) {
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
}
*p = x;

return ZYAN_STATUS_SUCCESS;
}

static ZyanStatus AllocatorDeallocate(
ZyanAllocator* allocator, void* p, ZyanUSize element_size, ZyanUSize n) {
ZYAN_ASSERT(allocator);
ZYAN_ASSERT(p);
ZYAN_ASSERT(element_size);
ZYAN_ASSERT(n);

ZYAN_UNUSED(allocator);
ZYAN_UNUSED(element_size);
ZYAN_UNUSED(n);

ZYAN_FREE(p);

return ZYAN_STATUS_SUCCESS;
}

/* ---------------------------------------------------------------------------------------------- */

Expand All @@ -160,9 +184,17 @@ static ZyanStatus TestStatic(void)
*
* @return A zyan status code.
*/
static ZyanStatus TestAllocator(void)
{
return ZYAN_STATUS_SUCCESS;
static ZyanStatus TestAllocator(void) {
ZyanAllocator allocator;
ZYAN_CHECK(
ZyanAllocatorInit(&allocator, AllocatorAllocate, AllocatorReallocate, AllocatorDeallocate));

ZyanString string;
ZYAN_CHECK(ZyanStringInitEx(&string, 20, &allocator, 10, 0));

ZYAN_CHECK(PerformBasicTests(&string));

return ZyanStringDestroy(&string);
}

/* ---------------------------------------------------------------------------------------------- */
Expand All @@ -171,18 +203,14 @@ static ZyanStatus TestAllocator(void)
/* Entry point */
/* ============================================================================================== */

int main()
{
if (!ZYAN_SUCCESS(TestDynamic()))
{
int main(void) {
if (!ZYAN_SUCCESS(TestDynamic())) {
return EXIT_FAILURE;
}
if (!ZYAN_SUCCESS(TestStatic()))
{
if (!ZYAN_SUCCESS(TestStatic())) {
return EXIT_FAILURE;
}
if (!ZYAN_SUCCESS(TestAllocator()))
{
if (!ZYAN_SUCCESS(TestAllocator())) {
return EXIT_FAILURE;
}

Expand Down
109 changes: 106 additions & 3 deletions tests/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
*/

#include <gtest/gtest.h>
#include <Zycore/Allocator.h>
#include <Zycore/Defines.h>
#include <Zycore/LibC.h>
#include <Zycore/String.h>
#include <Zycore/Types.h>

/* ============================================================================================== */
/* Enums and types */
Expand All @@ -42,17 +46,116 @@
/* Helper functions */
/* ============================================================================================== */

/* ---------------------------------------------------------------------------------------------- */
/* Custom allocator */
/* ---------------------------------------------------------------------------------------------- */

static ZyanStatus AllocatorAllocate(
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
ZYAN_ASSERT(allocator);
ZYAN_ASSERT(p);
ZYAN_ASSERT(element_size);
ZYAN_ASSERT(n);

ZYAN_UNUSED(allocator);

*p = ZYAN_MALLOC(element_size * n);
if (!*p) {
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
}

return ZYAN_STATUS_SUCCESS;
}

static ZyanStatus AllocatorReallocate(
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
ZYAN_ASSERT(allocator);
ZYAN_ASSERT(p);
ZYAN_ASSERT(element_size);
ZYAN_ASSERT(n);

ZYAN_UNUSED(allocator);

void* const x = ZYAN_REALLOC(*p, element_size * n);
if (!x) {
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
}
*p = x;

return ZYAN_STATUS_SUCCESS;
}

static ZyanStatus AllocatorDeallocate(
ZyanAllocator* allocator, void* p, ZyanUSize element_size, ZyanUSize n) {
ZYAN_ASSERT(allocator);
ZYAN_ASSERT(p);
ZYAN_ASSERT(element_size);
ZYAN_ASSERT(n);

ZYAN_UNUSED(allocator);
ZYAN_UNUSED(element_size);
ZYAN_UNUSED(n);

ZYAN_FREE(p);

return ZYAN_STATUS_SUCCESS;
}

/* ============================================================================================== */
/* Tests */
/* ============================================================================================== */

/* ---------------------------------------------------------------------------------------------- */
/* */
/* ---------------------------------------------------------------------------------------------- */
TEST(StringTest, InitDynamic)
{
ZyanString string;

ASSERT_EQ(ZyanStringInit(&string, 0), ZYAN_STATUS_SUCCESS);
EXPECT_EQ(string.vector.allocator, ZyanAllocatorDefault());
EXPECT_EQ(string.vector.growth_factor, ZYAN_STRING_DEFAULT_GROWTH_FACTOR);
EXPECT_EQ(string.vector.shrink_threshold, ZYAN_STRING_DEFAULT_SHRINK_THRESHOLD);
EXPECT_EQ(string.vector.size, static_cast<ZyanUSize>(1));
EXPECT_EQ(string.vector.capacity, static_cast<ZyanUSize>(ZYAN_STRING_MIN_CAPACITY + 1));
EXPECT_EQ(string.vector.element_size, sizeof(char));
EXPECT_NE(string.vector.data, ZYAN_NULL);
EXPECT_EQ(ZyanStringDestroy(&string), ZYAN_STATUS_SUCCESS);
}

TEST(StringTest, InitStatic)
{
ZyanString string;

static char buffer[32];
EXPECT_EQ(ZyanStringInitCustomBuffer(&string, buffer, 0), ZYAN_STATUS_INVALID_ARGUMENT);
ASSERT_EQ(ZyanStringInitCustomBuffer(&string, buffer, ZYAN_ARRAY_LENGTH(buffer)),
ZYAN_STATUS_SUCCESS);
EXPECT_EQ(string.vector.allocator, ZYAN_NULL);
EXPECT_EQ(string.vector.growth_factor, 1);
EXPECT_EQ(string.vector.shrink_threshold, 0);
EXPECT_EQ(string.vector.size, static_cast<ZyanUSize>(1));
EXPECT_EQ(string.vector.capacity, ZYAN_ARRAY_LENGTH(buffer));
EXPECT_EQ(string.vector.element_size, sizeof(char));
EXPECT_EQ(string.vector.data, &buffer);
EXPECT_EQ(ZyanStringDestroy(&string), ZYAN_STATUS_SUCCESS);
}

TEST(StringTest, InitAdvanced)
{
ZyanString string;
ZyanAllocator allocator;

ASSERT_EQ(
ZyanAllocatorInit(&allocator, AllocatorAllocate, AllocatorReallocate, AllocatorDeallocate),
ZYAN_STATUS_SUCCESS);
ASSERT_EQ(ZyanStringInitEx(&string, 0, &allocator, 1, 0), ZYAN_STATUS_SUCCESS);
EXPECT_EQ(string.vector.allocator, &allocator);
EXPECT_EQ(string.vector.growth_factor, 1);
EXPECT_EQ(string.vector.shrink_threshold, 0);
EXPECT_EQ(string.vector.size, static_cast<ZyanUSize>(1));
EXPECT_EQ(string.vector.capacity, static_cast<ZyanUSize>(ZYAN_STRING_MIN_CAPACITY + 1));
EXPECT_EQ(string.vector.element_size, sizeof(char));
EXPECT_NE(string.vector.data, ZYAN_NULL);
EXPECT_EQ(ZyanStringDestroy(&string), ZYAN_STATUS_SUCCESS);
}

/* ---------------------------------------------------------------------------------------------- */

Expand Down

0 comments on commit 7afed0e

Please sign in to comment.