|
15 | 15 |
|
16 | 16 | #include "fossil/io/framework.h" |
17 | 17 |
|
18 | | -#ifdef _WIN32 |
19 | | -// For Windows, use tmpfile and fwrite to simulate input |
20 | | -FILE *cpp_create_mock_input(const char *input) { |
21 | | - FILE *mock_input = tmpfile(); |
22 | | - fwrite(input, sizeof(char), strlen(input), mock_input); |
23 | | - rewind(mock_input); |
24 | | - return mock_input; |
25 | | -} |
26 | | -#else |
27 | | -// For Unix-like systems, use fmemopen |
28 | | -FILE *cpp_create_mock_input(const char *input) { |
29 | | - return fmemopen((void *)input, strlen(input), "r"); |
30 | | -} |
31 | | -#endif |
32 | | - |
33 | 18 | // * * * * * * * * * * * * * * * * * * * * * * * * |
34 | 19 | // * Fossil Logic Test Utilites |
35 | 20 | // * * * * * * * * * * * * * * * * * * * * * * * * |
@@ -58,68 +43,77 @@ FOSSIL_TEARDOWN(cpp_input_suite) { |
58 | 43 | // as samples for library usage. |
59 | 44 | // * * * * * * * * * * * * * * * * * * * * * * * * |
60 | 45 |
|
61 | | -FOSSIL_TEST_CASE(cpp_test_io_gets) { |
62 | | - const char *input = "Hello, World!\n"; |
| 46 | +FOSSIL_TEST_CASE(cpp_test_io_gets_empty_input) { |
| 47 | + const char *input = "\n"; |
63 | 48 | FILE *mock_input = cpp_create_mock_input(input); |
64 | 49 | char buf[50]; |
65 | 50 |
|
66 | 51 | // Pass the mock input stream directly to the function |
67 | 52 | char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input); |
68 | 53 | ASSUME_NOT_CNULL(result); |
69 | 54 | printf("buf: %s\n", buf); |
70 | | - ASSUME_ITS_EQUAL_CSTR("Hello, World!", buf); |
| 55 | + ASSUME_ITS_EQUAL_CSTR("", buf); |
71 | 56 |
|
72 | 57 | fclose(mock_input); |
73 | 58 | } |
74 | 59 |
|
75 | | -FOSSIL_TEST_CASE(cpp_test_io_gets_buffer_too_small) { |
| 60 | +FOSSIL_TEST_CASE(cpp_test_io_gets_null_buffer) { |
76 | 61 | const char *input = "Hello, World!\n"; |
77 | 62 | FILE *mock_input = cpp_create_mock_input(input); |
78 | | - char buf[5]; // Buffer smaller than the input |
79 | 63 |
|
80 | | - // Pass the mock input stream directly to the function |
81 | | - char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input); |
82 | | - ASSUME_NOT_CNULL(result); |
83 | | - printf("buf: %s\n", buf); |
84 | | - ASSUME_ITS_EQUAL_CSTR("Hell", buf); // Only part of the input should fit |
| 64 | + // Pass a NULL buffer to the function |
| 65 | + char *result = fossil_io_gets_from_stream(NULL, 50, mock_input); |
| 66 | + ASSUME_IS_NULL(result); |
85 | 67 |
|
86 | 68 | fclose(mock_input); |
87 | 69 | } |
88 | 70 |
|
89 | | -FOSSIL_TEST_CASE(cpp_test_io_gets_with_dialog) { |
90 | | - const char *input = "Hello, Dialog!\n"; |
| 71 | +FOSSIL_TEST_CASE(cpp_test_io_gets_null_stream) { |
| 72 | + char buf[50]; |
| 73 | + |
| 74 | + // Pass a NULL stream to the function |
| 75 | + char *result = fossil_io_gets_from_stream(buf, sizeof(buf), NULL); |
| 76 | + ASSUME_IS_NULL(result); |
| 77 | +} |
| 78 | + |
| 79 | +FOSSIL_TEST_CASE(cpp_test_io_gets_with_dialog_null_dialog) { |
| 80 | + const char *input = "Hello, World!\n"; |
91 | 81 | FILE *mock_input = cpp_create_mock_input(input); |
92 | 82 | char buf[50]; |
93 | | - const char *dialog = "Please enter input: "; |
94 | 83 |
|
95 | | - // Simulate dialog display and read input from the mock stream |
96 | | - printf("%s", dialog); |
97 | | - char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input); |
| 84 | + // Pass a NULL dialog to the function |
| 85 | + char *result = fossil_io_gets_with_dialog(buf, sizeof(buf), NULL); |
98 | 86 | ASSUME_NOT_CNULL(result); |
99 | | - ASSUME_ITS_EQUAL_CSTR("Hello, Dialog!", buf); |
| 87 | + ASSUME_ITS_EQUAL_CSTR("Hello, World!", buf); |
100 | 88 |
|
101 | 89 | fclose(mock_input); |
102 | 90 | } |
103 | 91 |
|
104 | | -FOSSIL_TEST_CASE(cpp_test_io_gets_with_dialog_empty_buffer) { |
| 92 | +FOSSIL_TEST_CASE(cpp_test_io_gets_with_dialog_long_input) { |
| 93 | + const char *input = "This is a very long input string that exceeds the buffer size.\n"; |
| 94 | + FILE *mock_input = cpp_create_mock_input(input); |
| 95 | + char buf[20]; |
105 | 96 | const char *dialog = "Please enter input: "; |
106 | | - char buf[1]; // Buffer size is too small to read anything |
107 | 97 |
|
108 | | - // Simulate dialog display and attempt to read input |
| 98 | + // Simulate dialog display and read input from the mock stream |
109 | 99 | printf("%s", dialog); |
110 | | - char *result = fossil_io_gets_from_stream(buf, sizeof(buf), stdin); |
111 | | - ASSUME_NOT_CNULL(result); // Should be NULL as buffer size is insufficient |
112 | | -} |
| 100 | + char *result = fossil_io_gets_with_dialog(buf, sizeof(buf), dialog); |
| 101 | + ASSUME_NOT_CNULL(result); |
| 102 | + ASSUME_ITS_EQUAL_CSTR("This is a very lo", buf); // Only part of the input should fit |
113 | 103 |
|
114 | | -// * * * * * * * * * * * * * * * * * * * * * * * * |
115 | | -// * Fossil Logic Test Pool |
116 | | -// * * * * * * * * * * * * * * * * * * * * * * * * |
| 104 | + fclose(mock_input); |
| 105 | +} |
117 | 106 |
|
118 | | -FOSSIL_TEST_GROUP(cpp_input_tests) { |
| 107 | +FOSSIL_TEST_REGISTER(cpp_input_suite); |
119 | 108 | FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets); |
120 | 109 | FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_buffer_too_small); |
121 | 110 | FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_with_dialog); |
122 | 111 | FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_with_dialog_empty_buffer); |
| 112 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_empty_input); |
| 113 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_null_buffer); |
| 114 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_null_stream); |
| 115 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_with_dialog_null_dialog); |
| 116 | + FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_with_dialog_long_input); |
123 | 117 |
|
124 | 118 | FOSSIL_TEST_REGISTER(cpp_input_suite); |
125 | 119 | } |
0 commit comments