[Asterisk-code-review] test data buffer.c: Add unit tests for data buffer API. (asterisk[15])

Benjamin Keith Ford asteriskteam at digium.com
Mon Mar 19 09:46:05 CDT 2018


Benjamin Keith Ford has uploaded this change for review. ( https://gerrit.asterisk.org/8605


Change subject: test_data_buffer.c: Add unit tests for data buffer API.
......................................................................

test_data_buffer.c: Add unit tests for data buffer API.

Added unit tests for the data buffer API. These tests include creating a
data buffer, putting payloads into the buffer, resizing the buffer, and
the nominal case for data buffer usage, which consists of adding
the max number of payloads to the buffer, checking to see if the correct
payloads are present, then adding more payloads and checking again to
see if the previous payloads were replaced or not.

For more information, refer to the wiki page:
https://wiki.asterisk.org/wiki/display/AST/WebRTC+User+Experience+Improvements

Change-Id: Id5b599aa15a5e61d0ec080f97cd0c57bd07e6f8f
---
A tests/test_data_buffer.c
1 file changed, 424 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/05/8605/1

diff --git a/tests/test_data_buffer.c b/tests/test_data_buffer.c
new file mode 100644
index 0000000..f05a96f
--- /dev/null
+++ b/tests/test_data_buffer.c
@@ -0,0 +1,424 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2018, Digium, Inc.
+ *
+ * Ben Ford <bford 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 Media Stream API Unit Tests
+ *
+ * \author Ben Ford <bford at digium.com>
+ *
+ */
+
+/*** MODULEINFO
+	<depend>TEST_FRAMEWORK</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/test.h"
+#include "asterisk/module.h"
+#include "asterisk/data_buffer.h"
+
+#define BUFFER_MAX_NOMINAL 10
+
+struct mock_payload
+{
+	int id;
+};
+
+AST_TEST_DEFINE(buffer_create)
+{
+	struct ast_data_buffer *buffer;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "buffer_create";
+		info->category = "/main/data_buffer/";
+		info->summary = "buffer create unit test";
+		info->description =
+			"Test that creating a data buffer results in a buffer with the expected values";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	buffer = ast_data_buffer_alloc(ast_free, 5);
+	if (!buffer) {
+		ast_test_status_update(test, "Failed to create buffer with valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_data_buffer_count(buffer) != 0) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Newly created buffer does not have the expected payload count");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_data_buffer_max(buffer) != 5) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Newly created buffer does not have the expected max size");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_data_buffer_cache_count(buffer) != CACHED_PAYLOADS_START) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Newly created buffer does not have the expected cached payload count");
+		return AST_TEST_FAIL;
+	}
+
+	ast_data_buffer_free(buffer);
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(buffer_put)
+{
+	struct ast_data_buffer *buffer;
+	struct mock_payload *payload;
+	struct mock_payload *fetched_payload;
+	int ret;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "buffer_put";
+		info->category = "/main/data_buffer/";
+		info->summary = "buffer put unit test";
+		info->description =
+			"Test that putting payloads in the buffer yields the expected results";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	buffer = ast_data_buffer_alloc(ast_free, 2);
+	if (!buffer) {
+		ast_test_status_update(test, "Failed to create buffer with valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_data_buffer_count(buffer)) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Newly created buffer is not empty");
+		return AST_TEST_FAIL;
+	}
+
+	payload = ast_calloc(1, sizeof(*payload));
+	if (!payload) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to allocate memory for first payload");
+		return AST_TEST_FAIL;
+	}
+
+	payload->id = 2;
+
+	ret = ast_data_buffer_put(buffer, 2, payload);
+	if (ret) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Adding a payload to an empty buffer did not return the "
+				"expected value");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_data_buffer_count(buffer) != 1) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Adding a payload to an empty buffer did not update count "
+				"to the expected value");
+		return AST_TEST_FAIL;
+	}
+
+	fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);
+	if (!fetched_payload) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to get only payload from buffer given valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	payload = ast_calloc(1, sizeof(*payload));
+	if (!payload) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to allocate memory for second payload");
+		return AST_TEST_FAIL;
+	}
+
+	payload->id = 1;
+
+	ast_data_buffer_put(buffer, 1, payload);
+	fetched_payload = ast_data_buffer_get(buffer, -1);
+	if (!fetched_payload) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to get a payload from buffer given valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_data_buffer_count(buffer) != 2) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Buffer does not have the expected count after removing a payload");
+		return AST_TEST_FAIL;
+	}
+
+	if (fetched_payload->id != 1) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Did not get the expected payload from the buffer");
+		return AST_TEST_FAIL;
+	}
+
+	payload = ast_calloc(1, sizeof(*payload));
+	if (!payload) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to allocate memory for third payload");
+		return AST_TEST_FAIL;
+	}
+
+	payload->id = 3;
+
+	ret = ast_data_buffer_put(buffer, 3, payload);
+	if (ret) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to replace a payload in the buffer");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_data_buffer_count(buffer) > 2) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Buffer count exceeded the max");
+		return AST_TEST_FAIL;
+	}
+
+	fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 3);
+	if (!fetched_payload) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to get a payload from buffer at position 3 given valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	if (fetched_payload->id != 3) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Did not get the expected payload at position 3 from the buffer");
+		return AST_TEST_FAIL;
+	}
+
+	fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);
+	if (!fetched_payload) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Failed to get a payload from buffer at position 2 given valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	if (fetched_payload->id != 2) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Did not get the expected payload at position 2 from the buffer");
+		return AST_TEST_FAIL;
+	}
+
+	ast_data_buffer_free(buffer);
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(buffer_resize)
+{
+	struct ast_data_buffer *buffer;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "buffer_resize";
+		info->category = "/main/data_buffer/";
+		info->summary = "buffer resize unit test";
+		info->description =
+			"Tests resizing a data buffer to make sure it has the expected outcome";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	buffer = ast_data_buffer_alloc(ast_free, CACHED_PAYLOADS_START);
+	if (!buffer) {
+		ast_test_status_update(test, "Failed to create buffer with valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	ast_data_buffer_resize(buffer, 0);
+	if (!ast_data_buffer_max(buffer)) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Buffer max cannot be resized to below 1");
+		return AST_TEST_FAIL;
+	}
+
+	ast_data_buffer_resize(buffer, CACHED_PAYLOADS_START);
+	if (ast_data_buffer_max(buffer) != CACHED_PAYLOADS_START) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Trying to resize buffer to same size should not change "
+				"its max size");
+		return AST_TEST_FAIL;
+	}
+	
+	if (ast_data_buffer_cache_count(buffer) != CACHED_PAYLOADS_START) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Trying to resize buffer to same size should not change "
+				"cached payload count");
+		return AST_TEST_FAIL;
+	}
+
+	ast_data_buffer_resize(buffer, CACHED_PAYLOADS_START + 2);
+	if (ast_data_buffer_max(buffer) != CACHED_PAYLOADS_START + 2) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Increasing buffer size did not return the expected max");
+		return AST_TEST_FAIL;
+	}
+	
+	if (ast_data_buffer_cache_count(buffer) != CACHED_PAYLOADS_START + 2) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Increasing buffer size did not return the expected cache count");
+		return AST_TEST_FAIL;
+	}
+
+	ast_data_buffer_resize(buffer, 1);
+	if (ast_data_buffer_max(buffer) != 1) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Decreasing buffer size did not return the expected max");
+		return AST_TEST_FAIL;
+	}
+	
+	if (ast_data_buffer_cache_count(buffer) != 1) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Decreasing buffer size did not return the expected cache count");
+		return AST_TEST_FAIL;
+	}
+
+	ast_data_buffer_free(buffer);
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(buffer_nominal)
+{
+	struct ast_data_buffer *buffer;
+	struct mock_payload *payload;
+	struct mock_payload *fetched_payload;
+	int ret;
+	int i;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "buffer_nominal";
+		info->category = "/main/data_buffer/";
+		info->summary = "buffer nominal unit test";
+		info->description =
+			"Tests the normal usage of a data buffer to ensure the expected payloads "
+			"are present after multiple insertions";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	buffer = ast_data_buffer_alloc(ast_free, BUFFER_MAX_NOMINAL);
+	if (!buffer) {
+		ast_test_status_update(test, "Failed to create buffer with valid arguments");
+		return AST_TEST_FAIL;
+	}
+
+	for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+		payload = ast_calloc(1, sizeof(*payload));
+		if (!payload) {
+			ast_data_buffer_free(buffer);
+			ast_test_status_update(test, "Failed to allocate memory for payload %d", i);
+			return AST_TEST_FAIL;
+		}
+
+		ret = ast_data_buffer_put(buffer, i, payload);
+		if (ret) {
+			ast_data_buffer_free(buffer);
+			ast_test_status_update(test, "Failed to add payload %d to buffer", i);
+			return AST_TEST_FAIL;
+		}
+	}
+
+	if (ast_data_buffer_count(buffer) != BUFFER_MAX_NOMINAL) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Buffer does not have the expected count after adding payloads");
+		return AST_TEST_FAIL;
+	}
+
+	for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+		fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);
+		if (!fetched_payload) {
+			ast_data_buffer_free(buffer);
+			ast_test_status_update(test, "Failed to get payload at position %d during first loop", i);
+			return AST_TEST_FAIL;
+		}
+	}
+
+	for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+		payload = ast_calloc(1, sizeof(*payload));
+		if (!payload) {
+			ast_data_buffer_free(buffer);
+			ast_test_status_update(test, "Failed to allocate memory for payload %d", i + BUFFER_MAX_NOMINAL);
+			return AST_TEST_FAIL;
+		}
+
+		ret = ast_data_buffer_put(buffer, i + BUFFER_MAX_NOMINAL, payload);
+		if (ret) {
+			ast_data_buffer_free(buffer);
+			ast_test_status_update(test, "Failed to add payload %d to buffer", i + BUFFER_MAX_NOMINAL);
+			return AST_TEST_FAIL;
+		}
+	}
+
+	if (ast_data_buffer_count(buffer) != BUFFER_MAX_NOMINAL) {
+		ast_data_buffer_free(buffer);
+		ast_test_status_update(test, "Buffer does not have the expected count after replacing payloads");
+		return AST_TEST_FAIL;
+	}
+
+	for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+		fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);
+		if (fetched_payload) {
+			ast_data_buffer_free(buffer);
+			ast_test_status_update(test, "Got an unexpected payload at position %d", i);
+			return AST_TEST_FAIL;
+		}
+
+		fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i + BUFFER_MAX_NOMINAL);
+		if (!fetched_payload) {
+			ast_data_buffer_free(buffer);
+			ast_test_status_update(test, "Failed to get payload at position %d during second loop", i + BUFFER_MAX_NOMINAL);
+			return AST_TEST_FAIL;
+		}
+	}
+
+	ast_data_buffer_free(buffer);
+	return AST_TEST_PASS;
+}
+
+static int unload_module(void)
+{
+	AST_TEST_UNREGISTER(buffer_create);
+	AST_TEST_UNREGISTER(buffer_put);
+	AST_TEST_UNREGISTER(buffer_resize);
+	AST_TEST_UNREGISTER(buffer_nominal);
+	return 0;
+}
+
+static int load_module(void)
+{
+	AST_TEST_REGISTER(buffer_create);
+	AST_TEST_REGISTER(buffer_put);
+	AST_TEST_REGISTER(buffer_resize);
+	AST_TEST_REGISTER(buffer_nominal);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Data buffer API test module");

-- 
To view, visit https://gerrit.asterisk.org/8605
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id5b599aa15a5e61d0ec080f97cd0c57bd07e6f8f
Gerrit-Change-Number: 8605
Gerrit-PatchSet: 1
Gerrit-Owner: Benjamin Keith Ford <bford at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180319/b31b95ea/attachment-0001.html>


More information about the asterisk-code-review mailing list