add simple test run

This commit is contained in:
Tilen Majerle 2024-09-15 21:14:39 +02:00
parent b261ba5f1c
commit affd048d9c
7 changed files with 206 additions and 320 deletions

View File

@ -9,6 +9,7 @@ else()
add_executable(${PROJECT_NAME}) add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE target_sources(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/dev/main.c ${CMAKE_CURRENT_LIST_DIR}/dev/main.c
${CMAKE_CURRENT_LIST_DIR}/tests/test.c
) )
target_include_directories(${PROJECT_NAME} PUBLIC target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/dev ${CMAKE_CURRENT_LIST_DIR}/dev

View File

@ -1,31 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28922.388
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lwshell_dev", "lwshell_dev.vcxproj", "{C095C533-523E-4604-B3EF-B5025E4D96C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Debug|x64.ActiveCfg = Debug|x64
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Debug|x64.Build.0 = Debug|x64
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Debug|x86.ActiveCfg = Debug|Win32
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Debug|x86.Build.0 = Debug|Win32
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Release|x64.ActiveCfg = Release|x64
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Release|x64.Build.0 = Release|x64
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Release|x86.ActiveCfg = Release|Win32
{C095C533-523E-4604-B3EF-B5025E4D96C0}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0D5E996C-005B-40C8-95F9-90D0632471E5}
EndGlobalSection
EndGlobal

View File

@ -1,152 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{C095C533-523E-4604-B3EF-B5025E4D96C0}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>dynamic_memory</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>lwshell_dev</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>..\lwshell\src\include;.;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level1</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;LWMEM_DEV;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;LWMEM_DEV;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\examples\example_minimal.c" />
<ClCompile Include="..\lwshell\src\lwshell\lwshell.c" />
<ClCompile Include="main.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Source Files\Tests">
<UniqueIdentifier>{87f67bb1-45c3-4724-b7de-f1e8551453e3}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\LWSHELL">
<UniqueIdentifier>{a9bad49b-d114-4596-8fe8-162c60f482ee}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lwshell\src\lwshell\lwshell.c">
<Filter>Source Files\LWSHELL</Filter>
</ClCompile>
<ClCompile Include="..\examples\example_minimal.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include "lwshell/lwshell.h"
#include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "lwshell/lwshell.h"
/** /**
* \brief Reading one character at a time * \brief Reading one character at a time
@ -18,6 +18,7 @@
#endif #endif
void example_minimal(void); void example_minimal(void);
extern void run_test(void);
#if LWSHELL_CFG_USE_OUTPUT #if LWSHELL_CFG_USE_OUTPUT
@ -140,6 +141,9 @@ main(void) {
lwshell_set_output_fn(shell_output); lwshell_set_output_fn(shell_output);
#endif /* LWSHELL_CFG_USE_OUTPUT */ #endif /* LWSHELL_CFG_USE_OUTPUT */
/* Run the test */
run_test();
#if LWSHELL_CFG_USE_DYNAMIC_COMMANDS #if LWSHELL_CFG_USE_DYNAMIC_COMMANDS
/* Define shell commands */ /* Define shell commands */
lwshell_register_cmd("addint", addint_cmd, "Adds 2 integer numbers and prints them"); lwshell_register_cmd("addint", addint_cmd, "Adds 2 integer numbers and prints them");

View File

@ -93,14 +93,15 @@ prv_parse_input(lwshell_t* lwobj) {
size_t s_len; size_t s_len;
char* str; char* str;
/* Check string length and compare with buffer pointer */ /*
* Check string length and compare with buffer pointer
* Must be more than `1` character since we have to include end of line
*/
s_len = strlen(lwobj->buff); s_len = strlen(lwobj->buff);
if (s_len != lwobj->buff_ptr) { if (s_len != lwobj->buff_ptr || lwobj->buff_ptr == 0) {
return; return;
} }
/* Must be more than `1` character since we have to include end of line */
if (lwobj->buff_ptr > 0) {
/* Set default values */ /* Set default values */
lwobj->argc = 0; lwobj->argc = 0;
lwobj->argv[0] = lwobj->buff; lwobj->argv[0] = lwobj->buff;
@ -222,9 +223,7 @@ prv_parse_input(lwshell_t* lwobj) {
#if LWSHELL_CFG_USE_LIST_CMD #if LWSHELL_CFG_USE_LIST_CMD
", use listcmd to list available commands" ", use listcmd to list available commands"
#endif /* LWSHELL_CFG_USE_LIST_CMD */ #endif /* LWSHELL_CFG_USE_LIST_CMD */
"\r\n" "\r\n");
);
}
} }
} }
} }

91
tests/test.c Normal file
View File

@ -0,0 +1,91 @@
#include <stdio.h>
#include "lwshell/lwshell.h"
typedef struct {
const char* command;
const char* args_list[16];
} test_str_t;
/* List of commands */
static const test_str_t commands[] = {
{
.command = "test 123 456 789\n",
.args_list =
{
"test",
"123",
"456",
"789",
},
},
{
.command = "test 123 longer text\n",
.args_list =
{
"test",
"123",
"longer",
"text",
},
},
{
.command = "test 123 \"longer text\"\n",
.args_list =
{
"test",
"123",
"longer text",
},
},
};
static uint32_t current_cmd_index;
/**
* \brief Test command function
*
* \param argc
* \param argv
* \return int32_t
*/
int32_t
prv_test_cmd(int32_t argc, char** argv) {
const test_str_t* cmd = &commands[current_cmd_index];
size_t cmd_args_count = 0;
/* Get list of arguments from test command */
for (cmd_args_count; cmd->args_list[cmd_args_count] != NULL; ++cmd_args_count) {}
if (cmd_args_count != argc) {
printf("Test failed: Expected argument count (%02u) does not match actual argument count (%02u)\r\n",
(unsigned)cmd_args_count, (unsigned)argc);
return -1;
}
/* Check if parameters match */
for (size_t idx = 0; idx < argc; ++idx) {
/* Argument failed */
if (strcmp(cmd->args_list[idx], argv[idx]) != 0) {
printf("Test failed: Argument index %02u, value \"%s\" does not match actual argument value \"%s\"\r\n",
(unsigned)idx, cmd->args_list[idx], argv[idx]);
return -1;
}
}
return 0;
}
/**
* \brief Global test run function
*
*/
void
run_test(void) {
lwshell_register_cmd("test", prv_test_cmd, "Test command function\r\n");
printf("Running test...\r\n");
/* Run all commands */
for (current_cmd_index = 0; current_cmd_index < LWSHELL_ARRAYSIZE(commands); ++current_cmd_index) {
lwshell_input(commands[current_cmd_index].command, strlen(commands[current_cmd_index].command));
}
printf("Tests completed...\r\n");
}