queue_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <unistd.h>
00026 #include <time.h>
00027 
00028 #include <isc/queue.h>
00029 
00030 #include "isctest.h"
00031 
00032 typedef struct item item_t;
00033 struct item {
00034         int                     value;
00035         ISC_QLINK(item_t)       qlink;
00036 };
00037 
00038 typedef ISC_QUEUE(item_t) item_queue_t;
00039 
00040 static void
00041 item_init(item_t *item, int value) {
00042         item->value = value;
00043         ISC_QLINK_INIT(item, qlink);
00044 }
00045 
00046 /*
00047  * Individual unit tests
00048  */
00049 
00050 /* Test UDP sendto/recv (IPv4) */
00051 ATF_TC(queue_valid);
00052 ATF_TC_HEAD(queue_valid, tc) {
00053         atf_tc_set_md_var(tc, "descr", "Check queue validity");
00054 }
00055 ATF_TC_BODY(queue_valid, tc) {
00056         isc_result_t result;
00057         item_queue_t queue;
00058         item_t one, two, three, four, five;
00059         item_t *p;
00060 
00061         UNUSED(tc);
00062 
00063         ISC_QUEUE_INIT(queue, qlink);
00064 
00065         item_init(&one, 1);
00066         item_init(&two, 2);
00067         item_init(&three, 3);
00068         item_init(&four, 4);
00069         item_init(&five, 5);
00070 
00071         result = isc_test_begin(NULL, ISC_TRUE);
00072         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00073 
00074         ATF_CHECK(ISC_QUEUE_EMPTY(queue));
00075 
00076         ISC_QUEUE_POP(queue, qlink, p);
00077         ATF_CHECK(p == NULL);
00078 
00079         ATF_CHECK(! ISC_QLINK_LINKED(&one, qlink));
00080         ISC_QUEUE_PUSH(queue, &one, qlink);
00081         ATF_CHECK(ISC_QLINK_LINKED(&one, qlink));
00082 
00083         ATF_CHECK(! ISC_QUEUE_EMPTY(queue));
00084 
00085         ISC_QUEUE_POP(queue, qlink, p);
00086         ATF_REQUIRE(p != NULL);
00087         ATF_CHECK_EQ(p->value, 1);
00088         ATF_CHECK(ISC_QUEUE_EMPTY(queue));
00089         ATF_CHECK(! ISC_QLINK_LINKED(p, qlink));
00090 
00091         ISC_QUEUE_PUSH(queue, p, qlink);
00092         ATF_CHECK(! ISC_QUEUE_EMPTY(queue));
00093         ATF_CHECK(ISC_QLINK_LINKED(p, qlink));
00094 
00095         ATF_CHECK(! ISC_QLINK_LINKED(&two, qlink));
00096         ISC_QUEUE_PUSH(queue, &two, qlink);
00097         ATF_CHECK(ISC_QLINK_LINKED(&two, qlink));
00098 
00099         ATF_CHECK(! ISC_QLINK_LINKED(&three, qlink));
00100         ISC_QUEUE_PUSH(queue, &three, qlink);
00101         ATF_CHECK(ISC_QLINK_LINKED(&three, qlink));
00102 
00103         ATF_CHECK(! ISC_QLINK_LINKED(&four, qlink));
00104         ISC_QUEUE_PUSH(queue, &four, qlink);
00105         ATF_CHECK(ISC_QLINK_LINKED(&four, qlink));
00106 
00107         ATF_CHECK(! ISC_QLINK_LINKED(&five, qlink));
00108         ISC_QUEUE_PUSH(queue, &five, qlink);
00109         ATF_CHECK(ISC_QLINK_LINKED(&five, qlink));
00110 
00111         /* Test unlink by removing one item from the middle */
00112         ISC_QUEUE_UNLINK(queue, &three, qlink);
00113 
00114         ISC_QUEUE_POP(queue, qlink, p);
00115         ATF_REQUIRE(p != NULL);
00116         ATF_CHECK_EQ(p->value, 1);
00117 
00118         ISC_QUEUE_POP(queue, qlink, p);
00119         ATF_REQUIRE(p != NULL);
00120         ATF_CHECK_EQ(p->value, 2);
00121 
00122         ISC_QUEUE_POP(queue, qlink, p);
00123         ATF_REQUIRE(p != NULL);
00124         ATF_CHECK_EQ(p->value, 4);
00125 
00126         ISC_QUEUE_POP(queue, qlink, p);
00127         ATF_REQUIRE(p != NULL);
00128         ATF_CHECK_EQ(p->value, 5);
00129 
00130         ATF_CHECK(ISC_QUEUE_EMPTY(queue));
00131 
00132         ISC_QUEUE_DESTROY(queue);
00133         isc_test_end();
00134 }
00135 
00136 /*
00137  * Main
00138  */
00139 ATF_TP_ADD_TCS(tp) {
00140         ATF_TP_ADD_TC(tp, queue_valid);
00141 
00142         return (atf_no_error());
00143 }
00144 

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha