Skip to content

Commit eafde92

Browse files
committed
Implement ZyanString example
1 parent 2360a87 commit eafde92

File tree

2 files changed

+211
-80
lines changed

2 files changed

+211
-80
lines changed

examples/String.c

Lines changed: 105 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
/* Helper functions */
4646
/* ============================================================================================== */
4747

48+
static ZyanStatus PrintString(ZyanString* string) {
49+
const char* cstring;
50+
ZYAN_CHECK(ZyanStringGetData(string, &cstring));
51+
52+
printf("(*ZyanString)%p = %s\n", (void*)string, cstring);
53+
54+
return ZYAN_STATUS_SUCCESS;
55+
}
56+
4857
/* ============================================================================================== */
4958
/* Tests */
5059
/* ============================================================================================== */
@@ -60,12 +69,23 @@
6069
*
6170
* @return A zyan status code.
6271
*/
63-
static ZyanStatus PerformBasicTests(ZyanString* string)
64-
{
72+
static ZyanStatus PerformBasicTests(ZyanString* string) {
6573
ZYAN_ASSERT(string);
66-
ZYAN_UNUSED(string);
6774

75+
ZyanUSize size;
76+
ZYAN_CHECK(ZyanStringGetSize(string, &size));
6877

78+
ZyanStringView view1 = ZYAN_DEFINE_STRING_VIEW("The quick brown fox jumps over the lazy dog");
79+
ZyanStringView view2 = ZYAN_DEFINE_STRING_VIEW("big ");
80+
81+
ZYAN_CHECK(ZyanStringAppend(string, &view1));
82+
PrintString(string);
83+
84+
ZYAN_CHECK(ZyanStringInsert(string, 4, &view2));
85+
PrintString(string);
86+
87+
ZYAN_CHECK(ZyanStringSetChar(string, 7, ','));
88+
PrintString(string);
6989

7090
return ZYAN_STATUS_SUCCESS;
7191
}
@@ -75,82 +95,86 @@ static ZyanStatus PerformBasicTests(ZyanString* string)
7595
*
7696
* @return A zyan status code.
7797
*/
78-
static ZyanStatus TestDynamic(void)
79-
{
80-
PerformBasicTests(ZYAN_NULL);
81-
return ZYAN_STATUS_SUCCESS;
98+
static ZyanStatus TestDynamic(void) {
99+
ZyanString string;
100+
101+
ZYAN_CHECK(ZyanStringInit(&string, 10));
102+
103+
ZYAN_CHECK(PerformBasicTests(&string));
104+
105+
return ZyanStringDestroy(&string);
82106
}
83107

84108
/**
85109
* Performs basic tests on a string that uses a static buffer.
86110
*
87111
* @return A zyan status code.
88112
*/
89-
static ZyanStatus TestStatic(void)
90-
{
91-
PerformBasicTests(ZYAN_NULL);
92-
return ZYAN_STATUS_SUCCESS;
113+
static ZyanStatus TestStatic(void) {
114+
static char buffer[50];
115+
ZyanString string;
116+
117+
ZYAN_CHECK(ZyanStringInitCustomBuffer(&string, buffer, sizeof(buffer)));
118+
119+
ZYAN_CHECK(PerformBasicTests(&string));
120+
121+
return ZyanStringDestroy(&string);
93122
}
94123

95124
/* ---------------------------------------------------------------------------------------------- */
96125
/* Custom allocator */
97126
/* ---------------------------------------------------------------------------------------------- */
98127

99-
//static ZyanStatus AllocatorAllocate(ZyanAllocator* allocator, void** p, ZyanUSize element_size,
100-
// ZyanUSize n)
101-
//{
102-
// ZYAN_ASSERT(allocator);
103-
// ZYAN_ASSERT(p);
104-
// ZYAN_ASSERT(element_size);
105-
// ZYAN_ASSERT(n);
106-
//
107-
// ZYAN_UNUSED(allocator);
108-
//
109-
// *p = ZYAN_MALLOC(element_size * n);
110-
// if (!*p)
111-
// {
112-
// return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
113-
// }
114-
//
115-
// return ZYAN_STATUS_SUCCESS;
116-
//}
117-
//
118-
//static ZyanStatus AllocatorReallocate(ZyanAllocator* allocator, void** p, ZyanUSize element_size,
119-
// ZyanUSize n)
120-
//{
121-
// ZYAN_ASSERT(allocator);
122-
// ZYAN_ASSERT(p);
123-
// ZYAN_ASSERT(element_size);
124-
// ZYAN_ASSERT(n);
125-
//
126-
// ZYAN_UNUSED(allocator);
127-
//
128-
// void* const x = ZYAN_REALLOC(*p, element_size * n);
129-
// if (!x)
130-
// {
131-
// return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
132-
// }
133-
// *p = x;
134-
//
135-
// return ZYAN_STATUS_SUCCESS;
136-
//}
137-
//
138-
//static ZyanStatus AllocatorDeallocate(ZyanAllocator* allocator, void* p, ZyanUSize element_size,
139-
// ZyanUSize n)
140-
//{
141-
// ZYAN_ASSERT(allocator);
142-
// ZYAN_ASSERT(p);
143-
// ZYAN_ASSERT(element_size);
144-
// ZYAN_ASSERT(n);
145-
//
146-
// ZYAN_UNUSED(allocator);
147-
// ZYAN_UNUSED(element_size);
148-
// ZYAN_UNUSED(n);
149-
//
150-
// ZYAN_FREE(p);
151-
//
152-
// return ZYAN_STATUS_SUCCESS;
153-
//}
128+
static ZyanStatus AllocatorAllocate(
129+
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
130+
ZYAN_ASSERT(allocator);
131+
ZYAN_ASSERT(p);
132+
ZYAN_ASSERT(element_size);
133+
ZYAN_ASSERT(n);
134+
135+
ZYAN_UNUSED(allocator);
136+
137+
*p = ZYAN_MALLOC(element_size * n);
138+
if (!*p) {
139+
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
140+
}
141+
142+
return ZYAN_STATUS_SUCCESS;
143+
}
144+
145+
static ZyanStatus AllocatorReallocate(
146+
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
147+
ZYAN_ASSERT(allocator);
148+
ZYAN_ASSERT(p);
149+
ZYAN_ASSERT(element_size);
150+
ZYAN_ASSERT(n);
151+
152+
ZYAN_UNUSED(allocator);
153+
154+
void* const x = ZYAN_REALLOC(*p, element_size * n);
155+
if (!x) {
156+
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
157+
}
158+
*p = x;
159+
160+
return ZYAN_STATUS_SUCCESS;
161+
}
162+
163+
static ZyanStatus AllocatorDeallocate(
164+
ZyanAllocator* allocator, void* p, ZyanUSize element_size, ZyanUSize n) {
165+
ZYAN_ASSERT(allocator);
166+
ZYAN_ASSERT(p);
167+
ZYAN_ASSERT(element_size);
168+
ZYAN_ASSERT(n);
169+
170+
ZYAN_UNUSED(allocator);
171+
ZYAN_UNUSED(element_size);
172+
ZYAN_UNUSED(n);
173+
174+
ZYAN_FREE(p);
175+
176+
return ZYAN_STATUS_SUCCESS;
177+
}
154178

155179
/* ---------------------------------------------------------------------------------------------- */
156180

@@ -160,9 +184,17 @@ static ZyanStatus TestStatic(void)
160184
*
161185
* @return A zyan status code.
162186
*/
163-
static ZyanStatus TestAllocator(void)
164-
{
165-
return ZYAN_STATUS_SUCCESS;
187+
static ZyanStatus TestAllocator(void) {
188+
ZyanAllocator allocator;
189+
ZYAN_CHECK(
190+
ZyanAllocatorInit(&allocator, AllocatorAllocate, AllocatorReallocate, AllocatorDeallocate));
191+
192+
ZyanString string;
193+
ZYAN_CHECK(ZyanStringInitEx(&string, 20, &allocator, 10, 0));
194+
195+
ZYAN_CHECK(PerformBasicTests(&string));
196+
197+
return ZyanStringDestroy(&string);
166198
}
167199

168200
/* ---------------------------------------------------------------------------------------------- */
@@ -171,18 +203,14 @@ static ZyanStatus TestAllocator(void)
171203
/* Entry point */
172204
/* ============================================================================================== */
173205

174-
int main()
175-
{
176-
if (!ZYAN_SUCCESS(TestDynamic()))
177-
{
206+
int main(void) {
207+
if (!ZYAN_SUCCESS(TestDynamic())) {
178208
return EXIT_FAILURE;
179209
}
180-
if (!ZYAN_SUCCESS(TestStatic()))
181-
{
210+
if (!ZYAN_SUCCESS(TestStatic())) {
182211
return EXIT_FAILURE;
183212
}
184-
if (!ZYAN_SUCCESS(TestAllocator()))
185-
{
213+
if (!ZYAN_SUCCESS(TestAllocator())) {
186214
return EXIT_FAILURE;
187215
}
188216

tests/String.cpp

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
*/
3131

3232
#include <gtest/gtest.h>
33+
#include <Zycore/Allocator.h>
34+
#include <Zycore/Defines.h>
35+
#include <Zycore/LibC.h>
3336
#include <Zycore/String.h>
37+
#include <Zycore/Types.h>
3438

3539
/* ============================================================================================== */
3640
/* Enums and types */
@@ -42,17 +46,116 @@
4246
/* Helper functions */
4347
/* ============================================================================================== */
4448

49+
/* ---------------------------------------------------------------------------------------------- */
50+
/* Custom allocator */
51+
/* ---------------------------------------------------------------------------------------------- */
52+
53+
static ZyanStatus AllocatorAllocate(
54+
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
55+
ZYAN_ASSERT(allocator);
56+
ZYAN_ASSERT(p);
57+
ZYAN_ASSERT(element_size);
58+
ZYAN_ASSERT(n);
59+
60+
ZYAN_UNUSED(allocator);
61+
62+
*p = ZYAN_MALLOC(element_size * n);
63+
if (!*p) {
64+
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
65+
}
66+
67+
return ZYAN_STATUS_SUCCESS;
68+
}
69+
70+
static ZyanStatus AllocatorReallocate(
71+
ZyanAllocator* allocator, void** p, ZyanUSize element_size, ZyanUSize n) {
72+
ZYAN_ASSERT(allocator);
73+
ZYAN_ASSERT(p);
74+
ZYAN_ASSERT(element_size);
75+
ZYAN_ASSERT(n);
76+
77+
ZYAN_UNUSED(allocator);
78+
79+
void* const x = ZYAN_REALLOC(*p, element_size * n);
80+
if (!x) {
81+
return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
82+
}
83+
*p = x;
84+
85+
return ZYAN_STATUS_SUCCESS;
86+
}
4587

88+
static ZyanStatus AllocatorDeallocate(
89+
ZyanAllocator* allocator, void* p, ZyanUSize element_size, ZyanUSize n) {
90+
ZYAN_ASSERT(allocator);
91+
ZYAN_ASSERT(p);
92+
ZYAN_ASSERT(element_size);
93+
ZYAN_ASSERT(n);
94+
95+
ZYAN_UNUSED(allocator);
96+
ZYAN_UNUSED(element_size);
97+
ZYAN_UNUSED(n);
98+
99+
ZYAN_FREE(p);
100+
101+
return ZYAN_STATUS_SUCCESS;
102+
}
46103

47104
/* ============================================================================================== */
48105
/* Tests */
49106
/* ============================================================================================== */
50107

51-
/* ---------------------------------------------------------------------------------------------- */
52-
/* */
53-
/* ---------------------------------------------------------------------------------------------- */
108+
TEST(StringTest, InitDynamic)
109+
{
110+
ZyanString string;
111+
112+
ASSERT_EQ(ZyanStringInit(&string, 0), ZYAN_STATUS_SUCCESS);
113+
EXPECT_EQ(string.vector.allocator, ZyanAllocatorDefault());
114+
EXPECT_EQ(string.vector.growth_factor, ZYAN_STRING_DEFAULT_GROWTH_FACTOR);
115+
EXPECT_EQ(string.vector.shrink_threshold, ZYAN_STRING_DEFAULT_SHRINK_THRESHOLD);
116+
EXPECT_EQ(string.vector.size, static_cast<ZyanUSize>(1));
117+
EXPECT_EQ(string.vector.capacity, static_cast<ZyanUSize>(ZYAN_STRING_MIN_CAPACITY + 1));
118+
EXPECT_EQ(string.vector.element_size, sizeof(char));
119+
EXPECT_NE(string.vector.data, ZYAN_NULL);
120+
EXPECT_EQ(ZyanStringDestroy(&string), ZYAN_STATUS_SUCCESS);
121+
}
54122

123+
TEST(StringTest, InitStatic)
124+
{
125+
ZyanString string;
126+
127+
static char buffer[32];
128+
EXPECT_EQ(ZyanStringInitCustomBuffer(&string, buffer, 0), ZYAN_STATUS_INVALID_ARGUMENT);
129+
ASSERT_EQ(ZyanStringInitCustomBuffer(&string, buffer, ZYAN_ARRAY_LENGTH(buffer)),
130+
ZYAN_STATUS_SUCCESS);
131+
EXPECT_EQ(string.vector.allocator, ZYAN_NULL);
132+
EXPECT_EQ(string.vector.growth_factor, 1);
133+
EXPECT_EQ(string.vector.shrink_threshold, 0);
134+
EXPECT_EQ(string.vector.size, static_cast<ZyanUSize>(1));
135+
EXPECT_EQ(string.vector.capacity, ZYAN_ARRAY_LENGTH(buffer));
136+
EXPECT_EQ(string.vector.element_size, sizeof(char));
137+
EXPECT_EQ(string.vector.data, &buffer);
138+
EXPECT_EQ(ZyanStringDestroy(&string), ZYAN_STATUS_SUCCESS);
139+
}
55140

141+
TEST(StringTest, InitAdvanced)
142+
{
143+
ZyanString string;
144+
ZyanAllocator allocator;
145+
146+
ASSERT_EQ(
147+
ZyanAllocatorInit(&allocator, AllocatorAllocate, AllocatorReallocate, AllocatorDeallocate),
148+
ZYAN_STATUS_SUCCESS);
149+
ASSERT_EQ(ZyanStringInitEx(&string, 0, &allocator, 1, 0), ZYAN_STATUS_SUCCESS);
150+
EXPECT_EQ(string.vector.allocator, &allocator);
151+
EXPECT_EQ(string.vector.growth_factor, 1);
152+
EXPECT_EQ(string.vector.shrink_threshold, 0);
153+
EXPECT_EQ(string.vector.size, static_cast<ZyanUSize>(1));
154+
EXPECT_EQ(string.vector.capacity, static_cast<ZyanUSize>(ZYAN_STRING_MIN_CAPACITY + 1));
155+
EXPECT_EQ(string.vector.element_size, sizeof(char));
156+
EXPECT_NE(string.vector.data, ZYAN_NULL);
157+
EXPECT_EQ(ZyanStringDestroy(&string), ZYAN_STATUS_SUCCESS);
158+
}
56159

57160
/* ---------------------------------------------------------------------------------------------- */
58161

0 commit comments

Comments
 (0)