45
45
/* Helper functions */
46
46
/* ============================================================================================== */
47
47
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
+
48
57
/* ============================================================================================== */
49
58
/* Tests */
50
59
/* ============================================================================================== */
60
69
*
61
70
* @return A zyan status code.
62
71
*/
63
- static ZyanStatus PerformBasicTests (ZyanString * string )
64
- {
72
+ static ZyanStatus PerformBasicTests (ZyanString * string ) {
65
73
ZYAN_ASSERT (string );
66
- ZYAN_UNUSED (string );
67
74
75
+ ZyanUSize size ;
76
+ ZYAN_CHECK (ZyanStringGetSize (string , & size ));
68
77
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 );
69
89
70
90
return ZYAN_STATUS_SUCCESS ;
71
91
}
@@ -75,82 +95,86 @@ static ZyanStatus PerformBasicTests(ZyanString* string)
75
95
*
76
96
* @return A zyan status code.
77
97
*/
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 );
82
106
}
83
107
84
108
/**
85
109
* Performs basic tests on a string that uses a static buffer.
86
110
*
87
111
* @return A zyan status code.
88
112
*/
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 );
93
122
}
94
123
95
124
/* ---------------------------------------------------------------------------------------------- */
96
125
/* Custom allocator */
97
126
/* ---------------------------------------------------------------------------------------------- */
98
127
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
+ }
154
178
155
179
/* ---------------------------------------------------------------------------------------------- */
156
180
@@ -160,9 +184,17 @@ static ZyanStatus TestStatic(void)
160
184
*
161
185
* @return A zyan status code.
162
186
*/
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 );
166
198
}
167
199
168
200
/* ---------------------------------------------------------------------------------------------- */
@@ -171,18 +203,14 @@ static ZyanStatus TestAllocator(void)
171
203
/* Entry point */
172
204
/* ============================================================================================== */
173
205
174
- int main ()
175
- {
176
- if (!ZYAN_SUCCESS (TestDynamic ()))
177
- {
206
+ int main (void ) {
207
+ if (!ZYAN_SUCCESS (TestDynamic ())) {
178
208
return EXIT_FAILURE ;
179
209
}
180
- if (!ZYAN_SUCCESS (TestStatic ()))
181
- {
210
+ if (!ZYAN_SUCCESS (TestStatic ())) {
182
211
return EXIT_FAILURE ;
183
212
}
184
- if (!ZYAN_SUCCESS (TestAllocator ()))
185
- {
213
+ if (!ZYAN_SUCCESS (TestAllocator ())) {
186
214
return EXIT_FAILURE ;
187
215
}
188
216
0 commit comments