Skip to content

Commit 25d4e4c

Browse files
update input cases
1 parent d1adb74 commit 25d4e4c

File tree

2 files changed

+72
-84
lines changed

2 files changed

+72
-84
lines changed

code/tests/cases/test_input.c

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@
1515

1616
#include "fossil/io/framework.h"
1717

18-
#ifdef _WIN32
19-
// For Windows, use tmpfile and fwrite to simulate input
20-
FILE *c_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 *c_create_mock_input(const char *input) {
29-
return fmemopen((void *)input, strlen(input), "r");
30-
}
31-
#endif
32-
3318
// * * * * * * * * * * * * * * * * * * * * * * * *
3419
// * Fossil Logic Test Utilites
3520
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -58,68 +43,77 @@ FOSSIL_TEARDOWN(c_input_suite) {
5843
// as samples for library usage.
5944
// * * * * * * * * * * * * * * * * * * * * * * * *
6045

61-
FOSSIL_TEST_CASE(c_test_io_gets) {
62-
const char *input = "Hello, World!\n";
46+
FOSSIL_TEST_CASE(c_test_io_gets_empty_input) {
47+
const char *input = "\n";
6348
FILE *mock_input = c_create_mock_input(input);
6449
char buf[50];
6550

6651
// Pass the mock input stream directly to the function
6752
char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input);
6853
ASSUME_NOT_CNULL(result);
6954
printf("buf: %s\n", buf);
70-
ASSUME_ITS_EQUAL_CSTR("Hello, World!", buf);
55+
ASSUME_ITS_EQUAL_CSTR("", buf);
7156

7257
fclose(mock_input);
7358
}
7459

75-
FOSSIL_TEST_CASE(c_test_io_gets_buffer_too_small) {
60+
FOSSIL_TEST_CASE(c_test_io_gets_null_buffer) {
7661
const char *input = "Hello, World!\n";
7762
FILE *mock_input = c_create_mock_input(input);
78-
char buf[5]; // Buffer smaller than the input
7963

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);
8567

8668
fclose(mock_input);
8769
}
8870

89-
FOSSIL_TEST_CASE(c_test_io_gets_with_dialog) {
90-
const char *input = "Hello, Dialog!\n";
71+
FOSSIL_TEST_CASE(c_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(c_test_io_gets_with_dialog_null_dialog) {
80+
const char *input = "Hello, World!\n";
9181
FILE *mock_input = c_create_mock_input(input);
9282
char buf[50];
93-
const char *dialog = "Please enter input: ";
9483

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);
9886
ASSUME_NOT_CNULL(result);
99-
ASSUME_ITS_EQUAL_CSTR("Hello, Dialog!", buf);
87+
ASSUME_ITS_EQUAL_CSTR("Hello, World!", buf);
10088

10189
fclose(mock_input);
10290
}
10391

104-
FOSSIL_TEST_CASE(c_test_io_gets_with_dialog_empty_buffer) {
92+
FOSSIL_TEST_CASE(c_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 = c_create_mock_input(input);
95+
char buf[20];
10596
const char *dialog = "Please enter input: ";
106-
char buf[1]; // Buffer size is too small to read anything
10797

108-
// Simulate dialog display and attempt to read input
98+
// Simulate dialog display and read input from the mock stream
10999
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
113103

114-
// * * * * * * * * * * * * * * * * * * * * * * * *
115-
// * Fossil Logic Test Pool
116-
// * * * * * * * * * * * * * * * * * * * * * * * *
104+
fclose(mock_input);
105+
}
117106

118-
FOSSIL_TEST_GROUP(c_input_tests) {
107+
FOSSIL_TEST_REGISTER(c_input_suite);
119108
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets);
120109
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_buffer_too_small);
121110
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_with_dialog);
122111
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_with_dialog_empty_buffer);
112+
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_empty_input);
113+
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_null_buffer);
114+
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_null_stream);
115+
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_with_dialog_null_dialog);
116+
FOSSIL_TEST_ADD(c_input_suite, c_test_io_gets_with_dialog_long_input);
123117

124118
FOSSIL_TEST_REGISTER(c_input_suite);
125119
}

code/tests/cases/test_input.cpp

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@
1515

1616
#include "fossil/io/framework.h"
1717

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-
3318
// * * * * * * * * * * * * * * * * * * * * * * * *
3419
// * Fossil Logic Test Utilites
3520
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -58,68 +43,77 @@ FOSSIL_TEARDOWN(cpp_input_suite) {
5843
// as samples for library usage.
5944
// * * * * * * * * * * * * * * * * * * * * * * * *
6045

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";
6348
FILE *mock_input = cpp_create_mock_input(input);
6449
char buf[50];
6550

6651
// Pass the mock input stream directly to the function
6752
char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input);
6853
ASSUME_NOT_CNULL(result);
6954
printf("buf: %s\n", buf);
70-
ASSUME_ITS_EQUAL_CSTR("Hello, World!", buf);
55+
ASSUME_ITS_EQUAL_CSTR("", buf);
7156

7257
fclose(mock_input);
7358
}
7459

75-
FOSSIL_TEST_CASE(cpp_test_io_gets_buffer_too_small) {
60+
FOSSIL_TEST_CASE(cpp_test_io_gets_null_buffer) {
7661
const char *input = "Hello, World!\n";
7762
FILE *mock_input = cpp_create_mock_input(input);
78-
char buf[5]; // Buffer smaller than the input
7963

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);
8567

8668
fclose(mock_input);
8769
}
8870

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";
9181
FILE *mock_input = cpp_create_mock_input(input);
9282
char buf[50];
93-
const char *dialog = "Please enter input: ";
9483

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);
9886
ASSUME_NOT_CNULL(result);
99-
ASSUME_ITS_EQUAL_CSTR("Hello, Dialog!", buf);
87+
ASSUME_ITS_EQUAL_CSTR("Hello, World!", buf);
10088

10189
fclose(mock_input);
10290
}
10391

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];
10596
const char *dialog = "Please enter input: ";
106-
char buf[1]; // Buffer size is too small to read anything
10797

108-
// Simulate dialog display and attempt to read input
98+
// Simulate dialog display and read input from the mock stream
10999
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
113103

114-
// * * * * * * * * * * * * * * * * * * * * * * * *
115-
// * Fossil Logic Test Pool
116-
// * * * * * * * * * * * * * * * * * * * * * * * *
104+
fclose(mock_input);
105+
}
117106

118-
FOSSIL_TEST_GROUP(cpp_input_tests) {
107+
FOSSIL_TEST_REGISTER(cpp_input_suite);
119108
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets);
120109
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_buffer_too_small);
121110
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_gets_with_dialog);
122111
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);
123117

124118
FOSSIL_TEST_REGISTER(cpp_input_suite);
125119
}

0 commit comments

Comments
 (0)