cosms-core/tests/unit/file.c

231 lines
9.1 KiB
C
Raw Normal View History

/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "unit/file.h"
#include <cosms-core/file.h>
#include <string.h>
#include <stdlib.h>
COSMS_CORE_TEST_TEST(file_open,
COSMS_CORE_TEST_SUB_TEST(opening small file for reading,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for reading,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening small file for writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening small file for appending,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for appending,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_APPEND);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening small file for reading and writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for reading and writting,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening non-existing file for reading,
struct cosms_core_file file;
CosmsCoreFileError error = cosms_core_file_open(&file, "non-existing-file.cosms", COSMS_CORE_FILE_MODE_READ);
if (error != COSMS_CORE_FILE_NOT_FOUND) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
if (error == COSMS_CORE_FILE_OK) {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
}
)
)
COSMS_CORE_TEST_TEST(file_close,
COSMS_CORE_TEST_SUB_TEST(closing 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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(closing non-existing file,
struct cosms_core_file file;
file.native_file = 0;
CosmsCoreFileError error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_INVALID_FILE) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
)
)
COSMS_CORE_TEST_TEST(file_size,
COSMS_CORE_TEST_SUB_TEST(reading small file size,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
unsigned long long size = 0;
error = cosms_core_file_get_size(&file, &size);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
if (size != 13) {
cosms_core_test_sub_test_error = "result does not match expected result";
}
}
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(reading large file size,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
unsigned long long size = 0;
error = cosms_core_file_get_size(&file, &size);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
if (size != 5242880000ULL) {
cosms_core_test_sub_test_error = "result does not match expected result";
}
}
error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(reading non-existing file size,
struct cosms_core_file file;
file.native_file = 0;
unsigned long long size = 0;
CosmsCoreFileError error = cosms_core_file_get_size(&file, &size);
if (error != COSMS_CORE_FILE_INVALID_FILE) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
}
)
)
COSMS_CORE_TEST_TEST(file_read,
)
COSMS_CORE_TEST_TEST(file_write,
)
COSMS_CORE_TEST_TEST(file_delete,
)