Загрузка данных


#include <stdio.h>

#include "list.h"

#define SUCCESS 1
#define FAIL 0

int add_door_test(void);
int remove_door_middle_test(void);
int remove_door_root_test(void);
void print_test_result(const char *test_name, int result);

int main(void) {
    print_test_result("add_door_test", add_door_test());
    print_test_result("remove_door_middle_test", remove_door_middle_test());
    print_test_result("remove_door_root_test", remove_door_root_test());

    return 0;
}

int add_door_test(void) {
    int result = FAIL;
    struct door door_1 = {1, 0};
    struct door door_2 = {2, 1};

    struct node *root = init(door_1);
    const struct node *added = add_door(root, door_2);

    if (root != NULL && added != NULL && root->next == added && added->data.id == 2 &&
        added->data.status == 1 && added->next == NULL) {
        result = SUCCESS;
    }

    destroy(root);

    return result;
}

int remove_door_middle_test(void) {
    int result = FAIL;
    struct door door_1 = {1, 0};
    struct door door_2 = {2, 1};
    struct door door_3 = {3, 0};

    struct node *root = init(door_1);
    struct node *second = add_door(root, door_2);
    const struct node *third = add_door(second, door_3);

    root = remove_door(second, root);

    if (root != NULL && root->next == third && find_door(2, root) == NULL) {
        result = SUCCESS;
    }

    destroy(root);

    return result;
}

int remove_door_root_test(void) {
    int result = FAIL;
    struct door door_1 = {1, 0};
    struct door door_2 = {2, 1};

    struct node *root = init(door_1);
    const struct node *second = add_door(root, door_2);

    root = remove_door(root, root);

    if (root == second && root != NULL && root->data.id == 2) {
        result = SUCCESS;
    }

    destroy(root);

    return result;
}

void print_test_result(const char *test_name, int result) {
    printf("%s: ", test_name);
    if (result == SUCCESS) {
        printf("SUCCESS");
    } else {
        printf("FAIL");
    }
    printf("\n");
}