Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libcurses/slave tests/libcurses: don't access args...



details:   https://anonhg.NetBSD.org/src/rev/40a6271f8d3e
branches:  trunk
changeset: 952309:40a6271f8d3e
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Feb 12 18:20:05 2021 +0000

description:
tests/libcurses: don't access args directly

By providing declarative syntax for accessing the arguments, the
unnecessarily detailed boilerplate code is hidden.  This allows easy
inspection by tools and humans, to check for typos and other mistakes.

diffstat:

 tests/lib/libcurses/slave/curses_commands.c |  347 +++++++++++++++------------
 1 files changed, 191 insertions(+), 156 deletions(-)

diffs (truncated from 821 to 300 lines):

diff -r 734ad2b6f317 -r 40a6271f8d3e tests/lib/libcurses/slave/curses_commands.c
--- a/tests/lib/libcurses/slave/curses_commands.c       Fri Feb 12 16:59:32 2021 +0000
+++ b/tests/lib/libcurses/slave/curses_commands.c       Fri Feb 12 18:20:05 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: curses_commands.c,v 1.18 2021/02/12 16:59:32 rillig Exp $      */
+/*     $NetBSD: curses_commands.c,v 1.19 2021/02/12 18:20:05 rillig Exp $      */
 
 /*-
  * Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -119,10 +119,23 @@
                return
 
 #define ARG_CHTYPE(i, arg) \
-       chtype arg = ((chtype *)args[i])[0]
+       chtype arg = ((const chtype *)args[i])[0]
+
+#define ARG_WCHAR(i, arg) \
+       wchar_t arg = ((const wchar_t *)args[i])[0]
+
+#define ARG_STRING(i, arg) \
+       const char *arg = args[i]
+
+/* Only used for legacy interfaces that are missing the 'const'. */
+#define ARG_MODIFIABLE_STRING(i, arg) \
+       char *arg = args[i]
+
+#define ARG_CHTYPE_STRING(i, arg) \
+       const chtype *arg = (const chtype *)args[i]
 
 #define ARG_CCHAR_STRING(i, arg) \
-       cchar_t *arg = (cchar_t *)args[i]
+       const cchar_t *arg = (const cchar_t *)args[i]
 
 #define ARG_WCHAR_STRING(i, arg) \
        wchar_t *arg = (wchar_t *)args[i]
@@ -152,11 +165,11 @@
 cmd_addbytes(int nargs, char **args)
 {
        ARGC(2);
-       /* TODO: arg 0 */
+       ARG_STRING(0, str);
        ARG_INT(1, count);
 
        report_count(1);
-       report_return(addbytes(args[0], count));
+       report_return(addbytes(str, count));
 }
 
 
@@ -175,11 +188,11 @@
 cmd_addchnstr(int nargs, char **args)
 {
        ARGC(2);
-       /* TODO: arg 0 */
+       ARG_CHTYPE_STRING(0, chstr);
        ARG_INT(1, count);
 
        report_count(1);
-       report_return(addchnstr((chtype *) args[0], count));
+       report_return(addchnstr(chstr, count));
 }
 
 
@@ -187,10 +200,10 @@
 cmd_addchstr(int nargs, char **args)
 {
        ARGC(1);
-       /* TODO: arg 0 */
-
-       report_count(1);
-       report_return(addchstr((chtype *) args[0]));
+       ARG_CHTYPE_STRING(0, chstr);
+
+       report_count(1);
+       report_return(addchstr(chstr));
 }
 
 
@@ -198,11 +211,11 @@
 cmd_addnstr(int nargs, char **args)
 {
        ARGC(2);
-       /* TODO: arg 0 */
+       ARG_STRING(0, str);
        ARG_INT(1, count);
 
        report_count(1);
-       report_return(addnstr(args[0], count));
+       report_return(addnstr(str, count));
 }
 
 
@@ -210,10 +223,10 @@
 cmd_addstr(int nargs, char **args)
 {
        ARGC(1);
-       /* TODO: arg 0 */
-
-       report_count(1);
-       report_return(addstr(args[0]));
+       ARG_STRING(0, str);
+
+       report_count(1);
+       report_return(addstr(str));
 }
 
 
@@ -684,11 +697,11 @@
 {
        ARGC(3);
        ARG_WINDOW(0, win);
-       /* TODO: arg 1 */
+       ARG_STRING(1, str);
        ARG_INT(2, count);
 
        report_count(1);
-       report_return(waddbytes(win, args[1], count));
+       report_return(waddbytes(win, str, count));
 }
 
 
@@ -697,10 +710,10 @@
 {
        ARGC(2);
        ARG_WINDOW(0, win);
-       /* TODO: arg 1 */
-
-       report_count(1);
-       report_return(waddstr(win, args[1]));
+       ARG_STRING(1, str);
+
+       report_count(1);
+       report_return(waddstr(win, str));
 }
 
 
@@ -710,11 +723,11 @@
        ARGC(4);
        ARG_INT(0, y);
        ARG_INT(1, x);
-       /* TODO: arg 2 */
+       ARG_STRING(2, str);
        ARG_INT(3, count);
 
        report_count(1);
-       report_return(mvaddbytes(y, x, args[2], count));
+       report_return(mvaddbytes(y, x, str, count));
 }
 
 
@@ -737,11 +750,11 @@
        ARGC(4);
        ARG_INT(0, y);
        ARG_INT(1, x);
-       /* TODO: arg 2 */
+       ARG_CHTYPE_STRING(2, chstr);
        ARG_INT(3, count);
 
        report_count(1);
-       report_return(mvaddchnstr(y, x, (chtype *) args[2], count));
+       report_return(mvaddchnstr(y, x, chstr, count));
 }
 
 
@@ -751,10 +764,10 @@
        ARGC(3);
        ARG_INT(0, y);
        ARG_INT(1, x);
-       /* TODO: arg 2 */
-
-       report_count(1);
-       report_return(mvaddchstr(y, x, (chtype *) args[2]));
+       ARG_CHTYPE_STRING(2, chstr);
+
+       report_count(1);
+       report_return(mvaddchstr(y, x, chstr));
 }
 
 
@@ -764,11 +777,11 @@
        ARGC(4);
        ARG_INT(0, y);
        ARG_INT(1, x);
-       /* TODO: arg 2 */
+       ARG_STRING(2, str);
        ARG_INT(3, count);
 
        report_count(1);
-       report_return(mvaddnstr(y, x, args[2], count));
+       report_return(mvaddnstr(y, x, str, count));
 }
 
 
@@ -778,10 +791,10 @@
        ARGC(3);
        ARG_INT(0, y);
        ARG_INT(1, x);
-       /* TODO: arg 2 */
-
-       report_count(1);
-       report_return(mvaddstr(y, x, args[2]));
+       ARG_STRING(2, str);
+
+       report_count(1);
+       report_return(mvaddstr(y, x, str));
 }
 
 
@@ -956,11 +969,11 @@
        ARG_WINDOW(0, win);
        ARG_INT(1, y);
        ARG_INT(2, x);
-       /* TODO: arg 3 */
+       ARG_STRING(3, str);
        ARG_INT(4, count);
 
        report_count(1);
-       report_return(mvwaddbytes(win, y, x, args[3], count));
+       report_return(mvwaddbytes(win, y, x, str, count));
 }
 
 
@@ -985,10 +998,11 @@
        ARG_WINDOW(0, win);
        ARG_INT(1, y);
        ARG_INT(2, x);
+       ARG_CHTYPE_STRING(3, chstr);
        ARG_INT(4, count);
 
        report_count(1);
-       report_return(mvwaddchnstr(win, y, x, (chtype *) args[3], count));
+       report_return(mvwaddchnstr(win, y, x, chstr, count));
 }
 
 
@@ -999,9 +1013,10 @@
        ARG_WINDOW(0, win);
        ARG_INT(1, y);
        ARG_INT(2, x);
-
-       report_count(1);
-       report_return(mvwaddchstr(win, y, x, (chtype *) args[3]));
+       ARG_CHTYPE_STRING(3, chstr);
+
+       report_count(1);
+       report_return(mvwaddchstr(win, y, x, chstr));
 }
 
 
@@ -1012,11 +1027,11 @@
        ARG_WINDOW(0, win);
        ARG_INT(1, y);
        ARG_INT(2, x);
-       /* TODO: arg 3 */
+       ARG_STRING(3, str);
        ARG_INT(4, count);
 
        report_count(1);
-       report_return(mvwaddnstr(win, y, x, args[3], count));
+       report_return(mvwaddnstr(win, y, x, str, count));
 }
 
 
@@ -1027,9 +1042,10 @@
        ARG_WINDOW(0, win);
        ARG_INT(1, y);
        ARG_INT(2, x);
-
-       report_count(1);
-       report_return(mvwaddstr(win, y, x, args[3]));
+       ARG_STRING(3, str);
+
+       report_count(1);
+       report_return(mvwaddstr(win, y, x, str));
 }
 
 
@@ -1162,16 +1178,13 @@
 void
 cmd_box(int nargs, char **args)
 {
-       chtype *vertical, *horizontal;
-
        ARGC(3);
        ARG_WINDOW(0, win);
-
-       vertical = (chtype *) args[1];
-       horizontal = (chtype *) args[2];
-
-       report_count(1);
-       report_return(box(win, vertical[0], horizontal[0]));
+       ARG_CHTYPE(1, vertical);
+       ARG_CHTYPE(2, horizontal);
+
+       report_count(1);
+       report_return(box(win, vertical, horizontal));
 }
 
 
@@ -1279,10 +1292,11 @@



Home | Main Index | Thread Index | Old Index