|
| 1 | +/* |
| 2 | + * ----------------------------------------------------------------------------- |
| 3 | + * Project: Fossil Logic |
| 4 | + * |
| 5 | + * This file is part of the Fossil Logic project, which aims to develop high- |
| 6 | + * performance, cross-platform applications and libraries. The code contained |
| 7 | + * herein is subject to the terms and conditions defined in the project license. |
| 8 | + * |
| 9 | + * Author: Michael Gene Brockus (Dreamer) |
| 10 | + * |
| 11 | + * Copyright (C) 2024 Fossil Logic. All rights reserved. |
| 12 | + * ----------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | +#include <fossil/test/framework.h> |
| 15 | + |
| 16 | +#include "fossil/io/framework.h" |
| 17 | + |
| 18 | + |
| 19 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 20 | +// * Fossil Logic Test Utilites |
| 21 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 22 | +// Setup steps for things like test fixtures and |
| 23 | +// mock objects are set here. |
| 24 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 25 | + |
| 26 | +// Define the test suite and add test cases |
| 27 | +FOSSIL_TEST_SUITE(c_string_suite); |
| 28 | +fossil_fstream_t c_string; |
| 29 | + |
| 30 | +// Setup function for the test suite |
| 31 | +FOSSIL_SETUP(c_string_suite) { |
| 32 | + // Setup code here |
| 33 | +} |
| 34 | + |
| 35 | +// Teardown function for the test suite |
| 36 | +FOSSIL_TEARDOWN(c_string_suite) { |
| 37 | + // Teardown code here |
| 38 | +} |
| 39 | + |
| 40 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 41 | +// * Fossil Logic Test Cases |
| 42 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 43 | +// The test cases below are provided as samples, inspired |
| 44 | +// by the Meson build system's approach of using test cases |
| 45 | +// as samples for library usage. |
| 46 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 47 | + |
| 48 | +FOSSIL_TEST_CASE(c_test_cstring_create_and_free) { |
| 49 | + const char *init = "Hello, World!"; |
| 50 | + cstring str = fossil_io_cstring_create(init); |
| 51 | + ASSUME_NOT_CNULL(str); |
| 52 | + ASSUME_ITS_EQUAL_CSTR(init, str); |
| 53 | + fossil_io_cstring_free(str); |
| 54 | +} |
| 55 | + |
| 56 | +FOSSIL_TEST_CASE(c_test_cstring_copy) { |
| 57 | + const char *init = "Hello, World!"; |
| 58 | + cstring str = fossil_io_cstring_create(init); |
| 59 | + cstring copy = fossil_io_cstring_copy(str); |
| 60 | + ASSUME_NOT_CNULL(copy); |
| 61 | + ASSUME_ITS_EQUAL_CSTR(init, copy); |
| 62 | + fossil_io_cstring_free(str); |
| 63 | + fossil_io_cstring_free(copy); |
| 64 | +} |
| 65 | + |
| 66 | +FOSSIL_TEST_CASE(c_test_cstring_concat) { |
| 67 | + const char *s1 = "Hello, "; |
| 68 | + const char *s2 = "World!"; |
| 69 | + cstring result = fossil_io_cstring_concat(s1, s2); |
| 70 | + ASSUME_NOT_CNULL(result); |
| 71 | + ASSUME_ITS_EQUAL_CSTR("Hello, World!", result); |
| 72 | + fossil_io_cstring_free(result); |
| 73 | +} |
| 74 | + |
| 75 | +FOSSIL_TEST_CASE(c_test_cstring_length) { |
| 76 | + const char *str = "Hello, World!"; |
| 77 | + size_t length = fossil_io_cstring_length(str); |
| 78 | + ASSUME_ITS_EQUAL_SIZE(strlen(str), length); |
| 79 | +} |
| 80 | + |
| 81 | +FOSSIL_TEST_CASE(c_test_cstring_compare) { |
| 82 | + const char *s1 = "Hello"; |
| 83 | + const char *s2 = "Hello"; |
| 84 | + const char *s3 = "World"; |
| 85 | + ASSUME_ITS_EQUAL_I32(0, fossil_io_cstring_compare(s1, s2)); |
| 86 | + ASSUME_ITS_TRUE(fossil_io_cstring_compare(s1, s3) < 0); |
| 87 | + ASSUME_ITS_TRUE(fossil_io_cstring_compare(s3, s1) > 0); |
| 88 | +} |
| 89 | + |
| 90 | +FOSSIL_TEST_CASE(c_test_cstring_trim) { |
| 91 | + cstring str = fossil_io_cstring_create(" Hello, World! "); |
| 92 | + fossil_io_cstring_trim(str); |
| 93 | + ASSUME_ITS_EQUAL_CSTR("Hello, World!", str); |
| 94 | + fossil_io_cstring_free(str); |
| 95 | +} |
| 96 | + |
| 97 | +FOSSIL_TEST_CASE(c_test_cstring_split) { |
| 98 | + const char *str = "Hello,World,Test"; |
| 99 | + size_t count; |
| 100 | + cstring *result = fossil_io_cstring_split(str, ',', &count); |
| 101 | + ASSUME_NOT_CNULL(result); |
| 102 | + ASSUME_ITS_EQUAL_SIZE(3, count); |
| 103 | + ASSUME_ITS_EQUAL_CSTR("Hello", result[0]); |
| 104 | + ASSUME_ITS_EQUAL_CSTR("World", result[1]); |
| 105 | + ASSUME_ITS_EQUAL_CSTR("Test", result[2]); |
| 106 | + for (size_t i = 0; i < count; i++) { |
| 107 | + fossil_io_cstring_free(result[i]); |
| 108 | + } |
| 109 | + free(result); |
| 110 | +} |
| 111 | + |
| 112 | +FOSSIL_TEST_CASE(c_test_cstring_replace) { |
| 113 | + const char *str = "Hello, World!"; |
| 114 | + const char *old = "World"; |
| 115 | + const char *new_str = "Fossil"; |
| 116 | + cstring result = fossil_io_cstring_replace(str, old, new_str); |
| 117 | + ASSUME_NOT_CNULL(result); |
| 118 | + ASSUME_ITS_EQUAL_CSTR("Hello, Fossil!", result); |
| 119 | + fossil_io_cstring_free(result); |
| 120 | +} |
| 121 | + |
| 122 | +FOSSIL_TEST_CASE(c_test_cstring_to_upper) { |
| 123 | + cstring str = fossil_io_cstring_create("Hello, World!"); |
| 124 | + cstring result = fossil_io_cstring_to_upper(str); |
| 125 | + ASSUME_NOT_CNULL(result); |
| 126 | + ASSUME_ITS_EQUAL_CSTR("HELLO, WORLD!", result); |
| 127 | + fossil_io_cstring_free(str); |
| 128 | + fossil_io_cstring_free(result); |
| 129 | +} |
| 130 | + |
| 131 | +FOSSIL_TEST_CASE(c_test_cstring_to_lower) { |
| 132 | + cstring str = fossil_io_cstring_create("Hello, World!"); |
| 133 | + cstring result = fossil_io_cstring_to_lower(str); |
| 134 | + ASSUME_NOT_CNULL(result); |
| 135 | + ASSUME_ITS_EQUAL_CSTR("hello, world!", result); |
| 136 | + fossil_io_cstring_free(str); |
| 137 | + fossil_io_cstring_free(result); |
| 138 | +} |
| 139 | + |
| 140 | +FOSSIL_TEST_CASE(c_test_cstring_starts_with) { |
| 141 | + const char *str = "Hello, World!"; |
| 142 | + const char *prefix = "Hello"; |
| 143 | + ASSUME_ITS_TRUE(fossil_io_cstring_starts_with(str, prefix)); |
| 144 | +} |
| 145 | + |
| 146 | +FOSSIL_TEST_CASE(c_test_cstring_ends_with) { |
| 147 | + const char *str = "Hello, World!"; |
| 148 | + const char *suffix = "World!"; |
| 149 | + ASSUME_ITS_TRUE(fossil_io_cstring_ends_with(str, suffix)); |
| 150 | +} |
| 151 | + |
| 152 | +FOSSIL_TEST_CASE(c_test_cstring_substring) { |
| 153 | + const char *str = "Hello, World!"; |
| 154 | + cstring result = fossil_io_cstring_substring(str, 7, 5); |
| 155 | + ASSUME_NOT_CNULL(result); |
| 156 | + ASSUME_ITS_EQUAL_CSTR("World", result); |
| 157 | + fossil_io_cstring_free(result); |
| 158 | +} |
| 159 | + |
| 160 | +FOSSIL_TEST_CASE(c_test_cstring_reverse) { |
| 161 | + cstring str = fossil_io_cstring_create("Hello, World!"); |
| 162 | + cstring result = fossil_io_cstring_reverse(str); |
| 163 | + ASSUME_NOT_CNULL(result); |
| 164 | + ASSUME_ITS_EQUAL_CSTR("!dlroW ,olleH", result); |
| 165 | + fossil_io_cstring_free(str); |
| 166 | + fossil_io_cstring_free(result); |
| 167 | +} |
| 168 | + |
| 169 | +FOSSIL_TEST_CASE(c_test_cstring_contains) { |
| 170 | + const char *str = "Hello, World!"; |
| 171 | + const char *substr = "World"; |
| 172 | + ASSUME_ITS_TRUE(fossil_io_cstring_contains(str, substr)); |
| 173 | +} |
| 174 | + |
| 175 | +FOSSIL_TEST_CASE(c_test_cstring_repeat) { |
| 176 | + const char *str = "Hello"; |
| 177 | + cstring result = fossil_io_cstring_repeat(str, 3); |
| 178 | + ASSUME_NOT_CNULL(result); |
| 179 | + ASSUME_ITS_EQUAL_CSTR("HelloHelloHello", result); |
| 180 | + fossil_io_cstring_free(result); |
| 181 | +} |
| 182 | + |
| 183 | +FOSSIL_TEST_CASE(c_test_cstring_strip) { |
| 184 | + const char *str = "!!!Hello, World!!!"; |
| 185 | + cstring result = fossil_io_cstring_strip(str, '!'); |
| 186 | + ASSUME_NOT_CNULL(result); |
| 187 | + ASSUME_ITS_EQUAL_CSTR("Hello, World", result); |
| 188 | + fossil_io_cstring_free(result); |
| 189 | +} |
| 190 | + |
| 191 | +FOSSIL_TEST_CASE(c_test_cstring_count) { |
| 192 | + const char *str = "Hello, World! Hello, Fossil!"; |
| 193 | + const char *substr = "Hello"; |
| 194 | + size_t count = fossil_io_cstring_count(str, substr); |
| 195 | + ASSUME_ITS_EQUAL_SIZE(2, count); |
| 196 | +} |
| 197 | + |
| 198 | +FOSSIL_TEST_CASE(c_test_cstring_pad_left) { |
| 199 | + const char *str = "Hello"; |
| 200 | + cstring result = fossil_io_cstring_pad_left(str, 10, '*'); |
| 201 | + ASSUME_NOT_CNULL(result); |
| 202 | + ASSUME_ITS_EQUAL_CSTR("*****Hello", result); |
| 203 | + fossil_io_cstring_free(result); |
| 204 | +} |
| 205 | + |
| 206 | +FOSSIL_TEST_CASE(c_test_cstring_pad_right) { |
| 207 | + const char *str = "Hello"; |
| 208 | + cstring result = fossil_io_cstring_pad_right(str, 10, '*'); |
| 209 | + ASSUME_NOT_CNULL(result); |
| 210 | + ASSUME_ITS_EQUAL_CSTR("Hello*****", result); |
| 211 | + fossil_io_cstring_free(result); |
| 212 | +} |
| 213 | + |
| 214 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 215 | +// * Fossil Logic Test Pool |
| 216 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 217 | + |
| 218 | +FOSSIL_TEST_GROUP(c_string_tests) { |
| 219 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_create_and_free); |
| 220 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_copy); |
| 221 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_concat); |
| 222 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_length); |
| 223 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_compare); |
| 224 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_trim); |
| 225 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_split); |
| 226 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_replace); |
| 227 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_to_upper); |
| 228 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_to_lower); |
| 229 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_starts_with); |
| 230 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_ends_with); |
| 231 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_substring); |
| 232 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_reverse); |
| 233 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_contains); |
| 234 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_repeat); |
| 235 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_strip); |
| 236 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_count); |
| 237 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_pad_left); |
| 238 | + FOSSIL_TEST_ADD(c_string_suite, c_test_cstring_pad_right); |
| 239 | + |
| 240 | + FOSSIL_TEST_REGISTER(c_string_suite); |
| 241 | +} |
0 commit comments