refactor: string extend method

This commit is contained in:
2025-09-07 22:36:30 +02:00
parent 3e0584d139
commit 16ee246956
17 changed files with 2164 additions and 2123 deletions

View File

@@ -1,37 +1,37 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../string/mystring.h"
void test_str1(void) {
size_t length;
size_t capacity;
char *c_str;
/* Allocate a new dynamic string with an initial capacity */
mcl_string_s *str = mcl_string_new("Hello, world!", 512);
assert(str != NULL);
/* Retrieve a C str from string with mcl_string_cstr() */
c_str = mcl_string_cstr(str);
length = mcl_string_length(str);
capacity = mcl_string_capacity(str);
assert(strcmp(c_str, "Hello, world!") == 0);
assert(length == 13);
assert(capacity == 512);
/* Append text to a mcl_string */
assert(mcl_string_append(str, " How are you?") == 0);
c_str = mcl_string_cstr(str);
length = mcl_string_length(str);
capacity = mcl_string_capacity(str);
assert(strcmp(c_str, "Hello, world! How are you?") == 0);
assert(length == 26);
assert(capacity == 512);
/* Always deallocate memory */
mcl_string_free(str);
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../string/mystring.h"
void test_str1(void) {
size_t length;
size_t capacity;
char *c_str;
/* Allocate a new dynamic string with an initial capacity */
mcl_string_s *str = mcl_string_new("Hello, world!", 512);
assert(str != NULL);
/* Retrieve a C str from string with mcl_string_cstr() */
c_str = mcl_string_cstr(str);
length = mcl_string_length(str);
capacity = mcl_string_capacity(str);
assert(strcmp(c_str, "Hello, world!") == 0);
assert(length == 13);
assert(capacity == 512);
/* Append text to a mcl_string */
assert(mcl_string_append(str, " How are you?") == 0);
c_str = mcl_string_cstr(str);
length = mcl_string_length(str);
capacity = mcl_string_capacity(str);
assert(strcmp(c_str, "Hello, world! How are you?") == 0);
assert(length == 26);
assert(capacity == 512);
/* Always deallocate memory */
mcl_string_free(str);
}

View File

@@ -1,27 +1,34 @@
#include <assert.h>
#include <string.h>
#include "../../string/mystring.h"
void test_str2(void) {
mcl_string_s *s1 = mcl_string_new("Hello, world!", 0);
assert(s1 != NULL);
mcl_string_s *s2 = mcl_string_new("Hello, world!", 0);
assert(s2 != NULL);
/* Don't call mcl_string_cstr() more than once in the same printf function */
int ret = mcl_string_compare(s1, s2);
assert(ret == 0);
mcl_string_clear(s1);
ret = mcl_string_compare(s1, s2);
assert(ret != 0);
mcl_string_tolower(s1);
mcl_string_toupper(s2);
assert(strcmp(mcl_string_cstr(s1), "") == 0);
assert(strcmp(mcl_string_cstr(s2), "HELLO, WORLD!") == 0);
mcl_string_free(s1);
mcl_string_free(s2);
}
#include <assert.h>
#include <string.h>
#include "../../string/mystring.h"
void test_str2(void) {
mcl_string_s *s1 = mcl_string_new("Hello, world!", 0);
assert(s1 != NULL);
mcl_string_s *s2 = mcl_string_new("Hello, world!", 0);
assert(s2 != NULL);
/* Don't call mcl_string_cstr() more than once in the same printf function */
int ret = mcl_string_compare(s1, s2);
assert(ret == 0);
mcl_string_clear(s1);
ret = mcl_string_compare(s1, s2);
assert(ret != 0);
mcl_string_tolower(s1);
mcl_string_toupper(s2);
assert(strcmp(mcl_string_cstr(s1), "") == 0);
assert(strcmp(mcl_string_cstr(s2), "HELLO, WORLD!") == 0);
/* Extend a string */
mcl_string_s *extend_me = mcl_string_new("This string is suuuuuuuuuuuuuuuuuuuuuper extended!", 0);
mcl_string_extend(s1, extend_me);
assert(mcl_string_length(s1) == 50);
assert(mcl_string_capacity(s1) == 64);
mcl_string_free(s1);
mcl_string_free(s2);
mcl_string_free(extend_me);
}