113 lines
3.0 KiB
C
113 lines
3.0 KiB
C
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "unity.h"
|
|
#include "namesarchive.h"
|
|
|
|
|
|
void test_addNameAddsNamesInCorrectOrder(void)
|
|
{
|
|
const char *observedName;
|
|
const char *expectedNames[] = {"Fred", "Gerd", "Walter", "Anna", "Berta"};
|
|
const unsigned int expectedLen = sizeof(expectedNames) / sizeof(expectedNames[0]);
|
|
unsigned int nameIdx = 0;
|
|
|
|
for(int i = 0; i < expectedLen; i++)
|
|
{
|
|
addName(expectedNames[i]);
|
|
}
|
|
|
|
initIter();
|
|
|
|
for(nameIdx = 0; ((observedName = getNextName()) != NULL) && nameIdx < expectedLen; nameIdx++)
|
|
{
|
|
TEST_ASSERT_EQUAL_STRING(expectedNames[nameIdx], observedName);
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(expectedLen, nameIdx);
|
|
TEST_ASSERT_EQUAL_PTR(NULL, getNextName());
|
|
}
|
|
|
|
void test_removeNameRemovesCorrectName(void)
|
|
{
|
|
const char *observedName;
|
|
const char *expectedNames[] = {"Fred", "Gerd", "Walter", "Anna", "Berta"};
|
|
const char *nameToRemove = "Tom";
|
|
const unsigned int expectedLen = sizeof(expectedNames) / sizeof(expectedNames[0]);
|
|
unsigned int nameIdx = 0;
|
|
|
|
for(int i = 0; i < expectedLen; i++)
|
|
{
|
|
|
|
if(i == 2)
|
|
addName(nameToRemove);
|
|
|
|
addName(expectedNames[i]);
|
|
}
|
|
|
|
removeName(nameToRemove);
|
|
|
|
initIter();
|
|
|
|
for(nameIdx = 0; ((observedName = getNextName()) != NULL) && nameIdx < expectedLen; nameIdx++)
|
|
{
|
|
TEST_ASSERT_EQUAL_STRING(expectedNames[nameIdx], observedName);
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(expectedLen, nameIdx);
|
|
TEST_ASSERT_EQUAL_PTR(NULL, getNextName());
|
|
}
|
|
|
|
static int compareNames(const void *arg1, const void *arg2)
|
|
{
|
|
return strcmp(*(const char **)arg1, *(const char **)arg2);
|
|
}
|
|
|
|
void test_addNameSortedAddsNamesInCorrectOrder(void)
|
|
{
|
|
const char *observedName;
|
|
const char *expectedNames[] = {"Fred", "Gerd", "Walter", "Anna", "Berta"};
|
|
const unsigned int expectedLen = sizeof(expectedNames) / sizeof(expectedNames[0]);
|
|
unsigned int nameIdx = 0;
|
|
|
|
for(int i = 0; i < expectedLen; i++)
|
|
{
|
|
addNameSorted(expectedNames[i]);
|
|
}
|
|
|
|
qsort(expectedNames, expectedLen, sizeof(expectedNames[0]), compareNames);
|
|
|
|
initIter();
|
|
|
|
for(nameIdx = 0; ((observedName = getNextName()) != NULL) && nameIdx < expectedLen; nameIdx++)
|
|
{
|
|
TEST_ASSERT_EQUAL_STRING(expectedNames[nameIdx], observedName);
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(expectedLen, nameIdx);
|
|
TEST_ASSERT_EQUAL_PTR(NULL, getNextName());
|
|
}
|
|
|
|
void setUp(void) {
|
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
|
}
|
|
|
|
void tearDown(void) {
|
|
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
|
clearArchive();
|
|
}
|
|
|
|
int main()
|
|
{
|
|
UNITY_BEGIN();
|
|
|
|
printf("\n============================\nNames archive tests\n============================\n");
|
|
RUN_TEST(test_addNameAddsNamesInCorrectOrder);
|
|
RUN_TEST(test_addNameSortedAddsNamesInCorrectOrder);
|
|
RUN_TEST(test_removeNameRemovesCorrectName);
|
|
|
|
//printNames();
|
|
|
|
return UNITY_END();
|
|
} |