|
| 1 | +/* |
| 2 | + * ----------------------------------------------------------------------------- |
| 3 | + * Project: Fossil Logic |
| 4 | + * |
| 5 | + * This file is part of the Fossil Logic project, which aims to develop high- |
| 6 | + * performance, cross-platform applications and libraries. The code contained |
| 7 | + * herein is subject to the terms and conditions defined in the project license. |
| 8 | + * |
| 9 | + * Author: Michael Gene Brockus (Dreamer) |
| 10 | + * |
| 11 | + * Copyright (C) 2024 Fossil Logic. All rights reserved. |
| 12 | + * ----------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | +#include <fossil/test/framework.h> |
| 15 | + |
| 16 | +#include "fossil/io/framework.h" |
| 17 | + |
| 18 | + |
| 19 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 20 | +// * Fossil Logic Test Utilites |
| 21 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 22 | +// Setup steps for things like test fixtures and |
| 23 | +// mock objects are set here. |
| 24 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 25 | + |
| 26 | +// Define the test suite and add test cases |
| 27 | +FOSSIL_TEST_SUITE(c_stream_suite); |
| 28 | +fossil_fstream_t c_stream; |
| 29 | + |
| 30 | +// Setup function for the test suite |
| 31 | +FOSSIL_SETUP(c_stream_suite) { |
| 32 | + // Setup code here |
| 33 | +} |
| 34 | + |
| 35 | +// Teardown function for the test suite |
| 36 | +FOSSIL_TEARDOWN(c_stream_suite) { |
| 37 | + // Teardown code here |
| 38 | +} |
| 39 | + |
| 40 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 41 | +// * Fossil Logic Test Cases |
| 42 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 43 | +// The test cases below are provided as samples, inspired |
| 44 | +// by the Meson build system's approach of using test cases |
| 45 | +// as samples for library usage. |
| 46 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 47 | + |
| 48 | +FOSSIL_TEST_CASE(c_test_stream_let_write_and_read_file) { |
| 49 | + const char *filename = "testfile.txt"; |
| 50 | + const char *content = "This is a test."; |
| 51 | + |
| 52 | + // Write data to the file |
| 53 | + ASSUME_ITS_EQUAL_I32(0, fossil_fstream_open(&c_stream, filename, "w")); |
| 54 | + fossil_fstream_write(&c_stream, content, strlen(content), 1); |
| 55 | + fossil_fstream_close(&c_stream); |
| 56 | + |
| 57 | + // Read data from the file |
| 58 | + char buffer[1024]; |
| 59 | + ASSUME_ITS_EQUAL_I32(0, fossil_fstream_open(&c_stream, filename, "r")); |
| 60 | + fossil_fstream_read(&c_stream, buffer, sizeof(buffer), 1); |
| 61 | + fossil_fstream_close(&c_stream); |
| 62 | +} |
| 63 | + |
| 64 | +FOSSIL_TEST_CASE(c_test_stream_let_open_and_close_file) { |
| 65 | + const char *filename = "testfile.txt"; |
| 66 | + |
| 67 | + // Open the file |
| 68 | + ASSUME_ITS_EQUAL_I32(0, fossil_fstream_open(&c_stream, filename, "w")); |
| 69 | + fossil_fstream_close(&c_stream); |
| 70 | +} |
| 71 | + |
| 72 | +FOSSIL_TEST_CASE(c_test_stream_multiple_files) { |
| 73 | + const char *filename1 = "testfile1.txt"; |
| 74 | + const char *filename2 = "testfile2.txt"; |
| 75 | + |
| 76 | + // Open the first file |
| 77 | + ASSUME_ITS_EQUAL_I32(0, fossil_fstream_open(&c_stream, filename1, "w")); |
| 78 | + fossil_fstream_close(&c_stream); |
| 79 | + |
| 80 | + // Open the second file |
| 81 | + ASSUME_ITS_EQUAL_I32(0, fossil_fstream_open(&c_stream, filename2, "w")); |
| 82 | + fossil_fstream_close(&c_stream); |
| 83 | +} |
| 84 | + |
| 85 | +FOSSIL_TEST_CASE(c_test_stream_seek_and_tell) { |
| 86 | + const char *filename = "testfile.txt"; |
| 87 | + const char *content = "This is a test."; |
| 88 | + |
| 89 | + // Write data to the file |
| 90 | + ASSUME_ITS_EQUAL_I32(0, fossil_fstream_open(&c_stream, filename, "w")); |
| 91 | + fossil_fstream_write(&c_stream, content, strlen(content), 1); |
| 92 | + fossil_fstream_close(&c_stream); |
| 93 | + |
| 94 | + // Open the file |
| 95 | + ASSUME_ITS_EQUAL_I32(0, fossil_fstream_open(&c_stream, filename, "r")); |
| 96 | + |
| 97 | + // Seek to the end of the file |
| 98 | + fossil_fstream_seek(&c_stream, 0, SEEK_END); |
| 99 | + |
| 100 | + // Get the current position |
| 101 | + long position = fossil_fstream_tell(&c_stream); |
| 102 | + |
| 103 | + ASSUME_ITS_TRUE(position > 0); |
| 104 | + |
| 105 | + // Close the file |
| 106 | + fossil_fstream_close(&c_stream); |
| 107 | +} |
| 108 | + |
| 109 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 110 | +// * Fossil Logic Test Pool |
| 111 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 112 | + |
| 113 | +FOSSIL_TEST_GROUP(c_file_tests) { |
| 114 | + FOSSIL_TEST_ADD(c_stream_suite, c_test_stream_let_write_and_read_file); |
| 115 | + FOSSIL_TEST_ADD(c_stream_suite, c_test_stream_let_open_and_close_file); |
| 116 | + FOSSIL_TEST_ADD(c_stream_suite, c_test_stream_multiple_files); |
| 117 | + |
| 118 | + FOSSIL_TEST_REGISTER(c_stream_suite); |
| 119 | +} |
0 commit comments