blob: 6e1b00b5785b7a55627608ec396741248c21b7ff [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
2 * Copyright (c) 1998-2007,2008 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 ****************************************************************************/
28/*
29 * hashtest.c -- test hash mapping
30 *
31 * Generate timing statistics for vertical-motion optimization.
32 *
33 * $Id: hashtest.c,v 1.29 2008/08/16 17:26:44 tom Exp $
34 */
35
36#include <test.priv.h>
37
38#define LO_CHAR ' '
39#define HI_CHAR '~'
40
41static bool continuous = FALSE;
42static bool reverse_loops = FALSE;
43static bool single_step = FALSE;
44static bool extend_corner = FALSE;
45static int foot_lines = 0;
46static int head_lines = 0;
47
48static void
49cleanup(void)
50{
51 move(LINES - 1, 0);
52 clrtoeol();
53 refresh();
54 endwin();
55}
56
57static RETSIGTYPE
58finish(int sig GCC_UNUSED)
59{
60 cleanup();
61 ExitProgram(EXIT_FAILURE);
62}
63
64static void
65genlines(int base)
66{
67 int i, j;
68
69#if USE_TRACE
70 if (base == 'a')
71 Trace(("Resetting screen"));
72 else
73 Trace(("Painting `%c' screen", base));
74#endif
75
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.
79 */
80 scrollok(stdscr, FALSE);
81
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) : '-'));
86
87 move(head_lines, 0);
88 for (i = head_lines; i < LINES - foot_lines; i++) {
89 chtype c = (base - LO_CHAR + i) % (HI_CHAR - LO_CHAR + 1) + LO_CHAR;
90 int hi = (extend_corner || (i < LINES - 1)) ? COLS : COLS - 1;
91 for (j = 0; j < hi; j++)
92 addch(c);
93 }
94
95 for (i = LINES - foot_lines; i < LINES; i++) {
96 move(i, 0);
97 for (j = 0; j < (extend_corner ? COLS : COLS - 1); j++)
98 addch(UChar((j % 8 == 0) ? ('A' + j / 8) : '-'));
99 }
100
101 scrollok(stdscr, TRUE);
102 if (single_step) {
103 move(LINES - 1, 0);
104 getch();
105 } else
106 refresh();
107}
108
109static void
110one_cycle(int ch)
111{
112 if (continuous) {
113 genlines(ch);
114 } else if (ch != 'a') {
115 genlines('a');
116 genlines(ch);
117 }
118}
119
120static void
121run_test(bool optimized GCC_UNUSED)
122{
123 char ch;
124 int lo = continuous ? LO_CHAR : 'a' - LINES;
125 int hi = continuous ? HI_CHAR : 'a' + LINES;
126
127 if (lo < LO_CHAR)
128 lo = LO_CHAR;
129 if (hi > HI_CHAR)
130 hi = HI_CHAR;
131
132#if defined(TRACE) || defined(NCURSES_TEST)
133 if (optimized) {
134 Trace(("With hash mapping"));
135 _nc_optimize_enable |= OPTIMIZE_HASHMAP;
136 } else {
137 Trace(("Without hash mapping"));
138 _nc_optimize_enable &= ~OPTIMIZE_HASHMAP;
139 }
140#endif
141
142 if (reverse_loops)
143 for (ch = hi; ch >= lo; ch--)
144 one_cycle(ch);
145 else
146 for (ch = lo; ch <= hi; ch++)
147 one_cycle(ch);
148}
149
150static void
151usage(void)
152{
153 static const char *const tbl[] =
154 {
155 "Usage: hashtest [options]"
156 ,""
157 ,"Options:"
158 ," -c continuous (don't reset between refresh's)"
159 ," -f num leave 'num' lines constant for footer"
160 ," -h num leave 'num' lines constant for header"
161 ," -l num repeat test 'num' times"
162 ," -n test the normal optimizer"
163 ," -o test the hashed optimizer"
164 ," -r reverse the loops"
165 ," -s single-step"
166 ," -x assume lower-right corner extension"
167 };
168 size_t n;
169
170 for (n = 0; n < SIZEOF(tbl); n++)
171 fprintf(stderr, "%s\n", tbl[n]);
172 ExitProgram(EXIT_FAILURE);
173}
174
175int
176main(int argc, char *argv[])
177{
178 int c;
179 int test_loops = 1;
180 int test_normal = FALSE;
181 int test_optimize = FALSE;
182
183 setlocale(LC_ALL, "");
184
185 while ((c = getopt(argc, argv, "cf:h:l:norsx")) != -1) {
186 switch (c) {
187 case 'c':
188 continuous = TRUE;
189 break;
190 case 'f':
191 foot_lines = atoi(optarg);
192 break;
193 case 'h':
194 head_lines = atoi(optarg);
195 break;
196 case 'l':
197 test_loops = atoi(optarg);
198 assert(test_loops >= 0);
199 break;
200 case 'n':
201 test_normal = TRUE;
202 break;
203 case 'o':
204 test_optimize = TRUE;
205 break;
206 case 'r':
207 reverse_loops = TRUE;
208 break;
209 case 's':
210 single_step = TRUE;
211 break;
212 case 'x':
213 extend_corner = TRUE;
214 break;
215 default:
216 usage();
217 }
218 }
219 if (!test_normal && !test_optimize) {
220 test_normal = TRUE;
221 test_optimize = TRUE;
222 }
223#if USE_TRACE
224 trace(TRACE_TIMES);
225#endif
226
227 CATCHALL(finish); /* arrange interrupts to terminate */
228
229 (void) initscr(); /* initialize the curses library */
230 keypad(stdscr, TRUE); /* enable keyboard mapping */
231 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
232 (void) cbreak(); /* take input chars one at a time, no wait for \n */
233 (void) noecho(); /* don't echo input */
234 scrollok(stdscr, TRUE);
235
236 while (test_loops-- > 0) {
237 if (test_normal)
238 run_test(FALSE);
239 if (test_optimize)
240 run_test(TRUE);
241 }
242
243 cleanup(); /* we're done */
244 ExitProgram(EXIT_SUCCESS);
245}
246/* hashtest.c ends here */