Annotation of embedaddon/sudo/common/regress/tailq/hltq_test.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
! 3: *
! 4: * Permission to use, copy, modify, and distribute this software for any
! 5: * purpose with or without fee is hereby granted, provided that the above
! 6: * copyright notice and this permission notice appear in all copies.
! 7: *
! 8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 15: */
! 16:
! 17: #include <config.h>
! 18:
! 19: #include <sys/types.h>
! 20: #include <stdio.h>
! 21: #ifdef STDC_HEADERS
! 22: # include <stdlib.h>
! 23: # include <stddef.h>
! 24: #else
! 25: # ifdef HAVE_STDLIB_H
! 26: # include <stdlib.h>
! 27: # endif
! 28: #endif /* STDC_HEADERS */
! 29: #ifdef HAVE_STRING_H
! 30: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
! 31: # include <memory.h>
! 32: # endif
! 33: # include <string.h>
! 34: #endif /* HAVE_STRING_H */
! 35: #ifdef HAVE_STRINGS_H
! 36: # include <strings.h>
! 37: #endif /* HAVE_STRINGS_H */
! 38: #ifdef HAVE_STDBOOL_H
! 39: # include <stdbool.h>
! 40: #else
! 41: # include "compat/stdbool.h"
! 42: #endif
! 43:
! 44: #include "missing.h"
! 45: #include "fatal.h"
! 46: #include "queue.h"
! 47: #include "sudo_util.h"
! 48:
! 49: __dso_public int main(int argc, char *argv[]);
! 50:
! 51: /*
! 52: * Note: HLTQ_ENTRY is intentionally in the middle of the struct
! 53: * to catch bad assumptions in the PREV/NEXT macros.
! 54: */
! 55: struct test_data {
! 56: int a;
! 57: HLTQ_ENTRY(test_data) entries;
! 58: char b;
! 59: };
! 60:
! 61: TAILQ_HEAD(test_data_list, test_data);
! 62:
! 63: /*
! 64: * Simple tests for headless tail queue macros.
! 65: */
! 66: int
! 67: main(int argc, char *argv[])
! 68: {
! 69: struct test_data d1, d2, d3;
! 70: struct test_data *hltq;
! 71: struct test_data_list tq;
! 72: int errors = 0;
! 73: int ntests = 0;
! 74:
! 75: initprogname(argc > 0 ? argv[0] : "hltq_test");
! 76:
! 77: /*
! 78: * Initialize three data elements and concatenate them in order.
! 79: */
! 80: HLTQ_INIT(&d1, entries);
! 81: d1.a = 1;
! 82: d1.b = 'a';
! 83: if (HLTQ_FIRST(&d1) != &d1) {
! 84: warningx_nodebug("FAIL: HLTQ_FIRST(1 entry) doesn't return first element: got %p, expected %p", HLTQ_FIRST(&d1), &d1);
! 85: errors++;
! 86: }
! 87: ntests++;
! 88: if (HLTQ_LAST(&d1, test_data, entries) != &d1) {
! 89: warningx_nodebug("FAIL: HLTQ_LAST(1 entry) doesn't return first element: got %p, expected %p", HLTQ_LAST(&d1, test_data, entries), &d1);
! 90: errors++;
! 91: }
! 92: ntests++;
! 93: if (HLTQ_PREV(&d1, test_data, entries) != NULL) {
! 94: warningx_nodebug("FAIL: HLTQ_PREV(1 entry) doesn't return NULL: got %p", HLTQ_PREV(&d1, test_data, entries));
! 95: errors++;
! 96: }
! 97: ntests++;
! 98:
! 99: HLTQ_INIT(&d2, entries);
! 100: d2.a = 2;
! 101: d2.b = 'b';
! 102:
! 103: HLTQ_INIT(&d3, entries);
! 104: d3.a = 3;
! 105: d3.b = 'c';
! 106:
! 107: HLTQ_CONCAT(&d1, &d2, entries);
! 108: HLTQ_CONCAT(&d1, &d3, entries);
! 109: hltq = &d1;
! 110:
! 111: /*
! 112: * Verify that HLTQ_FIRST, HLTQ_LAST, HLTQ_NEXT, HLTQ_PREV
! 113: * work as expected.
! 114: */
! 115: if (HLTQ_FIRST(hltq) != &d1) {
! 116: warningx_nodebug("FAIL: HLTQ_FIRST(3 entries) doesn't return first element: got %p, expected %p", HLTQ_FIRST(hltq), &d1);
! 117: errors++;
! 118: }
! 119: ntests++;
! 120: if (HLTQ_LAST(hltq, test_data, entries) != &d3) {
! 121: warningx_nodebug("FAIL: HLTQ_LAST(3 entries) doesn't return third element: got %p, expected %p", HLTQ_LAST(hltq, test_data, entries), &d3);
! 122: errors++;
! 123: }
! 124: ntests++;
! 125:
! 126: if (HLTQ_NEXT(&d1, entries) != &d2) {
! 127: warningx_nodebug("FAIL: HLTQ_NEXT(&d1) doesn't return &d2: got %p, expected %p", HLTQ_NEXT(&d1, entries), &d2);
! 128: errors++;
! 129: }
! 130: ntests++;
! 131: if (HLTQ_NEXT(&d2, entries) != &d3) {
! 132: warningx_nodebug("FAIL: HLTQ_NEXT(&d2) doesn't return &d3: got %p, expected %p", HLTQ_NEXT(&d2, entries), &d3);
! 133: errors++;
! 134: }
! 135: ntests++;
! 136: if (HLTQ_NEXT(&d3, entries) != NULL) {
! 137: warningx_nodebug("FAIL: HLTQ_NEXT(&d3) doesn't return NULL: got %p", HLTQ_NEXT(&d3, entries));
! 138: errors++;
! 139: }
! 140: ntests++;
! 141:
! 142: if (HLTQ_PREV(&d1, test_data, entries) != NULL) {
! 143: warningx_nodebug("FAIL: HLTQ_PREV(&d1) doesn't return NULL: got %p", HLTQ_PREV(&d1, test_data, entries));
! 144: errors++;
! 145: }
! 146: ntests++;
! 147: if (HLTQ_PREV(&d2, test_data, entries) != &d1) {
! 148: warningx_nodebug("FAIL: HLTQ_PREV(&d2) doesn't return &d1: got %p, expected %p", HLTQ_PREV(&d2, test_data, entries), &d1);
! 149: errors++;
! 150: }
! 151: ntests++;
! 152: if (HLTQ_PREV(&d3, test_data, entries) != &d2) {
! 153: warningx_nodebug("FAIL: HLTQ_PREV(&d3) doesn't return &d2: got %p, expected %p", HLTQ_PREV(&d3, test_data, entries), &d2);
! 154: errors++;
! 155: }
! 156: ntests++;
! 157:
! 158: /* Test conversion to TAILQ. */
! 159: HLTQ_TO_TAILQ(&tq, hltq, entries);
! 160:
! 161: if (TAILQ_FIRST(&tq) != &d1) {
! 162: warningx_nodebug("FAIL: TAILQ_FIRST(&tq) doesn't return first element: got %p, expected %p", TAILQ_FIRST(&tq), &d1);
! 163: errors++;
! 164: }
! 165: ntests++;
! 166: if (TAILQ_LAST(&tq, test_data_list) != &d3) {
! 167: warningx_nodebug("FAIL: TAILQ_LAST(&tq) doesn't return third element: got %p, expected %p", TAILQ_LAST(&tq, test_data_list), &d3);
! 168: errors++;
! 169: }
! 170: ntests++;
! 171:
! 172: if (TAILQ_NEXT(&d1, entries) != &d2) {
! 173: warningx_nodebug("FAIL: TAILQ_NEXT(&d1) doesn't return &d2: got %p, expected %p", TAILQ_NEXT(&d1, entries), &d2);
! 174: errors++;
! 175: }
! 176: ntests++;
! 177: if (TAILQ_NEXT(&d2, entries) != &d3) {
! 178: warningx_nodebug("FAIL: TAILQ_NEXT(&d2) doesn't return &d3: got %p, expected %p", TAILQ_NEXT(&d2, entries), &d3);
! 179: errors++;
! 180: }
! 181: ntests++;
! 182: if (TAILQ_NEXT(&d3, entries) != NULL) {
! 183: warningx_nodebug("FAIL: TAILQ_NEXT(&d3) doesn't return NULL: got %p", TAILQ_NEXT(&d3, entries));
! 184: errors++;
! 185: }
! 186: ntests++;
! 187:
! 188: if (TAILQ_PREV(&d1, test_data_list, entries) != NULL) {
! 189: warningx_nodebug("FAIL: TAILQ_PREV(&d1) doesn't return NULL: got %p", TAILQ_PREV(&d1, test_data_list, entries));
! 190: errors++;
! 191: }
! 192: ntests++;
! 193: if (TAILQ_PREV(&d2, test_data_list, entries) != &d1) {
! 194: warningx_nodebug("FAIL: TAILQ_PREV(&d2) doesn't return &d1: got %p, expected %p", TAILQ_PREV(&d2, test_data_list, entries), &d1);
! 195: errors++;
! 196: }
! 197: ntests++;
! 198: if (TAILQ_PREV(&d3, test_data_list, entries) != &d2) {
! 199: warningx_nodebug("FAIL: TAILQ_PREV(&d3) doesn't return &d2: got %p, expected %p", TAILQ_PREV(&d3, test_data_list, entries), &d2);
! 200: errors++;
! 201: }
! 202: ntests++;
! 203:
! 204: printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(),
! 205: ntests, errors, (ntests - errors) * 100 / ntests);
! 206:
! 207: exit(errors);
! 208: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>