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