32_bit_os/programs/stdlib/test/test_os.c
2023-01-04 22:37:08 -05:00

279 lines
8.1 KiB
C

#include "test_os.h"
#include "os.h"
#include "status.h"
#include "unity.h"
void test_open_filename_empty_string() {
TEST_ASSERT_EQUAL(-EINVARG, os_fopen("", "r"));
}
void test_open_filename_file_not_there() {
TEST_ASSERT_EQUAL(-EIO, os_fopen("0:/missing.txt", "r"));
}
void test_open_filename_file_there() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_open_filename_without_disk() {
int f = os_fopen("hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_open_multiple() {
int first_open = os_fopen("0:/hello.txt", "r");
int second_open = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, first_open);
TEST_ASSERT_GREATER_OR_EQUAL(1, second_open);
TEST_ASSERT_NOT_EQUAL(first_open, second_open);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(first_open));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(second_open));
}
void test_open_bad_disk() {
TEST_ASSERT_EQUAL(-EIO, os_fopen("1:/hello.txt", "r"));
}
void test_open_invalid_mode() {
TEST_ASSERT_EQUAL(-EINVARG, os_fopen("0:/hello.txt", "x"));
}
void test_open_mode_r() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_open_mode_wa() {
//TODO will need to be fixed when writable FS implemented
TEST_ASSERT_EQUAL(-ERDONLY, os_fopen("0:/hello.txt", "w"));
TEST_ASSERT_EQUAL(-ERDONLY, os_fopen("0:/hello.txt", "a"));
}
void test_open_mode_empty() {
TEST_ASSERT_EQUAL(-EINVARG, os_fopen("0:/hello.txt", ""));
}
void test_read_zero_size() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
char buf[100];
TEST_ASSERT_EQUAL(-EINVARG, os_fread(buf, 0, 1, f));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_read_zero_nmemb() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
char buf[100];
TEST_ASSERT_EQUAL(-EINVARG, os_fread(buf, 1, 0, f));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_read_malformed_fd() {
char buf[100];
TEST_ASSERT_EQUAL(-EINVARG, os_fread(buf, 1, 0, -1));
}
void test_read_invalid_fd() {
char buf[100];
TEST_ASSERT_EQUAL(-EINVARG, os_fread(buf, 1, 0, 2000));
}
void test_read_ptr_null() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(-EINVARG, os_fread(NULL, 1, 1, f));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_close_invalid_fd() {
TEST_ASSERT_EQUAL(-EIO, os_fclose(2000));
}
void test_close_malformed_fd() {
TEST_ASSERT_EQUAL(-EIO, os_fclose(-1));
}
void test_close_valid_fd() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_read_hello_txt() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
char expected[100] = "Hello world\n";
char buf[100];
int ret = os_fread(buf, 12, 1, f);
TEST_ASSERT_EQUAL(1, ret);
TEST_ASSERT_EQUAL_CHAR_ARRAY(expected, buf, 12);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_read_hello_txt_one_byte_at_a_time() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
char *expected = "Hello world\n ";
char buf[100];
int ret = 0;
int total = 0;
while (ret = os_fread(buf, 1, 1, f), ret == 1 && total < 15) {
TEST_ASSERT_EQUAL(1, ret);
TEST_ASSERT_EQUAL_CHAR(*expected, *buf);
expected += 1;
total += 1;
}
TEST_ASSERT_EQUAL(12, total);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_read_hello_txt_size_two_bytes() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
char *expected = "Hello world\n ";
char buf[100];
int ret = 0;
int total = 0;
while (ret = os_fread(buf, 2, 1, f), ret == 1 && total < 7) {
TEST_ASSERT_EQUAL_CHAR_ARRAY(expected, buf, 2);
total += 1;
expected += 2;
}
TEST_ASSERT_EQUAL(6, total);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_read_hello_txt_size_one_byte_two_memb() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
char *expected = "Hello world\n ";
char buf[100];
int ret = 0;
int total = 0;
while (ret = os_fread(buf, 1, 2, f), ret == 2 && total < 15) {
TEST_ASSERT_EQUAL_CHAR_ARRAY(expected, buf, 2);
total += 1;
expected += 2;
}
TEST_ASSERT_EQUAL(6, total);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_stat_invalid_fd() {
struct file_stat s;
TEST_ASSERT_EQUAL(-EIO, os_fstat(2000, &s));
}
void test_stat_malformed_fd() {
struct file_stat s;
TEST_ASSERT_EQUAL(-EIO, os_fstat(-1, &s));
}
void test_stat_valid_fd() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
struct file_stat s;
TEST_ASSERT_EQUAL(ALL_OK, os_fstat(f, &s));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_stat_hello_txt() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
struct file_stat s;
TEST_ASSERT_EQUAL(ALL_OK, os_fstat(f, &s));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
TEST_ASSERT_EQUAL(12, s.filesize);
}
void test_seek_invalid_fd() {
TEST_ASSERT_EQUAL(-EIO, os_fseek(2000, 0, SEEK_SET));
}
void test_seek_malformed_fd() {
TEST_ASSERT_EQUAL(-EIO, os_fseek(-1, 0, SEEK_SET));
}
void test_seek_valid_fd() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(ALL_OK, os_fseek(f, 0, SEEK_SET));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_seek_invalid_whence() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(-EINVARG, os_fseek(f, 0, 1000));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_seek_whence_end() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(-EUNIMP, os_fseek(f, 0, SEEK_END));
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_seek_whence_set() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(ALL_OK, os_fseek(f, 6, SEEK_SET));
char buf[100];
TEST_ASSERT_EQUAL(1, os_fread(buf, 5, 1, f));
TEST_ASSERT_EQUAL_CHAR_ARRAY("world", buf, 5);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_seek_whence_cur() {
int f = os_fopen("0:/hello.txt", "r");
TEST_ASSERT_GREATER_OR_EQUAL(1, f);
TEST_ASSERT_EQUAL(ALL_OK, os_fseek(f, 3, SEEK_CUR));
TEST_ASSERT_EQUAL(ALL_OK, os_fseek(f, 3, SEEK_CUR));
char buf[100];
TEST_ASSERT_EQUAL(1, os_fread(buf, 5, 1, f));
TEST_ASSERT_EQUAL_CHAR_ARRAY("world", buf, 5);
TEST_ASSERT_EQUAL(ALL_OK, os_fclose(f));
}
void test_os() {
RUN_TEST(test_open_filename_empty_string);
RUN_TEST(test_open_filename_file_not_there);
RUN_TEST(test_open_filename_file_there);
RUN_TEST(test_open_multiple);
RUN_TEST(test_open_bad_disk);
RUN_TEST(test_open_invalid_mode);
RUN_TEST(test_open_mode_r);
RUN_TEST(test_open_mode_wa);
RUN_TEST(test_open_mode_empty);
RUN_TEST(test_open_filename_without_disk);
RUN_TEST(test_read_zero_size);
RUN_TEST(test_read_zero_nmemb);
RUN_TEST(test_read_malformed_fd);
RUN_TEST(test_read_invalid_fd);
RUN_TEST(test_read_ptr_null);
RUN_TEST(test_close_invalid_fd);
RUN_TEST(test_close_malformed_fd);
RUN_TEST(test_close_valid_fd);
RUN_TEST(test_stat_invalid_fd);
RUN_TEST(test_stat_malformed_fd);
RUN_TEST(test_stat_valid_fd);
RUN_TEST(test_stat_hello_txt);
RUN_TEST(test_seek_invalid_fd);
RUN_TEST(test_seek_malformed_fd);
RUN_TEST(test_seek_valid_fd);
RUN_TEST(test_seek_invalid_whence);
RUN_TEST(test_seek_whence_end);
RUN_TEST(test_seek_whence_set);
RUN_TEST(test_seek_whence_cur);
RUN_TEST(test_read_hello_txt);
RUN_TEST(test_read_hello_txt_one_byte_at_a_time);
RUN_TEST(test_read_hello_txt_size_two_bytes);
RUN_TEST(test_read_hello_txt_size_one_byte_two_memb);
}