[svn-commits] twilson: branch twilson/calendaring r182448 - in /team/twilson/calendaring: a...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Mar 17 00:29:32 CDT 2009


Author: twilson
Date: Tue Mar 17 00:29:29 2009
New Revision: 182448

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=182448
Log:
Add missing files

Added:
    team/twilson/calendaring/apps/app_playtones.c   (with props)
    team/twilson/calendaring/include/asterisk/heap.h   (with props)
    team/twilson/calendaring/main/heap.c   (with props)
    team/twilson/calendaring/tests/test_heap.c   (with props)
    team/twilson/calendaring/tests/test_sched.c   (with props)

Added: team/twilson/calendaring/apps/app_playtones.c
URL: http://svn.digium.com/svn-view/asterisk/team/twilson/calendaring/apps/app_playtones.c?view=auto&rev=182448
==============================================================================
--- team/twilson/calendaring/apps/app_playtones.c (added)
+++ team/twilson/calendaring/apps/app_playtones.c Tue Mar 17 00:29:29 2009
@@ -1,0 +1,129 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Playtones application
+ *
+ * \author Russell Bryant <russell at digium.com>
+ *
+ * \ingroup applications
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 176627 $")
+
+#include "asterisk/module.h"
+#include "asterisk/pbx.h"
+#include "asterisk/channel.h"
+#include "asterisk/indications.h"
+
+static const char playtones_app[] = "PlayTones";
+static const char stopplaytones_app[] = "StopPlayTones";
+
+/*** DOCUMENTATION
+	<application name="PlayTones" language="en_US">
+		<synopsis>
+			Play a tone list.
+		</synopsis>
+		<syntax>
+			<parameter name="arg" required="true">
+				<para>Arg is either the tone name defined in the <filename>indications.conf</filename>
+				configuration file, or a directly specified list of frequencies and durations.</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para>Plays a tone list. Execution will continue with the next step in the dialplan
+			immediately while the tones continue to play.</para>
+			<para>See the sample <filename>indications.conf</filename> for a description of the
+			specification of a tonelist.</para>
+		</description>
+		<see-also>
+			<ref type="application">StopPlayTones</ref>
+		</see-also>
+	</application>
+	<application name="StopPlayTones" language="en_US">
+		<synopsis>
+			Stop playing a tone list.
+		</synopsis>
+		<syntax />
+		<description>
+			<para>Stop playing a tone list, initiated by PlayTones().</para>
+		</description>
+		<see-also>
+			<ref type="application">PlayTones</ref>
+		</see-also>
+	</application>
+ ***/
+
+static int handle_playtones(struct ast_channel *chan, void *data)
+{
+	struct ast_tone_zone_sound *ts;
+	int res;
+	const char *str = data;
+
+	if (ast_strlen_zero(str)) {
+		ast_log(LOG_NOTICE,"Nothing to play\n");
+		return -1;
+	}
+
+	ts = ast_get_indication_tone(chan->zone, str);
+
+	if (ts) {
+		res = ast_playtones_start(chan, 0, ts->data, 0);
+		ts = ast_tone_zone_sound_unref(ts);
+	} else {
+		res = ast_playtones_start(chan, 0, str, 0);
+	}
+
+	if (res) {
+		ast_log(LOG_NOTICE, "Unable to start playtones\n");
+	}
+
+	return res;
+}
+
+static int handle_stopplaytones(struct ast_channel *chan, void *data)
+{
+	ast_playtones_stop(chan);
+
+	return 0;
+}
+
+static int unload_module(void)
+{
+	int res;
+
+	res = ast_unregister_application(playtones_app);
+	res |= ast_unregister_application(stopplaytones_app);
+
+	return res;
+}
+
+static int load_module(void)
+{
+	int res;
+
+	res = ast_register_application_xml(playtones_app, handle_playtones);
+	res |= ast_register_application_xml(stopplaytones_app, handle_stopplaytones);
+
+	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playtones Application");

Propchange: team/twilson/calendaring/apps/app_playtones.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/twilson/calendaring/apps/app_playtones.c
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: team/twilson/calendaring/apps/app_playtones.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/twilson/calendaring/include/asterisk/heap.h
URL: http://svn.digium.com/svn-view/asterisk/team/twilson/calendaring/include/asterisk/heap.h?view=auto&rev=182448
==============================================================================
--- team/twilson/calendaring/include/asterisk/heap.h (added)
+++ team/twilson/calendaring/include/asterisk/heap.h Tue Mar 17 00:29:29 2009
@@ -1,0 +1,240 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Max Heap data structure
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#ifndef __AST_HEAP_H__
+#define __AST_HEAP_H__
+
+/*!
+ * \brief A max heap.
+ *
+ * \note Thread-safety is left to the user of the API.  The heap API provides
+ *       no locking of its own.  If the heap will be accessed by multiple threads,
+ *       then a lock must be used to ensure that only a single operation is
+ *       done on the heap at a time.  For the sake of convenience, a lock is
+ *       provided for the user of the API to use if another lock is not already
+ *       available to protect the heap.
+ */
+struct ast_heap;
+
+/*!
+ * \brief Function type for comparing nodes in a heap
+ *
+ * \param elm1 the first element
+ * \param elm2 the second element
+ *
+ * \retval negative if elm1 < elm2
+ * \retval 0 if elm1 == elm2
+ * \retval positive if elm1 > elm2
+ *
+ * \note This implementation is of a max heap.  However, if a min heap is
+ *       desired, simply swap the return values of this function.
+ */
+typedef int (*ast_heap_cmp_fn)(void *elm1, void *elm2);
+
+/*!
+ * \brief Create a max heap
+ *
+ * \param init_height The initial height of the heap to allocate space for.
+ *        To start out, there will be room for (2 ^ init_height) - 1 entries.
+ *        However, the heap will grow as needed.
+ * \param cmp_fn The function that should be used to compare elements in the heap.
+ * \param index_offset This parameter is optional, but must be provided to be able
+ *        to use ast_heap_remove().  This is the number of bytes into the element
+ *        where an ssize_t has been made available for the heap's internal
+ *        use.  The heap will use this field to keep track of the element's current
+ *        position in the heap.  The offsetof() macro is useful for providing a
+ *        proper value for this argument.  If ast_heap_remove() will not be used,
+ *        then a negative value can be provided to indicate that no field for an
+ *        offset has been allocated.
+ *
+ * Example Usage:
+ *
+ * \code
+ *
+ * struct myobj {
+ *    int foo;
+ *    int bar;
+ *    char stuff[8];
+ *    char things[8];
+ *    ssize_t __heap_index;
+ * };
+ *
+ * ...
+ *
+ * static int myobj_cmp(void *obj1, void *obj2);
+ *
+ * ...
+ *
+ * struct ast_heap *h;
+ *
+ * h = ast_heap_create(8, myobj_cmp, offsetof(struct myobj, __heap_index));
+ *
+ * \endcode
+ *
+ * \return An instance of a max heap
+ */
+struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn,
+		ssize_t index_offset);
+
+/*!
+ * \brief Destroy a max heap
+ *
+ * \param h the heap to destroy
+ *
+ * \return NULL for convenience
+ */
+struct ast_heap *ast_heap_destroy(struct ast_heap *h);
+
+/*!
+ * \brief Push an element on to a heap
+ *
+ * \param h the heap being added to
+ * \param elm the element being put on the heap
+ *
+ * \retval 0 success
+ * \retval non-zero failure
+ */
+int ast_heap_push(struct ast_heap *h, void *elm);
+
+/*!
+ * \brief Pop the max element off of the heap
+ *
+ * \param h the heap
+ *
+ * \return this will return the element on the top of the heap, which has the
+ *         largest value according to the element comparison function that was
+ *         provided when the heap was created.  The element will be removed before
+ *         being returned.
+ */
+void *ast_heap_pop(struct ast_heap *h);
+
+/*!
+ * \brief Remove a specific element from a heap
+ *
+ * \param h the heap to remove from
+ * \param elm the element to remove
+ *
+ * \return elm, if the removal was successful, or NULL if it failed
+ *
+ * \note the index_offset parameter to ast_heap_create() is required to be able
+ *       to use this function.
+ */
+void *ast_heap_remove(struct ast_heap *h, void *elm);
+
+/*!
+ * \brief Peek at an element on a heap
+ *
+ * \param h the heap
+ * \param index index of the element to return.  The first element is at index 1,
+ *        and the last element is at the index == the size of the heap.
+ *
+ * \return an element at the specified index on the heap.  This element will \b not
+ *         be removed before being returned.
+ *
+ * \note If this function is being used in combination with ast_heap_size() for
+ *       purposes of traversing the heap, the heap must be locked for the entire
+ *       duration of the traversal.
+ *
+ * Example code for a traversal:
+ * \code
+ *
+ * struct ast_heap *h;
+ *
+ * ...
+ *
+ * size_t size, i;
+ * void *cur_obj;
+ *
+ * ast_heap_rdlock(h);
+ *
+ * size = ast_heap_size(h);
+ *
+ * for (i = 1; i <= size && (cur_obj = ast_heap_peek(h, i)); i++) {
+ *     ... Do stuff with cur_obj ...
+ * }
+ *
+ * ast_heap_unlock(h);
+ *
+ * \endcode
+ */
+void *ast_heap_peek(struct ast_heap *h, unsigned int index);
+
+/*!
+ * \brief Get the current size of a heap
+ *
+ * \param h the heap
+ *
+ * \return the number of elements currently in the heap
+ */
+size_t ast_heap_size(struct ast_heap *h);
+
+/*!
+ * \brief Write-Lock a heap
+ *
+ * \arg h the heap
+ *
+ * A lock is provided for convenience.  It can be assumed that none of the
+ * ast_heap API calls are thread safe.  This lock does not have to be used
+ * if another one is already available to protect the heap.
+ *
+ * \return see the documentation for pthread_rwlock_wrlock()
+ */
+int ast_heap_wrlock(struct ast_heap *h);
+
+/*!
+ * \brief Read-Lock a heap
+ *
+ * \arg h the heap
+ *
+ * A lock is provided for convenience.  It can be assumed that none of the
+ * ast_heap API calls are thread safe.  This lock does not have to be used
+ * if another one is already available to protect the heap.
+ *
+ * \return see the documentation for pthread_rwlock_rdlock()
+ */
+int ast_heap_rdlock(struct ast_heap *h);
+
+/*!
+ * \brief Unlock a heap
+ *
+ * \arg h the heap
+ *
+ * \return see the documentation for pthread_rwlock_unlock()
+ */
+int ast_heap_unlock(struct ast_heap *h);
+
+/*!
+ * \brief Verify that a heap has been properly constructed
+ *
+ * \param h a heap
+ *
+ * \retval 0 success
+ * \retval non-zero failure
+ *
+ * \note This function is mostly for debugging purposes.  It traverses an existing
+ *       heap and verifies that every node is properly placed relative to its children.
+ */
+int ast_heap_verify(struct ast_heap *h);
+
+#endif /* __AST_HEAP_H__ */

Propchange: team/twilson/calendaring/include/asterisk/heap.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/twilson/calendaring/include/asterisk/heap.h
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: team/twilson/calendaring/include/asterisk/heap.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/twilson/calendaring/main/heap.c
URL: http://svn.digium.com/svn-view/asterisk/team/twilson/calendaring/main/heap.c?view=auto&rev=182448
==============================================================================
--- team/twilson/calendaring/main/heap.c (added)
+++ team/twilson/calendaring/main/heap.c Tue Mar 17 00:29:29 2009
@@ -1,0 +1,284 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Max Heap data structure
+ *
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 176632 $")
+
+#include "asterisk/heap.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+
+struct ast_heap {
+	ast_rwlock_t lock;
+	ast_heap_cmp_fn cmp_fn;
+	ssize_t index_offset;
+	size_t cur_len;
+	size_t avail_len;
+	void **heap;
+};
+
+static inline int left_node(int i)
+{
+	return 2 * i;
+}
+
+static inline int right_node(int i)
+{
+	return 2 * i + 1;
+}
+
+static inline int parent_node(int i)
+{
+	return i / 2;
+}
+
+static inline void *heap_get(struct ast_heap *h, int i)
+{
+	return h->heap[i - 1];
+}
+
+static inline ssize_t get_index(struct ast_heap *h, void *elm)
+{
+	ssize_t *index;
+
+	if (h->index_offset < 0) {
+		return -1;
+	}
+
+	index = elm + h->index_offset;
+
+	return *index;
+}
+
+static inline void heap_set(struct ast_heap *h, int i, void *elm)
+{
+	h->heap[i - 1] = elm;
+
+	if (h->index_offset >= 0) {
+		ssize_t *index = elm + h->index_offset;
+		*index = i;
+	}
+}
+
+int ast_heap_verify(struct ast_heap *h)
+{
+	unsigned int i;
+
+	for (i = 1; i <= (h->cur_len / 2); i++) {
+		int l = left_node(i);
+		int r = right_node(i);
+
+		if (l <= h->cur_len) {
+			if (h->cmp_fn(heap_get(h, i), heap_get(h, l)) <= 0) {
+				return -1;
+			}
+		}
+
+		if (r <= h->cur_len) {
+			if (h->cmp_fn(heap_get(h, i), heap_get(h, r)) <= 0) {
+				return -1;
+			}
+		}
+	}
+
+	return 0;
+}
+
+struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn,
+		ssize_t index_offset)
+{
+	struct ast_heap *h;
+
+	if (!cmp_fn) {
+		ast_log(LOG_ERROR, "A comparison function must be provided\n");
+		return NULL;
+	}
+
+	if (!init_height) {
+		init_height = 8;
+	}
+
+	if (!(h = ast_calloc(1, sizeof(*h)))) {
+		return NULL;
+	}
+
+	h->cmp_fn = cmp_fn;
+	h->index_offset = index_offset;
+	h->avail_len = (1 << init_height) - 1;
+
+	if (!(h->heap = ast_calloc(1, h->avail_len * sizeof(void *)))) {
+		ast_free(h);
+		return NULL;
+	}
+
+	ast_rwlock_init(&h->lock);
+
+	return h;
+}
+
+struct ast_heap *ast_heap_destroy(struct ast_heap *h)
+{
+	ast_free(h->heap);
+	h->heap = NULL;
+
+	ast_rwlock_destroy(&h->lock);
+
+	ast_free(h);
+
+	return NULL;
+}
+
+/*!
+ * \brief Add a row of additional storage for the heap.
+ */
+static int grow_heap(struct ast_heap *h)
+{
+	h->avail_len = h->avail_len * 2 + 1;
+
+	if (!(h->heap = ast_realloc(h->heap, h->avail_len * sizeof(void *)))) {
+		h->cur_len = h->avail_len = 0;
+		return -1;
+	}
+
+	return 0;
+}
+
+static inline void heap_swap(struct ast_heap *h, int i, int j)
+{
+	void *tmp;
+
+	tmp = heap_get(h, i);
+	heap_set(h, i, heap_get(h, j));
+	heap_set(h, j, tmp);
+}
+
+static inline void max_heapify(struct ast_heap *h, int i)
+{
+	for (;;) {
+		int l = left_node(i);
+		int r = right_node(i);
+		int max;
+
+		if (l <= h->cur_len && h->cmp_fn(heap_get(h, l), heap_get(h, i)) > 0) {
+			max = l;
+		} else {
+			max = i;
+		}
+
+		if (r <= h->cur_len && h->cmp_fn(heap_get(h, r), heap_get(h, max)) > 0) {
+			max = r;
+		}
+
+		if (max == i) {
+			break;
+		}
+
+		heap_swap(h, i, max);
+
+		i = max;
+	}
+}
+
+int ast_heap_push(struct ast_heap *h, void *elm)
+{
+	int i;
+
+	if (h->cur_len == h->avail_len && grow_heap(h)) {
+		return -1;
+	}
+
+	heap_set(h, ++(h->cur_len), elm);
+
+	for (i = h->cur_len;
+			i > 1 && h->cmp_fn(heap_get(h, parent_node(i)), heap_get(h, i)) < 0;
+			i = parent_node(i)) {
+		heap_swap(h, i, parent_node(i));
+	}
+
+	return 0;
+}
+
+static void *_ast_heap_remove(struct ast_heap *h, unsigned int index)
+{
+	void *ret;
+
+	if (!index || index > h->cur_len) {
+		return NULL;
+	}
+
+	ret = heap_get(h, index);
+	heap_set(h, index, heap_get(h, (h->cur_len)--));
+
+	max_heapify(h, index);
+
+	return ret;
+}
+
+void *ast_heap_remove(struct ast_heap *h, void *elm)
+{
+	ssize_t i = get_index(h, elm);
+
+	if (i == -1) {
+		return NULL;
+	}
+
+	return _ast_heap_remove(h, i);
+}
+
+void *ast_heap_pop(struct ast_heap *h)
+{
+	return _ast_heap_remove(h, 1);
+}
+
+void *ast_heap_peek(struct ast_heap *h, unsigned int index)
+{
+	if (!h->cur_len || !index || index > h->cur_len) {
+		return NULL;
+	}
+
+	return heap_get(h, index);
+}
+
+size_t ast_heap_size(struct ast_heap *h)
+{
+	return h->cur_len;
+}
+
+int ast_heap_wrlock(struct ast_heap *h)
+{
+	return ast_rwlock_wrlock(&h->lock);
+}
+
+int ast_heap_rdlock(struct ast_heap *h)
+{
+	return ast_rwlock_rdlock(&h->lock);
+}
+
+int ast_heap_unlock(struct ast_heap *h)
+{
+	return ast_rwlock_unlock(&h->lock);
+}
+

Propchange: team/twilson/calendaring/main/heap.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/twilson/calendaring/main/heap.c
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: team/twilson/calendaring/main/heap.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/twilson/calendaring/tests/test_heap.c
URL: http://svn.digium.com/svn-view/asterisk/team/twilson/calendaring/tests/test_heap.c?view=auto&rev=182448
==============================================================================
--- team/twilson/calendaring/tests/test_heap.c (added)
+++ team/twilson/calendaring/tests/test_heap.c Tue Mar 17 00:29:29 2009
@@ -1,0 +1,219 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Heap data structure test module
+ *
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 176635 $")
+
+#include "asterisk/module.h"
+#include "asterisk/cli.h"
+#include "asterisk/utils.h"
+#include "asterisk/heap.h"
+
+struct node {
+	long val;
+	size_t index;
+};
+
+static int node_cmp(void *_n1, void *_n2)
+{
+	struct node *n1 = _n1;
+	struct node *n2 = _n2;
+
+	if (n1->val < n2->val) {
+		return -1;
+	} else if (n1->val == n2->val) {
+		return 0;
+	} else {
+		return 1;
+	}
+}
+
+static int test1(int fd)
+{
+	struct ast_heap *h;
+	struct node *obj;
+	struct node nodes[3] = {
+		{ 1, },
+		{ 2, },
+		{ 3, },
+	};
+
+	if (!(h = ast_heap_create(8, node_cmp, offsetof(struct node, index)))) {
+		return -1;
+	}
+
+	/* Pushing 1 2 3, and then popping 3 elements */
+
+	ast_cli(fd, "Test #1 - Push a few elements onto a heap and make sure that they "
+			"come back off in the right order.\n");
+
+	ast_heap_push(h, &nodes[0]);
+
+	ast_heap_push(h, &nodes[1]);
+
+	ast_heap_push(h, &nodes[2]);
+
+	obj = ast_heap_pop(h);
+	if (obj->val != 3) {
+		return -2;
+	}
+
+	obj = ast_heap_pop(h);
+	if (obj->val != 2) {
+		return -3;
+	}
+
+	obj = ast_heap_pop(h);
+	if (obj->val != 1) {
+		return -4;
+	}
+
+	obj = ast_heap_pop(h);
+	if (obj) {
+		return -5;
+	}
+
+	h = ast_heap_destroy(h);
+
+	ast_cli(fd, "Test #1 successful.\n");
+
+	return 0;
+}
+
+static int test2(int fd)
+{
+	struct ast_heap *h = NULL;
+	static const unsigned int one_million = 1000000;
+	struct node *nodes = NULL;
+	struct node *node;
+	unsigned int i = one_million;
+	long last = LONG_MAX, cur;
+	int res = 0;
+
+	ast_cli(fd, "Test #2 - Push a million random elements on to a heap, "
+			"verify that the heap has been properly constructed, "
+			"and then ensure that the elements are come back off in the proper order\n");
+
+	if (!(nodes = ast_malloc(one_million * sizeof(*node)))) {
+		res = -1;
+		goto return_cleanup;
+	}
+
+	if (!(h = ast_heap_create(20, node_cmp, offsetof(struct node, index)))) {
+		res = -2;
+		goto return_cleanup;
+	}
+
+	while (i--) {
+		nodes[i].val = ast_random();
+		ast_heap_push(h, &nodes[i]);
+	}
+
+	if (ast_heap_verify(h)) {
+		res = -3;
+		goto return_cleanup;
+	}
+
+	i = 0;
+	while ((node = ast_heap_pop(h))) {
+		cur = node->val;
+		if (cur > last) {
+			ast_cli(fd, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
+			res = -4;
+			goto return_cleanup;
+		}
+		last = cur;
+		i++;
+	}
+
+	if (i != one_million) {
+		ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i);
+		res = -5;
+		goto return_cleanup;
+	}
+
+	ast_cli(fd, "Test #2 successful.\n");
+
+return_cleanup:
+	if (h) {
+		h = ast_heap_destroy(h);
+	}
+	if (nodes) {
+		ast_free(nodes);
+	}
+
+	return res;
+}
+
+static char *handle_cli_heap_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int res;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "heap test";
+		e->usage = ""
+			"Usage: heap test\n"
+			"";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != e->args) {
+		return CLI_SHOWUSAGE;
+	}
+
+	if ((res = test1(a->fd))) {
+		ast_cli(a->fd, "Test 1 failed! (%d)\n", res);
+		return CLI_FAILURE;
+	}
+
+	if ((res = test2(a->fd))) {
+		ast_cli(a->fd, "Test 2 failed! (%d)\n", res);
+		return CLI_FAILURE;
+	}
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_heap[] = {
+	AST_CLI_DEFINE(handle_cli_heap_test, "Test the heap implementation"),
+};
+
+static int unload_module(void)
+{
+	ast_cli_unregister_multiple(cli_heap, ARRAY_LEN(cli_heap));
+	return 0;
+}
+
+static int load_module(void)
+{
+	ast_cli_register_multiple(cli_heap, ARRAY_LEN(cli_heap));
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Heap test module");

Propchange: team/twilson/calendaring/tests/test_heap.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/twilson/calendaring/tests/test_heap.c
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: team/twilson/calendaring/tests/test_heap.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/twilson/calendaring/tests/test_sched.c
URL: http://svn.digium.com/svn-view/asterisk/team/twilson/calendaring/tests/test_sched.c?view=auto&rev=182448
==============================================================================
--- team/twilson/calendaring/tests/test_sched.c (added)
+++ team/twilson/calendaring/tests/test_sched.c Tue Mar 17 00:29:29 2009
@@ -1,0 +1,130 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief ast_sched performance test module
+ *
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 176639 $")
+
+#include "asterisk/module.h"
+#include "asterisk/cli.h"
+#include "asterisk/utils.h"
+#include "asterisk/sched.h"
+
+static int sched_cb(const void *data)
+{
+	return 0;
+}
+
+static char *handle_cli_sched_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct sched_context *con;
+	struct timeval start;
+	unsigned int num, i;
+	int *sched_ids = NULL;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "sched test";
+		e->usage = ""
+			"Usage: sched test <num>\n"
+			"";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != e->args + 1) {
+		return CLI_SHOWUSAGE;
+	}
+
+	if (sscanf(a->argv[e->args], "%u", &num) != 1) {
+		return CLI_SHOWUSAGE;
+	}
+
+	if (!(con = sched_context_create())) {
+		ast_cli(a->fd, "Test failed - could not create scheduler context\n");
+		return CLI_FAILURE;
+	}
+
+	if (!(sched_ids = ast_malloc(sizeof(*sched_ids) * num))) {
+		ast_cli(a->fd, "Test failed - memory allocation failure\n");
+		goto return_cleanup;
+	}
+
+	ast_cli(a->fd, "Testing ast_sched_add() performance - timing how long it takes "
+			"to add %u entries at random time intervals from 0 to 60 seconds\n", num);
+
+	start = ast_tvnow();
+
+	for (i = 0; i < num; i++) {
+		int when = abs(ast_random()) % 60000;
+		if ((sched_ids[i] = ast_sched_add(con, when, sched_cb, NULL)) == -1) {
+			ast_cli(a->fd, "Test failed - sched_add returned -1\n");
+			goto return_cleanup;
+		}
+	}
+
+	ast_cli(a->fd, "Test complete - %ld us\n", ast_tvdiff_us(ast_tvnow(), start));
+
+	ast_cli(a->fd, "Testing ast_sched_del() performance - timing how long it takes "
+			"to delete %u entries with random time intervals from 0 to 60 seconds\n", num);
+
+	start = ast_tvnow();
+
+	for (i = 0; i < num; i++) {
+		if (ast_sched_del(con, sched_ids[i]) == -1) {
+			ast_cli(a->fd, "Test failed - sched_del returned -1\n");
+			goto return_cleanup;
+		}
+	}
+
+	ast_cli(a->fd, "Test complete - %ld us\n", ast_tvdiff_us(ast_tvnow(), start));
+
+return_cleanup:
+	sched_context_destroy(con);
+	if (sched_ids) {
+		ast_free(sched_ids);
+	}
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_sched[] = {
+	AST_CLI_DEFINE(handle_cli_sched_test, "Test ast_sched add/del performance"),
+};
+
+static int unload_module(void)
+{
+	ast_cli_unregister_multiple(cli_sched, ARRAY_LEN(cli_sched));
+	return 0;
+}
+
+static int load_module(void)
+{
+	ast_cli_register_multiple(cli_sched, ARRAY_LEN(cli_sched));
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_sched performance test module");

Propchange: team/twilson/calendaring/tests/test_sched.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/twilson/calendaring/tests/test_sched.c
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: team/twilson/calendaring/tests/test_sched.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the svn-commits mailing list