1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-09 21:32:02 +05:30

reverse-ramdisk.c: extend the test, add more IRs

This commit is contained in:
Intel A80486DX2-66 2024-01-03 21:57:49 +03:00
parent 0355b33d84
commit 7ab89ffc53
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -30,8 +30,13 @@ To-Do: Add thread-safe versions of functions (use postfix `_r`)
#ifdef DEBUG
# define LINE_FAIL(x) printf("failed on line %d\n", __LINE__ + x)
# define DBG_PRINT(...) do { \
printf(__VA_ARGS__); \
fflush(stdout); \
} while (0)
#else
# define LINE_FAIL(x)
# define DBG_PRINT(...)
#endif
#define RETREAT(s) do { \
@ -59,6 +64,8 @@ int tf_write(int ID, size_t offset, void* src, size_t data_size);
int tf_read(int ID, size_t offset, void* dest, size_t data_size);
int tf_alloc(size_t n, size_t type_size) {
DBG_PRINT("tf_alloc(%zu, %zu)\n", n, type_size);
// Create an empty file
size_t len_digit;
if (num_temp_files == 0)
@ -119,6 +126,8 @@ int tf_alloc(size_t n, size_t type_size) {
}
int tf_free(int ID) {
DBG_PRINT("tf_free(%d)\n", ID);
size_t index = (size_t) ID;
if (temp_files[index].locked) {
@ -147,6 +156,7 @@ int tf_free(int ID) {
// Reallocate memory for the temp_files array
if (--num_temp_files > 0) {
DBG_PRINT("num_temp_files = %zu\n", num_temp_files);
if ((temp_files = realloc(temp_files, num_temp_files
* sizeof(TempFile))) == NULL) {
LINE_FAIL(-2);
@ -160,6 +170,8 @@ int tf_free(int ID) {
}
int tf_write(int ID, size_t offset, void* src, size_t data_size) {
DBG_PRINT("tf_write(%d, %zu, %p, %zu)\n", ID, offset, src, data_size);
size_t index = (size_t) ID;
if (temp_files[index].locked) {
@ -226,6 +238,8 @@ int tf_write(int ID, size_t offset, void* src, size_t data_size) {
}
int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
DBG_PRINT("tf_read(%d, %zu, %p, %zu)\n", ID, offset, dest, data_size);
size_t index = (size_t) ID;
if (temp_files[index].locked) {
@ -302,30 +316,65 @@ int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
#ifdef TEST
int main(void) {
int ID = tf_alloc(4, sizeof(int));
if (ID == -1)
RETREAT("tf_alloc");
DBG_PRINT("started\n");
int test_data[4] = {123, 456, 789, -123};
int ID_1 = tf_alloc(4, sizeof(int));
int ID_2 = tf_alloc(16, sizeof(uint16_t));
if (ID_1 == -1 || ID_2 == -1)
RETREAT("tf_alloc");
DBG_PRINT("allocated memory\n");
int test_data_1[4] = {123, 456, 789, -123};
DBG_PRINT("initialized array 1\n");
uint16_t test_data_2[16];
for (size_t i = 0; i < 16; i++)
test_data_2[i] = 1 << i;
DBG_PRINT("initialized array 2\n");
for (size_t i = 0; i < 4; i++)
if (tf_write(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1)
if (tf_write(ID_1, i * sizeof(int), &test_data_1[i], sizeof(int)) == -1)
RETREAT("tf_write");
DBG_PRINT("wrote array 1\n");
for (size_t i = 0; i < 16; i++)
if (tf_write(ID_2, i * sizeof(uint16_t), &test_data_2[i],
sizeof(uint16_t)) == -1)
RETREAT("tf_write");
DBG_PRINT("wrote array 2\n");
// round-trip
test_data[0] = 111;
test_data[1] = 222;
test_data[2] = 333;
test_data[3] = 444;
test_data_1[0] = 111;
test_data_1[1] = 222;
test_data_1[2] = 333;
test_data_1[3] = 444;
DBG_PRINT("filled array 1 with garbage\n");
for (size_t i = 0; i < 16; i++)
test_data_2[i] ^= 1;
DBG_PRINT("filled array 2 with garbage\n");
for (size_t i = 0; i < 4; i++)
if (tf_read(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1)
if (tf_read(ID_1, i * sizeof(int), &test_data_1[i], sizeof(int)) == -1)
RETREAT("tf_read");
DBG_PRINT("restored array 1\n");
printf("Values: %d %d %d %d\n",
test_data[0], test_data[1], test_data[2], test_data[3]);
for (size_t i = 0; i < 16; i++)
if (tf_read(ID_2, i * sizeof(uint16_t), &test_data_2[i],
sizeof(uint16_t)) == -1)
RETREAT("tf_read");
DBG_PRINT("restored array 2\n");
tf_free(ID);
DBG_PRINT("Values (1): ");
for (size_t i = 0; i < 4; i++)
DBG_PRINT("%d%c", test_data_1[i], i == 3 ? '\n' : ' ');
DBG_PRINT("Values (2): ");
for (size_t i = 0; i < 16; i++)
DBG_PRINT("%d%c", test_data_2[i], i == 15 ? '\n' : ' ');
tf_free(ID_1);
tf_free(ID_2);
DBG_PRINT("freed both files\n");
return 0;
}