Add custom fseek and fread
Add test framework gtest and added my custom fread and fseek tests + functions
This commit is contained in:
2023-11-12 14:33:15 +01:00
committed by Sander Speetjens
parent d436447a21
commit 7c76a37ec9
7 changed files with 212 additions and 8 deletions

28
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,28 @@
find_package(GTest REQUIRED)
# Third Party
include_directories(${GTEST_INCLUDE_DIR})
link_directories(${GTEST_LIB_DIR})
# tests
file(GLOB_RECURSE TEST_SOURCES "*.cpp")
add_executable(tests ${TEST_SOURCES})
target_compile_options(tests PRIVATE $<$<CONFIG:Debug>:
-Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion
>)
target_link_libraries(tests
PRIVATE
gtest
GTest::gtest_main
)
target_include_directories(tests
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${PROJECT_BINARY_DIR}
)
include(GoogleTest)
gtest_discover_tests(tests)

45
tests/tfpt_tests.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include <gtest/gtest.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "tftp.hpp"
tftp_custom_file_t file = {
.data = (char*)"1234567890",
.len = 11,
.name = (char*)"test.txt",
.ofset = 0
};
TEST(TFTP, custom_fseek)
{
tftp_custom_fseek(&file, 5, SEEK_SET);
EXPECT_EQ(file.ofset, 5);
tftp_custom_fseek(&file, 5, SEEK_CUR);
EXPECT_EQ(file.ofset, 10);
}
TEST(TFTP, custom_fread)
{
char buf[11];
tftp_custom_fseek(&file, 0, SEEK_SET);
size_t bytes = tftp_custom_fread(buf, 11, &file);
EXPECT_EQ(bytes, 11);
EXPECT_EQ(file.ofset, 11);
memset(buf, 0, 11);
tftp_custom_fseek(&file, 0, SEEK_SET);
bytes = tftp_custom_fread(buf, 11, &file);
EXPECT_EQ(bytes, 11);
EXPECT_EQ(memcmp(buf, "1234567890", 10), 0);
tftp_custom_fseek(&file, 0, SEEK_SET);
bytes = tftp_custom_fread(buf, 5, &file);
EXPECT_EQ(bytes, 5);
EXPECT_EQ(memcmp(buf, "12345", 5), 0);
}

45
tests/tftp.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include "tftp.hpp"
/**
* @brief tftp custom file functions to set the offset and read the data
* @param[in,out] handle Custom file handles
* @param[in] offset The offset to set
* @param[in] whence The origin of the offset
*/
void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) {
switch (whence) {
case SEEK_SET:
handle->ofset = offset;
break;
case SEEK_CUR:
handle->ofset += offset;
break;
case SEEK_END:
break;
}
if (handle->ofset > handle->len) {
handle->ofset = handle->len;
}
}
/**
* @brief tftp custom file functions to read the data
* auto rolling over the offset
* if the bytes to read is bigger than the remaining bytes
* it will read the remaining bytes and set the bytes to 0
* @param[out] buf The buffer to write the data to
* @param[in] bytes The number of bytes to read
* @param[in,out] handle Custom file handles
*/
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
if (handle->ofset + bytes > handle->len) {
bytes = handle->len - handle->ofset;
}
memcpy(buf, handle->data + handle->ofset, bytes);
handle->ofset += bytes;
((char*)buf)[bytes] = '\0';
if (handle->ofset > handle->len) {
bytes = 0;
}
return bytes;
}

17
tests/tftp.hpp Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
typedef struct tftp_custom_file_s {
const char* data;
size_t len;
char*name;
size_t ofset;
}tftp_custom_file_t;
void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence);
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle);