]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/hashtest.c
ncurses 6.2 - patch 20200308
[ncurses.git] / test / hashtest.c
1 /****************************************************************************
2  * Copyright 2019,2020 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.36 2020/02/02 23:34:34 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(void)
154 {
155     static const char *const tbl[] =
156     {
157         "Usage: hashtest [options]"
158         ,""
159         ,"Options:"
160         ,"  -c      continuous (don't reset between refresh's)"
161         ,"  -f num  leave 'num' lines constant for footer"
162         ,"  -h num  leave 'num' lines constant for header"
163         ,"  -l num  repeat test 'num' times"
164         ,"  -n      test the normal optimizer"
165         ,"  -o      test the hashed optimizer"
166         ,"  -r      reverse the loops"
167         ,"  -s      single-step"
168         ,"  -x      assume lower-right corner extension"
169     };
170     size_t n;
171
172     for (n = 0; n < SIZEOF(tbl); n++)
173         fprintf(stderr, "%s\n", tbl[n]);
174     ExitProgram(EXIT_FAILURE);
175 }
176
177 int
178 main(int argc, char *argv[])
179 {
180     int c;
181     int test_loops = 1;
182     int test_normal = FALSE;
183     int test_optimize = FALSE;
184
185     setlocale(LC_ALL, "");
186
187     while ((c = getopt(argc, argv, "cf:h:l:norsx")) != -1) {
188         switch (c) {
189         case 'c':
190             continuous = TRUE;
191             break;
192         case 'f':
193             foot_lines = atoi(optarg);
194             break;
195         case 'h':
196             head_lines = atoi(optarg);
197             break;
198         case 'l':
199             test_loops = atoi(optarg);
200             assert(test_loops >= 0);
201             break;
202         case 'n':
203             test_normal = TRUE;
204             break;
205         case 'o':
206             test_optimize = TRUE;
207             break;
208         case 'r':
209             reverse_loops = TRUE;
210             break;
211         case 's':
212             single_step = TRUE;
213             break;
214         case 'x':
215             extend_corner = TRUE;
216             break;
217         default:
218             usage();
219         }
220     }
221     if (!test_normal && !test_optimize) {
222         test_normal = TRUE;
223         test_optimize = TRUE;
224     }
225 #if USE_TRACE
226     curses_trace(TRACE_TIMES);
227 #endif
228
229     InitAndCatch(initscr(), finish);
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 */