| 1 | |
|---|
| 2 | /* include file to perform test on any given link type */ |
|---|
| 3 | typedef struct data { |
|---|
| 4 | char buf1[16]; |
|---|
| 5 | LST_ENTRY(data) link; |
|---|
| 6 | int number; |
|---|
| 7 | char buf2[16]; |
|---|
| 8 | } data; |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | typedef struct data_head data_head; |
|---|
| 12 | |
|---|
| 13 | LST_HEAD(data_head, data); |
|---|
| 14 | |
|---|
| 15 | static void |
|---|
| 16 | list_populate( data_head *list) |
|---|
| 17 | { |
|---|
| 18 | data *itm; |
|---|
| 19 | int i; |
|---|
| 20 | |
|---|
| 21 | for(i=0;i< DATA_SET_LEN; i++) { |
|---|
| 22 | itm = BKNI_Malloc(sizeof(*itm)); |
|---|
| 23 | BKNI_Memset(itm, 0xCD, sizeof(*itm)); /* fill with junk */ |
|---|
| 24 | itm->number = data_set[i]; |
|---|
| 25 | LST_INSERT_HEAD(list, itm, link); |
|---|
| 26 | } |
|---|
| 27 | return; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | static void |
|---|
| 32 | list_populate2( data_head *list) |
|---|
| 33 | { |
|---|
| 34 | data *itm, *cur, *last; |
|---|
| 35 | int i; |
|---|
| 36 | |
|---|
| 37 | for(i=0;i< DATA_SET_LEN; i++) { |
|---|
| 38 | itm = BKNI_Malloc(sizeof(*itm)); |
|---|
| 39 | BKNI_Memset(itm, 0xCD, sizeof(*itm)); /* fill with junk */ |
|---|
| 40 | itm->number = data_set[i]; |
|---|
| 41 | |
|---|
| 42 | for (last=NULL, cur=LST_FIRST(list); cur; cur = LST_NEXT(cur, link)) { |
|---|
| 43 | last=cur; |
|---|
| 44 | } |
|---|
| 45 | if (last) { |
|---|
| 46 | LST_INSERT_AFTER(list, last, itm, link); |
|---|
| 47 | } else { |
|---|
| 48 | LST_INSERT_HEAD(list, itm, link); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | return; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | static int |
|---|
| 55 | list_compare_content( data_head *list) |
|---|
| 56 | { |
|---|
| 57 | int i; |
|---|
| 58 | data *itm; |
|---|
| 59 | |
|---|
| 60 | for(i=DATA_SET_LEN-1, itm=LST_FIRST(list); itm ; itm=LST_NEXT(itm, link), i--) { |
|---|
| 61 | if (itm->number!=data_set[i]) { |
|---|
| 62 | return -1; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | return 0; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | static int |
|---|
| 69 | list_compare_content2( data_head *list) |
|---|
| 70 | { |
|---|
| 71 | int i; |
|---|
| 72 | data *itm; |
|---|
| 73 | |
|---|
| 74 | for(i=0, itm=LST_FIRST(list); itm ; itm=LST_NEXT(itm, link), i++) { |
|---|
| 75 | if (itm->number!=data_set[i]) { |
|---|
| 76 | return -1; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | return 0; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | static void |
|---|
| 83 | list_destroy( data_head *list) |
|---|
| 84 | { |
|---|
| 85 | data *itm; |
|---|
| 86 | |
|---|
| 87 | while( NULL!=(itm=LST_FIRST(list))) { |
|---|
| 88 | LST_REMOVE_HEAD(list, link); |
|---|
| 89 | BKNI_Free(itm); |
|---|
| 90 | } |
|---|
| 91 | return; |
|---|
| 92 | |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | int |
|---|
| 96 | list_test(void) |
|---|
| 97 | { |
|---|
| 98 | data_head list1 = LST_INITIALIZER(&list1); |
|---|
| 99 | data_head list2; |
|---|
| 100 | int rc; |
|---|
| 101 | |
|---|
| 102 | BKNI_Printf("Start list_test for %s\n", list_name); |
|---|
| 103 | LST_INIT(&list2); |
|---|
| 104 | |
|---|
| 105 | list_populate(&list1); |
|---|
| 106 | list_populate(&list2); |
|---|
| 107 | |
|---|
| 108 | rc = list_compare_content(&list1); |
|---|
| 109 | if (rc<0) { |
|---|
| 110 | TEST_FAILED(list_compare_content(&list1)); |
|---|
| 111 | return rc; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | list_destroy(&list1); |
|---|
| 115 | |
|---|
| 116 | rc = list_compare_content(&list2); |
|---|
| 117 | if (rc<0) { |
|---|
| 118 | TEST_FAILED(list_compare_content(&list2)); |
|---|
| 119 | return rc; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | list_destroy(&list2); |
|---|
| 123 | |
|---|
| 124 | BKNI_Printf("."); |
|---|
| 125 | |
|---|
| 126 | list_populate2(&list1); |
|---|
| 127 | list_populate2(&list2); |
|---|
| 128 | |
|---|
| 129 | rc = list_compare_content2(&list1); |
|---|
| 130 | if (rc<0) { |
|---|
| 131 | TEST_FAILED(list_compare_content(&list1)); |
|---|
| 132 | return rc; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | list_destroy(&list1); |
|---|
| 136 | |
|---|
| 137 | rc = list_compare_content2(&list2); |
|---|
| 138 | if (rc<0) { |
|---|
| 139 | TEST_FAILED(list_compare_content(&list2)); |
|---|
| 140 | return rc; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | list_destroy(&list2); |
|---|
| 144 | |
|---|
| 145 | BKNI_Printf("."); |
|---|
| 146 | |
|---|
| 147 | BKNI_Printf("\nDone\n"); |
|---|
| 148 | |
|---|
| 149 | return 0; |
|---|
| 150 | |
|---|
| 151 | } |
|---|
| 152 | |
|---|