#include <string.h>
#include <stdio.h> // Needed for snprintf in calculating expected length if desired
#include "unity.h"
#include "measurement_string.h"
#include "date.h"
/* --- Test Infrastructure --- */
/* Fixed time for the test - Global state for the stub */
struct tm g_fixed_time;
/* Stub implementation for retrieving the current local time. */
struct tm *get_date(time_t *unused)
{
// The function under test might modify the struct (e.g., if it calls mktime),
// although unlikely for just formatting. Returning a pointer to the global
// is standard practice for simple stubs like this.
(void)unused; // Mark unused parameter
return &g_fixed_time;
}
/* Called before each test */
void setUp(void)
{
/* Reset the stub time */
memset(&g_fixed_time, 0, sizeof(g_fixed_time));
// Set a default valid time to avoid unset values in tests that don't care
g_fixed_time.tm_year = 123; // 2023
g_fixed_time.tm_mon = 5; // June
g_fixed_time.tm_mday = 15;
g_fixed_time.tm_hour = 10;
g_fixed_time.tm_min = 30;
g_fixed_time.tm_sec = 0;
g_fixed_time.tm_isdst = -1;
}
/* Called after each test (optional cleanup) */
void tearDown(void)
{
// No cleanup needed in this case
}
/* --- Helper Functions (Optional) --- */
// Helper to set the time easily
void set_fixed_time(int year, int mon, int mday, int hour, int min, int sec) {
g_fixed_time.tm_year = year - 1900;
g_fixed_time.tm_mon = mon - 1; // Month is 0-based
g_fixed_time.tm_mday = mday;
g_fixed_time.tm_hour = hour;
g_fixed_time.tm_min = min;
g_fixed_time.tm_sec = sec;
g_fixed_time.tm_isdst = -1; // Let system figure it out or ignore
}
/* --- Test Cases --- */
// Your original test, slightly renamed for clarity
void test_generate_measurement_string_HappyPath(void)
{
char output_buf[100];
const size_t buf_size = sizeof(output_buf);
int str_len;
const char *expected_str = "Measurement: 12.35, Timestamp: 1984-03-14 01:59:26";
const int expected_len = strlen(expected_str); // 50
set_fixed_time(1984, 3, 14, 1, 59, 26);
str_len = generate_measurement_string(12.345, output_buf, buf_size);
TEST_ASSERT_EQUAL_INT(expected_len, str_len);
TEST_ASSERT_EQUAL_STRING(expected_str, output_buf);
// Optional: Check null termination explicitly if paranoid
TEST_ASSERT_EQUAL_UINT8('\0', output_buf[str_len]);
}
void test_generate_measurement_string_ZeroValue(void)
{
char output_buf[100];
const size_t buf_size = sizeof(output_buf);
int str_len;
const char *expected_str = "Measurement: 0.00, Timestamp: 2023-06-15 10:30:00"; // Using default time from setUp
const int expected_len = strlen(expected_str);
// Using default time from setUp
str_len = generate_measurement_string(0.0, output_buf, buf_size);
TEST_ASSERT_EQUAL_INT(expected_len, str_len);
TEST_ASSERT_EQUAL_STRING(expected_str, output_buf);
}
void test_generate_measurement_string_NegativeValue(void)
{
char output_buf[100];
const size_t buf_size = sizeof(output_buf);
int str_len;
const char *expected_str = "Measurement: -98.77, Timestamp: 2023-06-15 10:30:00";
const int expected_len = strlen(expected_str);
// Using default time from setUp
str_len = generate_measurement_string(-98.765, output_buf, buf_size);
TEST_ASSERT_EQUAL_INT(expected_len, str_len);
TEST_ASSERT_EQUAL_STRING(expected_str, output_buf);
}
void test_generate_measurement_string_RoundingUp(void)
{
char output_buf[100];
const size_t buf_size = sizeof(output_buf);
int str_len;
const char *expected_str = "Measurement: 10.00, Timestamp: 2023-06-15 10:30:00";
const int expected_len = strlen(expected_str);
// Using default time from setUp
str_len = generate_measurement_string(9.999, output_buf, buf_size);
TEST_ASSERT_EQUAL_INT(expected_len, str_len);
TEST_ASSERT_EQUAL_STRING(expected_str, output_buf);
}
void test_generate_measurement_string_SingleDigitDateTime(void)
{
char output_buf[100];
const size_t buf_size = sizeof(output_buf);
int str_len;
const char *expected_str = "Measurement: 5.50, Timestamp: 2024-01-02 03:04:05"; // Note zero padding
const int expected_len = strlen(expected_str);
set_fixed_time(2024, 1, 2, 3, 4, 5);
str_len = generate_measurement_string(5.5, output_buf, buf_size);
TEST_ASSERT_EQUAL_INT(expected_len, str_len);
TEST_ASSERT_EQUAL_STRING(expected_str, output_buf);
}
// --- Buffer Size Tests ---
void test_generate_measurement_string_BufferExactSize(void)
{
// Expected string: "Measurement: 1.00, Timestamp: 2005-11-09 08:07:06"
// Expected length: 5 + 1 + 4 + 2 + 11 + 1 + 19 = 43 characters
const char *expected_str = "Measurement: 1.00, Timestamp: 2005-11-09 08:07:06";
const int expected_len = strlen(expected_str); // 43
const size_t buf_size = expected_len + 1; // Exact size including null terminator
char output_buf[buf_size]; // Use VLA or allocate dynamically if not supported
int str_len;
set_fixed_time(2005, 11, 9, 8, 7, 6);
str_len = generate_measurement_string(1.0, output_buf, buf_size);
// Assuming snprintf-like behavior: returns chars that *would* be written
TEST_ASSERT_EQUAL_INT(expected_len, str_len);
// String should be complete and null-terminated
TEST_ASSERT_EQUAL_STRING(expected_str, output_buf);
TEST_ASSERT_EQUAL_UINT8('\0', output_buf[expected_len]); // Explicit null check
}
void test_generate_measurement_string_BufferTooSmall_Truncation(void)
{
// Expected full string: "Measurement: 1.00, Timestamp: 2005-11-09 08:07:06" (43 chars)
const int expected_full_len = 43;
const size_t buf_size = 30; // Arbitrarily smaller size
char output_buf[buf_size];
int str_len;
// Expected truncated content (buf_size - 1 characters)
const char *expected_truncated_str = "Measurement: 1.00, Timestamp:"; // First 29 characters
set_fixed_time(2005, 11, 9, 8, 7, 6);
str_len = generate_measurement_string(1.0, output_buf, buf_size);
// Assuming snprintf-like behavior: returns chars that *would* have been written
TEST_ASSERT_EQUAL_INT(expected_full_len, str_len);
// Check the actual truncated content
TEST_ASSERT_EQUAL_STRING(expected_truncated_str, output_buf);
// Check null termination at the end of the buffer
TEST_ASSERT_EQUAL_UINT8('\0', output_buf[buf_size - 1]);
}
void test_generate_measurement_string_BufferTooSmall_Size1(void)
{
// Expected full string: "Measurement: 1.00, Timestamp: 2005-11-09 08:07:06" (43 chars)
const int expected_full_len = 43;
const size_t buf_size = 1; // Only space for null terminator
char output_buf[buf_size];
int str_len;
set_fixed_time(2005, 11, 9, 8, 7, 6);
str_len = generate_measurement_string(1.0, output_buf, buf_size);
// Assuming snprintf-like behavior: returns chars that *would* have been written
TEST_ASSERT_EQUAL_INT(expected_full_len, str_len);
// Buffer should contain only the null terminator
TEST_ASSERT_EQUAL_UINT8('\0', output_buf[0]);
}
void test_generate_measurement_string_BufferTooSmall_Size0(void)
{
// Expected full string: "Measurement: 1.00, Timestamp: 2005-11-09 08:07:06" (43 chars)
const int expected_full_len = 43;
const size_t buf_size = 0; // Zero size buffer
char *output_buf = NULL; // Or a zero-sized allocation if your compiler allows
// char output_buf[buf_size]; // This might be problematic depending on compiler
int str_len;
set_fixed_time(2005, 11, 9, 8, 7, 6);
// Behavior with size 0 can vary. snprintf returns chars needed, doesn't write.
// Let's assume the same for generate_measurement_string.
str_len = generate_measurement_string(1.0, output_buf, buf_size);
// Should return the length required, without attempting to write
TEST_ASSERT_EQUAL_INT(expected_full_len, str_len);
// Cannot check buffer content as it's NULL or zero-sized.
}
/* --- Main test runner --- */
/* Include this or similar in your main test file if you don't have one */
/*
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_generate_measurement_string_HappyPath);
RUN_TEST(test_generate_measurement_string_ZeroValue);
RUN_TEST(test_generate_measurement_string_NegativeValue);
RUN_TEST(test_generate_measurement_string_RoundingUp);
RUN_TEST(test_generate_measurement_string_SingleDigitDateTime);
RUN_TEST(test_generate_measurement_string_BufferExactSize);
RUN_TEST(test_generate_measurement_string_BufferTooSmall_Truncation);
RUN_TEST(test_generate_measurement_string_BufferTooSmall_Size1);
RUN_TEST(test_generate_measurement_string_BufferTooSmall_Size0);
return UNITY_END();
}
*/