test_str.c (7277B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "str.h" 17 #include "test_utils.h" 18 #include <stdarg.h> 19 #include <string.h> 20 21 #ifdef COMPILER_GCC 22 __attribute__((format(printf, 2, 3))) 23 #endif 24 static void 25 chk_vprintf(struct str* str, const char* fmt, ...) 26 { 27 va_list ap; 28 va_start(ap, fmt); 29 CHK(str_vprintf(str, fmt, ap) == RES_OK); 30 va_end(ap); 31 } 32 33 #ifdef COMPILER_GCC 34 __attribute__((format(printf, 2, 3))) 35 #endif 36 static void 37 chk_append_vprintf(struct str* str, const char* fmt, ...) 38 { 39 va_list ap; 40 va_start(ap, fmt); 41 CHK(str_append_vprintf(str, fmt, ap) == RES_OK); 42 va_end(ap); 43 } 44 45 46 int 47 main(int argc, char** argv) 48 { 49 struct mem_allocator allocator_proxy; 50 struct str str, str2; 51 (void)argc, (void)argv; 52 53 mem_init_proxy_allocator(&allocator_proxy, &mem_default_allocator); 54 55 str_init(&allocator_proxy, &str); 56 CHK(strcmp(str_get(&str), "") == 0); 57 CHK(str_len(&str) == 0); 58 CHK(str_is_empty(&str) == 1); 59 60 CHK(str_set(&str, "Foo") == RES_OK); 61 CHK(strcmp(str_get(&str), "Foo") == 0); 62 CHK(str_len(&str) == strlen("Foo")); 63 CHK(str_is_empty(&str) == 0); 64 65 str_clear(&str); 66 CHK(strcmp(str_get(&str), "") == 0); 67 CHK(str_len(&str) == 0); 68 69 CHK(str_set(&str, __FILE__) == RES_OK); 70 CHK(strcmp(str_get(&str), __FILE__) == 0); 71 72 str_release(&str); 73 str_init(&allocator_proxy, &str); 74 CHK(strcmp(str_get(&str), "") == 0); 75 CHK(str_len(&str) == 0); 76 77 CHK(str_set(&str, "Hello world!") == RES_OK); 78 CHK(strcmp(str_get(&str), "Hello world!") == 0); 79 CHK(str_insert(&str, 13, " insert") != RES_OK); 80 CHK(str_insert(&str, 12, " insert") == RES_OK); 81 CHK(strcmp(str_get(&str), "Hello world! insert") == 0); 82 83 CHK(str_insert(&str, 6, "abcdefgh ") == RES_OK); 84 CHK(strcmp(str_get(&str), "Hello abcdefgh world! insert") == 0); 85 CHK(str_insert(&str, 0, "ABC ") == RES_OK); 86 CHK(strcmp(str_get(&str), "ABC Hello abcdefgh world! insert") == 0); 87 CHK(str_insert(&str, 11, "0123456789ABCDEF") == RES_OK); 88 CHK(strcmp(str_get(&str), 89 "ABC Hello a0123456789ABCDEFbcdefgh world! insert") == 0); 90 CHK(str_len(&str) == 91 strlen("ABC Hello a0123456789ABCDEFbcdefgh world! insert")); 92 93 CHK(str_set(&str, "Hello world!") == RES_OK); 94 CHK(str_append(&str, " Append") == RES_OK); 95 CHK(strcmp(str_get(&str), "Hello world! Append") == 0); 96 CHK(str_append(&str, "!") == RES_OK); 97 CHK(strcmp(str_get(&str), "Hello world! Append!") == 0); 98 99 CHK(str_insert_char(&str, 21, 'a') != RES_OK); 100 CHK(strcmp(str_get(&str), "Hello world! Append!") == 0); 101 CHK(str_insert_char(&str, 20, 'a') == RES_OK); 102 CHK(strcmp(str_get(&str), "Hello world! Append!a") == 0); 103 CHK(str_insert_char(&str, 0, '0') == RES_OK); 104 CHK(strcmp(str_get(&str), "0Hello world! Append!a") == 0); 105 CHK(str_insert_char(&str, 13, 'A') == RES_OK); 106 CHK(strcmp(str_get(&str), "0Hello world!A Append!a") == 0); 107 108 CHK(str_append_char(&str, 'z') == RES_OK); 109 CHK(strcmp(str_get(&str), "0Hello world!A Append!az") == 0); 110 111 CHK(str_reserve(&str, 128) == RES_OK); 112 CHK(str_reserve(&str, 0) == RES_OK); 113 CHK(strcmp(str_get(&str), "0Hello world!A Append!az") == 0); 114 115 CHK(str_insert_char(&str, 13, '\0') == RES_OK); 116 CHK(strcmp(str_get(&str), "0Hello world!") == 0); 117 CHK(str_len(&str) == strlen("0Hello world!")); 118 CHK(str_append_char(&str, '\0') == RES_OK); 119 CHK(strcmp(str_get(&str), "0Hello world!") == 0); 120 CHK(str_len(&str) == strlen("0Hello world!")); 121 122 str_init(&allocator_proxy, &str2); 123 str_copy(&str2, &str); 124 CHK(strcmp(str_cget(&str2), "0Hello world!") == 0); 125 CHK(strcmp(str_cget(&str), "0Hello world!") == 0); 126 127 CHK(str_eq(&str, &str2) == 1); 128 129 CHK(str_hash(&str) == str_hash(&str2)); 130 str_clear(&str2); 131 CHK(str_hash(&str) != str_hash(&str2)); 132 133 str_copy_and_clear(&str2, &str); 134 CHK(strcmp(str_cget(&str2), "0Hello world!") == 0); 135 CHK(str_len(&str2) == strlen(str_cget(&str2))); 136 CHK(str_len(&str) == 0); 137 str_copy_and_release(&str, &str2); 138 CHK(strcmp(str_cget(&str), "0Hello world!") == 0); 139 140 str_init(&allocator_proxy, &str2); 141 str_set(&str2, "ABC Hello a0123456789ABCDEFbcdefgh world! insert"); 142 CHK(str_eq(&str, &str2) == 0); 143 CHK(str_cmp(&str, &str2) == strcmp(str_cget(&str), str_cget(&str2))); 144 145 CHK(str_hash(&str) != str_hash(&str2)); 146 str_copy_and_clear(&str, &str2); 147 CHK(!strcmp 148 (str_cget(&str), "ABC Hello a0123456789ABCDEFbcdefgh world! insert")); 149 CHK(str_len(&str) == strlen(str_cget(&str))); 150 151 str_set(&str2, "Hello world!"); 152 CHK(strcmp(str_cget(&str2), "Hello world!") == 0); 153 str_clear(&str2); 154 str_copy_and_clear(&str, &str2); 155 CHK(str_len(&str) == 0); 156 CHK(str_len(&str2) == 0); 157 CHK(strlen(str_cget(&str)) == 0); 158 159 str_copy_and_release(&str2, &str2); 160 161 str_set(&str, "Hello World!"); 162 str_copy_and_clear(&str, &str); 163 CHK(str_len(&str) == 0); 164 165 str_release(&str); 166 167 str_init(&allocator_proxy, &str); 168 CHK(str_printf(&str, "Hello world!") == RES_OK); 169 CHK(!strcmp(str_cget(&str), "Hello world!")); 170 CHK(str_printf(&str, "XARR-QRRC VA GUR QRNQ") == RES_OK); 171 CHK(!strcmp(str_cget(&str), "XARR-QRRC VA GUR QRNQ")); 172 CHK(str_printf(&str, "Rcvfbqr %d, GUR FUBERF BS URYY", 2) == RES_OK); 173 CHK(!strcmp(str_cget(&str), "Rcvfbqr 2, GUR FUBERF BS URYY")); 174 CHK(str_printf(&str, "%s:%lu:%s", __FILE__, 0xDECAFBADlu,__FILE__) == RES_OK); 175 CHK(!strcmp(str_cget(&str), __FILE__":3737844653:"__FILE__)); 176 str_release(&str); 177 178 str_init(&allocator_proxy, &str); 179 CHK(str_printf(&str, "Hello, ") == RES_OK); 180 CHK(str_append_printf(&str, "world!") == RES_OK); 181 CHK(!strcmp(str_cget(&str), "Hello, world!")); 182 CHK(str_printf(&str, "%s:%lu", __FILE__, 0xDECAFBADlu) == RES_OK); 183 CHK(str_append_printf(&str, ":%s", __FILE__) == RES_OK); 184 CHK(!strcmp(str_cget(&str), __FILE__":3737844653:"__FILE__)); 185 chk_vprintf(&str, "You should have received a copy of the %s %s %d", 186 "GNU", "General Public License", 3); 187 CHK(!strcmp(str_cget(&str), 188 "You should have received a copy of the " 189 "GNU General Public License 3")); 190 chk_append_vprintf(&str, " along with the %s library. If not, see %c%s%c.", 191 "RSys", '<', "http://www.gnu.org/licenses/", '>'); 192 CHK(!strcmp(str_cget(&str), 193 "You should have received a copy of the GNU General Public License 3 " 194 "along with the RSys library. If not, see <http://www.gnu.org/licenses/>.")); 195 str_release(&str); 196 197 str_init(&allocator_proxy, &str); 198 CHK(str_set(&str, "abcd") == RES_OK); 199 CHK(str_len(&str) == 4); 200 str_get(&str)[3] = '\0'; 201 CHK(str_len(&str) == 3); 202 203 check_memory_allocator(&allocator_proxy); 204 mem_shutdown_proxy_allocator(&allocator_proxy); 205 CHK(mem_allocated_size() == 0); 206 return 0; 207 }