Some stdio tests

This commit is contained in:
flysand7 2023-06-24 14:42:11 +11:00
parent 064dfe3a78
commit b6049794d1
3 changed files with 609 additions and 545 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ a.out
*.o *.o
*.4coder *.4coder
*.rdbg *.rdbg
test_folder/

View File

@ -526,6 +526,7 @@ int main(int argc, char **argv) {
FEATURE_END(); FEATURE_END();
FEATURE_START("ctype.h"); FEATURE_START("ctype.h");
{
// Test letters // Test letters
for(int i = 0; i != 10; ++i) { for(int i = 0; i != 10; ++i) {
int letter = random_between('a', 'z'); int letter = random_between('a', 'z');
@ -538,8 +539,67 @@ int main(int argc, char **argv) {
TEST(isalpha(digit) == 0, "isalpha('%c') returned true", digit); TEST(isalpha(digit) == 0, "isalpha('%c') returned true", digit);
TEST(isdigit(digit) != 0, "isdigit('%c') returned false", digit); TEST(isdigit(digit) != 0, "isdigit('%c') returned false", digit);
} }
}
FEATURE_END(); FEATURE_END();
FEATURE_START("IO functions (stdio.h)");
{
static int numbers[10] = {0,1,2,3,4,5,6,7,8,9};
// fread/fwrite
{
FILE *file = fopen("test_folder/file", "wb");
TEST(file != NULL, "Created file is NULL");
int cnt_written = fwrite(numbers, sizeof(int), 10, file);
TEST(cnt_written == 10, "fwrite didn't write all 10 objects");
TEST(fclose(file) == 0, "fclose didn't close the file");
TEST(rename("test_folder/file", "test_folder/file2") == 0, "Rename returned 0");
file = fopen("test_folder/file2", "rb");
TEST(file != NULL, "Created file is NULL");
int read_numbers[10];
int cnt_read = fread(numbers, sizeof(int), 10, file);
TEST(cnt_read == 10, "fread didn't read 10 objects");
bool all_ok = true;
for(int i = 0; i != 10; ++i) {
if(read_numbers[i] != numbers[i]) {
all_ok = false;
break;
}
}
TEST(all_ok, "The elements read didn't match the elements written");
TEST(fclose(file) == 0, "fclose didn't close the file");
TEST(remove("test_folder/file2") == 0, "remove didn't remove the file");
}
// Seeking and stuff
{
FILE *file = fopen("test_folder/seek", "wb");
TEST(file != NULL, "Created file is NULL");
TEST(fwrite(numbers, sizeof(int), 10, file) == 10, "fwrite didn't write all 10 objects");
TEST(fseek(file, 4*sizeof(int), SEEK_SET) == 0, "fseek couldn't seek to offset 4");
int num;
TEST(fread(&num, sizeof(int), 1, file) == 1, "fread didn't read the int");
TEST(num == 4, "Wrong value read at offset 4");
TEST(fseek(file, -4, SEEK_END) == 0, "fseek coudn't seek to the end");
TEST(fread(&num, sizeof(int), 1, file) == 1, "fread didn't read the int");
TEST(num == 9, "Wrong number read");
TEST(fclose(file) == 0, "fclose didn't close the file");
}
// Getchar, ungetchar
{
char str[] = "Hello, world!";
FILE *file = fopen("test_folder/getc", "wb");
TEST(file != NULL, "Created file is NULL");
TEST(fputs(str, file) >= 0, "fputs failed");
TEST(fputc('!', file) == '!', "fputc failed");
TEST(fflush(file) == 0, "fflush failed");
fclose(file);
file = freopen("test_folder/getc", "rb", file);
TEST(file != NULL, "Reopened file is NULL");
TEST(fgets(str, sizeof str, file) == str, "fgets failed");
TEST(fgetc(file) == '!', "fgetc read the wrong thing");
TEST(fclose(file) == 0, "fclose failed");
}
}
TESTS_PRINT_RESULT(); TESTS_PRINT_RESULT();
return 0; return 0;
} }

3
todo
View File

@ -53,6 +53,9 @@ stdio.h:
* tmpnam_s * tmpnam_s
* Printing 8.98846567431157854073e+307 * Printing 8.98846567431157854073e+307
* Printing negative zero * Printing negative zero
* Add functions for testing a file path, creating directories
* Fix the bug where if a file wasn't closed the thing crashes when
trying to flush the buffer
stdlib.h: stdlib.h:
* Strtod base 16 must be correctly rounded * Strtod base 16 must be correctly rounded