Add more tests

main
Hammy 3 years ago
parent fba53fe13b
commit 18860e2371

@ -1,12 +1,13 @@
#include <unistd.h>
#include "src/list.h" #include "src/list.h"
#include "pthread.h"
#include "string.h" #include "string.h"
#include "assert.h" #include "assert.h"
void printSuccess(const char method[]) { void printSuccess(const char method[]) {
unsigned long strLength = 18 + strlen(method);
char message[strLength];
printf("\n------------------------------"); printf("\n------------------------------");
char message[100] = "\n"; strcpy(message, "\n");
strcat(message, method); strcat(message, method);
strcat(message, " --> TEST PASSED"); strcat(message, " --> TEST PASSED");
printf("%s", message); printf("%s", message);
@ -57,7 +58,7 @@ void shouldReturnSortedList() {
// Assert // Assert
assert(returnCode == 0); assert(returnCode == 0);
for (int i = 0; i < 11; i++) { for (int i = 0; i < List_length(list); i++) {
assert(List_get(list, i) == i); assert(List_get(list, i) == i);
} }
List_destroy(&list); List_destroy(&list);
@ -65,39 +66,82 @@ void shouldReturnSortedList() {
printSuccess(__func__); printSuccess(__func__);
} }
typedef struct args { void shouldClearList() {
List *list; // Arrange
int elemToInsert; List *list = List_new();
} Arguments; List_append(list, 0);
List_append(list, 1);
List_append(list, 2);
List_append(list, 3);
void *append(void *arg) { // Act
Arguments *args = (Arguments *) arg; List_clear(list);
List *list = args->list;
List_append(list, args->elemToInsert);
pthread_exit(NULL);
}
int main() { // Assert
// shouldReturnListLengthZero(); assert(List_length(list) == 0);
// shouldReturnListLengthTwo(); List_destroy(&list);
// shouldReturnSortedList();
printSuccess(__func__);
}
void shouldReturnListMaxLengthTwenty() {
// Arrange
List *list = List_new(); List *list = List_new();
List_append(list, 0);
List_append(list, 1);
List_append(list, 2);
List_append(list, 3);
List_append(list, 4);
List_append(list, 5);
List_append(list, 6);
List_append(list, 7);
List_append(list, 8);
List_append(list, 9);
pthread_t pthreads[2]; // Mid-Assert
for (int i = 0; i < 2; i++) { assert(List_maxLength(list) == 10);
printf("\nElement To Insert -> %i", i);
Arguments args = {list, i}; // Act
pthread_create(&pthreads[i], NULL, append, &args); List_append(list, 10);
// Assert
assert(List_maxLength(list) == 20);
List_destroy(&list);
printSuccess(__func__);
} }
for (int i = 0; i < 2; i++) { void shouldCopyPopulatedList() {
pthread_join(pthreads[i], NULL); // Arrange
List *list = List_new();
List_append(list, 0);
List_append(list, 1);
List_append(list, 2);
List_append(list, 3);
List_append(list, 4);
// Act
List *copiedList = List_copy(list);
// Assert
assert(List_length(copiedList) == List_length(list));
assert(List_maxLength(copiedList) == List_maxLength(list));
for (int i = 0; i < List_length(copiedList); i++) {
assert(List_get(list, i) == List_get(copiedList, i));
} }
List_destroy(&list);
List_destroy(&copiedList);
printf("\n\nList: \n"); printSuccess(__func__);
List_print(list); }
int main() {
shouldReturnListLengthZero();
shouldReturnListLengthTwo();
shouldReturnSortedList();
shouldClearList();
shouldReturnListMaxLengthTwenty();
shouldCopyPopulatedList();
return 0; return 0;
} }

Loading…
Cancel
Save