Files

99 lines
2.4 KiB
C

// Copyright © 2024 Rot127 <[email protected]>
// SPDX-License-Identifier: BSD-3
#include "unit_test.h"
#include "../utils.h"
#include <stdio.h>
#include <string.h>
static bool test_str_append_no_realloc()
{
printf("Test test_str_append_no_realloc\n");
char str_a[] = "AAAA\0\0\0\0\0";
char str_b[] = "BBBB";
char str_c[] = "\0\0\0\0\0";
CHECK_NULL_RET_FALSE(str_append(NULL, NULL));
CHECK_NULL_RET_FALSE(str_append(str_a, NULL));
CHECK_NULL_RET_FALSE(str_append(NULL, str_b));
str_append_no_realloc(str_a, sizeof(str_a), str_c);
CHECK_STR_EQUAL_RET_FALSE(str_a, "AAAA");
str_append_no_realloc(str_a, sizeof(str_a), str_b);
CHECK_STR_EQUAL_RET_FALSE(str_a, "AAAABBBB");
str_append_no_realloc(str_c, sizeof(str_c), str_b);
CHECK_STR_EQUAL_RET_FALSE(str_c, "BBBB");
str_append_no_realloc(str_b, sizeof(str_b), str_c);
CHECK_STR_EQUAL_RET_FALSE(str_b, "BBBB");
return true;
}
static bool test_str_append()
{
printf("Test test_str_append\n");
char *str_a = NULL;
char *str_b = NULL;
CHECK_NULL_RET_FALSE(str_append(str_a, str_b));
str_a = cs_mem_calloc(5, sizeof(char));
memcpy(str_a, "AAAA", 5);
CHECK_NULL_RET_FALSE(str_append(str_a, str_b));
str_b = cs_mem_calloc(5, sizeof(char));
str_a = str_append(str_a, str_b);
CHECK_STR_EQUAL_RET_FALSE(str_a, "AAAA");
memcpy(str_b, "BBBB", 5);
str_a = str_append(str_a, str_b);
CHECK_STR_EQUAL_RET_FALSE(str_a, "AAAABBBB");
memset(str_a, 0, strlen(str_a) + 1);
str_a = str_append(str_a, str_b);
CHECK_STR_EQUAL_RET_FALSE(str_a, "BBBB");
free(str_a);
free(str_b);
return true;
}
static bool test_append_to_str_lower()
{
printf("Test test_append_to_str_lower\n");
char str_a[16] = "OP ";
append_to_str_lower(str_a, sizeof(str_a), "Foo");
CHECK_STR_EQUAL_RET_FALSE(str_a, "OP foo");
append_to_str_lower(NULL, 8, "x");
append_to_str_lower(str_a, sizeof(str_a), NULL);
CHECK_STR_EQUAL_RET_FALSE(str_a, "OP foo");
// A src longer than the remaining space must be truncated and the
// buffer must stay NUL terminated within its bounds.
char str_b[8] = "abc";
append_to_str_lower(str_b, sizeof(str_b), "DEFGHIJK");
CHECK_STR_EQUAL_RET_FALSE(str_b, "abcdefg");
return true;
}
int main()
{
bool result = true;
result &= test_str_append();
result &= test_str_append_no_realloc();
result &= test_append_to_str_lower();
if (result) {
printf("All tests passed.\n");
} else {
printf("Some tests failed.\n");
}
return result ? 0 : -1;
}