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