Browse Source

fs module; test for it

master
Gitea 2 years ago
parent
commit
6316f055ae
  1. 61
      src/fs/fs.c
  2. 21
      src/fs/fs.h
  3. BIN
      testing/copied_ppm_file.ppm
  4. 25
      testing/test.c

61
src/fs/fs.c

@ -0,0 +1,61 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "fs.h"
// Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong
uint64_t file_size(char* path) {
FILE* file_stream;
uint64_t file_size;
file_stream = fopen(path, "rb");
if (!file_stream) {
return UINT64_MAX;
}
fseek(file_stream, 0, SEEK_END);
file_size = ftell(file_stream);
fseek(file_stream, 0, SEEK_SET);
fclose(file_stream);
return file_size;
}
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns EXIT_FAILURE in case of an error
int copy_file(char* path_src, char* path_dst) {
FILE* src_file;
FILE* dst_file;
const uint16_t buf_size = 1024;
uint8_t buf[buf_size];
uint32_t bytes_processed;
src_file = fopen(path_src, "rb");
if (!src_file) {
return EXIT_FAILURE;
}
dst_file = fopen(path_dst, "wb");
if (!dst_file) {
return EXIT_FAILURE;
}
while (!feof(src_file)) {
bytes_processed = fread(buf, 1, buf_size, src_file);
fwrite(buf, 1, bytes_processed, dst_file);
}
fclose(src_file);
fclose(dst_file);
return EXIT_SUCCESS;
}

21
src/fs/fs.h

@ -0,0 +1,21 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong
uint64_t file_size(char* path);
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns EXIT_FAILURE in case of an error
int copy_file(char* path_src, char* path_dst);

BIN
testing/copied_ppm_file.ppm

Binary file not shown.

25
testing/test.c

@ -20,6 +20,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include "../src/rng/rng.h" #include "../src/rng/rng.h"
#include "../src/img/ppm.h" #include "../src/img/ppm.h"
#include "../src/endian/endian.h" #include "../src/endian/endian.h"
#include "../src/fs/fs.h"
int test_rng() { int test_rng() {
lcg(76); lcg(76);
@ -81,6 +82,7 @@ int test_endian() {
const uint16_t test_num16 = 25000; const uint16_t test_num16 = 25000;
// 10101000 01100001 // 10101000 01100001
const uint16_t test_num16_swapped = 43105; const uint16_t test_num16_swapped = 43105;
if (swap_endian16(test_num16) != test_num16_swapped) { if (swap_endian16(test_num16) != test_num16_swapped) {
printf("[INFO] Failed to swap endianness for 16bit integer: %u -> %u; supposed to get %u\n", printf("[INFO] Failed to swap endianness for 16bit integer: %u -> %u; supposed to get %u\n",
test_num16, swap_endian16(test_num16), test_num16_swapped test_num16, swap_endian16(test_num16), test_num16_swapped
@ -92,6 +94,7 @@ int test_endian() {
const uint32_t test_num32 = 7270133; const uint32_t test_num32 = 7270133;
// 11110101 11101110 01101110 00000000 // 11110101 11101110 01101110 00000000
const uint32_t test_num32_swapped = 4126043648; const uint32_t test_num32_swapped = 4126043648;
if ((swap_endian32(test_num32)) != test_num32_swapped) { if ((swap_endian32(test_num32)) != test_num32_swapped) {
printf("[INFO] Failed to swap endianness for 32bit integer: %u -> %u; supposed to get %u\n", printf("[INFO] Failed to swap endianness for 32bit integer: %u -> %u; supposed to get %u\n",
test_num32, swap_endian32(test_num32), test_num32_swapped test_num32, swap_endian32(test_num32), test_num32_swapped
@ -115,6 +118,20 @@ int test_endian() {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int test_fs() {
if (file_size("test_img512x512.ppm") == UINT64_MAX) {
printf("[INFO] Failed to determine file size of test ppm image");
return EXIT_FAILURE;
}
if (copy_file("test_img512x512.ppm", "copied_ppm_file.ppm") == EXIT_FAILURE) {
printf("[INFO] Failed to copy test ppm image\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main() { int main() {
// rng // rng
printf("[INFO] Testing rng...\n"); printf("[INFO] Testing rng...\n");
@ -140,5 +157,13 @@ int main() {
printf("[INFO] Endian test passed\n\n"); printf("[INFO] Endian test passed\n\n");
} }
// fs
printf("[INFO] Testing fs...\n");
if (test_fs() == EXIT_FAILURE) {
printf("[INFO] FS test failed\n\n");
} else {
printf("[INFO] FS test passed\n\n");
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
Loading…
Cancel
Save