|
| 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 | +// * Fossil Logic Test Utilites |
| 20 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 21 | +// Setup steps for things like test fixtures and |
| 22 | +// mock objects are set here. |
| 23 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 24 | + |
| 25 | +// Define the test suite and add test cases |
| 26 | +FOSSIL_TEST_SUITE(cpp_input_suite); |
| 27 | + |
| 28 | +// Setup function for the test suite |
| 29 | +FOSSIL_SETUP(cpp_input_suite) { |
| 30 | + // Setup code here |
| 31 | +} |
| 32 | + |
| 33 | +// Teardown function for the test suite |
| 34 | +FOSSIL_TEARDOWN(cpp_input_suite) { |
| 35 | + // Teardown code here |
| 36 | +} |
| 37 | + |
| 38 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 39 | +// * Fossil Logic Test Cases |
| 40 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 41 | +// The test cases below are provided as samples, inspired |
| 42 | +// by the Meson build system's approach of using test cases |
| 43 | +// as samples for library usage. |
| 44 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 45 | + |
| 46 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream) { |
| 47 | + const char *input_data = "test input\n"; |
| 48 | + FILE *input_stream = tmpfile(); |
| 49 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 50 | + rewind(input_stream); |
| 51 | + |
| 52 | + char buf[20]; |
| 53 | + char *result = fossil_io_gets_from_stream(buf, sizeof(buf), input_stream); |
| 54 | + ASSUME_ITS_EQUAL_CSTR("test input", buf); |
| 55 | + ASSUME_NOT_CNULL(result); |
| 56 | + fclose(input_stream); |
| 57 | +} |
| 58 | + |
| 59 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_no_offensive) { |
| 60 | + char input[] = "This is a clean sentence.\n"; |
| 61 | + char expected[] = "This is a clean sentence."; |
| 62 | + char buffer[256]; |
| 63 | + |
| 64 | + FILE *stream = tmpfile(); |
| 65 | + fwrite(input, 1, strlen(input), stream); |
| 66 | + rewind(stream); |
| 67 | + char *result = fossil_io_gets_from_stream(buffer, sizeof(buffer), stream); |
| 68 | + fclose(stream); |
| 69 | + |
| 70 | + ASSUME_ITS_EQUAL_CSTR(expected, result); |
| 71 | +} |
| 72 | + |
| 73 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_with_punctuation) { |
| 74 | + char input[] = "This is a test with curse1, and racist_phrase1!\n"; |
| 75 | + char expected[] = "This is a test with ***, and ***!"; |
| 76 | + char buffer[256]; |
| 77 | + |
| 78 | + FILE *stream = tmpfile(); |
| 79 | + fwrite(input, 1, strlen(input), stream); |
| 80 | + rewind(stream); |
| 81 | + char *result = fossil_io_gets_from_stream(buffer, sizeof(buffer), stream); |
| 82 | + fclose(stream); |
| 83 | + |
| 84 | + ASSUME_ITS_EQUAL_CSTR(expected, result); |
| 85 | +} |
| 86 | + |
| 87 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_empty_input) { |
| 88 | + const char *input_data = "\n"; |
| 89 | + FILE *input_stream = tmpfile(); |
| 90 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 91 | + rewind(input_stream); |
| 92 | + |
| 93 | + char buf[20]; |
| 94 | + char *result = fossil_io_gets_from_stream(buf, sizeof(buf), input_stream); |
| 95 | + ASSUME_ITS_EQUAL_CSTR("", buf); |
| 96 | + ASSUME_NOT_CNULL(result); |
| 97 | + fclose(input_stream); |
| 98 | +} |
| 99 | + |
| 100 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_only_whitespace) { |
| 101 | + const char *input_data = " \n"; |
| 102 | + FILE *input_stream = tmpfile(); |
| 103 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 104 | + rewind(input_stream); |
| 105 | + |
| 106 | + char buf[20]; |
| 107 | + char *result = fossil_io_gets_from_stream(buf, sizeof(buf), input_stream); |
| 108 | + ASSUME_ITS_EQUAL_CSTR("", buf); |
| 109 | + ASSUME_NOT_CNULL(result); |
| 110 | + fclose(input_stream); |
| 111 | +} |
| 112 | + |
| 113 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_long_input) { |
| 114 | + const char *input_data = "This is a very long input string that exceeds the buffer size\n"; |
| 115 | + FILE *input_stream = tmpfile(); |
| 116 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 117 | + rewind(input_stream); |
| 118 | + |
| 119 | + char buf[20]; |
| 120 | + char *result = fossil_io_gets_from_stream(buf, sizeof(buf), input_stream); |
| 121 | + ASSUME_ITS_EQUAL_CSTR("This is a very long", buf); |
| 122 | + ASSUME_NOT_CNULL(result); |
| 123 | + fclose(input_stream); |
| 124 | +} |
| 125 | + |
| 126 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_ex) { |
| 127 | + const char *input_data = "test input\n"; |
| 128 | + FILE *input_stream = tmpfile(); |
| 129 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 130 | + rewind(input_stream); |
| 131 | + |
| 132 | + char buf[20]; |
| 133 | + int error_code = 0; |
| 134 | + char *result = fossil_io_gets_from_stream_ex(buf, sizeof(buf), input_stream, &error_code); |
| 135 | + ASSUME_ITS_EQUAL_CSTR("test input", buf); |
| 136 | + ASSUME_NOT_CNULL(result); |
| 137 | + fclose(input_stream); |
| 138 | +} |
| 139 | + |
| 140 | +FOSSIL_TEST_CASE(cpp_test_io_gets_utf8) { |
| 141 | + const char *input_data = "test input\n"; |
| 142 | + FILE *input_stream = tmpfile(); |
| 143 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 144 | + rewind(input_stream); |
| 145 | + |
| 146 | + char buf[20]; |
| 147 | + char *result = fossil_io_gets_utf8(buf, sizeof(buf), input_stream); |
| 148 | + ASSUME_ITS_EQUAL_CSTR("test input", buf); |
| 149 | + ASSUME_NOT_CNULL(result); |
| 150 | + fclose(input_stream); |
| 151 | +} |
| 152 | + |
| 153 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_class) { |
| 154 | + const char *input_data = "test input\n"; |
| 155 | + FILE *input_stream = tmpfile(); |
| 156 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 157 | + rewind(input_stream); |
| 158 | + |
| 159 | + char buf[20]; |
| 160 | + char *result = fossil::io::Input::gets_from_stream(buf, sizeof(buf), input_stream); |
| 161 | + ASSUME_ITS_EQUAL_CSTR("test input", buf); |
| 162 | + ASSUME_NOT_CNULL(result); |
| 163 | + fclose(input_stream); |
| 164 | +} |
| 165 | + |
| 166 | +FOSSIL_TEST_CASE(cpp_test_io_gets_from_stream_ex_class) { |
| 167 | + const char *input_data = "test input\n"; |
| 168 | + FILE *input_stream = tmpfile(); |
| 169 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 170 | + rewind(input_stream); |
| 171 | + |
| 172 | + char buf[20]; |
| 173 | + int error_code = 0; |
| 174 | + char *result = fossil::io::Input::gets_from_stream_ex(buf, sizeof(buf), input_stream, &error_code); |
| 175 | + ASSUME_ITS_EQUAL_CSTR("test input", buf); |
| 176 | + ASSUME_NOT_CNULL(result); |
| 177 | + fclose(input_stream); |
| 178 | +} |
| 179 | + |
| 180 | +FOSSIL_TEST_CASE(cpp_test_io_validate_input_buffer_class) { |
| 181 | + const char *buf = "test buffer"; |
| 182 | + size_t size = strlen(buf); |
| 183 | + int result = fossil::io::Input::validate_input_buffer(buf, size); |
| 184 | + ASSUME_ITS_EQUAL_I32(1, result); |
| 185 | +} |
| 186 | + |
| 187 | +FOSSIL_TEST_CASE(cpp_test_io_gets_utf8_class) { |
| 188 | + const char *input_data = "test input\n"; |
| 189 | + FILE *input_stream = tmpfile(); |
| 190 | + fwrite(input_data, 1, strlen(input_data), input_stream); |
| 191 | + rewind(input_stream); |
| 192 | + |
| 193 | + char buf[20]; |
| 194 | + char *result = fossil::io::Input::gets_utf8(buf, sizeof(buf), input_stream); |
| 195 | + ASSUME_ITS_EQUAL_CSTR("test input", buf); |
| 196 | + ASSUME_NOT_CNULL(result); |
| 197 | + fclose(input_stream); |
| 198 | +} |
| 199 | + |
| 200 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 201 | +// * Fossil Logic Test Pool |
| 202 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 203 | + |
| 204 | +FOSSIL_TEST_GROUP(cpp_input_tests) { |
| 205 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream); |
| 206 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_no_offensive); |
| 207 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_with_punctuation); |
| 208 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_empty_input); |
| 209 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_only_whitespace); |
| 210 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_long_input); |
| 211 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_ex); |
| 212 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_utf8); |
| 213 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_class); |
| 214 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_from_stream_ex_class); |
| 215 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_validate_input_buffer_class); |
| 216 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_utf8_class); |
| 217 | + |
| 218 | + FOSSIL_TEST_REGISTER(cpp_input_suite); |
| 219 | +} |
0 commit comments