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: reduce boilerplat...



details:   https://anonhg.NetBSD.org/src/rev/78118478aadb
branches:  trunk
changeset: 959380:78118478aadb
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Feb 12 08:55:32 2021 +0000

description:
tests/libcurses: reduce boilerplate in function dispatcher

This makes the code more declarative and easier to reason about.

The generated code stays exactly the same.

diffstat:

 tests/lib/libcurses/slave/curses_commands.c |  1791 +++++++-------------------
 1 files changed, 519 insertions(+), 1272 deletions(-)

diffs (truncated from 4545 to 300 lines):

diff -r 98dba0792042 -r 78118478aadb tests/lib/libcurses/slave/curses_commands.c
--- a/tests/lib/libcurses/slave/curses_commands.c       Fri Feb 12 01:52:09 2021 +0000
+++ b/tests/lib/libcurses/slave/curses_commands.c       Fri Feb 12 08:55:32 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: curses_commands.c,v 1.13 2021/02/09 20:22:11 rillig Exp $      */
+/*     $NetBSD: curses_commands.c,v 1.14 2021/02/12 08:55:32 rillig Exp $      */
 
 /*-
  * Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -99,16 +99,25 @@
        return 0;
 }
 
+#define ARGC(n) \
+       if (check_arg_count(nargs, n) == 1)                             \
+               return
+
+#define ARG_INT(i, arg) \
+       int arg;                                                        \
+       if (set_int(args[i], &arg) != 0)                                \
+               return
+
+#define ARG_WINDOW(i, arg) \
+       WINDOW *arg;                                                    \
+       if (set_win(args[i], &arg) != 0)                                \
+               return
 
 void
 cmd_DRAIN(int nargs, char **args)
 {
-       WINDOW *win;
-       if (check_arg_count(nargs, 1) == 1)
-               return;
-
-       if (set_win(args[0], &win) != 0)
-               return;
+       ARGC(1);
+       ARG_WINDOW(0, win);
 
        while (wgetch(win) != ERR);
        report_count(1);
@@ -118,13 +127,8 @@
 void
 cmd_addbytes(int nargs, char **args)
 {
-       int count;
-
-       if (check_arg_count(nargs, 2) == 1)
-               return;
-
-       if (set_int(args[1], &count) != 0)
-               return;
+       ARGC(2);
+       ARG_INT(1, count);
 
        report_count(1);
        report_return(addbytes(args[0], count));
@@ -136,8 +140,7 @@
 {
        chtype *ch;
 
-       if (check_arg_count(nargs, 1) == 1)
-               return;
+       ARGC(1);
 
        ch = (chtype *) args[0];
        report_count(1);
@@ -148,13 +151,8 @@
 void
 cmd_addchnstr(int nargs, char **args)
 {
-       int count;
-
-       if (check_arg_count(nargs, 2) == 1)
-               return;
-
-       if (set_int(args[1], &count) != 0)
-               return;
+       ARGC(2);
+       ARG_INT(1, count);
 
        report_count(1);
        report_return(addchnstr((chtype *) args[0], count));
@@ -164,8 +162,7 @@
 void
 cmd_addchstr(int nargs, char **args)
 {
-       if (check_arg_count(nargs, 1) == 1)
-               return;
+       ARGC(1);
 
        report_count(1);
        report_return(addchstr((chtype *) args[0]));
@@ -175,13 +172,8 @@
 void
 cmd_addnstr(int nargs, char **args)
 {
-       int count;
-
-       if (check_arg_count(nargs, 2) == 1)
-               return;
-
-       if (set_int(args[1], &count) != 0)
-               return;
+       ARGC(2);
+       ARG_INT(1, count);
 
        report_count(1);
        report_return(addnstr(args[0], count));
@@ -191,8 +183,7 @@
 void
 cmd_addstr(int nargs, char **args)
 {
-       if (check_arg_count(nargs, 1) == 1)
-               return;
+       ARGC(1);
 
        report_count(1);
        report_return(addstr(args[0]));
@@ -206,8 +197,7 @@
        short colours;
        int retval;
 
-       if (check_arg_count(nargs, 0) == 1)
-               return;
+       ARGC(0);
 
        retval = attr_get(&attrs, &colours, NULL);
 
@@ -222,13 +212,8 @@
 void
 cmd_attr_off(int nargs, char **args)
 {
-       int attrib;
-
-       if (check_arg_count(nargs, 1) == 1)
-               return;
-
-       if (set_int(args[0], &attrib) != 0)
-               return;
+       ARGC(1);
+       ARG_INT(0, attrib);
 
        report_count(1);
        report_return(attr_off(attrib, NULL));
@@ -238,13 +223,8 @@
 void
 cmd_attr_on(int nargs, char **args)
 {
-       int attrib;
-
-       if (check_arg_count(nargs, 1) == 1)
-               return;
-
-       if (set_int(args[0], &attrib) != 0)
-               return;
+       ARGC(1);
+       ARG_INT(0, attrib);
 
        report_count(1);
        report_return(attr_on(attrib, NULL));
@@ -257,8 +237,7 @@
        int attrib;
        short pair;
 
-       if (check_arg_count(nargs, 2) == 1)
-               return;
+       ARGC(2);
 
        if ((set_int(args[0], &attrib) != 0) ||
            (set_short(args[1], &pair) != 0))
@@ -272,13 +251,8 @@
 void
 cmd_attroff(int nargs, char **args)
 {
-       int attrib;
-
-       if (check_arg_count(nargs, 1) == 1)
-               return;
-
-       if (set_int(args[0], &attrib) != 0)
-               return;
+       ARGC(1);
+       ARG_INT(0, attrib);
 
        report_count(1);
        report_return(attroff(attrib));
@@ -288,13 +262,8 @@
 void
 cmd_attron(int nargs, char **args)
 {
-       int attrib;
-
-       if (check_arg_count(nargs, 1) == 1)
-               return;
-
-       if (set_int(args[0], &attrib) != 0)
-               return;
+       ARGC(1);
+       ARG_INT(0, attrib);
 
        report_count(1);
        report_return(attron(attrib));
@@ -304,13 +273,8 @@
 void
 cmd_attrset(int nargs, char **args)
 {
-       int attrib;
-
-       if (check_arg_count(nargs, 1) == 1)
-               return;
-
-       if (set_int(args[0], &attrib) != 0)
-               return;
+       ARGC(1);
+       ARG_INT(0, attrib);
 
        report_count(1);
        report_return(attrset(attrib));
@@ -322,8 +286,7 @@
 {
        chtype *ch;
 
-       if (check_arg_count(nargs, 1) == 1)
-               return;
+       ARGC(1);
 
        ch = (chtype *) args[0];
        report_count(1);
@@ -336,8 +299,7 @@
 {
        chtype *ch;
 
-       if (check_arg_count(nargs, 1) == 1)
-               return;
+       ARGC(1);
 
        ch = (chtype *) args[0];
 
@@ -352,8 +314,7 @@
 {
        int ls, rs, ts, bs, tl, tr, bl, br;
 
-       if (check_arg_count(nargs, 8) == 1)
-               return;
+       ARGC(8);
 
        if ((set_int(args[0], &ls) != 0) ||
            (set_int(args[1], &rs) != 0) ||
@@ -373,8 +334,7 @@
 void
 cmd_clear(int nargs, char **args)
 {
-       if (check_arg_count(nargs, 0) == 1)
-               return;
+       ARGC(0);
 
        report_count(1);
        report_return(clear());
@@ -384,8 +344,7 @@
 void
 cmd_clrtobot(int nargs, char **args)
 {
-       if (check_arg_count(nargs, 0) == 1)
-               return;
+       ARGC(0);
 
        report_count(1);
        report_return(clrtobot());
@@ -395,8 +354,7 @@
 void
 cmd_clrtoeol(int nargs, char **args)
 {
-       if (check_arg_count(nargs, 0) == 1)
-               return;
+       ARGC(0);
 
        report_count(1);
        report_return(clrtoeol());
@@ -408,11 +366,11 @@
 {
        short colour_pair;
 
-       if (check_arg_count(nargs, 2) == 1)
-               return;
+       ARGC(2);
 
        if (set_short(args[0], &colour_pair) != 0)
                return;
+       /* XXX: args[1] is unused */
 
        report_count(1);
        report_return(color_set(colour_pair, NULL));
@@ -422,8 +380,7 @@
 void
 cmd_delch(int nargs, char **args)
 {
-       if (check_arg_count(nargs, 0) == 1)
-               return;
+       ARGC(0);



Home | Main Index | Thread Index | Old Index