<p>Benjamin Keith Ford has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/8605">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">test_data_buffer.c: Add unit tests for data buffer API.<br><br>Added unit tests for the data buffer API. These tests include creating a<br>data buffer, putting payloads into the buffer, resizing the buffer, and<br>the nominal case for data buffer usage, which consists of adding<br>the max number of payloads to the buffer, checking to see if the correct<br>payloads are present, then adding more payloads and checking again to<br>see if the previous payloads were replaced or not.<br><br>For more information, refer to the wiki page:<br>https://wiki.asterisk.org/wiki/display/AST/WebRTC+User+Experience+Improvements<br><br>Change-Id: Id5b599aa15a5e61d0ec080f97cd0c57bd07e6f8f<br>---<br>A tests/test_data_buffer.c<br>1 file changed, 424 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/05/8605/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/tests/test_data_buffer.c b/tests/test_data_buffer.c<br>new file mode 100644<br>index 0000000..f05a96f<br>--- /dev/null<br>+++ b/tests/test_data_buffer.c<br>@@ -0,0 +1,424 @@<br>+/*<br>+ * Asterisk -- An open source telephony toolkit.<br>+ *<br>+ * Copyright (C) 2018, Digium, Inc.<br>+ *<br>+ * Ben Ford <bford@digium.com><br>+ *<br>+ * See http://www.asterisk.org for more information about<br>+ * the Asterisk project. Please do not directly contact<br>+ * any of the maintainers of this project for assistance;<br>+ * the project provides a web site, mailing lists and IRC<br>+ * channels for your use.<br>+ *<br>+ * This program is free software, distributed under the terms of<br>+ * the GNU General Public License Version 2. See the LICENSE file<br>+ * at the top of the source tree.<br>+ */<br>+<br>+/*!<br>+ * \file<br>+ * \brief Media Stream API Unit Tests<br>+ *<br>+ * \author Ben Ford <bford@digium.com><br>+ *<br>+ */<br>+<br>+/*** MODULEINFO<br>+        <depend>TEST_FRAMEWORK</depend><br>+  <support_level>core</support_level><br>+ ***/<br>+<br>+#include "asterisk.h"<br>+<br>+#include "asterisk/test.h"<br>+#include "asterisk/module.h"<br>+#include "asterisk/data_buffer.h"<br>+<br>+#define BUFFER_MAX_NOMINAL 10<br>+<br>+struct mock_payload<br>+{<br>+        int id;<br>+};<br>+<br>+AST_TEST_DEFINE(buffer_create)<br>+{<br>+ struct ast_data_buffer *buffer;<br>+<br>+   switch (cmd) {<br>+       case TEST_INIT:<br>+              info->name = "buffer_create";<br>+           info->category = "/main/data_buffer/";<br>+          info->summary = "buffer create unit test";<br>+              info->description =<br>+                       "Test that creating a data buffer results in a buffer with the expected values";<br>+           return AST_TEST_NOT_RUN;<br>+     case TEST_EXECUTE:<br>+           break;<br>+       }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free, 5);<br>+ if (!buffer) {<br>+               ast_test_status_update(test, "Failed to create buffer with valid arguments");<br>+              return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (ast_data_buffer_count(buffer) != 0) {<br>+            ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Newly created buffer does not have the expected payload count");<br>+             return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (ast_data_buffer_max(buffer) != 5) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Newly created buffer does not have the expected max size");<br>+          return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (ast_data_buffer_cache_count(buffer) != CACHED_PAYLOADS_START) {<br>+          ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Newly created buffer does not have the expected cached payload count");<br>+              return AST_TEST_FAIL;<br>+        }<br>+<br>+ ast_data_buffer_free(buffer);<br>+        return AST_TEST_PASS;<br>+}<br>+<br>+AST_TEST_DEFINE(buffer_put)<br>+{<br>+       struct ast_data_buffer *buffer;<br>+      struct mock_payload *payload;<br>+        struct mock_payload *fetched_payload;<br>+        int ret;<br>+<br>+  switch (cmd) {<br>+       case TEST_INIT:<br>+              info->name = "buffer_put";<br>+              info->category = "/main/data_buffer/";<br>+          info->summary = "buffer put unit test";<br>+         info->description =<br>+                       "Test that putting payloads in the buffer yields the expected results";<br>+            return AST_TEST_NOT_RUN;<br>+     case TEST_EXECUTE:<br>+           break;<br>+       }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free, 2);<br>+ if (!buffer) {<br>+               ast_test_status_update(test, "Failed to create buffer with valid arguments");<br>+              return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (ast_data_buffer_count(buffer)) {<br>+         ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Newly created buffer is not empty");<br>+         return AST_TEST_FAIL;<br>+        }<br>+<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+   if (!payload) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to allocate memory for first payload");<br>+               return AST_TEST_FAIL;<br>+        }<br>+<br>+ payload->id = 2;<br>+<br>+       ret = ast_data_buffer_put(buffer, 2, payload);<br>+       if (ret) {<br>+           ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Adding a payload to an empty buffer did not return the "<br>+                             "expected value");<br>+         return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (ast_data_buffer_count(buffer) != 1) {<br>+            ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Adding a payload to an empty buffer did not update count "<br>+                           "to the expected value");<br>+          return AST_TEST_FAIL;<br>+        }<br>+<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);<br>+     if (!fetched_payload) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to get only payload from buffer given valid arguments");<br>+              return AST_TEST_FAIL;<br>+        }<br>+<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+   if (!payload) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to allocate memory for second payload");<br>+              return AST_TEST_FAIL;<br>+        }<br>+<br>+ payload->id = 1;<br>+<br>+       ast_data_buffer_put(buffer, 1, payload);<br>+     fetched_payload = ast_data_buffer_get(buffer, -1);<br>+   if (!fetched_payload) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to get a payload from buffer given valid arguments");<br>+         return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (ast_data_buffer_count(buffer) != 2) {<br>+            ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Buffer does not have the expected count after removing a payload");<br>+          return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (fetched_payload->id != 1) {<br>+           ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Did not get the expected payload from the buffer");<br>+          return AST_TEST_FAIL;<br>+        }<br>+<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+   if (!payload) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to allocate memory for third payload");<br>+               return AST_TEST_FAIL;<br>+        }<br>+<br>+ payload->id = 3;<br>+<br>+       ret = ast_data_buffer_put(buffer, 3, payload);<br>+       if (ret) {<br>+           ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to replace a payload in the buffer");<br>+         return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (ast_data_buffer_count(buffer) > 2) {<br>+          ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Buffer count exceeded the max");<br>+             return AST_TEST_FAIL;<br>+        }<br>+<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 3);<br>+     if (!fetched_payload) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to get a payload from buffer at position 3 given valid arguments");<br>+           return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (fetched_payload->id != 3) {<br>+           ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Did not get the expected payload at position 3 from the buffer");<br>+            return AST_TEST_FAIL;<br>+        }<br>+<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);<br>+     if (!fetched_payload) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Failed to get a payload from buffer at position 2 given valid arguments");<br>+           return AST_TEST_FAIL;<br>+        }<br>+<br>+ if (fetched_payload->id != 2) {<br>+           ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Did not get the expected payload at position 2 from the buffer");<br>+            return AST_TEST_FAIL;<br>+        }<br>+<br>+ ast_data_buffer_free(buffer);<br>+        return AST_TEST_PASS;<br>+}<br>+<br>+AST_TEST_DEFINE(buffer_resize)<br>+{<br>+    struct ast_data_buffer *buffer;<br>+<br>+   switch (cmd) {<br>+       case TEST_INIT:<br>+              info->name = "buffer_resize";<br>+           info->category = "/main/data_buffer/";<br>+          info->summary = "buffer resize unit test";<br>+              info->description =<br>+                       "Tests resizing a data buffer to make sure it has the expected outcome";<br>+           return AST_TEST_NOT_RUN;<br>+     case TEST_EXECUTE:<br>+           break;<br>+       }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free, CACHED_PAYLOADS_START);<br>+     if (!buffer) {<br>+               ast_test_status_update(test, "Failed to create buffer with valid arguments");<br>+              return AST_TEST_FAIL;<br>+        }<br>+<br>+ ast_data_buffer_resize(buffer, 0);<br>+   if (!ast_data_buffer_max(buffer)) {<br>+          ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Buffer max cannot be resized to below 1");<br>+           return AST_TEST_FAIL;<br>+        }<br>+<br>+ ast_data_buffer_resize(buffer, CACHED_PAYLOADS_START);<br>+       if (ast_data_buffer_max(buffer) != CACHED_PAYLOADS_START) {<br>+          ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Trying to resize buffer to same size should not change "<br>+                             "its max size");<br>+           return AST_TEST_FAIL;<br>+        }<br>+    <br>+     if (ast_data_buffer_cache_count(buffer) != CACHED_PAYLOADS_START) {<br>+          ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Trying to resize buffer to same size should not change "<br>+                             "cached payload count");<br>+           return AST_TEST_FAIL;<br>+        }<br>+<br>+ ast_data_buffer_resize(buffer, CACHED_PAYLOADS_START + 2);<br>+   if (ast_data_buffer_max(buffer) != CACHED_PAYLOADS_START + 2) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Increasing buffer size did not return the expected max");<br>+            return AST_TEST_FAIL;<br>+        }<br>+    <br>+     if (ast_data_buffer_cache_count(buffer) != CACHED_PAYLOADS_START + 2) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Increasing buffer size did not return the expected cache count");<br>+            return AST_TEST_FAIL;<br>+        }<br>+<br>+ ast_data_buffer_resize(buffer, 1);<br>+   if (ast_data_buffer_max(buffer) != 1) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Decreasing buffer size did not return the expected max");<br>+            return AST_TEST_FAIL;<br>+        }<br>+    <br>+     if (ast_data_buffer_cache_count(buffer) != 1) {<br>+              ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Decreasing buffer size did not return the expected cache count");<br>+            return AST_TEST_FAIL;<br>+        }<br>+<br>+ ast_data_buffer_free(buffer);<br>+        return AST_TEST_PASS;<br>+}<br>+<br>+AST_TEST_DEFINE(buffer_nominal)<br>+{<br>+   struct ast_data_buffer *buffer;<br>+      struct mock_payload *payload;<br>+        struct mock_payload *fetched_payload;<br>+        int ret;<br>+     int i;<br>+<br>+    switch (cmd) {<br>+       case TEST_INIT:<br>+              info->name = "buffer_nominal";<br>+          info->category = "/main/data_buffer/";<br>+          info->summary = "buffer nominal unit test";<br>+             info->description =<br>+                       "Tests the normal usage of a data buffer to ensure the expected payloads "<br>+                 "are present after multiple insertions";<br>+           return AST_TEST_NOT_RUN;<br>+     case TEST_EXECUTE:<br>+           break;<br>+       }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free, BUFFER_MAX_NOMINAL);<br>+        if (!buffer) {<br>+               ast_test_status_update(test, "Failed to create buffer with valid arguments");<br>+              return AST_TEST_FAIL;<br>+        }<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+               payload = ast_calloc(1, sizeof(*payload));<br>+           if (!payload) {<br>+                      ast_data_buffer_free(buffer);<br>+                        ast_test_status_update(test, "Failed to allocate memory for payload %d", i);<br>+                       return AST_TEST_FAIL;<br>+                }<br>+<br>+         ret = ast_data_buffer_put(buffer, i, payload);<br>+               if (ret) {<br>+                   ast_data_buffer_free(buffer);<br>+                        ast_test_status_update(test, "Failed to add payload %d to buffer", i);<br>+                     return AST_TEST_FAIL;<br>+                }<br>+    }<br>+<br>+ if (ast_data_buffer_count(buffer) != BUFFER_MAX_NOMINAL) {<br>+           ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Buffer does not have the expected count after adding payloads");<br>+             return AST_TEST_FAIL;<br>+        }<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+               fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);<br>+             if (!fetched_payload) {<br>+                      ast_data_buffer_free(buffer);<br>+                        ast_test_status_update(test, "Failed to get payload at position %d during first loop", i);<br>+                 return AST_TEST_FAIL;<br>+                }<br>+    }<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+               payload = ast_calloc(1, sizeof(*payload));<br>+           if (!payload) {<br>+                      ast_data_buffer_free(buffer);<br>+                        ast_test_status_update(test, "Failed to allocate memory for payload %d", i + BUFFER_MAX_NOMINAL);<br>+                  return AST_TEST_FAIL;<br>+                }<br>+<br>+         ret = ast_data_buffer_put(buffer, i + BUFFER_MAX_NOMINAL, payload);<br>+          if (ret) {<br>+                   ast_data_buffer_free(buffer);<br>+                        ast_test_status_update(test, "Failed to add payload %d to buffer", i + BUFFER_MAX_NOMINAL);<br>+                        return AST_TEST_FAIL;<br>+                }<br>+    }<br>+<br>+ if (ast_data_buffer_count(buffer) != BUFFER_MAX_NOMINAL) {<br>+           ast_data_buffer_free(buffer);<br>+                ast_test_status_update(test, "Buffer does not have the expected count after replacing payloads");<br>+          return AST_TEST_FAIL;<br>+        }<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+               fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);<br>+             if (fetched_payload) {<br>+                       ast_data_buffer_free(buffer);<br>+                        ast_test_status_update(test, "Got an unexpected payload at position %d", i);<br>+                       return AST_TEST_FAIL;<br>+                }<br>+<br>+         fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i + BUFFER_MAX_NOMINAL);<br>+                if (!fetched_payload) {<br>+                      ast_data_buffer_free(buffer);<br>+                        ast_test_status_update(test, "Failed to get payload at position %d during second loop", i + BUFFER_MAX_NOMINAL);<br>+                   return AST_TEST_FAIL;<br>+                }<br>+    }<br>+<br>+ ast_data_buffer_free(buffer);<br>+        return AST_TEST_PASS;<br>+}<br>+<br>+static int unload_module(void)<br>+{<br>+    AST_TEST_UNREGISTER(buffer_create);<br>+  AST_TEST_UNREGISTER(buffer_put);<br>+     AST_TEST_UNREGISTER(buffer_resize);<br>+  AST_TEST_UNREGISTER(buffer_nominal);<br>+ return 0;<br>+}<br>+<br>+static int load_module(void)<br>+{<br>+  AST_TEST_REGISTER(buffer_create);<br>+    AST_TEST_REGISTER(buffer_put);<br>+       AST_TEST_REGISTER(buffer_resize);<br>+    AST_TEST_REGISTER(buffer_nominal);<br>+   return AST_MODULE_LOAD_SUCCESS;<br>+}<br>+<br>+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Data buffer API test module");<br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/8605">change 8605</a>. To unsubscribe, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/8605"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 15 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Id5b599aa15a5e61d0ec080f97cd0c57bd07e6f8f </div>
<div style="display:none"> Gerrit-Change-Number: 8605 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Benjamin Keith Ford <bford@digium.com> </div>