|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Intel Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <assert.h> |
| 10 | +#include <errno.h> |
| 11 | +#include <fcntl.h> |
| 12 | +#include <sys/stat.h> |
| 13 | +#include <unistd.h> |
| 14 | + |
| 15 | +#define PATH_TEST_FILE "test.txt" |
| 16 | +#define FILE_TEXT "Hello, world!" |
| 17 | +#define WORLD_OFFSET 7 |
| 18 | +#define NAME_REPLACMENT "James" |
| 19 | +#define NAME_REPLACMENT_LEN (sizeof(NAME_REPLACMENT) - 1) |
| 20 | +#define ADDITIONAL_SPACE 10 |
| 21 | + |
| 22 | +int |
| 23 | +main(int argc, char **argv) |
| 24 | +{ |
| 25 | + FILE *file; |
| 26 | + const char *text = FILE_TEXT; |
| 27 | + char buffer[1000]; |
| 28 | + int ret; |
| 29 | + |
| 30 | + // Test: File opening (fopen) |
| 31 | + printf("Opening a file..\n"); |
| 32 | + file = fopen(PATH_TEST_FILE, "w+"); |
| 33 | + if (file == NULL) { |
| 34 | + printf("Error! errno: %d\n", errno); |
| 35 | + } |
| 36 | + assert(file != NULL); |
| 37 | + printf("[Test] File opening passed.\n"); |
| 38 | + |
| 39 | + // Test: Writing to a file (fprintf) |
| 40 | + printf("Writing to the file..\n"); |
| 41 | + ret = fprintf(file, "%s", text); |
| 42 | + assert(ret == strlen(text)); |
| 43 | + printf("[Test] File writing passed.\n"); |
| 44 | + |
| 45 | + // Test: Reading from a file (fseek) |
| 46 | + printf("Moving the cursor to the start of the file..\n"); |
| 47 | + ret = fseek(file, 0, SEEK_SET); |
| 48 | + assert(ret == 0); |
| 49 | + |
| 50 | + printf("Reading from the file, up to 1000 characters..\n"); |
| 51 | + fread(buffer, 1, sizeof(buffer), file); |
| 52 | + printf("Text read: %s\n", buffer); |
| 53 | + assert(strncmp(text, buffer, strlen(text)) == 0); |
| 54 | + printf("[Test] File reading passed.\n"); |
| 55 | + |
| 56 | + // Test: end of file detection (feof) |
| 57 | + printf("Determine whether we reach the end of the file..\n"); |
| 58 | + int is_end_of_file = feof(file); |
| 59 | + printf("Is the end of file? %d\n", is_end_of_file); |
| 60 | + assert(is_end_of_file == 1); |
| 61 | + printf("[Test] End of file detection passed.\n"); |
| 62 | + |
| 63 | + // Test: retrieving file offset (ftell) |
| 64 | + printf("Getting the plaintext size..\n"); |
| 65 | + long plaintext_size = ftell(file); |
| 66 | + printf("The plaintext size is %ld.\n", plaintext_size); |
| 67 | + assert(plaintext_size == 13); |
| 68 | + printf("[Test] Retrieving file offset passed.\n"); |
| 69 | + |
| 70 | + // Test: persist changes on disk (fflush) |
| 71 | + printf("Force actual write of all the cached data to the disk..\n"); |
| 72 | + ret = fflush(file); |
| 73 | + assert(ret == 0); |
| 74 | + printf("[Test] Retrieving file offset passed.\n"); |
| 75 | + |
| 76 | + // Test: writing at specified offset (pwrite) |
| 77 | + printf("Writing 5 characters at offset %d..\n", WORLD_OFFSET); |
| 78 | + ret = pwrite(fileno(file), NAME_REPLACMENT, NAME_REPLACMENT_LEN, |
| 79 | + WORLD_OFFSET); |
| 80 | + printf("File current offset: %ld\n", ftell(file)); |
| 81 | + assert(ret == NAME_REPLACMENT_LEN); |
| 82 | + assert(ftell(file) == strlen(FILE_TEXT)); |
| 83 | + printf("[Test] Writing at specified offset passed.\n"); |
| 84 | + |
| 85 | + // Test: reading at specified offset (pread) |
| 86 | + printf("Reading %ld characters at offset %d..\n", NAME_REPLACMENT_LEN, |
| 87 | + WORLD_OFFSET); |
| 88 | + buffer[NAME_REPLACMENT_LEN] = '\0'; |
| 89 | + pread(fileno(file), buffer, NAME_REPLACMENT_LEN, WORLD_OFFSET); |
| 90 | + printf("Text read: %s\n", buffer); |
| 91 | + printf("File current offset: %ld\n", ftell(file)); |
| 92 | + assert(strcmp(NAME_REPLACMENT, buffer) == 0); |
| 93 | + assert(ftell(file) == strlen(FILE_TEXT)); |
| 94 | + printf("[Test] Reading at specified offset passed.\n"); |
| 95 | + |
| 96 | + // Test: allocate more space to the file (posix_fallocate) |
| 97 | + printf("Allocate more space to the file..\n"); |
| 98 | + posix_fallocate(fileno(file), ftell(file), ADDITIONAL_SPACE); |
| 99 | + printf("File current offset: %ld\n", ftell(file)); |
| 100 | + printf("Moving to the end..\n"); |
| 101 | + fseek(file, 0, SEEK_END); |
| 102 | + printf("File current offset: %ld\n", ftell(file)); |
| 103 | + assert(ftell(file) == strlen(text) + ADDITIONAL_SPACE); |
| 104 | + printf("[Test] Allocation or more space passed.\n"); |
| 105 | + |
| 106 | + // Test: allocate more space to the file (ftruncate) |
| 107 | + printf("Extend the file size of 10 bytes using ftruncate..\n"); |
| 108 | + ftruncate(fileno(file), ftell(file) + 10); |
| 109 | + assert(ftell(file) == strlen(text) + ADDITIONAL_SPACE); |
| 110 | + printf("File current offset: %ld\n", ftell(file)); |
| 111 | + printf("Moving to the end..\n"); |
| 112 | + fseek(file, 0, SEEK_END); |
| 113 | + printf("File current offset: %ld\n", ftell(file)); |
| 114 | + assert(ftell(file) == strlen(text) + 2 * ADDITIONAL_SPACE); |
| 115 | + printf("[Test] Extension of the file size passed.\n"); |
| 116 | + |
| 117 | + // Test: closing the file (fclose) |
| 118 | + printf("Closing from the file..\n"); |
| 119 | + ret = fclose(file); |
| 120 | + assert(ret == 0); |
| 121 | + printf("[Test] Closing file passed.\n"); |
| 122 | + |
| 123 | + // Display some debug information |
| 124 | + printf("Getting the size of the file on disk..\n"); |
| 125 | + struct stat st; |
| 126 | + stat(PATH_TEST_FILE, &st); |
| 127 | + printf("The file size is %lld.\n", st.st_size); |
| 128 | + |
| 129 | + printf("All the tests passed!\n"); |
| 130 | + |
| 131 | + return 0; |
| 132 | +} |
0 commit comments