Skip to content

Commit

Permalink
Added unit test for rgb function
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkinyua committed Oct 31, 2024
1 parent 0a3c310 commit c1ce1c7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
19 changes: 10 additions & 9 deletions firmware/Core/Inc/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif

/// @brief Struct to hold RGB values.
typedef struct {
uint8_t RGB_red; // Red component (0-255)
uint8_t RGB_green; // Green component (0-255)
uint8_t RGB_blue; // Blue component (0-255)
} LED_RGB_t;
/// @brief Struct to hold RGB values.
typedef struct
{
uint8_t RGB_red; // Red component (0-255)
uint8_t RGB_green; // Green component (0-255)
uint8_t RGB_blue; // Blue component (0-255)
} LED_RGB_t;


uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result);
uint8_t RGB_convert_hex_to_rgb(const char *hex_color_code, LED_RGB_t *rgb_result);

#ifdef __cplusplus
}
Expand Down
8 changes: 8 additions & 0 deletions firmware/Core/Inc/unit_tests/unit_test_rgb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __INCLUDE_GUARD__UNIT_TEST_RGB_H__
#define __INCLUDE_GUARD__UNIT_TEST_RGB_H__

#include <stdint.h>

uint8_t TEST_EXEC__RGB_convert_hex_to_rgb();

#endif // __INCLUDE_GUARD__UNIT_TEST_PWM_H__
31 changes: 18 additions & 13 deletions firmware/Core/Src/rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,51 @@
/// @param hex_color_code - The Hex color code to be parsed (string)
/// @param rgb_result - The struct to store the parsed RGB values
/// @return 0 on success, > 0 on error
uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result) {
uint8_t RGB_convert_hex_to_rgb(const char *hex_color_code, LED_RGB_t *rgb_result)
{

// The string, hex_color_code, should be in the form of #RRGGBB

// Check if the string is empty
if (hex_color_code == NULL || hex_color_code[0] == '\0'){
if (hex_color_code == NULL || hex_color_code[0] == '\0')
{
// Empty or NULL string, return error
return 1;
}

// Check if the string starts with "#"
if(hex_color_code[0] != '#'){
if (hex_color_code[0] != '#')
{
// Error: Missing sync_char "#"
return 2;
}

// Check if the string contains all RGB values (length should be 7 for "#RRGGBB")
const uint8_t hex_code_length = strlen(hex_color_code);
if(hex_code_length != 7){
// Error: Invalide length- missing hex values for the RGB
if (hex_code_length != 7)
{
// Error: Invalid length- missing hex values for the RGB
return 3;
}

// Check if the string contains valid hex values (skip the first character "#")
const uint8_t hex_check = is_valid_hex_string(hex_color_code + 1,hex_code_length-1);
if(hex_check > 0 ){
const uint8_t hex_check = is_valid_hex_string(hex_color_code + 1, hex_code_length - 1);
if (hex_check > 0)
{
// Error: Invalid hexadecimal within the string
return 4;
}

// Parse the string and pass the values into the struct holding the RBG Values
// Extracting the string RGB values from hex_color_code
char red[3] = { hex_color_code[1], hex_color_code[2], '\0' };
char green[3] = { hex_color_code[3], hex_color_code[4], '\0' };
char blue[3] = { hex_color_code[5], hex_color_code[6], '\0' };
char red[3] = {hex_color_code[1], hex_color_code[2], '\0'};
char green[3] = {hex_color_code[3], hex_color_code[4], '\0'};
char blue[3] = {hex_color_code[5], hex_color_code[6], '\0'};

// Converting the string values into decimals and passing them into the struct
rgb_result->RGB_red = (uint8_t) strtol(red,NULL,16);
rgb_result->RGB_green = (uint8_t) strtol(green,NULL,16);
rgb_result->RGB_blue = (uint8_t) strtol(blue,NULL,16);
rgb_result->RGB_red = (uint8_t)strtol(red, NULL, 16);
rgb_result->RGB_green = (uint8_t)strtol(green, NULL, 16);
rgb_result->RGB_blue = (uint8_t)strtol(blue, NULL, 16);

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
@@ -1,6 +1,7 @@
#include "unit_tests/unit_test_helpers.h"
#include "unit_tests/unit_test_inventory.h"
#include "unit_tests/unit_test_pwm.h"
#include "unit_tests/unit_test_rgb.h"

// extern
const TEST_Definition_t TEST_definitions[] = {
Expand All @@ -9,6 +10,10 @@ const TEST_Definition_t TEST_definitions[] = {
.test_file = "unit_tests/unit_test_pwm",
.test_func_name = "LED_set_dimming"},

{.test_func = TEST_EXEC__RGB_convert_hex_to_rgb,
.test_file = "unit_tests/unit_test_rgb",
.test_func_name = "RGB_convert_hex_to_rgb"},

};

// extern
Expand Down
44 changes: 44 additions & 0 deletions firmware/Core/Src/unit_tests/unit_test_rgb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "unit_tests/unit_test_helpers.h"
#include "unit_tests/unit_test_rgb.h"
#include "rgb.h"

#include <string.h>

uint8_t TEST_EXEC__RGB_convert_hex_to_rgb()
{
char hex_color_code[10] = "\0";
LED_RGB_t rgb_result;
uint8_t status;

// Fail Criteria

// Empty string
status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result);
TEST_ASSERT_TRUE(status == 1);

// Missing "#"
strcpy(hex_color_code, "FFAABB");
status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result);
TEST_ASSERT_TRUE(status == 2);

// Invalid Length - Missing hex value
strcpy(hex_color_code, "#FFAAB");
status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result);
TEST_ASSERT_TRUE(status == 3);

// Invalid Hex Value
strcpy(hex_color_code, "#K2AABB");
status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result);
TEST_ASSERT_TRUE(status == 4);

// Pass Criteria
strcpy(hex_color_code, "#D2AABB");
status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result);
TEST_ASSERT_TRUE(status == 0);

TEST_ASSERT_TRUE(rgb_result.RGB_red == 0xD2);
TEST_ASSERT_TRUE(rgb_result.RGB_green == 0xAA);
TEST_ASSERT_TRUE(rgb_result.RGB_blue == 0xBB);

return 0;
}

0 comments on commit c1ce1c7

Please sign in to comment.