Merge pull request #1534 from LudwigOrtmann/queue-cleanup-two
core/queue: queue -> priority_queue && dynamic initializersdev/timer
commit
4c6ba818ed
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (C) 2013, 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup core_util
|
||||
* @{
|
||||
*
|
||||
* @file priority_queue.h
|
||||
* @brief A simple priority queue
|
||||
*
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*/
|
||||
|
||||
#ifndef __QUEUE_H
|
||||
#define __QUEUE_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* data type for priority queue nodes
|
||||
*/
|
||||
typedef struct priority_queue_node_t {
|
||||
struct priority_queue_node_t *next; /**< next queue node */
|
||||
unsigned int data; /**< queue node data */
|
||||
uint32_t priority; /**< queue node priority */
|
||||
} priority_queue_node_t;
|
||||
|
||||
/**
|
||||
* data type for priority queues
|
||||
*/
|
||||
typedef struct queue {
|
||||
priority_queue_node_t *first; /**< first queue node */
|
||||
} priority_queue_t;
|
||||
|
||||
/**
|
||||
* @brief Static initializer for priority_queue_node_t.
|
||||
*/
|
||||
#define PRIORITY_QUEUE_NODE_INIT { NULL, 0, 0 }
|
||||
|
||||
/**
|
||||
* @brief Initialize a priority queue node object.
|
||||
* @details For initialization of variables use PRIORITY_QUEUE_NODE_INIT
|
||||
* instead. Only use this function for dynamically allocated
|
||||
* priority queue nodes.
|
||||
* @param[out] priority_queue_node
|
||||
* pre-allocated priority_queue_node_t object, must not be NULL.
|
||||
*/
|
||||
static inline void priority_queue_node_init(
|
||||
priority_queue_node_t *priority_queue_node)
|
||||
{
|
||||
priority_queue_node_t qn = PRIORITY_QUEUE_NODE_INIT;
|
||||
*priority_queue_node = qn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Static initializer for priority_queue_t.
|
||||
*/
|
||||
#define PRIORITY_QUEUE_INIT { NULL }
|
||||
|
||||
/**
|
||||
* @brief Initialize a priority queue object.
|
||||
* @details For initialization of variables use PRIORITY_QUEUE_INIT
|
||||
* instead. Only use this function for dynamically allocated
|
||||
* priority queues.
|
||||
* @param[out] priority_queue
|
||||
* pre-allocated priority_queue_t object, must not be NULL.
|
||||
*/
|
||||
static inline void priority_queue_init(priority_queue_t *priority_queue)
|
||||
{
|
||||
priority_queue_t q = PRIORITY_QUEUE_INIT;
|
||||
*priority_queue = q;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief remove the priority queue's head
|
||||
*
|
||||
* @param[out] root the queue's root
|
||||
*
|
||||
* @return the old head
|
||||
*/
|
||||
priority_queue_node_t *priority_queue_remove_head(priority_queue_t *root);
|
||||
|
||||
/**
|
||||
* @brief insert `new_obj` into `root` based on its priority
|
||||
*
|
||||
* @details
|
||||
* The new object will be appended after objects with the same priority.
|
||||
*
|
||||
* @param[in,out] root the queue's root
|
||||
* @param[in] new_obj the object to prepend
|
||||
*/
|
||||
void priority_queue_add(priority_queue_t *root, priority_queue_node_t *new_obj);
|
||||
|
||||
/**
|
||||
* @brief remove `node` from `root`
|
||||
*
|
||||
* @param[in,out] root the priority queue's root
|
||||
* @param[in] node the node to remove
|
||||
*/
|
||||
void priority_queue_remove(priority_queue_t *root, priority_queue_node_t *node);
|
||||
|
||||
#if ENABLE_DEBUG
|
||||
void priority_queue_print(priority_queue_t *root);
|
||||
void priority_queue_print_node(priority_queue_t *root);
|
||||
#endif
|
||||
|
||||
/** @} */
|
||||
#endif /* __QUEUE_H */
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Freie Universität Berlin
|
||||
*
|
||||
* This file subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup core_util
|
||||
* @{
|
||||
*
|
||||
* @file queue.h
|
||||
* @brief A simple queue implementation
|
||||
*
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*/
|
||||
|
||||
#ifndef __QUEUE_H
|
||||
#define __QUEUE_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* data type for priority queue nodes
|
||||
*/
|
||||
typedef struct queue_node_t {
|
||||
struct queue_node_t *next; /**< next queue node */
|
||||
unsigned int data; /**< queue node data */
|
||||
uint32_t priority; /**< queue node priority */
|
||||
} queue_node_t;
|
||||
|
||||
/**
|
||||
* data type for priority queues
|
||||
*/
|
||||
typedef struct queue {
|
||||
queue_node_t *first; /**< first queue node */
|
||||
} queue_t;
|
||||
|
||||
/**
|
||||
* @brief Static initializer for queue_node_t.
|
||||
*/
|
||||
#define QUEUE_NODE_INIT { NULL, 0, 0 }
|
||||
|
||||
/**
|
||||
* @brief Static initializer for queue_t.
|
||||
*/
|
||||
#define QUEUE_INIT { NULL }
|
||||
|
||||
/**
|
||||
* @brief remove the queue's head
|
||||
*
|
||||
* @param[out] root the queue's root
|
||||
*
|
||||
* @return the old head
|
||||
*/
|
||||
queue_node_t *queue_remove_head(queue_t *root);
|
||||
|
||||
/**
|
||||
* @brief insert `new_obj` into `root` based on its priority
|
||||
*
|
||||
* @details
|
||||
* The new object will be appended after objects with the same priority.
|
||||
*
|
||||
* @param[in,out] root the queue's root
|
||||
* @param[in] new_obj the object to prepend
|
||||
*/
|
||||
void queue_priority_add(queue_t *root, queue_node_t *new_obj);
|
||||
|
||||
/**
|
||||
* @brief remove `node` from `root`
|
||||
*
|
||||
* @param[in,out] root the queue's root
|
||||
* @param[in] node the node to remove
|
||||
*/
|
||||
void queue_remove(queue_t *root, queue_node_t *node);
|
||||
|
||||
#if ENABLE_DEBUG
|
||||
void queue_print(queue_t *root);
|
||||
void queue_print_node(queue_t *root);
|
||||
#endif
|
||||
|
||||
/** @} */
|
||||
#endif /* __QUEUE_H */
|
@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Martin Lenders
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "embUnit/embUnit.h"
|
||||
|
||||
#include "priority_queue.h"
|
||||
|
||||
#include "tests-core.h"
|
||||
|
||||
#define Q_LEN (4)
|
||||
|
||||
static priority_queue_t q = PRIORITY_QUEUE_INIT;
|
||||
static priority_queue_node_t qe[Q_LEN];
|
||||
|
||||
static void set_up(void)
|
||||
{
|
||||
priority_queue_init(&q);
|
||||
for (unsigned i = 0; i < sizeof(qe)/sizeof(priority_queue_node_t); ++i) {
|
||||
priority_queue_node_init(&(qe[i]));
|
||||
}
|
||||
}
|
||||
|
||||
static void test_priority_queue_remove_head_empty(void)
|
||||
{
|
||||
priority_queue_t *root = &q;
|
||||
priority_queue_node_t *res;
|
||||
|
||||
res = priority_queue_remove_head(root);
|
||||
|
||||
TEST_ASSERT_NULL(res);
|
||||
}
|
||||
|
||||
static void test_priority_queue_remove_head_one(void)
|
||||
{
|
||||
priority_queue_t *root = &q;
|
||||
priority_queue_node_t *elem = &(qe[1]), *res;
|
||||
|
||||
elem->data = 62801;
|
||||
|
||||
priority_queue_add(root, elem);
|
||||
|
||||
res = priority_queue_remove_head(root);
|
||||
|
||||
TEST_ASSERT(res == elem);
|
||||
TEST_ASSERT_EQUAL_INT(62801, res->data);
|
||||
|
||||
res = priority_queue_remove_head(root);
|
||||
|
||||
TEST_ASSERT_NULL(res);
|
||||
}
|
||||
|
||||
static void test_priority_queue_add_one(void)
|
||||
{
|
||||
priority_queue_t *root = &q;
|
||||
priority_queue_node_t *elem = &(qe[1]);
|
||||
|
||||
elem->data = 7317;
|
||||
elem->priority = 713643658;
|
||||
|
||||
priority_queue_add(root, elem);
|
||||
|
||||
TEST_ASSERT(root->first == elem);
|
||||
TEST_ASSERT_EQUAL_INT(7317, root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(713643658, root->first->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next);
|
||||
}
|
||||
|
||||
static void test_priority_queue_add_two_equal(void)
|
||||
{
|
||||
priority_queue_t *root = &q;
|
||||
priority_queue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
|
||||
|
||||
elem1->data = 27088;
|
||||
elem1->priority = 14202;
|
||||
|
||||
elem2->data = 4356;
|
||||
elem2->priority = 14202;
|
||||
|
||||
priority_queue_add(root, elem1);
|
||||
priority_queue_add(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem1);
|
||||
TEST_ASSERT_EQUAL_INT(27088, root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(14202, root->first->priority);
|
||||
|
||||
TEST_ASSERT(root->first->next == elem2);
|
||||
TEST_ASSERT_EQUAL_INT(4356, root->first->next->data);
|
||||
TEST_ASSERT_EQUAL_INT(14202, root->first->next->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
static void test_priority_queue_add_two_distinct(void)
|
||||
{
|
||||
priority_queue_t *root = &q;
|
||||
priority_queue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
|
||||
|
||||
elem1->data = 46421;
|
||||
elem1->priority = 4567;
|
||||
|
||||
elem2->data = 43088;
|
||||
elem2->priority = 1234;
|
||||
|
||||
priority_queue_add(root, elem1);
|
||||
priority_queue_add(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem2);
|
||||
TEST_ASSERT_EQUAL_INT(43088, root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(1234, root->first->priority);
|
||||
|
||||
TEST_ASSERT(root->first->next == elem1);
|
||||
TEST_ASSERT_EQUAL_INT(46421, root->first->next->data);
|
||||
TEST_ASSERT_EQUAL_INT(4567, root->first->next->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
static void test_priority_queue_remove_one(void)
|
||||
{
|
||||
priority_queue_t *root = &q;
|
||||
priority_queue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]), *elem3 = &(qe[3]);
|
||||
|
||||
priority_queue_add(root, elem1);
|
||||
priority_queue_add(root, elem2);
|
||||
priority_queue_add(root, elem3);
|
||||
priority_queue_remove(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem1);
|
||||
TEST_ASSERT(root->first->next == elem3);
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
Test *tests_core_priority_queue_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_priority_queue_remove_head_empty),
|
||||
new_TestFixture(test_priority_queue_remove_head_one),
|
||||
new_TestFixture(test_priority_queue_add_one),
|
||||
new_TestFixture(test_priority_queue_add_two_equal),
|
||||
new_TestFixture(test_priority_queue_add_two_distinct),
|
||||
new_TestFixture(test_priority_queue_remove_one),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(core_priority_queue_tests, set_up, NULL,
|
||||
fixtures);
|
||||
|
||||
return (Test *)&core_priority_queue_tests;
|
||||
}
|
@ -1,153 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Martin Lenders
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "embUnit/embUnit.h"
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
#include "tests-core.h"
|
||||
|
||||
#define Q_LEN (4)
|
||||
|
||||
static queue_t q;
|
||||
static queue_node_t qe[Q_LEN];
|
||||
|
||||
static void set_up(void)
|
||||
{
|
||||
q.first = NULL;
|
||||
memset(qe, 0, sizeof(qe));
|
||||
}
|
||||
|
||||
static void test_queue_remove_head_empty(void)
|
||||
{
|
||||
queue_t *root = &q;
|
||||
queue_node_t *res;
|
||||
|
||||
res = queue_remove_head(root);
|
||||
|
||||
TEST_ASSERT_NULL(res);
|
||||
}
|
||||
|
||||
static void test_queue_remove_head_one(void)
|
||||
{
|
||||
queue_t *root = &q;
|
||||
queue_node_t *elem = &(qe[1]), *res;
|
||||
|
||||
elem->data = 62801;
|
||||
|
||||
queue_priority_add(root, elem);
|
||||
|
||||
res = queue_remove_head(root);
|
||||
|
||||
TEST_ASSERT(res == elem);
|
||||
TEST_ASSERT_EQUAL_INT(62801, res->data);
|
||||
|
||||
res = queue_remove_head(root);
|
||||
|
||||
TEST_ASSERT_NULL(res);
|
||||
}
|
||||
|
||||
static void test_queue_priority_add_one(void)
|
||||
{
|
||||
queue_t *root = &q;
|
||||
queue_node_t *elem = &(qe[1]);
|
||||
|
||||
elem->data = 7317;
|
||||
elem->priority = 713643658;
|
||||
|
||||
queue_priority_add(root, elem);
|
||||
|
||||
TEST_ASSERT(root->first == elem);
|
||||
TEST_ASSERT_EQUAL_INT(7317, root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(713643658, root->first->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next);
|
||||
}
|
||||
|
||||
static void test_queue_priority_add_two_equal(void)
|
||||
{
|
||||
queue_t *root = &q;
|
||||
queue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
|
||||
|
||||
elem1->data = 27088;
|
||||
elem1->priority = 14202;
|
||||
|
||||
elem2->data = 4356;
|
||||
elem2->priority = 14202;
|
||||
|
||||
queue_priority_add(root, elem1);
|
||||
queue_priority_add(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem1);
|
||||
TEST_ASSERT_EQUAL_INT(27088, root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(14202, root->first->priority);
|
||||
|
||||
TEST_ASSERT(root->first->next == elem2);
|
||||
TEST_ASSERT_EQUAL_INT(4356, root->first->next->data);
|
||||
TEST_ASSERT_EQUAL_INT(14202, root->first->next->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
static void test_queue_priority_add_two_distinct(void)
|
||||
{
|
||||
queue_t *root = &q;
|
||||
queue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
|
||||
|
||||
elem1->data = 46421;
|
||||
elem1->priority = 4567;
|
||||
|
||||
elem2->data = 43088;
|
||||
elem2->priority = 1234;
|
||||
|
||||
queue_priority_add(root, elem1);
|
||||
queue_priority_add(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem2);
|
||||
TEST_ASSERT_EQUAL_INT(43088, root->first->data);
|
||||
TEST_ASSERT_EQUAL_INT(1234, root->first->priority);
|
||||
|
||||
TEST_ASSERT(root->first->next == elem1);
|
||||
TEST_ASSERT_EQUAL_INT(46421, root->first->next->data);
|
||||
TEST_ASSERT_EQUAL_INT(4567, root->first->next->priority);
|
||||
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
static void test_queue_remove_one(void)
|
||||
{
|
||||
queue_t *root = &q;
|
||||
queue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]), *elem3 = &(qe[3]);
|
||||
|
||||
queue_priority_add(root, elem1);
|
||||
queue_priority_add(root, elem2);
|
||||
queue_priority_add(root, elem3);
|
||||
queue_remove(root, elem2);
|
||||
|
||||
TEST_ASSERT(root->first == elem1);
|
||||
TEST_ASSERT(root->first->next == elem3);
|
||||
TEST_ASSERT_NULL(root->first->next->next);
|
||||
}
|
||||
|
||||
Test *tests_core_queue_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_queue_remove_head_empty),
|
||||
new_TestFixture(test_queue_remove_head_one),
|
||||
new_TestFixture(test_queue_priority_add_one),
|
||||
new_TestFixture(test_queue_priority_add_two_equal),
|
||||
new_TestFixture(test_queue_priority_add_two_distinct),
|
||||
new_TestFixture(test_queue_remove_one),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(core_queue_tests, set_up, NULL,
|
||||
fixtures);
|
||||
|
||||
return (Test *)&core_queue_tests;
|
||||
}
|
Loading…
Reference in New Issue