Skip to content

Commit

Permalink
Added unit test for function_arg_helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkinyua committed Oct 31, 2024
1 parent c1ce1c7 commit 58dec44
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
8 changes: 8 additions & 0 deletions firmware/Core/Inc/unit_tests/unit_test_function_arg_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __INCLUDE_GUARD__UNIT_TEST_FUNCTION_ARG_HELPERS_H__
#define __INCLUDE_GUARD__UNIT_TEST_FUNCTION_ARG_HELPERS_H__

#include <stdint.h>

uint8_t TEST__is_valid_hex_string();

#endif // __INCLUDE_GUARD__UNIT_TEST_FUNCTION_ARG_HELPERS_H__
16 changes: 10 additions & 6 deletions firmware/Core/Src/function_arg_helpers.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "function_arg_helpers.h"
#include "function_arg_helpers.h"

#include <stdio.h>
#include <stdint.h>
Expand All @@ -8,18 +8,22 @@
/// @param hex_string - The HEX string to be checked (const char *)
/// @param string_len - The length of the passed string to be checked (uint8_t)
/// @return 0 on success, > 0 on error
uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len){
uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len)
{

if (hex_string == NULL || string_len == 0 || *hex_string == '\0') {
if (hex_string == NULL || string_len == 0 || *hex_string == '\0')
{
// Error if string and string_len is NULL or empty
return 1;
return 1;
}

for(uint8_t i = 0; i < string_len; i++){
for (uint8_t i = 0; i < string_len; i++)
{
char character = *hex_string;

// Checking if the character is between 0-9, A-F, or a-f
if(!(isdigit(character) || (toupper(character) >= 'A' && toupper(character) <= 'F'))){
if (!(isdigit(character) || (toupper(character) >= 'A' && toupper(character) <= 'F')))
{
// Error: Invalid hex character within the string
return 2;
}
Expand Down
29 changes: 29 additions & 0 deletions firmware/Core/Src/unit_tests/unit_test_function_arg_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "unit_tests/unit_test_helpers.h"
#include "unit_tests/unit_test_function_arg_helpers.h"
#include "function_arg_helpers.h"

#include <string.h>

uint8_t TEST__is_valid_hex_string()
{
char hex_color_code[20] = "\0";
uint8_t status;

// Fail Criteria

// Empty string
status = is_valid_hex_string(hex_color_code, strlen(hex_color_code));
TEST_ASSERT_TRUE(status == 1);

// Invalid Hex Value ie "#" is not a hex value
strcpy(hex_color_code, "#FFAABB");
status = is_valid_hex_string(hex_color_code, strlen(hex_color_code));
TEST_ASSERT_TRUE(status == 2);

// Pass Criteria
strcpy(hex_color_code, "1234567890ABCDEF");
status = is_valid_hex_string(hex_color_code, strlen(hex_color_code));
TEST_ASSERT_TRUE(status == 0);

return 0;
}
5 changes: 5 additions & 0 deletions firmware/Core/Src/unit_tests/unit_test_inventory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
#include "unit_tests/unit_test_inventory.h"
#include "unit_tests/unit_test_pwm.h"
#include "unit_tests/unit_test_rgb.h"
#include "unit_tests/unit_test_function_arg_helpers.h"

// extern
const TEST_Definition_t TEST_definitions[] = {

{.test_func = TEST__is_valid_hex_string,
.test_file = "unit_tests/unit_test_function_arg_helpers",
.test_func_name = "is_valid_hex_string"},

{.test_func = TEST_EXEC__LED_set_dimming,
.test_file = "unit_tests/unit_test_pwm",
.test_func_name = "LED_set_dimming"},
Expand Down

0 comments on commit 58dec44

Please sign in to comment.