]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/hashtest.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / hashtest.c
1 /****************************************************************************
2  * Copyright 2019-2020,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2013,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * hashtest.c -- test hash mapping
31  *
32  * Generate timing statistics for vertical-motion optimization.
33  *
34  * $Id: hashtest.c,v 1.39 2022/12/04 00:40:11 tom Exp $
35  */
36
37 #include <test.priv.h>
38
39 #define LO_CHAR ' '
40 #define HI_CHAR '~'
41
42 static bool continuous = FALSE;
43 static bool reverse_loops = FALSE;
44 static bool single_step = FALSE;
45 static bool extend_corner = FALSE;
46 static int foot_lines = 0;
47 static int head_lines = 0;
48
49 static void
50 cleanup(void)
51 {
52     move(LINES - 1, 0);
53     clrtoeol();
54     refresh();
55     endwin();
56 }
57
58 static void
59 finish(int sig GCC_UNUSED)
60 {
61     cleanup();
62     ExitProgram(EXIT_FAILURE);
63 }
64
65 static void
66 genlines(int base)
67 {
68     int i, j;
69
70 #if USE_TRACE
71     if (base == 'a')
72         Trace(("Resetting screen"));
73     else
74         Trace(("Painting `%c' screen", base));
75 #endif
76
77     /* Do this so writes to lower-right corner don't cause a spurious
78      * scrolling operation.  This _shouldn't_ break the scrolling
79      * optimization, since that's computed in the refresh() call.
80      */
81     scrollok(stdscr, FALSE);
82
83     move(0, 0);
84     for (i = 0; i < head_lines; i++)
85         for (j = 0; j < COLS; j++)
86             AddCh(UChar((j % 8 == 0) ? ('A' + j / 8) : '-'));
87
88     move(head_lines, 0);
89     for (i = head_lines; i < LINES - foot_lines; i++) {
90         chtype c = (chtype) ((base - LO_CHAR + i) % (HI_CHAR - LO_CHAR + 1)
91                              + LO_CHAR);
92         int hi = (extend_corner || (i < LINES - 1)) ? COLS : COLS - 1;
93         for (j = 0; j < hi; j++)
94             AddCh(c);
95     }
96
97     for (i = LINES - foot_lines; i < LINES; i++) {
98         move(i, 0);
99         for (j = 0; j < (extend_corner ? COLS : COLS - 1); j++)
100             AddCh(UChar((j % 8 == 0) ? ('A' + j / 8) : '-'));
101     }
102
103     scrollok(stdscr, TRUE);
104     if (single_step) {
105         move(LINES - 1, 0);
106         getch();
107     } else
108         refresh();
109 }
110
111 static void
112 one_cycle(int ch)
113 {
114     if (continuous) {
115         genlines(ch);
116     } else if (ch != 'a') {
117         genlines('a');
118         genlines(ch);
119     }
120 }
121
122 static void
123 run_test(bool optimized GCC_UNUSED)
124 {
125     char ch;
126     int lo = continuous ? LO_CHAR : 'a' - LINES;
127     int hi = continuous ? HI_CHAR : 'a' + LINES;
128
129     if (lo < LO_CHAR)
130         lo = LO_CHAR;
131     if (hi > HI_CHAR)
132         hi = HI_CHAR;
133
134 #if defined(TRACE) || defined(NCURSES_TEST)
135     if (optimized) {
136         Trace(("With hash mapping"));
137         _nc_optimize_enable |= OPTIMIZE_HASHMAP;
138     } else {
139         Trace(("Without hash mapping"));
140         _nc_optimize_enable &= ~OPTIMIZE_HASHMAP;
141     }
142 #endif
143
144     if (reverse_loops)
145         for (ch = (char) hi; ch >= lo; ch--)
146             one_cycle(ch);
147     else
148         for (ch = (char) lo; ch <= hi; ch++)
149             one_cycle(ch);
150 }
151
152 static void
153 usage(int ok)
154 {
155     static const char *const tbl[] =
156     {
157         "Usage: hashtest [options]"
158         ,""
159         ,USAGE_COMMON
160         ,"Options:"
161         ," -c       continuous (don't reset between refresh's)"
162         ," -F num   leave 'num' lines constant for footer"
163         ," -H num   leave 'num' lines constant for header"
164         ," -l num   repeat test 'num' times"
165         ," -n       test the normal optimizer"
166         ," -o       test the hashed optimizer"
167         ," -r       reverse the loops"
168         ," -s       single-step"
169         ," -x       assume lower-right corner extension"
170     };
171     size_t n;
172
173     for (n = 0; n < SIZEOF(tbl); n++)
174         fprintf(stderr, "%s\n", tbl[n]);
175     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
176 }
177 /* *INDENT-OFF* */
178 VERSION_COMMON()
179 /* *INDENT-ON* */
180
181 int
182 main(int argc, char *argv[])
183 {
184     int ch;
185     int test_loops = 1;
186     int test_normal = FALSE;
187     int test_optimize = FALSE;
188
189     setlocale(LC_ALL, "");
190
191     while ((ch = getopt(argc, argv, OPTS_COMMON "cF:H:l:norsx")) != -1) {
192         switch (ch) {
193         case 'c':
194             continuous = TRUE;
195             break;
196         case 'F':
197             foot_lines = atoi(optarg);
198             break;
199         case 'H':
200             head_lines = atoi(optarg);
201             break;
202         case 'l':
203             test_loops = atoi(optarg);
204             assert(test_loops >= 0);
205             break;
206         case 'n':
207             test_normal = TRUE;
208             break;
209         case 'o':
210             test_optimize = TRUE;
211             break;
212         case 'r':
213             reverse_loops = TRUE;
214             break;
215         case 's':
216             single_step = TRUE;
217             break;
218         case 'x':
219             extend_corner = TRUE;
220             break;
221         case OPTS_VERSION:
222             show_version(argv);
223             ExitProgram(EXIT_SUCCESS);
224         default:
225             usage(ch == OPTS_USAGE);
226             /* NOTREACHED */
227         }
228     }
229     if (!test_normal && !test_optimize) {
230         test_normal = TRUE;
231         test_optimize = TRUE;
232     }
233 #if USE_TRACE
234     curses_trace(TRACE_TIMES);
235 #endif
236
237     InitAndCatch(initscr(), finish);
238     keypad(stdscr, TRUE);       /* enable keyboard mapping */
239     (void) nonl();              /* tell curses not to do NL->CR/NL on output */
240     (void) cbreak();            /* take input chars one at a time, no wait for \n */
241     (void) noecho();            /* don't echo input */
242     scrollok(stdscr, TRUE);
243
244     while (test_loops-- > 0) {
245         if (test_normal)
246             run_test(FALSE);
247         if (test_optimize)
248             run_test(TRUE);
249     }
250
251     cleanup();                  /* we're done */
252     ExitProgram(EXIT_SUCCESS);
253 }
254 /* hashtest.c ends here */