blob: be79c0e10372de14c74e53e0c5729fc8f979533b [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2013,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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 *
micky3879b9f5e72025-07-08 18:04:53 -040034 * $Id: hashtest.c,v 1.39 2022/12/04 00:40:11 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010035 */
36
37#include <test.priv.h>
38
39#define LO_CHAR ' '
40#define HI_CHAR '~'
41
42static bool continuous = FALSE;
43static bool reverse_loops = FALSE;
44static bool single_step = FALSE;
45static bool extend_corner = FALSE;
46static int foot_lines = 0;
47static int head_lines = 0;
48
49static void
50cleanup(void)
51{
52 move(LINES - 1, 0);
53 clrtoeol();
54 refresh();
55 endwin();
56}
57
58static void
59finish(int sig GCC_UNUSED)
60{
61 cleanup();
62 ExitProgram(EXIT_FAILURE);
63}
64
65static void
66genlines(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++)
micky3879b9f5e72025-07-08 18:04:53 -040086 AddCh(UChar((j % 8 == 0) ? ('A' + j / 8) : '-'));
Steve Kondikae271bc2015-11-15 02:50:53 +010087
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++)
micky3879b9f5e72025-07-08 18:04:53 -040094 AddCh(c);
Steve Kondikae271bc2015-11-15 02:50:53 +010095 }
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++)
micky3879b9f5e72025-07-08 18:04:53 -0400100 AddCh(UChar((j % 8 == 0) ? ('A' + j / 8) : '-'));
Steve Kondikae271bc2015-11-15 02:50:53 +0100101 }
102
103 scrollok(stdscr, TRUE);
104 if (single_step) {
105 move(LINES - 1, 0);
106 getch();
107 } else
108 refresh();
109}
110
111static void
112one_cycle(int ch)
113{
114 if (continuous) {
115 genlines(ch);
116 } else if (ch != 'a') {
117 genlines('a');
118 genlines(ch);
119 }
120}
121
122static void
123run_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
152static void
micky3879b9f5e72025-07-08 18:04:53 -0400153usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100154{
155 static const char *const tbl[] =
156 {
157 "Usage: hashtest [options]"
158 ,""
micky3879b9f5e72025-07-08 18:04:53 -0400159 ,USAGE_COMMON
Steve Kondikae271bc2015-11-15 02:50:53 +0100160 ,"Options:"
micky3879b9f5e72025-07-08 18:04:53 -0400161 ," -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"
Steve Kondikae271bc2015-11-15 02:50:53 +0100170 };
171 size_t n;
172
173 for (n = 0; n < SIZEOF(tbl); n++)
174 fprintf(stderr, "%s\n", tbl[n]);
micky3879b9f5e72025-07-08 18:04:53 -0400175 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100176}
micky3879b9f5e72025-07-08 18:04:53 -0400177/* *INDENT-OFF* */
178VERSION_COMMON()
179/* *INDENT-ON* */
Steve Kondikae271bc2015-11-15 02:50:53 +0100180
181int
182main(int argc, char *argv[])
183{
micky3879b9f5e72025-07-08 18:04:53 -0400184 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100185 int test_loops = 1;
186 int test_normal = FALSE;
187 int test_optimize = FALSE;
188
189 setlocale(LC_ALL, "");
190
micky3879b9f5e72025-07-08 18:04:53 -0400191 while ((ch = getopt(argc, argv, OPTS_COMMON "cF:H:l:norsx")) != -1) {
192 switch (ch) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100193 case 'c':
194 continuous = TRUE;
195 break;
micky3879b9f5e72025-07-08 18:04:53 -0400196 case 'F':
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 foot_lines = atoi(optarg);
198 break;
micky3879b9f5e72025-07-08 18:04:53 -0400199 case 'H':
Steve Kondikae271bc2015-11-15 02:50:53 +0100200 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;
micky3879b9f5e72025-07-08 18:04:53 -0400221 case OPTS_VERSION:
222 show_version(argv);
223 ExitProgram(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100224 default:
micky3879b9f5e72025-07-08 18:04:53 -0400225 usage(ch == OPTS_USAGE);
226 /* NOTREACHED */
Steve Kondikae271bc2015-11-15 02:50:53 +0100227 }
228 }
229 if (!test_normal && !test_optimize) {
230 test_normal = TRUE;
231 test_optimize = TRUE;
232 }
233#if USE_TRACE
micky3879b9f5e72025-07-08 18:04:53 -0400234 curses_trace(TRACE_TIMES);
Steve Kondikae271bc2015-11-15 02:50:53 +0100235#endif
236
micky3879b9f5e72025-07-08 18:04:53 -0400237 InitAndCatch(initscr(), finish);
Steve Kondikae271bc2015-11-15 02:50:53 +0100238 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 */