[01579ad] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012-2013 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file
|
---|
| 30 | *
|
---|
| 31 | * Helper functions for working with list of items.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <assert.h>
|
---|
| 35 | #include <stdlib.h>
|
---|
| 36 | #include "internal.h"
|
---|
[134ac5d] | 37 | #include <pcut/pcut.h>
|
---|
[01579ad] | 38 |
|
---|
| 39 |
|
---|
| 40 | /** Find next item with actual content.
|
---|
| 41 | *
|
---|
| 42 | * @param item Head of the list.
|
---|
| 43 | * @return First item with actual content or NULL on end of list.
|
---|
| 44 | */
|
---|
| 45 | pcut_item_t *pcut_get_real_next(pcut_item_t *item) {
|
---|
| 46 | if (item == NULL) {
|
---|
| 47 | return NULL;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | do {
|
---|
| 51 | item = item->next;
|
---|
| 52 | } while ((item != NULL) && (item->kind == PCUT_KIND_SKIP));
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | return item;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /** Retrieve the first item with actual content.
|
---|
| 59 | *
|
---|
| 60 | * Unlike pcut_get_real_next(), where we always advance after the
|
---|
| 61 | * first item, here the @p item itself could be returned.
|
---|
| 62 | *
|
---|
| 63 | * @param item Head of the list.
|
---|
| 64 | * @return First item with actual content or NULL on end of list.
|
---|
| 65 | */
|
---|
| 66 | pcut_item_t *pcut_get_real(pcut_item_t *item) {
|
---|
| 67 | if (item == NULL) {
|
---|
| 68 | return NULL;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | if (item->kind == PCUT_KIND_SKIP) {
|
---|
| 72 | return pcut_get_real_next(item);
|
---|
| 73 | } else {
|
---|
| 74 | return item;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | /** In-line nested lists into the parent.
|
---|
| 80 | *
|
---|
| 81 | * @param nested Head of the nested list.
|
---|
| 82 | */
|
---|
| 83 | static void inline_nested_lists(pcut_item_t *nested) {
|
---|
| 84 | if (nested->kind != PCUT_KIND_NESTED) {
|
---|
| 85 | return;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | if (nested->nested.last == NULL) {
|
---|
| 89 | nested->kind = PCUT_KIND_SKIP;
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | pcut_item_t *first = pcut_fix_list_get_real_head(nested->nested.last);
|
---|
| 94 | nested->nested.last->next = nested->next;
|
---|
| 95 | if (nested->next != NULL) {
|
---|
| 96 | nested->next->previous = nested->nested.last;
|
---|
| 97 | }
|
---|
| 98 | nested->next = first;
|
---|
| 99 | first->previous = nested;
|
---|
| 100 |
|
---|
| 101 | nested->kind = PCUT_KIND_SKIP;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Assing unique ids to each item in the list.
|
---|
| 105 | *
|
---|
| 106 | * @param first List head.
|
---|
| 107 | */
|
---|
| 108 | static void set_ids(pcut_item_t *first) {
|
---|
| 109 | assert(first != NULL);
|
---|
| 110 | int id = 1;
|
---|
| 111 | if (first->kind == PCUT_KIND_SKIP) {
|
---|
| 112 | first = pcut_get_real_next(first);
|
---|
| 113 | }
|
---|
| 114 | for (pcut_item_t *it = first; it != NULL; it = pcut_get_real_next(it)) {
|
---|
| 115 | it->id = id;
|
---|
| 116 | id++;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[134ac5d] | 120 | /** Hide tests that are marked to be skipped.
|
---|
| 121 | *
|
---|
| 122 | * Go through all tests and those that have PCUT_EXTRA_SKIP mark
|
---|
| 123 | * as skipped with PCUT_KIND_SKIP.
|
---|
| 124 | *
|
---|
| 125 | * @param first Head of the list.
|
---|
| 126 | */
|
---|
| 127 | static void detect_skipped_tests(pcut_item_t *first) {
|
---|
| 128 | assert(first != NULL);
|
---|
| 129 | if (first->kind == PCUT_KIND_SKIP) {
|
---|
| 130 | first = pcut_get_real_next(first);
|
---|
| 131 | }
|
---|
| 132 | for (pcut_item_t *it = first; it != NULL; it = pcut_get_real_next(it)) {
|
---|
| 133 | if (it->kind != PCUT_KIND_TEST) {
|
---|
| 134 | continue;
|
---|
| 135 | }
|
---|
| 136 | pcut_extra_t *extras = it->test.extras;
|
---|
| 137 | while (extras->type != PCUT_EXTRA_LAST) {
|
---|
| 138 | if (extras->type == PCUT_EXTRA_SKIP) {
|
---|
| 139 | it->kind = PCUT_KIND_SKIP;
|
---|
| 140 | break;
|
---|
| 141 | }
|
---|
| 142 | extras++;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[01579ad] | 147 | /** Convert the static single-linked list into a flat double-linked list.
|
---|
| 148 | *
|
---|
| 149 | * The conversion includes
|
---|
| 150 | * * adding forward links
|
---|
| 151 | * * flattening of any nested lists
|
---|
| 152 | * * assigning of unique ids
|
---|
| 153 | *
|
---|
| 154 | * @param last Tail of the list.
|
---|
| 155 | * @return Head of the fixed list.
|
---|
| 156 | */
|
---|
| 157 | pcut_item_t *pcut_fix_list_get_real_head(pcut_item_t *last) {
|
---|
| 158 | last->next = NULL;
|
---|
| 159 |
|
---|
| 160 | inline_nested_lists(last);
|
---|
| 161 |
|
---|
| 162 | pcut_item_t *next = last;
|
---|
| 163 |
|
---|
| 164 | pcut_item_t *it = last->previous;
|
---|
| 165 | while (it != NULL) {
|
---|
| 166 | it->next = next;
|
---|
| 167 | inline_nested_lists(it);
|
---|
| 168 | next = it;
|
---|
| 169 | it = it->previous;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[134ac5d] | 172 | detect_skipped_tests(next);
|
---|
| 173 |
|
---|
[01579ad] | 174 | set_ids(next);
|
---|
| 175 |
|
---|
| 176 | return next;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /** Compute the number of all tests in a list.
|
---|
| 180 | *
|
---|
| 181 | * @param it Head of the list.
|
---|
| 182 | * @return Number of tests.
|
---|
| 183 | */
|
---|
| 184 | int pcut_count_tests(pcut_item_t *it) {
|
---|
| 185 | int count = 0;
|
---|
| 186 | while (it != NULL) {
|
---|
| 187 | if (it->kind == PCUT_KIND_TEST) {
|
---|
| 188 | count++;
|
---|
| 189 | }
|
---|
| 190 | it = pcut_get_real_next(it);
|
---|
| 191 | }
|
---|
| 192 | return count;
|
---|
| 193 | }
|
---|