implement struct tests
This commit is contained in:
85
src/test/struct/struct.c
Normal file
85
src/test/struct/struct.c
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "struct.h"
|
||||||
|
#include "../print/print.h"
|
||||||
|
|
||||||
|
// C equivalent implementations of the funcitons written in k
|
||||||
|
struct testStruct* cGetTestStruct(long a, bool b, long c) {
|
||||||
|
struct testStruct* result = (struct testStruct*) malloc(sizeof(struct testStruct));
|
||||||
|
result->a = a;
|
||||||
|
result->b = b;
|
||||||
|
result->c = c;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct testStructRec* cGetTestStructRec(long a, struct testStructRec* b) {
|
||||||
|
struct testStructRec* result = (struct testStructRec*) malloc(sizeof(struct testStructRec));
|
||||||
|
result->a = a;
|
||||||
|
result->b = b;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int struct_testExpected(char *name, long expected, long result)
|
||||||
|
{
|
||||||
|
if (expected == result)
|
||||||
|
{
|
||||||
|
succ(name, expected, result);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
err(name, expected, result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int testStructCreation() {
|
||||||
|
printf("\nStruct creation tests\n");
|
||||||
|
struct testStruct* result = getTestStruct(1, false, 20);
|
||||||
|
|
||||||
|
struct_testExpected("init field a", 1, result->a);
|
||||||
|
struct_testExpected("init field b", false, result->b);
|
||||||
|
struct_testExpected("init field c", 20, result->c);
|
||||||
|
|
||||||
|
free(result);
|
||||||
|
|
||||||
|
printf("\n Recursive struct creation tests\n");
|
||||||
|
struct testStructRec* innerStruct = getTestStructRec(20, NULL);
|
||||||
|
struct testStructRec* resultRec = getTestStructRec(10, innerStruct);
|
||||||
|
|
||||||
|
struct_testExpected("init rec field a", 10, resultRec->a);
|
||||||
|
struct_testExpected("init rec field b", innerStruct, resultRec->b);
|
||||||
|
struct_testExpected("init inner field a", 20, resultRec->b->a);
|
||||||
|
struct_testExpected("init inner field b", NULL, resultRec->b->b);
|
||||||
|
|
||||||
|
free(resultRec);
|
||||||
|
free(innerStruct);
|
||||||
|
}
|
||||||
|
|
||||||
|
int testStructGet() {
|
||||||
|
printf("\nStruct getter tests\n");
|
||||||
|
struct testStruct* result = getTestStruct(1, false, 20);
|
||||||
|
|
||||||
|
struct_testExpected("get field a", 1, getStructFieldA(result));
|
||||||
|
struct_testExpected("get field b", false, getStructFieldB(result));
|
||||||
|
struct_testExpected("get field c", 20, getStructFieldC(result));
|
||||||
|
|
||||||
|
free(result);
|
||||||
|
|
||||||
|
printf("\nStruct getter tests\n");
|
||||||
|
struct testStructRec* innerStruct = getTestStructRec(1, NULL);
|
||||||
|
struct testStructRec* resultRec = getTestStructRec(20, innerStruct);
|
||||||
|
|
||||||
|
struct_testExpected("get rec field a", 20, getStructFieldRecA(resultRec));
|
||||||
|
struct_testExpected("get rec field b", innerStruct, getStructFieldRecB(resultRec));
|
||||||
|
struct_testExpected("get inner field a", 1, getStructFieldRecA(getStructFieldRecB(resultRec)));
|
||||||
|
struct_testExpected("get inner field b", NULL, getStructFieldRecB(getStructFieldRecB(resultRec)));
|
||||||
|
|
||||||
|
free(resultRec);
|
||||||
|
free(innerStruct);
|
||||||
|
}
|
||||||
|
|
||||||
|
void runStructTests() {
|
||||||
|
testStructCreation();
|
||||||
|
testStructGet();
|
||||||
|
}
|
||||||
31
src/test/struct/struct.h
Normal file
31
src/test/struct/struct.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct testStruct
|
||||||
|
{
|
||||||
|
long a;
|
||||||
|
bool b;
|
||||||
|
long c;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct testStructRec
|
||||||
|
{
|
||||||
|
long a;
|
||||||
|
struct testStructRec *b;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct testStruct* getTestStruct(long a, bool b, long c);
|
||||||
|
struct testStructRec* getTestStructRec(long a, struct testStructRec* b);
|
||||||
|
|
||||||
|
long getStructFieldA(struct testStruct *);
|
||||||
|
bool getStructFieldB(struct testStruct *);
|
||||||
|
long getStructFieldC(struct testStruct *);
|
||||||
|
|
||||||
|
struct testStruct *setStructFieldA(struct testStruct *t, long a);
|
||||||
|
struct testStruct *setStructFieldB(struct testStruct *t, bool b);
|
||||||
|
struct testStruct *setStructFieldC(struct testStruct *t, long c);
|
||||||
|
|
||||||
|
long getStructFieldRecA(struct testStructRec *t);
|
||||||
|
struct testStructRec *getStructFieldRecB(struct testStructRec *t);
|
||||||
|
|
||||||
|
struct testStructRec *setStructFieldRecA(struct testStructRec *t, long a);
|
||||||
|
struct testStructRec *setStructFieldRecB(struct testStructRec *t, struct testStructRec *b);
|
||||||
Reference in New Issue
Block a user