From 1000998228ad9b3bbb7c2482e556a84cd4f5bab9 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 19 Jan 2012 21:57:26 +0900 Subject: [PATCH] Added spdylay_queue --- lib/Makefile.am | 4 +- lib/spdylay_queue.c | 93 ++++++++++++++++++++++++++++++++++++++ lib/spdylay_queue.h | 51 +++++++++++++++++++++ tests/Makefile.am | 4 +- tests/main.c | 4 +- tests/spdylay_queue_test.c | 49 ++++++++++++++++++++ tests/spdylay_queue_test.h | 30 ++++++++++++ 7 files changed, 230 insertions(+), 5 deletions(-) create mode 100644 lib/spdylay_queue.c create mode 100644 lib/spdylay_queue.h create mode 100644 tests/spdylay_queue_test.c create mode 100644 tests/spdylay_queue_test.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 3b93f7c..0fe52b4 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -31,9 +31,9 @@ DISTCLEANFILES = $(pkgconfig_DATA) lib_LTLIBRARIES = libspdylay.la -OBJECTS = spdylay_pq.c spdylay_map.c +OBJECTS = spdylay_pq.c spdylay_map.c spdylay_queue.c -HFILES = spdylay_pq.h spdylay_int.h spdylay_map.h +HFILES = spdylay_pq.h spdylay_int.h spdylay_map.h spdylay_queue.h libspdylay_la_SOURCES = $(HFILES) $(OBJECTS) libspdylay_la_LDFLAGS = -no-undefined \ diff --git a/lib/spdylay_queue.c b/lib/spdylay_queue.c new file mode 100644 index 0000000..d718e58 --- /dev/null +++ b/lib/spdylay_queue.c @@ -0,0 +1,93 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2012 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "spdylay_queue.h" + +#include +#include + +void spdylay_queue_init(spdylay_queue *queue) +{ + queue->front = queue->back = NULL; +} + +void spdylay_queue_free(spdylay_queue *queue) +{ + if(!queue) { + return; + } + spdylay_queue_cell *p = queue->front; + while(p) { + spdylay_queue_cell *next = p->next; + free(p); + p = next; + } +} + +int spdylay_queue_push(spdylay_queue *queue, void *data) +{ + spdylay_queue_cell *new_cell = (spdylay_queue_cell*)malloc + (sizeof(spdylay_queue_cell)); + if(!new_cell) { + return SPDYLAY_ERR_NOMEM; + } + new_cell->data = data; + new_cell->next = NULL; + if(queue->back) { + queue->back->next = new_cell; + queue->back = new_cell; + + } else { + queue->front = queue->back = new_cell; + } + return 0; +} + +void spdylay_queue_pop(spdylay_queue *queue) +{ + spdylay_queue_cell *front = queue->front; + assert(front); + queue->front = front->next; + if(front == queue->back) { + queue->back = NULL; + } + free(front); +} + +void* spdylay_queue_front(spdylay_queue *queue) +{ + assert(queue->front); + return queue->front->data; +} + +void* spdylay_queue_back(spdylay_queue *queue) +{ + assert(queue->back); + return queue->back->data; +} + +int spdylay_queue_empty(spdylay_queue *queue) +{ + return queue->front == NULL; +} diff --git a/lib/spdylay_queue.h b/lib/spdylay_queue.h new file mode 100644 index 0000000..ff20bca --- /dev/null +++ b/lib/spdylay_queue.h @@ -0,0 +1,51 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2012 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef SPDYLAY_QUEUE_H +#define SPDYLAY_QUEUE_H + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include + +typedef struct spdylay_queue_cell { + void *data; + struct spdylay_queue_cell *next; +} spdylay_queue_cell; + +typedef struct { + spdylay_queue_cell *front, *back; +} spdylay_queue; + +void spdylay_queue_init(spdylay_queue *queue); +void spdylay_queue_free(spdylay_queue *queue); +int spdylay_queue_push(spdylay_queue *queue, void *data); +void spdylay_queue_pop(spdylay_queue *queue); +void* spdylay_queue_front(spdylay_queue *queue); +void* spdylay_queue_back(spdylay_queue *queue); +int spdylay_queue_empty(spdylay_queue *queue); + +#endif /* SPDYLAY_QUEUE_H */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 24518ac..34c2683 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -25,9 +25,9 @@ if HAVE_CUNIT check_PROGRAMS = main -OBJECTS = main.c spdylay_pq_test.c spdylay_map_test.c +OBJECTS = main.c spdylay_pq_test.c spdylay_map_test.c spdylay_queue_test.c -HFILES = spdylay_pq_test.h spdylay_map_test.h +HFILES = spdylay_pq_test.h spdylay_map_test.h spdylay_queue_test.h main_SOURCES = $(HFILES) $(OBJECTS) diff --git a/tests/main.c b/tests/main.c index 9adee55..8a36234 100644 --- a/tests/main.c +++ b/tests/main.c @@ -28,6 +28,7 @@ /* include test cases' include files here */ #include "spdylay_pq_test.h" #include "spdylay_map_test.h" +#include "spdylay_queue_test.h" int init_suite1(void) { @@ -57,7 +58,8 @@ int main() /* add the tests to the suite */ if(!CU_add_test(pSuite, "pq", test_spdylay_pq) || - !CU_add_test(pSuite, "map", test_spdylay_map)) { + !CU_add_test(pSuite, "map", test_spdylay_map) || + !CU_add_test(pSuite, "queue", test_spdylay_queue)) { CU_cleanup_registry(); return CU_get_error(); } diff --git a/tests/spdylay_queue_test.c b/tests/spdylay_queue_test.c new file mode 100644 index 0000000..d46526f --- /dev/null +++ b/tests/spdylay_queue_test.c @@ -0,0 +1,49 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2012 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "spdylay_queue_test.h" + +#include + +#include "spdylay_queue.h" + +void test_spdylay_queue() +{ + int ints[] = { 1, 2, 3, 4, 5 }; + int i; + spdylay_queue queue; + spdylay_queue_init(&queue); + CU_ASSERT(spdylay_queue_empty(&queue)); + for(i = 0; i < 5; ++i) { + spdylay_queue_push(&queue, &ints[i]); + CU_ASSERT_EQUAL(ints[0], *(int*)(spdylay_queue_front(&queue))); + CU_ASSERT(!spdylay_queue_empty(&queue)); + } + for(i = 0; i < 5; ++i) { + CU_ASSERT_EQUAL(ints[i], *(int*)(spdylay_queue_front(&queue))); + spdylay_queue_pop(&queue); + } + CU_ASSERT(spdylay_queue_empty(&queue)); + spdylay_queue_free(&queue); +} diff --git a/tests/spdylay_queue_test.h b/tests/spdylay_queue_test.h new file mode 100644 index 0000000..3540837 --- /dev/null +++ b/tests/spdylay_queue_test.h @@ -0,0 +1,30 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2012 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef SPDYLAY_QUEUE_TEST_H +#define SPDYLAY_QUEUE_TEST_H + +void test_spdylay_queue(); + +#endif // SPDYLAY_QUEUE_TEST_H