]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/test_tparm.c
ncurses 6.2 - patch 20210306
[ncurses.git] / test / test_tparm.c
index 40ffc4fb1d07f0916a46110c39cd4efe98d0908a..01fadfee714f4c5bc9943a5c13fff36792f8217e 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright 2020 Thomas E. Dickey                                          *
+ * Copyright 2020,2021 Thomas E. Dickey                                     *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -29,7 +29,7 @@
 /*
  * Author: Thomas E. Dickey
  *
 /*
  * Author: Thomas E. Dickey
  *
- * $Id: test_tparm.c,v 1.4 2020/05/31 00:51:32 tom Exp $
+ * $Id: test_tparm.c,v 1.17 2021/03/06 23:39:14 tom Exp $
  *
  * Exercise tparm, either for all possible capabilities with fixed parameters,
  * or one capability with all possible parameters.
  *
  * Exercise tparm, either for all possible capabilities with fixed parameters,
  * or one capability with all possible parameters.
@@ -53,8 +53,35 @@ failed(const char *msg)
 #if HAVE_TIGETSTR
 
 static int a_opt;
 #if HAVE_TIGETSTR
 
 static int a_opt;
+static int p_opt;
 static int v_opt;
 
 static int v_opt;
 
+/*
+ * Total tests (and failures):
+ */
+static long total_tests;
+static long total_fails;
+
+/*
+ * Total characters formatted for tputs:
+ */
+static long total_nulls;
+static long total_ctrls;
+static long total_print;
+
+static int
+output_func(int ch)
+{
+    if (ch == 0) {
+       total_nulls++;
+    } else if (ch < 32 || (ch >= 127 && ch < 160)) {
+       total_ctrls++;
+    } else {
+       total_print++;
+    }
+    return ch;
+}
+
 static int
 isNumeric(char *source)
 {
 static int
 isNumeric(char *source)
 {
@@ -65,21 +92,30 @@ isNumeric(char *source)
     return result;
 }
 
     return result;
 }
 
-static char *
-validate(const char *name)
+static int
+relevant(const char *name, const char *value)
 {
 {
-    char *value = tigetstr(name);
-    if (!VALID_STRING(value)) {
-       if (v_opt > 1) {
+    int code = 1;
+    if (VALID_STRING(value)) {
+       if (strstr(value, "%p") == 0
+           && strstr(value, "%d") == 0
+           && strstr(value, "%s") == 0
+           && (!p_opt || strstr(value, "$<") == 0)) {
+           if (v_opt > 2)
+               printf("? %s noparams\n", name);
+           code = 0;
+       }
+    } else {
+       if (v_opt > 2) {
            printf("? %s %s\n",
                   (value == ABSENT_STRING)
                   ? "absent"
                   : "cancel",
                   name);
        }
            printf("? %s %s\n",
                   (value == ABSENT_STRING)
                   ? "absent"
                   : "cancel",
                   name);
        }
-       value = 0;
+       code = 0;
     }
     }
-    return value;
+    return code;
 }
 
 static int
 }
 
 static int
@@ -108,34 +144,37 @@ increment(int *all_parms, int *num_parms, int len_parms, int end_parms)
 }
 
 static void
 }
 
 static void
-test_tparm(const char *name, int *number)
+test_tparm(const char *name, const char *format, int *number)
 {
 {
-    char *format = tigetstr(name);
-    if ((format = validate(name)) != 0) {
-       char *result = tparm(format,
-                            number[0],
-                            number[1],
-                            number[2],
-                            number[3],
-                            number[4],
-                            number[5],
-                            number[6],
-                            number[7],
-                            number[8]);
-       if (v_opt > 1)
-           printf(".. %2d = %2d %2d %2d %2d %2d %2d %2d %2d %2d %s\n",
-                  result != 0 ? (int) strlen(result) : -1,
-                  number[0],
-                  number[1],
-                  number[2],
-                  number[3],
-                  number[4],
-                  number[5],
-                  number[6],
-                  number[7],
-                  number[8],
-                  name);
+    char *result = tparm(format,
+                        number[0],
+                        number[1],
+                        number[2],
+                        number[3],
+                        number[4],
+                        number[5],
+                        number[6],
+                        number[7],
+                        number[8]);
+    total_tests++;
+    if (result != NULL) {
+       tputs(result, 1, output_func);
+    } else {
+       total_fails++;
     }
     }
+    if (v_opt > 1)
+       printf(".. %2d = %2d %2d %2d %2d %2d %2d %2d %2d %2d %s\n",
+              result != 0 ? (int) strlen(result) : -1,
+              number[0],
+              number[1],
+              number[2],
+              number[3],
+              number[4],
+              number[5],
+              number[6],
+              number[7],
+              number[8],
+              name);
 }
 
 static void
 }
 
 static void
@@ -145,12 +184,14 @@ usage(void)
     {
        "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]",
        "",
     {
        "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]",
        "",
-       "Print all distinct combinations of given capability.",
+       "Use tparm/tputs for all distinct combinations of given capability.",
        "",
        "Options:",
        " -T TERM  override $TERM; this may be a comma-separated list or \"-\"",
        "          to read a list from standard-input",
        "",
        "Options:",
        " -T TERM  override $TERM; this may be a comma-separated list or \"-\"",
        "          to read a list from standard-input",
-       " -a       if capability is given, test all combinations of values",
+       " -a       test all combinations of parameters",
+       "          [value1...] forms a vector of maximum parameter-values.",
+       " -p       test capabilities with no parameters but having padding",
        " -r NUM   repeat tests NUM times",
        " -v       show values and results",
     };
        " -r NUM   repeat tests NUM times",
        " -v       show values and results",
     };
@@ -164,6 +205,8 @@ usage(void)
 #define PLURAL(n) n, (n != 1) ? "s" : ""
 #define COLONS(n) (n >= 1) ? ":" : ""
 
 #define PLURAL(n) n, (n != 1) ? "s" : ""
 #define COLONS(n) (n >= 1) ? ":" : ""
 
+#define NUMFORM "%10ld"
+
 int
 main(int argc, char *argv[])
 {
 int
 main(int argc, char *argv[])
 {
@@ -172,22 +215,31 @@ main(int argc, char *argv[])
     char *old_term = getenv("TERM");
     int r_opt = 1;
     char *t_opt = 0;
     char *old_term = getenv("TERM");
     int r_opt = 1;
     char *t_opt = 0;
-    int len_names = 0;         /* cur # of items in all_names[] */
-    int use_names = 10;                /* max # of items in all_names[] */
-    char **all_names = typeCalloc(char *, use_names);
+
+    int len_caps = 0;          /* cur # of items in all_caps[] */
+    int max_caps = 10;         /* max # of items in all_caps[] */
+    char **all_caps = typeCalloc(char *, max_caps);
+
     int all_parms[10];         /* workspace for "-a" option */
     int all_parms[10];         /* workspace for "-a" option */
+
     int len_terms = 0;         /* cur # of items in all_terms[] */
     int len_terms = 0;         /* cur # of items in all_terms[] */
-    int use_terms = 10;                /* max # of items in all_terms[] */
-    char **all_terms = typeCalloc(char *, use_terms);
+    int max_terms = 10;                /* max # of items in all_terms[] */
+    char **all_terms = typeCalloc(char *, max_terms);
+
+    int use_caps;
+    char **cap_name;
+    char **cap_data;
+
     int len_parms = 0;         /* cur # of items in num_parms[], str_parms[] */
     int len_parms = 0;         /* cur # of items in num_parms[], str_parms[] */
-    int use_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
-    int *num_parms = typeCalloc(int, use_parms);
-    char **str_parms = typeCalloc(char *, use_parms);
+    int max_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
+    int *num_parms = typeCalloc(int, max_parms);
+    char **str_parms = typeCalloc(char *, max_parms);
+    long use_parms = 1;
 
 
-    if (all_names == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
+    if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
        failed("no memory");
 
        failed("no memory");
 
-    while ((n = getopt(argc, argv, "T:ar:v")) != -1) {
+    while ((n = getopt(argc, argv, "T:apr:v")) != -1) {
        switch (n) {
        case 'T':
            t_opt = optarg;
        switch (n) {
        case 'T':
            t_opt = optarg;
@@ -195,6 +247,9 @@ main(int argc, char *argv[])
        case 'a':
            ++a_opt;
            break;
        case 'a':
            ++a_opt;
            break;
+       case 'p':
+           ++p_opt;
+           break;
        case 'r':
            r_opt = atoi(optarg);
            break;
        case 'r':
            r_opt = atoi(optarg);
            break;
@@ -213,14 +268,14 @@ main(int argc, char *argv[])
      */
     if (optind < argc) {
        if (!isNumeric(argv[optind])) {
      */
     if (optind < argc) {
        if (!isNumeric(argv[optind])) {
-           all_names[len_names++] = strdup(argv[optind++]);
+           all_caps[len_caps++] = strdup(argv[optind++]);
        }
     }
 
     /*
      * Any remaining arguments must be possible parameter values.  If numeric,
        }
     }
 
     /*
      * Any remaining arguments must be possible parameter values.  If numeric,
-     * and "-a" is not set, use those as the maximum values within which the
-     * test parameters should vary.
+     * and "-a" is not set, use those as the actual values for which the
+     * capabilities are tested.
      */
     while (optind < argc) {
        if (isNumeric(argv[optind])) {
      */
     while (optind < argc) {
        if (isNumeric(argv[optind])) {
@@ -232,14 +287,16 @@ main(int argc, char *argv[])
        ++optind;
        ++len_parms;
     }
        ++optind;
        ++len_parms;
     }
-    for (n = len_parms; n < use_parms; ++n) {
+    for (n = len_parms; n < max_parms; ++n) {
        static char dummy[1];
        str_parms[n] = dummy;
     }
     if (v_opt) {
        printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
        static char dummy[1];
        str_parms[n] = dummy;
     }
     if (v_opt) {
        printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
-       for (n = 0; n < len_parms; ++n) {
-           printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
+       if (v_opt > 3) {
+           for (n = 0; n < len_parms; ++n) {
+               printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
+           }
        }
     }
 
        }
     }
 
@@ -260,9 +317,9 @@ main(int argc, char *argv[])
                while (t != s && isspace(UChar(t[-1])))
                    *--t = '\0';
                s = strdup(s);
                while (t != s && isspace(UChar(t[-1])))
                    *--t = '\0';
                s = strdup(s);
-               if (len_terms + 2 >= use_terms) {
-                   use_terms *= 2;
-                   all_terms = typeRealloc(char *, use_terms, all_terms);
+               if (len_terms + 2 >= max_terms) {
+                   max_terms *= 2;
+                   all_terms = typeRealloc(char *, max_terms, all_terms);
                    if (all_terms == 0)
                        failed("no memory: all_terms");
                }
                    if (all_terms == 0)
                        failed("no memory: all_terms");
                }
@@ -273,9 +330,9 @@ main(int argc, char *argv[])
            char *t;
            while ((t = strtok(s, ",")) != 0) {
                s = 0;
            char *t;
            while ((t = strtok(s, ",")) != 0) {
                s = 0;
-               if (len_terms + 2 >= use_terms) {
-                   use_terms *= 2;
-                   all_terms = typeRealloc(char *, use_terms, all_terms);
+               if (len_terms + 2 >= max_terms) {
+                   max_terms *= 2;
+                   all_terms = typeRealloc(char *, max_terms, all_terms);
                    if (all_terms == 0)
                        failed("no memory: all_terms");
                }
                    if (all_terms == 0)
                        failed("no memory: all_terms");
                }
@@ -288,8 +345,10 @@ main(int argc, char *argv[])
     all_terms[len_terms] = 0;
     if (v_opt) {
        printf("%d term%s:\n", PLURAL(len_terms));
     all_terms[len_terms] = 0;
     if (v_opt) {
        printf("%d term%s:\n", PLURAL(len_terms));
-       for (n = 0; n < len_terms; ++n) {
-           printf(" %d: %s\n", n + 1, all_terms[n]);
+       if (v_opt > 3) {
+           for (n = 0; n < len_terms; ++n) {
+               printf(" %d: %s\n", n + 1, all_terms[n]);
+           }
        }
     }
 
        }
     }
 
@@ -300,34 +359,45 @@ main(int argc, char *argv[])
      * TODO: To address the "other" systems which do not follow SVr4,
      * just use the output from infocmp on $TERM.
      */
      * TODO: To address the "other" systems which do not follow SVr4,
      * just use the output from infocmp on $TERM.
      */
-    if (len_names == 0) {
+    if (len_caps == 0) {
 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
        for (n = 0; strnames[n] != 0; ++n) {
 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
        for (n = 0; strnames[n] != 0; ++n) {
-           if (len_names + 2 >= use_names) {
-               use_names *= 2;
-               all_names = typeRealloc(char *, use_names, all_names);
-               if (all_names == 0) {
-                   failed("no memory: all_names");
+           if (len_caps + 2 >= max_caps) {
+               max_caps *= 2;
+               all_caps = typeRealloc(char *, max_caps, all_caps);
+               if (all_caps == 0) {
+                   failed("no memory: all_caps");
                }
            }
                }
            }
-           all_names[len_names++] = strdup(strnames[n]);
+           all_caps[len_caps++] = strdup(strnames[n]);
        }
 #else
        }
 #else
-       all_names[len_names++] = strdup("cup");
-       all_names[len_names++] = strdup("sgr");
+       all_caps[len_caps++] = strdup("cup");
+       all_caps[len_caps++] = strdup("sgr");
 #endif
     }
 #endif
     }
-    all_names[len_names] = 0;
+    all_caps[len_caps] = 0;
     if (v_opt) {
     if (v_opt) {
-       printf("%d name%s%s\n", PLURAL(len_names), COLONS(len_names));
-       for (n = 0; n < len_names; ++n) {
-           printf(" %d: %s\n", n + 1, all_names[n]);
+       printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
+       if (v_opt > 3) {
+           for (n = 0; n < len_caps; ++n) {
+               printf(" %d: %s\n", n + 1, all_caps[n]);
+           }
        }
     }
 
        }
     }
 
+    cap_name = typeMalloc(char *, len_caps);
+    cap_data = typeMalloc(char *, len_caps);
+
     if (r_opt <= 0)
        r_opt = 1;
 
     if (r_opt <= 0)
        r_opt = 1;
 
+    if (a_opt) {
+       for (n = 0; n < use_parms; ++n)
+           if (num_parms[n])
+               use_parms *= (num_parms[n] + 1);
+    }
+
     for (r_run = 0; r_run < r_opt; ++r_run) {
        for (t_run = 0; t_run < len_terms; ++t_run) {
            int errs;
     for (r_run = 0; r_run < r_opt; ++r_run) {
        for (t_run = 0; t_run < len_terms; ++t_run) {
            int errs;
@@ -336,23 +406,42 @@ main(int argc, char *argv[])
                printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
            }
 
                printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
            }
 
-           if (v_opt)
-               printf("** testing %s\n", all_terms[t_run]);
-           if (len_names == 1) {
-               if (a_opt) {
-                   /* for each combination of values */
-                   memset(all_parms, 0, sizeof(all_parms));
-                   do {
-                       test_tparm(all_names[0], all_parms);
+           /*
+            * Most of the capabilities have no parameters, e.g., they are
+            * function-keys or simple operations such as clear-display.
+            * Ignore those, since they do not really exercise tparm.
+            */
+           use_caps = 0;
+           for (n = 0; n < len_caps; ++n) {
+               char *value = tigetstr(all_caps[n]);
+               if (relevant(all_caps[n], value)) {
+                   cap_name[use_caps] = all_caps[n];
+                   cap_data[use_caps] = value;
+                   use_caps++;
+               }
+           }
+
+           if (v_opt) {
+               printf("[%d:%d] %d cap%s * %ld param%s \"%s\"\n",
+                      r_run + 1, r_opt,
+                      PLURAL(use_caps),
+                      PLURAL(use_parms),
+                      all_terms[t_run]);
+           }
+
+           if (a_opt) {
+               /* for each combination of values */
+               memset(all_parms, 0, sizeof(all_parms));
+               do {
+                   for (n_run = 0; n_run < use_caps; ++n_run) {
+                       test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
                    }
                    }
-                   while (increment(all_parms, num_parms, len_parms, 0));
-               } else {
-                   /* for the given values */
-                   test_tparm(all_names[0], num_parms);
                }
                }
+               while (increment(all_parms, num_parms, len_parms, 0));
            } else {
            } else {
-               for (n_run = 0; n_run < len_names; ++n_run) {
-                   test_tparm(all_names[n_run], num_parms);
+               /* for the given values */
+               for (n_run = 0; n_run < use_caps; ++n_run) {
+                   test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
                }
            }
            if (cur_term != 0) {
                }
            }
            if (cur_term != 0) {
@@ -362,11 +451,21 @@ main(int argc, char *argv[])
            }
        }
     }
            }
        }
     }
+
+    printf("Tests:\n");
+    printf(NUMFORM " total\n", total_tests);
+    if (total_fails)
+       printf(NUMFORM " failed\n", total_fails);
+    printf("Characters:\n");
+    printf(NUMFORM " nulls\n", total_nulls);
+    printf(NUMFORM " controls\n", total_ctrls);
+    printf(NUMFORM " printable\n", total_print);
+    printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
 #if NO_LEAKS
 #if NO_LEAKS
-    for (n = 0; n < len_names; ++n) {
-       free(all_names[n]);
+    for (n = 0; n < len_caps; ++n) {
+       free(all_caps[n]);
     }
     }
-    free(all_names);
+    free(all_caps);
     free(old_term);
     for (n = 0; n < len_terms; ++n) {
        free(all_terms[n]);
     free(old_term);
     for (n = 0; n < len_terms; ++n) {
        free(all_terms[n]);
@@ -374,6 +473,8 @@ main(int argc, char *argv[])
     free(all_terms);
     free(num_parms);
     free(str_parms);
     free(all_terms);
     free(num_parms);
     free(str_parms);
+    free(cap_name);
+    free(cap_data);
 #endif
 
     ExitProgram(EXIT_SUCCESS);
 #endif
 
     ExitProgram(EXIT_SUCCESS);