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