📄 list_test.c
字号:
/* $Id: list_test.c,v 1.7 2000/04/06 07:26:53 jm Exp $ * Tests on list module * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2000, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#include <stdio.h>#include <stddef.h>#include <assert.h>#include "list.h"/* defines */#define ASSERT assert#ifndef FALSE#define FALSE 0#endif#ifndef TRUE#define TRUE 1#endif/* structures */struct koe { struct node node1; struct node node2; int n;};/* show numbers */static void show_numbers(struct list *list){ int i; struct node *node; struct koe *koe; node = list_get_first(list); i = 1; if (node == NULL) { printf("<EMPTY LIST>\n"); } while(node != NULL) { koe = (struct koe *) node; printf("item #%d = %d\n", i, koe->n); node = list_get_next(node); i++; }}/* main */int main(void){ int i; struct list list1, list2; struct koe koe1, koe2, koe3, koe4, koe5; struct node *node; struct koe *koe, *koeb; printf("Testing list-module...\n"); /* M01 */ printf("Test case M01: Initialize a list structure\n"); list_init(&list1); ASSERT(list1.head == (struct node *) &list1.tail); ASSERT(list1.tail == NULL); ASSERT(list1.tailpred == (struct node *) &list1.head); ASSERT(list_is_empty(&list1) == TRUE); list_init(&list2); ASSERT(list2.head == (struct node *) &list2.tail); ASSERT(list2.tail == NULL); ASSERT(list2.tailpred == (struct node *) &list2.head); ASSERT(list_is_empty(&list2) == TRUE); printf("M01 test cases (2) OK\n"); /* M02 */ printf("Test case M02: Initialize a node structure\n"); list_init_node(&koe1.node1); ASSERT(koe1.node1.succ == NULL); ASSERT(koe1.node1.pred == NULL); list_init_node(&koe1.node2); ASSERT(koe1.node2.succ == NULL); ASSERT(koe1.node2.pred == NULL); list_init_node(&koe2.node1); ASSERT(koe2.node1.succ == NULL); ASSERT(koe2.node1.pred == NULL); list_init_node(&koe2.node2); ASSERT(koe2.node2.succ == NULL); ASSERT(koe2.node2.pred == NULL); list_init_node(&koe3.node1); ASSERT(koe3.node1.succ == NULL); ASSERT(koe3.node1.pred == NULL); list_init_node(&koe3.node2); ASSERT(koe3.node2.succ == NULL); ASSERT(koe3.node2.pred == NULL); list_init_node(&koe4.node1); ASSERT(koe4.node1.succ == NULL); ASSERT(koe4.node1.pred == NULL); list_init_node(&koe4.node2); ASSERT(koe4.node2.succ == NULL); ASSERT(koe4.node2.pred == NULL); list_init_node(&koe5.node1); ASSERT(koe5.node1.succ == NULL); ASSERT(koe5.node1.pred == NULL); list_init_node(&koe5.node2); ASSERT(koe5.node2.succ == NULL); ASSERT(koe5.node2.pred == NULL); printf("M02 test cases (5) OK\n"); koe1.n = 1; koe2.n = 2; koe3.n = 3; koe4.n = 4; koe5.n = 5; show_numbers(&list1); ASSERT(list_is_empty(&list1) == TRUE); /* M03 */ printf("Test case M03: Add a node into the beginning of an empty " "list\n"); list_add_head(&list1, &koe2.node1); ASSERT(list1.head == &koe2.node1); ASSERT(list1.tailpred == &koe2.node1); printf("M03 test cases (1) OK\n"); /* M04 */ printf("Test case M04: Add a node into the beginning of a non-empty " "list\n"); printf("list_add_head(list1, koe4)\n"); list_add_head(&list1, &koe4.node1); ASSERT(list1.head == &koe4.node1); printf("M04 test cases (1) OK\n"); /* M05 */ printf("Test case M05: Add a node into the end of an empty list\n"); list_add_tail(&list2, &koe3.node2); ASSERT(list2.head == &koe3.node2); ASSERT(list2.tailpred == &koe3.node2); printf("M05 test cases (1) OK\n"); /* M06 */ printf("Test case M06: Add a node into the end of a non-empty list\n"); printf("list_add_tail(list2, koe1.node2)\n"); list_add_tail(&list2, &koe1.node2); ASSERT(list2.tailpred == &koe1.node2); printf("M06 test cases (1) OK\n"); /* M07 */ printf("Test case M07: Insert a node after a NULL node -> add head\n"); printf("list_insert(list1, koe1, NULL)\n"); list_insert(&list1, &koe1.node1, NULL); ASSERT(list1.head == &koe1.node1); printf("M07 test cases (1) OK\n"); show_numbers(&list1); ASSERT(list_is_empty(&list1) == FALSE); /* M08 */ printf("Test case M08: Insert a node (3) after a node (2) in the " "list\n"); printf("list_insert(list1, koe3, koe2)\n"); list_insert(&list1, &koe3.node1, &koe2.node1); ASSERT(koe2.node1.succ == &koe3.node1); ASSERT(koe3.node1.pred == &koe2.node1); printf("M08 test cases (1) OK\n"); show_numbers(&list1); list_remove(&koe1.node2); list_remove(&koe3.node2); ASSERT(list_is_empty(&list2)); /* M09 */ printf("Test case M09: Get the first node in an empty list\n"); koe = (struct koe *) list_get_first(&list2); ASSERT(koe == NULL); printf("M09 test cases (1) OK\n"); /* M10 */ printf("Test case M10: Get the first node in a non-empty list\n"); koe = (struct koe *) list_get_first(&list1); ASSERT(koe != NULL); ASSERT(koe == &koe1); printf("M10 test cases (1) OK\n"); /* M11 */ printf("Test case M11: Get the next node in the list when the node is " "the last one\n"); koe = (struct koe *) list_get_next(list1.tailpred); ASSERT(koe == NULL); printf("M11 test cases (1) OK\n"); /* M12 */ printf("Test case M12: Get the next node in the list when the node is " "not the last one\n"); koe = (struct koe *) list_get_next(list1.head); ASSERT(koe != NULL); ASSERT(koe == &koe4); printf("M12 test cases (1) OK\n"); /* M13 */ printf("Test case M13: Remove the first node from an empty list\n"); koe = (struct koe *) list_remove_first(&list2); ASSERT(koe == NULL); printf("M13 test cases (1) OK\n"); /* M14 */ printf("Test case M14: Remove the first node from a non-empty list\n"); koe = (struct koe *) list_remove_first(&list1); ASSERT(koe != NULL); ASSERT(koe == &koe1); printf("M14 test cases (1) OK\n"); list_add_head(&list1, &koe->node1); /* M15 */ printf("Test case M15: Remove the last node from an empty list\n"); koe = (struct koe *) list_remove_last(&list2); ASSERT(koe == NULL); printf("M15 test cases (1) OK\n"); /* M16 */ printf("Test case M16: Remove the last node from a non-empty list\n"); koe = (struct koe *) list_remove_last(&list1); ASSERT(koe != NULL); ASSERT(koe == &koe3); printf("M16 test cases (1) OK\n"); list_add_tail(&list1, &koe->node1); /* M17 */ printf("Test case M17: Remove the first node directly from list\n"); koe = (struct koe *) list_get_first(&list1); ASSERT(koe != NULL); ASSERT(list1.head == &koe->node1); list_remove(&koe->node1); ASSERT(list1.head != &koe->node1); printf("M17 test cases (1) OK\n"); list_add_head(&list1, &koe->node1); /* M18 */ printf("Test case M18: Remove the node in the middle of list\n"); koe = (struct koe *) list_get_next(&koe->node1); ASSERT(koe != NULL); list_remove(&koe->node1); printf("M18 test cases (1) OK\n"); koeb = (struct koe *) list_get_first(&list1); ASSERT(koeb != NULL); list_insert(&list1, &koe->node1, &koeb->node1); /* M19 */ printf("Test case M19: Remove the last node directly from list\n"); koe = (struct koe *) list1.tailpred; ASSERT(koe != NULL); ASSERT(list1.tailpred == &koe->node1); list_remove(&koe->node1); ASSERT(list1.tailpred != &koe->node1); printf("M19 test cases (1) OK\n"); list_add_tail(&list1, &koe->node1); /* M20 */ printf("Test case M20: Is an empty list empty?\n"); ASSERT(list_is_empty(&list2) == TRUE); printf("M20 test cases (1) OK\n"); /* M21 */ printf("Test case M21: Is a non-empty list empty?\n"); ASSERT(list_is_empty(&list1) == FALSE); printf("M21 test cases (1) OK\n"); /* other tests */ printf("list_insert(list1, koe5, koe2)\n"); list_insert(&list1, &koe5.node1, &koe2.node1); show_numbers(&list1); printf("list_remove(list1, koe2)\n"); list_remove(&koe2.node1); koe = &koe2; if (koe != NULL) { printf("removed %d\n", koe->n); } else { printf("list is empty\n"); } show_numbers(&list1); printf("list_add_tail(list1, koe2.node1)\n"); list_add_tail(&list1, &koe2.node1); show_numbers(&list1); printf("list_add_tail(list2, koe2.node2)\n"); list_add_tail(&list2, &koe2.node2); printf("list_remove_first(list2)\n"); node = list_remove_first(&list2); if (node != NULL) { koe = (struct koe *) (((char *) node) - offsetof(struct koe, node2)); } else { koe = NULL; } if (koe != NULL) { printf("removed %d\n", koe->n); } else { printf("list is empty\n"); } printf("list_remove_first(list1)\n"); node = list_remove_first(&list1); koe = (struct koe *) node; if (koe != NULL) { printf("removed %d\n", koe->n); } else { printf("list is empty\n"); } show_numbers(&list1); printf("list_remove_last(list1)\n"); koe = (struct koe *) list_remove_last(&list1); if (koe != NULL) { printf("removed %d\n", koe->n); } else { printf("list is empty\n"); } show_numbers(&list1); for (i = 0; i < 4; i++) { printf("list_remove_first(list1)\n"); koe = (struct koe *) list_remove_first(&list1); if (koe != NULL) { printf("removed %d\n", koe->n); } else { printf("list is empty\n"); ASSERT(list_is_empty(&list1) == TRUE); } show_numbers(&list1); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -