From ae2556be02f8a2e57ba3db27ccd0fc257f14015a Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Wed, 19 Dec 2018 19:24:36 -0500
Subject: [PATCH] add tests, comments, and set freed member to null

---
 pd/src/x_interface.c                          |  9 +++-
 scripts/regression_tests.pd                   |  8 ++++
 scripts/regression_tests/msg_click.pd         |  2 +-
 scripts/regression_tests/unpost_error.pd      | 29 ++++++++++++
 scripts/regression_tests/unpost_error2.pd     | 31 +++++++++++++
 .../regression_tests/unpost_long_message.pd   | 22 ++++++++++
 scripts/regression_tests/unpost_print.pd      | 44 +++++++++++++++++++
 scripts/regression_tests/unpost_sanity.pd     | 28 ++++++++++++
 8 files changed, 170 insertions(+), 3 deletions(-)
 create mode 100644 scripts/regression_tests/unpost_error.pd
 create mode 100644 scripts/regression_tests/unpost_error2.pd
 create mode 100644 scripts/regression_tests/unpost_long_message.pd
 create mode 100644 scripts/regression_tests/unpost_print.pd
 create mode 100644 scripts/regression_tests/unpost_sanity.pd

diff --git a/pd/src/x_interface.c b/pd/src/x_interface.c
index b59e56730..34307dbea 100644
--- a/pd/src/x_interface.c
+++ b/pd/src/x_interface.c
@@ -198,7 +198,6 @@ static void *unpost_frame_new(t_unpost *x)
 
 static void unpost_frame_resize(t_unpost_frame *u, int newsize)
 {
-// error check
     u->u_buf = (char *)t_resizebytes(u->u_buf, sizeof(*u->u_buf) * u->u_bufsize,
        sizeof(*u->u_buf) * newsize);
     u->u_bufsize = newsize;
@@ -207,6 +206,7 @@ static void unpost_frame_resize(t_unpost_frame *u, int newsize)
 static void unpost_frame_free(t_unpost_frame* u)
 {
     t_freebytes(u->u_buf, sizeof(*u->u_buf));
+    u->u_buf = NULL;
 }
 
 static void *unpost_new(t_symbol *s)
@@ -233,10 +233,15 @@ static void unpost_printhook(const char *s) {
         unpost_frame_resize(current_unpost,
             current_unpost->u_bufsize * (1 << n));
     }
+
     else if (strlen(current_unpost->u_buf) + strlen(s) <
         current_unpost->u_bufsize / 4)
     {
-        /* Make the buffer smaller if it's four times too big. */
+        /* Pretty sure I prematurely optimized here. There's no good reason to
+           shrink the buffer in the midst of receiving some enormous number of
+           posts from an object chain. Besides, we free the buffer once the
+           object chain downstream from the [unpost] outlet finishes computing.
+           Anyhow, here we are. */
         unpost_frame_resize(current_unpost,
             (current_unpost->u_bufsize / 4 < NEWBUFSIZE) ?
                 NEWBUFSIZE :
diff --git a/scripts/regression_tests.pd b/scripts/regression_tests.pd
index fcdd0847a..37ba956bc 100644
--- a/scripts/regression_tests.pd
+++ b/scripts/regression_tests.pd
@@ -37,6 +37,10 @@ is handy for some binbuf tests.;
 #X obj 391 374 spigot;
 #X obj 407 440 route 1;
 #X obj 407 495 print success;
+#X obj 145 358 rtest unpost_sanity;
+#X obj 145 414 rtest unpost_error;
+#X obj 145 465 rtest unpost_print;
+#X obj 145 516 rtest unpost_long_message;
 #X connect 0 0 1 0;
 #X connect 1 0 7 0;
 #X connect 1 1 29 0;
@@ -56,9 +60,13 @@ is handy for some binbuf tests.;
 #X connect 15 0 13 0;
 #X connect 16 0 17 0;
 #X connect 17 0 18 0;
+#X connect 18 0 32 0;
 #X connect 20 0 21 0;
 #X connect 21 0 22 0;
 #X connect 22 0 14 0;
 #X connect 29 0 7 0;
 #X connect 30 0 31 0;
 #X connect 30 1 2 0;
+#X connect 32 0 33 0;
+#X connect 33 0 34 0;
+#X connect 34 0 35 0;
diff --git a/scripts/regression_tests/msg_click.pd b/scripts/regression_tests/msg_click.pd
index 4e715320f..49e54fefb 100644
--- a/scripts/regression_tests/msg_click.pd
+++ b/scripts/regression_tests/msg_click.pd
@@ -1,4 +1,4 @@
-#N canvas 34 75 582 396 12;
+#N canvas 37 104 582 396 12;
 #X obj 36 25 inlet;
 #X msg 152 97 click 0 0 0 0 0;
 #X msg 152 122 \$1;
diff --git a/scripts/regression_tests/unpost_error.pd b/scripts/regression_tests/unpost_error.pd
new file mode 100644
index 000000000..f799db619
--- /dev/null
+++ b/scripts/regression_tests/unpost_error.pd
@@ -0,0 +1,29 @@
+#N canvas 170 138 650 396 12;
+#X obj 36 25 inlet;
+#X obj 36 349 outlet;
+#X msg 36 63 unknown_method;
+#X obj 61 126 unpost error;
+#X obj 36 226 list;
+#X obj 36 251 route bang;
+#X text 155 16 Unpost takes an argument "error" to set it to only redirect
+error messages. Here we make sure it redirects an error message.;
+#X obj 36 88 trigger bang anything bang;
+#X obj 142 181 float;
+#X text 189 181 <- trigger an error;
+#X obj 36 276 f 0;
+#X msg 103 276 1;
+#X obj 36 309 list append unpost with "error" argument should redirect
+an error message;
+#X connect 0 0 2 0;
+#X connect 2 0 7 0;
+#X connect 3 0 4 1;
+#X connect 3 1 8 0;
+#X connect 4 0 5 0;
+#X connect 5 0 10 0;
+#X connect 5 1 11 0;
+#X connect 7 0 4 0;
+#X connect 7 1 3 0;
+#X connect 7 2 4 1;
+#X connect 10 0 12 0;
+#X connect 11 0 12 0;
+#X connect 12 0 1 0;
diff --git a/scripts/regression_tests/unpost_error2.pd b/scripts/regression_tests/unpost_error2.pd
new file mode 100644
index 000000000..a6aed2efc
--- /dev/null
+++ b/scripts/regression_tests/unpost_error2.pd
@@ -0,0 +1,31 @@
+#N canvas 170 138 650 396 12;
+#X obj 36 25 inlet;
+#X obj 36 349 outlet;
+#X msg 36 63 unknown_method;
+#X text 155 16 Unpost takes an argument "error" to set it to only redirect
+error messages. Here we make sure it doesn't allow non-error messages
+through;
+#X obj 61 126 unpost error;
+#X obj 142 211 print -n;
+#X obj 36 226 list;
+#X obj 36 88 trigger bang bang bang;
+#X obj 36 251 route bang;
+#X obj 36 309 list append unpost with "error" argument should not redirect
+non-error messages;
+#X obj 36 276 f 1;
+#X msg 103 276 0;
+#X msg 142 186 unpost_error test message;
+#X connect 0 0 2 0;
+#X connect 2 0 7 0;
+#X connect 4 0 6 1;
+#X connect 4 1 12 0;
+#X connect 6 0 8 0;
+#X connect 7 0 6 0;
+#X connect 7 1 4 0;
+#X connect 7 2 6 1;
+#X connect 8 0 10 0;
+#X connect 8 1 11 0;
+#X connect 9 0 1 0;
+#X connect 10 0 9 0;
+#X connect 11 0 9 0;
+#X connect 12 0 5 0;
diff --git a/scripts/regression_tests/unpost_long_message.pd b/scripts/regression_tests/unpost_long_message.pd
new file mode 100644
index 000000000..e3738436d
--- /dev/null
+++ b/scripts/regression_tests/unpost_long_message.pd
@@ -0,0 +1,22 @@
+#N canvas 86 182 696 396 12;
+#X obj 36 25 inlet;
+#X obj 36 349 outlet;
+#X obj 36 266 f 1;
+#X text 155 16 Unpost should be able to handle long messages. Here
+we trigger one by sending a list of all the class names to the left
+inlet of unpost.;
+#X msg 36 63 classlist;
+#X obj 36 88 unpost;
+#X obj 75 116 pdinfo;
+#X obj 75 148 print long_message;
+#X obj 36 113 b;
+#X obj 36 299 list append unpost should print long messages without
+crashing;
+#X connect 0 0 4 0;
+#X connect 2 0 9 0;
+#X connect 4 0 5 0;
+#X connect 5 0 8 0;
+#X connect 5 1 6 0;
+#X connect 6 0 7 0;
+#X connect 8 0 2 0;
+#X connect 9 0 1 0;
diff --git a/scripts/regression_tests/unpost_print.pd b/scripts/regression_tests/unpost_print.pd
new file mode 100644
index 000000000..7dda0dabe
--- /dev/null
+++ b/scripts/regression_tests/unpost_print.pd
@@ -0,0 +1,44 @@
+#N canvas 35 102 696 396 12;
+#X obj 36 25 inlet;
+#X obj 36 349 outlet;
+#X msg 36 63 unknown_method;
+#X obj 36 266 f 1;
+#X obj 528 228 print unpost_print;
+#X obj 378 146 unpost;
+#X msg 528 146 testing unpost...;
+#X obj 198 228 print unpost_print;
+#X msg 198 146 testing unpost error...;
+#X obj 58 146 unpost error;
+#X obj 139 171 float;
+#X obj 58 171 b;
+#X msg 58 201 test message;
+#X obj 36 88 trigger bang anything bang bang bang;
+#X text 155 16 Unpost allows printing from the left outlet *without*
+redirecting back to the outlet. This prevents infinite loops. Without
+this protection this test would crash.;
+#X obj 36 299 list append when unpost's left-outlet messages are printed
+to the console an infinite loop should not occur;
+#X obj 58 228 print -n;
+#X obj 378 228 print -n;
+#X obj 378 171 b;
+#X msg 378 196 test message;
+#X obj 417 171 print test;
+#X connect 0 0 2 0;
+#X connect 2 0 13 0;
+#X connect 3 0 15 0;
+#X connect 5 0 18 0;
+#X connect 5 1 20 0;
+#X connect 6 0 4 0;
+#X connect 8 0 7 0;
+#X connect 9 0 11 0;
+#X connect 9 1 10 0;
+#X connect 11 0 12 0;
+#X connect 12 0 16 0;
+#X connect 13 0 3 0;
+#X connect 13 1 9 0;
+#X connect 13 2 8 0;
+#X connect 13 3 5 0;
+#X connect 13 4 6 0;
+#X connect 15 0 1 0;
+#X connect 18 0 19 0;
+#X connect 19 0 17 0;
diff --git a/scripts/regression_tests/unpost_sanity.pd b/scripts/regression_tests/unpost_sanity.pd
new file mode 100644
index 000000000..d9fc5715a
--- /dev/null
+++ b/scripts/regression_tests/unpost_sanity.pd
@@ -0,0 +1,28 @@
+#N canvas 164 80 650 396 12;
+#X obj 36 25 inlet;
+#X obj 36 309 outlet;
+#X text 155 16 Unpost is used to redirect messages from the console.
+We need it for some of the following tests \, so we make sure it operates
+correctly here first.;
+#X obj 61 136 unpost;
+#X msg 36 63 unknown_method;
+#X obj 100 166 float;
+#X obj 36 166 list;
+#X obj 36 88 trigger bang anything bang;
+#X obj 36 191 route bang;
+#X obj 36 269 list append unpost should redirect an error message;
+#X msg 103 225 1;
+#X obj 36 226 f 0;
+#X connect 0 0 4 0;
+#X connect 3 0 6 1;
+#X connect 3 1 5 0;
+#X connect 4 0 7 0;
+#X connect 6 0 8 0;
+#X connect 7 0 6 0;
+#X connect 7 1 3 0;
+#X connect 7 2 6 1;
+#X connect 8 0 11 0;
+#X connect 8 1 10 0;
+#X connect 9 0 1 0;
+#X connect 10 0 9 0;
+#X connect 11 0 9 0;
-- 
GitLab