TU-Programmieren_2/exercise9/task2.test.c
2025-04-09 10:22:44 +02:00

127 lines
3.5 KiB
C

/// @file
/// @brief Task2: tests
#include "task2.h" // task2_copy, task2_copy_n, task2_compare, task2_compare_n
#include <assert.h> // assert
#include <stdio.h> // printf
int main() {
// testing 'task2_compare'
{
char a[] = "test";
char b[] = "test";
int res = task2_compare(a, b);
assert(res == 0); // fails if: equal strings do not return 0
}
{
char a[] = "test12";
char b[] = "test22";
int res = task2_compare(a, b);
assert(res < 0); // fails if: a before b does not return < 0
}
{
char a[] = "test31";
char b[] = "test22";
int res = task2_compare(a, b);
assert(res > 0); // fails if: b before a does not return > 0\n");
}
{
char a[] = "test1";
char b[] = "test12";
int res = task2_compare(a, b);
assert(res < 0); // fails if: strings equal up to end of a does not return < 0\n");
}
// testing 'task2_compare_n'
{
char a[] = "test1";
char b[] = "test12";
int res = task2_compare_n(a, b, 0);
assert(res == 0); // fails if: count == 0 does not return 0
}
{
char a[] = "test12";
char b[] = "test22";
int res = task2_compare_n(a, b, 4);
assert(res == 0); // fails if: string equal up to count does not return 0
}
{
char a[] = "test12";
char b[] = "test22";
int res = task2_compare_n(a, b, 5);
assert(res < 0); // fails if: a before b does not return < 0
}
{
char a[] = "test22";
char b[] = "test12";
int res = task2_compare_n(a, b, 5);
assert(res > 0); // fails if: b before a does not return > 0
}
{
char a[] = "test1\0XX";
char b[] = "test1\0YY";
int res = task2_compare_n(a, b, 8);
assert(res == 0); // fails if: characters after termination are considered in the comparison
}
// testing 'task2_copy'
{
const char src[] = "012345";
char dest[20] = "XXXXXXXXXXXXXXXXXXX";
char* res = task2_copy(dest, src);
assert(res == dest); // fails if: dest not returned from function
assert(dest[0] == '0'); // fails if: first char not present in dest
assert(dest[5] == '5'); // fails if: last char not present in dest
assert(dest[6] == '\0'); // fails if: dest not terminated correctly after copy
assert(dest[7] == 'X'); // fails if: manipulation of dest out of save range
}
// testing 'task2_copy_n'
char src[] = "0123456789";
src[6] = '\0';
{ // copying a range which terminates before count is reached
char dest[20] = "XXXXXXXXXXXXXXXXXXX";
int n = 10;
char* res = task2_copy_n(dest, src, n);
assert(res == dest); // fails if: dest not returned from function
int i = 0;
for (; i != 6; ++i)
assert(dest[i] == src[i]); // fails if: expected range not copied
assert(dest[i] == '\0'); // fails if: null termination not present
i = i + 1;
for (; i != 19; ++i)
assert(dest[i] == 'X'); // fails if: manipulation of dest out of save range
}
{ // copying where count is reached before source range terminates
char dest[20] = "XXXXXXXXXXXXXXXXXXX";
int n = 3;
char* res = task2_copy_n(dest, src, n);
assert(res == dest); // fails if: dest not returned from function
int i = 0;
for (; i != 2; ++i)
assert(dest[i] == src[i]); // fails if: expected range not copied
assert(dest[i] == src[i]); // fails if: null terminated
i = i + 1;
for (; i != 19; ++i)
assert(dest[i] == 'X'); // fails if: manipulation of dest out of save range
}
printf("task2.test.c: all asserts passed\n");
return 0;
}