feat(file): implemented read file tests
This commit is contained in:
parent
a74bb3cb45
commit
d98cb6c4ba
4 changed files with 179 additions and 7 deletions
|
|
@ -50,7 +50,7 @@ CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file);
|
||||||
|
|
||||||
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size);
|
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size);
|
||||||
|
|
||||||
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buffer, unsigned int bytes_to_read, unsigned int *bytes_read);
|
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read);
|
||||||
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read);
|
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read);
|
||||||
|
|
||||||
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written);
|
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written);
|
||||||
|
|
|
||||||
|
|
@ -164,13 +164,13 @@ CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsign
|
||||||
return COSMS_CORE_FILE_OK;
|
return COSMS_CORE_FILE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buffer, unsigned int bytes_to_read, unsigned int *bytes_read) {
|
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) {
|
||||||
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
|
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
|
||||||
return COSMS_CORE_FILE_INVALID_OPERATION;
|
return COSMS_CORE_FILE_INVALID_OPERATION;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
int read_bytes = read(file->native_file, *buffer, bytes_to_read);
|
int read_bytes = read(file->native_file, buffer, bytes_to_read);
|
||||||
if (read_bytes == -1) {
|
if (read_bytes == -1) {
|
||||||
return COSMS_CORE_FILE_FAILED_TO_READ;
|
return COSMS_CORE_FILE_FAILED_TO_READ;
|
||||||
}
|
}
|
||||||
|
|
@ -179,7 +179,7 @@ CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buf
|
||||||
(*bytes_read) = read_bytes;
|
(*bytes_read) = read_bytes;
|
||||||
}
|
}
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
if (ReadFile(file->native_file, *buffer, bytes_to_read, (LPDWORD)bytes_read, NULL) == 0) {
|
if (ReadFile(file->native_file, buffer, bytes_to_read, (LPDWORD)bytes_read, NULL) == 0) {
|
||||||
return COSMS_CORE_FILE_FAILED_TO_READ;
|
return COSMS_CORE_FILE_FAILED_TO_READ;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -242,7 +242,167 @@ COSMS_CORE_TEST_TEST(file_size_non_existing_file,
|
||||||
return NULL;
|
return NULL;
|
||||||
)
|
)
|
||||||
|
|
||||||
COSMS_CORE_TEST_TEST(file_read,
|
COSMS_CORE_TEST_TEST(file_read_small_file,
|
||||||
|
struct cosms_core_file file;
|
||||||
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int bytes_to_read = 13;
|
||||||
|
unsigned int bytes_read = 0;
|
||||||
|
|
||||||
|
char buffer[14];
|
||||||
|
error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bytes_to_read != bytes_read || strcmp(buffer, "Hello, World!") != 0) {
|
||||||
|
return "result does not match expected result";
|
||||||
|
}
|
||||||
|
|
||||||
|
error = cosms_core_file_close(&file);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
)
|
||||||
|
|
||||||
|
COSMS_CORE_TEST_TEST(file_read_large_file,
|
||||||
|
struct cosms_core_file file;
|
||||||
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long bytes_to_read = 5242880000ULL;
|
||||||
|
unsigned long long bytes_read = 0;
|
||||||
|
|
||||||
|
char *buffer = NULL;
|
||||||
|
error = cosms_core_file_read_all(&file, &buffer, &bytes_read);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *expected_result = (char*)malloc((bytes_to_read + 1) * sizeof(char));
|
||||||
|
if (expected_result == NULL) {
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
error = cosms_core_file_close(&file);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
return "Failed to allocate memory";
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned long long index = 0; index < bytes_to_read; index += 1) {
|
||||||
|
expected_result[index] = 'a';
|
||||||
|
}
|
||||||
|
|
||||||
|
expected_result[bytes_to_read] = '\0';
|
||||||
|
if (bytes_to_read != bytes_read || strcmp(buffer, expected_result) != 0) {
|
||||||
|
free(expected_result);
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
return "result does not match expected result";
|
||||||
|
}
|
||||||
|
|
||||||
|
free(expected_result);
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
error = cosms_core_file_close(&file);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
)
|
||||||
|
|
||||||
|
COSMS_CORE_TEST_TEST(file_read_non_existing_file,
|
||||||
|
struct cosms_core_file file;
|
||||||
|
file.mode = COSMS_CORE_FILE_MODE_READ;
|
||||||
|
file.native_file = 0;
|
||||||
|
|
||||||
|
unsigned int bytes_to_read = 13;
|
||||||
|
unsigned int bytes_read = 0;
|
||||||
|
|
||||||
|
char buffer[14];
|
||||||
|
CosmsCoreFileError error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read);
|
||||||
|
if (error != COSMS_CORE_FILE_FAILED_TO_READ) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
)
|
||||||
|
|
||||||
|
COSMS_CORE_TEST_TEST(file_read_full_non_existing_file,
|
||||||
|
struct cosms_core_file file;
|
||||||
|
file.mode = COSMS_CORE_FILE_MODE_READ;
|
||||||
|
file.native_file = 0;
|
||||||
|
|
||||||
|
unsigned long long bytes_read = 0;
|
||||||
|
char *buffer = NULL;
|
||||||
|
CosmsCoreFileError error = cosms_core_file_read_all(&file, &buffer, &bytes_read);
|
||||||
|
if (error != COSMS_CORE_FILE_COULD_NOT_READ_SIZE) {
|
||||||
|
if (error == COSMS_CORE_FILE_OK) {
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
)
|
||||||
|
|
||||||
|
COSMS_CORE_TEST_TEST(file_read_file_using_wrong_mode,
|
||||||
|
struct cosms_core_file file;
|
||||||
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_APPEND);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int bytes_to_read = 13;
|
||||||
|
unsigned int bytes_read = 0;
|
||||||
|
|
||||||
|
char buffer[14];
|
||||||
|
error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read);
|
||||||
|
if (error != COSMS_CORE_FILE_INVALID_OPERATION) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
error = cosms_core_file_close(&file);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
)
|
||||||
|
|
||||||
|
COSMS_CORE_TEST_TEST(file_read_full_file_using_wrong_mode,
|
||||||
|
struct cosms_core_file file;
|
||||||
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_APPEND);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long bytes_read = 0;
|
||||||
|
char *buffer = NULL;
|
||||||
|
error = cosms_core_file_read_all(&file, &buffer, &bytes_read);
|
||||||
|
if (error != COSMS_CORE_FILE_INVALID_OPERATION) {
|
||||||
|
if (error == COSMS_CORE_FILE_OK) {
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
error = cosms_core_file_close(&file);
|
||||||
|
if (error != COSMS_CORE_FILE_OK) {
|
||||||
|
return cosms_core_file_error_string(error);
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,13 @@ COSMS_CORE_TEST_DEFINE(file_size_small_file);
|
||||||
COSMS_CORE_TEST_DEFINE(file_size_large);
|
COSMS_CORE_TEST_DEFINE(file_size_large);
|
||||||
COSMS_CORE_TEST_DEFINE(file_size_non_existing_file);
|
COSMS_CORE_TEST_DEFINE(file_size_non_existing_file);
|
||||||
|
|
||||||
COSMS_CORE_TEST_DEFINE(file_read);
|
COSMS_CORE_TEST_DEFINE(file_read_small_file);
|
||||||
|
COSMS_CORE_TEST_DEFINE(file_read_large_file);
|
||||||
|
COSMS_CORE_TEST_DEFINE(file_read_non_existing_file);
|
||||||
|
COSMS_CORE_TEST_DEFINE(file_read_full_non_existing_file);
|
||||||
|
COSMS_CORE_TEST_DEFINE(file_read_file_using_wrong_mode);
|
||||||
|
COSMS_CORE_TEST_DEFINE(file_read_full_file_using_wrong_mode);
|
||||||
|
|
||||||
COSMS_CORE_TEST_DEFINE(file_write);
|
COSMS_CORE_TEST_DEFINE(file_write);
|
||||||
COSMS_CORE_TEST_DEFINE(file_delete);
|
COSMS_CORE_TEST_DEFINE(file_delete);
|
||||||
|
|
||||||
|
|
@ -50,7 +56,13 @@ COSMS_CORE_TEST_EXPORT(file,
|
||||||
COSMS_CORE_TEST_EXPORT_TEST(file_size_large),
|
COSMS_CORE_TEST_EXPORT_TEST(file_size_large),
|
||||||
COSMS_CORE_TEST_EXPORT_TEST(file_size_non_existing_file),
|
COSMS_CORE_TEST_EXPORT_TEST(file_size_non_existing_file),
|
||||||
|
|
||||||
COSMS_CORE_TEST_EXPORT_TEST(file_read),
|
COSMS_CORE_TEST_EXPORT_TEST(file_read_small_file),
|
||||||
|
COSMS_CORE_TEST_EXPORT_TEST(file_read_large_file),
|
||||||
|
COSMS_CORE_TEST_EXPORT_TEST(file_read_non_existing_file),
|
||||||
|
COSMS_CORE_TEST_EXPORT_TEST(file_read_full_non_existing_file),
|
||||||
|
COSMS_CORE_TEST_EXPORT_TEST(file_read_file_using_wrong_mode),
|
||||||
|
COSMS_CORE_TEST_EXPORT_TEST(file_read_full_file_using_wrong_mode),
|
||||||
|
|
||||||
COSMS_CORE_TEST_EXPORT_TEST(file_write),
|
COSMS_CORE_TEST_EXPORT_TEST(file_write),
|
||||||
COSMS_CORE_TEST_EXPORT_TEST(file_delete)
|
COSMS_CORE_TEST_EXPORT_TEST(file_delete)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue