[Asterisk-code-review] test data buffer.c: Add unit tests for data buffer API. (asterisk[master])
Jenkins2
asteriskteam at digium.com
Thu Mar 29 13:41:38 CDT 2018
Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/8607 )
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, 313 insertions(+), 0 deletions(-)
Approvals:
Sean Bright: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, approved
Jenkins2: Approved for Submit
diff --git a/tests/test_data_buffer.c b/tests/test_data_buffer.c
new file mode 100644
index 0000000..11fdc7b
--- /dev/null
+++ b/tests/test_data_buffer.c
@@ -0,0 +1,313 @@
+/*
+ * 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/module.h"
+#include "asterisk/test.h"
+#include "asterisk/data_buffer.h"
+
+#define BUFFER_MAX_NOMINAL 10
+
+struct mock_payload
+{
+ int id;
+};
+
+/* Ensures that RAII_VAR will not trip ast_assert(buffer != NULL) in the callback */
+static void ast_data_buffer_free_wrapper(struct ast_data_buffer *buffer)
+{
+ if (!buffer) {
+ return;
+ }
+
+ ast_data_buffer_free(buffer);
+}
+
+AST_TEST_DEFINE(buffer_create)
+{
+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);
+
+ 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_ptr, BUFFER_MAX_NOMINAL);
+
+ ast_test_validate(test, buffer != NULL,
+ "Failed to create buffer with valid arguments");
+ ast_test_validate(test, ast_data_buffer_count(buffer) == 0,
+ "Newly created buffer does not have the expected payload count");
+ ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL,
+ "Newly created buffer does not have the expected max size");
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(buffer_put)
+{
+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);
+ 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_ptr, 2);
+
+ ast_test_validate(test, buffer != NULL,
+ "Failed to create buffer with valid arguments");
+ ast_test_validate(test, ast_data_buffer_count(buffer) == 0,
+ "Newly created buffer is not empty");
+
+ payload = ast_calloc(1, sizeof(*payload));
+
+ ast_test_validate(test, payload != NULL,
+ "Failed to allocate memory for first payload");
+
+ payload->id = 2;
+ ret = ast_data_buffer_put(buffer, 2, payload);
+
+ ast_test_validate(test, ret == 0,
+ "Adding a payload to an empty buffer did not return the expected value");
+ ast_test_validate(test, ast_data_buffer_count(buffer) == 1,
+ "Adding a payload to an empty buffer did not update count to the expected value");
+
+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);
+
+ ast_test_validate(test, fetched_payload != NULL,
+ "Failed to get only payload from buffer given valid arguments");
+
+ ast_data_buffer_put(buffer, 2, payload);
+
+ ast_test_validate(test, ast_data_buffer_count(buffer) == 1,
+ "Adding a payload that is already in the buffer should not do anything");
+
+ payload = ast_calloc(1, sizeof(*payload));
+
+ ast_test_validate(test, payload != NULL,
+ "Failed to allocate memory for second payload");
+
+ payload->id = 1;
+ ast_data_buffer_put(buffer, 1, payload);
+ fetched_payload = ast_data_buffer_get(buffer, 1);
+
+ ast_test_validate(test, fetched_payload != NULL,
+ "Failed to get a payload from buffer given valid arguments");
+ ast_test_validate(test, ast_data_buffer_count(buffer) == 2,
+ "Buffer does not have the expected count after removing a payload");
+ ast_test_validate(test, fetched_payload->id == 1,
+ "Did not get the expected payload from the buffer");
+
+ payload = ast_calloc(1, sizeof(*payload));
+
+ ast_test_validate(test, payload != NULL,
+ "Failed to allocate memory for third payload");
+
+ payload->id = 3;
+ ret = ast_data_buffer_put(buffer, 3, payload);
+
+ ast_test_validate(test, ret == 0,
+ "Failed to replace a payload in the buffer");
+ ast_test_validate(test, ast_data_buffer_count(buffer) <= 2,
+ "Buffer count exceeded the max");
+
+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 3);
+
+ ast_test_validate(test, fetched_payload != NULL,
+ "Failed to get a payload from buffer at position 3 given valid arguments");
+ ast_test_validate(test, fetched_payload->id == 3,
+ "Did not get the expected payload at position 3 from the buffer");
+
+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);
+
+ ast_test_validate(test, fetched_payload != NULL,
+ "Failed to get a payload from buffer at position 2 given valid arguments");
+ ast_test_validate(test, fetched_payload->id == 2,
+ "Did not get the expected payload at position 2 from the buffer");
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(buffer_resize)
+{
+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);
+
+ 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_ptr, BUFFER_MAX_NOMINAL);
+
+ ast_test_validate(test, buffer != NULL,
+ "Failed to create buffer with valid arguments");
+
+ ast_data_buffer_resize(buffer, BUFFER_MAX_NOMINAL);
+
+ ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL,
+ "Trying to resize buffer to same size should not change its max size");
+
+ ast_data_buffer_resize(buffer, BUFFER_MAX_NOMINAL + 2);
+
+ ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL + 2,
+ "Increasing buffer size did not return the expected max");
+
+ ast_data_buffer_resize(buffer, 1);
+
+ ast_test_validate(test, ast_data_buffer_max(buffer) == 1,
+ "Decreasing buffer size did not return the expected max");
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(buffer_nominal)
+{
+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);
+ 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_ptr, BUFFER_MAX_NOMINAL);
+
+ ast_test_validate(test, buffer != NULL,
+ "Failed to create buffer with valid arguments");
+
+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+ payload = ast_calloc(1, sizeof(*payload));
+
+ ast_test_validate(test, payload != NULL,
+ "Failed to allocate memory for payload %d", i);
+
+ ret = ast_data_buffer_put(buffer, i, payload);
+
+ ast_test_validate(test, ret == 0,
+ "Failed to add payload %d to buffer", i);
+ }
+
+ ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL,
+ "Buffer does not have the expected count after adding payloads");
+
+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);
+
+ ast_test_validate(test, fetched_payload != NULL,
+ "Failed to get payload at position %d during first loop", i);
+ }
+
+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+ payload = ast_calloc(1, sizeof(*payload));
+
+ ast_test_validate(test, payload != NULL,
+ "Failed to allocate memory for payload %d", i + BUFFER_MAX_NOMINAL);
+
+ ret = ast_data_buffer_put(buffer, i + BUFFER_MAX_NOMINAL, payload);
+
+ ast_test_validate(test, ret == 0,
+ "Failed to add payload %d to buffer", i + BUFFER_MAX_NOMINAL);
+ }
+
+ ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL,
+ "Buffer does not have the expected count after replacing payloads");
+
+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);
+
+ ast_test_validate(test, fetched_payload == NULL,
+ "Got an unexpected payload at position %d", i);
+
+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i + BUFFER_MAX_NOMINAL);
+
+ ast_test_validate(test, fetched_payload != NULL,
+ "Failed to get payload at position %d during second loop", i + BUFFER_MAX_NOMINAL);
+ }
+
+ 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/8607
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Id5b599aa15a5e61d0ec080f97cd0c57bd07e6f8f
Gerrit-Change-Number: 8607
Gerrit-PatchSet: 23
Gerrit-Owner: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Matthew Fredrickson <creslin at digium.com>
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180329/3bfeab46/attachment-0001.html>
More information about the asterisk-code-review
mailing list