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