diff --git a/makefile b/makefile index b792156..7bfefea 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -```CC = gcc +CC = gcc FLAGS = -g -Wall -lm ifeq ($(OS),Windows_NT) diff --git a/stack.c b/stack.c index 4fabf41..d0105c7 100644 --- a/stack.c +++ b/stack.c @@ -10,7 +10,7 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) // es gibt bereits ein stack element, dieses wird übergeben + die daten, die in den stack müssen { - if(!stack || !data) + if(!data) return 0; StackNode *newNode = calloc(1, sizeof(StackNode)); //Speicher reserviert für data und prev, jeweils nur zeiger diff --git a/stack.o b/stack.o new file mode 100644 index 0000000..07999df Binary files /dev/null and b/stack.o differ diff --git a/test_runner b/test_runner new file mode 100755 index 0000000..6bd81fe Binary files /dev/null and b/test_runner differ diff --git a/test_stack.c b/test_stack.c index aaa0bd6..5ebf42f 100644 --- a/test_stack.c +++ b/test_stack.c @@ -9,9 +9,9 @@ void test_pushFailsOnNullPointer(StackNode *stack, void *data) { if (test != 0) { printf("Pass test_pushFailsOnNullPointerStack\n"); } else - printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED Stac Node\n"); + printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED StackNode\n"); - StackNode* test = push(stack, NULL); + test = push(stack, NULL); if (test == 0) { printf("Pass test_pushFailsOnNullPointerData\n"); return; @@ -24,35 +24,35 @@ void test_popFailsOnNullPointer() { StackNode* test = pop(NULL); if (test == 0) { - printf("Pass test_pushFailsOnNullPointerStack\n"); + printf("Pass test_popFailsOnNullPointerStack\n"); return; } - printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED 0\n"); + printf("Did Not Pass test_popFailsOnNullPointerStack EXPECTED 0\n"); return; } void test_topFailsOnNullPointer() { - int* test = top(NULL); + int* test = (int*)top(NULL); if (test == 0) { - printf("Pass test_pushFailsOnNullPointerStack\n"); + printf("Pass test_topFailsOnNullPointerStack\n"); return; } - printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED 0\n"); + printf("Did Not Pass test_topFailsOnNullPointerStack EXPECTED 0\n"); return; } int main() { - StackNode *stack0 = NULL; + int test0 = 3; int* dataTest0 = &test0; - stack0->data = dataTest0; + StackNode *stack0 = push(NULL, dataTest0); char test1[5] = "test\0"; - char* dataTest1 = &test1; + char* dataTest1 = test1; float test2 = 3.14; float* dataTest2 = &test2; @@ -72,7 +72,7 @@ int main() if(stack2->data == dataTest2) { printf("Pass test_pushFloat\n"); } else - printf("Fails test_pushFloat\n expected: %f\n was: %d\n", dataTest2, stack2->data); + printf("Fails test_pushFloat\n expected: %f\n was: %f\n", dataTest2, *(float*)(stack2->data)); int array[10] = {1,2,3,4,5,6,7,8,9,10}; @@ -80,8 +80,8 @@ int main() stack2 = push(stack2, &array[i]); } for(size_t i = 0; i<10; i++) { - int* data = top(stack2); - printf("%d\n", data); + int* data = (int*)top(stack2); + printf("%d\n", *data); stack2 = pop(stack2); } diff --git a/unittest.o b/unittest.o new file mode 100644 index 0000000..5f18b3b Binary files /dev/null and b/unittest.o differ diff --git a/unittest_stack.c b/unittest_stack.c new file mode 100644 index 0000000..361e3c7 --- /dev/null +++ b/unittest_stack.c @@ -0,0 +1,197 @@ + +#include +#include +#include "stack.h" +#include "unity.h" + + +void test_createMatrixFailsOnZeroDimensions(void) +{ + Matrix matrixToTest1 = createMatrix(0, 1); + Matrix matrixToTest2 = createMatrix(1, 0); + TEST_ASSERT_NULL(matrixToTest1.buffer); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest1.rows); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest1.cols); + TEST_ASSERT_NULL(matrixToTest2.buffer); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest2.rows); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest2.cols); +} + +void test_createMatrixReturnsCorrectMatrixDimensions(void) +{ + const unsigned int expectedRows = 15; + const unsigned int expectedCols = 10; + + Matrix matrixToTest = createMatrix(expectedRows, expectedCols); + TEST_ASSERT_EQUAL_UINT32(expectedRows, matrixToTest.rows); + TEST_ASSERT_EQUAL_UINT32(expectedCols, matrixToTest.cols); + free(matrixToTest.buffer); +} + +void test_clearMatrixSetsMembersToNull(void) +{ + Matrix matrixToTest = createMatrix(10, 10); + clearMatrix(&matrixToTest); + TEST_ASSERT_NULL(matrixToTest.buffer); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.rows); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.cols); +} + +void test_addReturnsCorrectResult(void) +{ + MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; + MatrixType buffer2[] = {7, 8, 9, 10, 11, 12}; + Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; + Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2}; + + Matrix result = add(matrix1, matrix2); + + float expectedResults[] = {8, 10, 12, 14, 16, 18}; + + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result.rows); + TEST_ASSERT_EQUAL_UINT32(matrix2.rows, result.rows); + TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result.cols); + TEST_ASSERT_EQUAL_UINT32(matrix2.cols, result.cols); + TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result.rows * result.cols); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows); + free(result.buffer); +} + +void test_addFailsOnDifferentInputDimensions(void) +{ + MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; + MatrixType buffer2[] = {7, 8, 9, 10, 11, 12}; + Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; + Matrix matrix2 = {.rows=3, .cols=2, .buffer=buffer2}; + + Matrix result = add(matrix1, matrix2); + + TEST_ASSERT_NULL(result.buffer); + TEST_ASSERT_EQUAL_UINT32(0, result.rows); + TEST_ASSERT_EQUAL_UINT32(0, result.cols); +} + +void test_addSupportsBroadcasting(void) +{ + MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; + MatrixType buffer2[] = {7, 8}; + Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; + Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2}; + + Matrix result1 = add(matrix1, matrix2); + Matrix result2 = add(matrix2, matrix1); + + float expectedResults[] = {8, 9, 10, 12, 13, 14}; + + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows); + TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols); + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows); + TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols); + + TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows); + TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows); + + free(result1.buffer); + free(result2.buffer); +} + +void test_multiplyReturnsCorrectResults(void) +{ + MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; + MatrixType buffer2[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; + Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; + Matrix matrix2 = {.rows=3, .cols=4, .buffer=buffer2}; + + Matrix result = multiply(matrix1, matrix2); + + float expectedResults[] = {74, 80, 86, 92, 173, 188, 203, 218}; + + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result.rows); + TEST_ASSERT_EQUAL_UINT32(matrix2.cols, result.cols); + TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result.rows * result.cols); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows); + + free(result.buffer); +} + +void test_multiplyFailsOnWrongInputDimensions(void) +{ + MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; + MatrixType buffer2[] = {7, 8, 9, 10, 11, 12}; + Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; + Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2}; + + Matrix result = multiply(matrix1, matrix2); + + TEST_ASSERT_NULL(result.buffer); + TEST_ASSERT_EQUAL_UINT32(0, result.rows); + TEST_ASSERT_EQUAL_UINT32(0, result.cols); +} + +void test_getMatrixAtReturnsCorrectResult(void) +{ + MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; + Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; + int expectedResult = buffer[4]; + + TEST_ASSERT_EQUAL_INT(expectedResult, getMatrixAt(matrixToTest, 1, 1)); +} + +void test_getMatrixAtFailsOnIndicesOutOfRange(void) +{ + MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; + + TEST_ASSERT_EQUAL_INT(UNDEFINED_MATRIX_VALUE, getMatrixAt(matrixToTest, 2, 3)); +} + +void test_setMatrixAtSetsCorrectValue(void) +{ + const int expectedResult = -1; + MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; + Matrix matrixUnderTest = {.rows=2, .cols=3, .buffer=buffer}; + + setMatrixAt(expectedResult, matrixUnderTest, 1, 2); + TEST_ASSERT_EQUAL_INT(expectedResult, buffer[5]); +} + +void test_setMatrixAtFailsOnIndicesOutOfRange(void) +{ + const float expectedResults[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; + + setMatrixAt(-1, matrixToTest, 2, 3); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType)); +} + +void setUp(void) { + // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden +} + +void tearDown(void) { + // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden +} + +int main() +{ + UNITY_BEGIN(); + + printf("============================\nMatrix tests\n============================\n"); + RUN_TEST(test_createMatrixReturnsCorrectMatrixDimensions); + RUN_TEST(test_createMatrixFailsOnZeroDimensions); + RUN_TEST(test_clearMatrixSetsMembersToNull); + RUN_TEST(test_addReturnsCorrectResult); + RUN_TEST(test_addFailsOnDifferentInputDimensions); + RUN_TEST(test_addSupportsBroadcasting); + RUN_TEST(test_multiplyReturnsCorrectResults); + RUN_TEST(test_multiplyFailsOnWrongInputDimensions); + RUN_TEST(test_getMatrixAtReturnsCorrectResult); + RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange); + RUN_TEST(test_setMatrixAtSetsCorrectValue); + RUN_TEST(test_setMatrixAtFailsOnIndicesOutOfRange); + + return UNITY_END(); +} \ No newline at end of file