#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include "astr.h"
static void
test_astr_alloc_empty(void)
{
char *s = astr_alloc_empty();
assert(s);
assert(0 == strlen(s));
free(s);
}
static void
test_astr_alloc_formatted(void)
{
char *s = astr_alloc_formatted("answer: %i", 42);
assert(s);
assert(10 == strlen(s));
assert(astr_eq("answer: 42", s));
free(s);
}
static void
test_astr_alloc_formatted_for_invalid_format(void)
{
errno = 0;
char *s = astr_alloc_formatted(NULL);
assert(!s);
assert(EINVAL == errno);
free(s);
}
static void
test_astr_cmp(void)
{
assert(0 == astr_cmp(NULL, NULL));
assert(0 == astr_cmp("foo", "foo"));
assert(astr_cmp(NULL, "") < 0);
assert(astr_cmp("", NULL) > 0);
assert(astr_cmp(NULL, "foo") < 0);
assert(astr_cmp("foo", NULL) > 0);
assert(astr_cmp("", "foo") < 0);
assert(astr_cmp("foo", "") > 0);
assert(astr_cmp("bar", "foo") < 0);
assert(astr_cmp("foo", "bar") > 0);
assert(astr_cmp("foo", "foobar") < 0);
assert(astr_cmp("foobar", "foo") > 0);
}
static void
test_astr_empty(void)
{
assert(astr_empty(NULL));
assert(astr_empty(""));
assert(!astr_empty("foo"));
}
static void
test_astr_eq(void)
{
char *s = astr_alloc_formatted("foo");
assert(astr_eq("foo", s));
assert(!astr_eq(s, "bar"));
free(s);
}
static void
test_astr_eq_for_NULL_string(void)
{
char *s = astr_alloc_formatted("foo");
assert(!astr_eq(s, NULL));
assert(!astr_eq(NULL, s));
assert(astr_eq(NULL, NULL));
free(s);
}
static void
test_astr_eq_for_empty_strings(void)
{
char *s1 = astr_alloc_empty();
char *s2 = astr_alloc_empty();
assert(s1 != s2);
assert(astr_eq(s1, s2));
free(s2);
free(s1);
}
static void
test_astr_formatted_length(void)
{
errno = 0;
size_t length = astr_formatted_length("answer: %i", 42);
assert(10 == length);
assert(0 == errno);
}
static void
test_astr_formatted_length_for_invalid_format(void)
{
errno = 0;
size_t length = astr_formatted_length(NULL);
assert(0 == length);
assert(EINVAL == errno);
}
static void
test_astr_realloc_append_formatted(void)
{
char *s = astr_alloc_formatted("foo");
int result = astr_realloc_append_formatted(&s, "bar %i", 42);
assert(0 == result);
assert(astr_eq("foobar 42", s));
free(s);
}
static void
test_astr_realloc_append_formatted_for_empty_format(void)
{
char *s = astr_alloc_formatted("foo");
char *original_s = s;
int result = astr_realloc_append_formatted(&s, "");
assert(0 == result);
assert(original_s == s);
assert(astr_eq("foo", s));
free(s);
}
static void
test_astr_realloc_append_formatted_for_NULL_string(void)
{
char *s = NULL;
int result = astr_realloc_append_formatted(&s, "bar %i", 42);
assert(0 == result);
assert(s);
assert(astr_eq("bar 42", s));
free(s);
}
static void
test_astr_realloc_append_formatted_for_invalid_string_pointer(void)
{
errno = 0;
int result = astr_realloc_append_formatted(NULL, "foobar");
assert(-1 == result);
assert(EINVAL == errno);
}
static void
test_astr_realloc_append_formatted_for_invalid_format(void)
{
char *s = astr_alloc_formatted("foo");
errno = 0;
int result = astr_realloc_append_formatted(&s, NULL);
assert(-1 == result);
assert(EINVAL == errno);
free(s);
}
int
main(int argc, char *argv[])
{
test_astr_alloc_empty();
test_astr_alloc_formatted();
test_astr_alloc_formatted_for_invalid_format();
test_astr_cmp();
test_astr_empty();
test_astr_eq();
test_astr_eq_for_NULL_string();
test_astr_eq_for_empty_strings();
test_astr_formatted_length();
test_astr_formatted_length_for_invalid_format();
test_astr_realloc_append_formatted_for_empty_format();
test_astr_realloc_append_formatted_for_NULL_string();
test_astr_realloc_append_formatted_for_invalid_string_pointer();
test_astr_realloc_append_formatted_for_invalid_format();
return EXIT_SUCCESS;
}