]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/pair_content.c
ncurses 6.3 - patch 20220416
[ncurses.git] / test / pair_content.c
1 /****************************************************************************
2  * Copyright 2018-2020,2022 Thomas E. Dickey                                *
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  * $Id: pair_content.c,v 1.15 2022/04/16 18:27:24 tom Exp $
30  */
31
32 #define NEED_TIME_H
33 #include <test.priv.h>
34
35 #if USE_EXTENDED_COLOR
36 typedef int my_color_t;
37 #else
38 typedef NCURSES_COLOR_T my_color_t;
39 #endif
40
41 typedef struct {
42     my_color_t fg;
43     my_color_t bg;
44 } MYPAIR;
45
46 static int f_opt;
47 static int i_opt;
48 static int l_opt;
49 static int n_opt;
50 static int p_opt;
51 static int r_opt;
52 static int s_opt;
53
54 #if USE_EXTENDED_COLOR
55 static int x_opt;
56 #endif
57
58 static MYPAIR *expected;
59
60 #if HAVE_GETTIMEOFDAY
61 static struct timeval initial_time;
62 static struct timeval finish_time;
63 #endif
64
65 static void
66 failed(const char *msg)
67 {
68     printw("%s", msg);
69     getch();
70     endwin();
71     ExitProgram(EXIT_FAILURE);
72 }
73
74 #if USE_EXTENDED_COLOR
75 static int
76 InitPair(int pair, int fg, int bg)
77 {
78     int rc;
79     if (x_opt) {
80         rc = init_extended_pair(pair, fg, bg);
81     } else {
82         rc = init_pair((NCURSES_PAIRS_T) pair,
83                        (NCURSES_COLOR_T) fg,
84                        (NCURSES_COLOR_T) bg);
85     }
86     return rc;
87 }
88
89 static int
90 PairContent(int pair, int *fgp, int *bgp)
91 {
92     int rc;
93     if (x_opt) {
94         rc = extended_pair_content(pair, fgp, bgp);
95     } else {
96         short fg, bg;
97         if ((rc = pair_content((short) pair, &fg, &bg)) == OK) {
98             *fgp = fg;
99             *bgp = bg;
100         }
101     }
102     return rc;
103 }
104 #else
105 #define InitPair(pair,fg,bg)      init_pair((NCURSES_COLOR_T)pair,(NCURSES_COLOR_T)fg,(NCURSES_COLOR_T)bg)
106 #define PairContent(pair,fgp,bgp) pair_content((NCURSES_PAIRS_T)pair,fgp,bgp)
107 #endif
108
109 static my_color_t
110 random_color(void)
111 {
112     return (my_color_t) (rand() % COLORS);
113 }
114
115 static void
116 setup_test(void)
117 {
118     setlocale(LC_ALL, "");
119     initscr();
120     cbreak();
121     noecho();
122     scrollok(stdscr, TRUE);
123     if (has_colors()) {
124         start_color();
125
126         if (!f_opt)
127             f_opt = 1;
128         if (!l_opt)
129             l_opt = COLOR_PAIRS;
130         if (l_opt <= 1)
131             failed("color-pair limit must be greater than one");
132
133         if (!n_opt) {
134             int pair;
135             size_t need = (size_t) ((l_opt > COLOR_PAIRS)
136                                     ? l_opt
137                                     : COLOR_PAIRS) + 1;
138
139             expected = typeCalloc(MYPAIR, need);
140             if (s_opt) {
141                 my_color_t fg;
142                 my_color_t bg;
143                 pair = f_opt;
144                 for (fg = 0; fg < COLORS; ++fg) {
145                     for (bg = 0; bg < COLORS; ++bg) {
146                         if (pair < l_opt) {
147                             InitPair(pair, fg, bg);
148                             expected[pair].fg = (my_color_t) fg;
149                             expected[pair].bg = (my_color_t) bg;
150                             ++pair;
151                         } else {
152                             break;
153                         }
154                     }
155                 }
156             } else {
157                 for (pair = f_opt; pair < l_opt; ++pair) {
158                     expected[pair].fg = random_color();
159                     expected[pair].bg = random_color();
160                     InitPair(pair, expected[pair].fg, expected[pair].bg);
161                 }
162             }
163         }
164     } else {
165         failed("This demo requires a color terminal");
166     }
167 #if HAVE_GETTIMEOFDAY
168     gettimeofday(&initial_time, 0);
169 #endif
170 }
171
172 static void
173 run_test(void)
174 {
175     int pair;
176     bool success = TRUE;
177     for (pair = 1; pair < l_opt; ++pair) {
178         my_color_t fg;
179         my_color_t bg;
180         if (PairContent(pair, &fg, &bg) == OK) {
181             if (expected != 0) {
182                 if (fg != expected[pair].fg)
183                     success = FALSE;
184                 if (bg != expected[pair].bg)
185                     success = FALSE;
186             }
187         }
188     }
189     if (i_opt) {
190         addch(success ? '.' : '?');
191         refresh();
192     }
193 }
194
195 static void
196 finish_test(void)
197 {
198     getch();
199     endwin();
200 }
201
202 #if HAVE_GETTIMEOFDAY
203 static double
204 seconds(struct timeval *mark)
205 {
206     double result = (double) mark->tv_sec;
207     result += ((double) mark->tv_usec / 1e6);
208     return result;
209 }
210 #endif
211
212 static void
213 usage(void)
214 {
215     static const char *msg[] =
216     {
217         "Usage: pair_content [options]"
218         ,""
219         ,"Options:"
220         ," -f PAIR  first color pair to test (default: 1)"
221         ," -i       interactive, showing test-progress"
222         ," -l PAIR  last color pair to test (default: max_pairs-1)"
223         ," -n       do not initialize color pairs"
224         ," -p       print data for color pairs instead of testing"
225         ," -r COUNT repeat for given count"
226         ," -s       initialize pairs sequentially rather than random"
227 #if USE_EXTENDED_COLOR
228         ," -x       use extended color pairs/values"
229 #endif
230     };
231     size_t n;
232     for (n = 0; n < SIZEOF(msg); n++)
233         fprintf(stderr, "%s\n", msg[n]);
234     ExitProgram(EXIT_FAILURE);
235 }
236
237 int
238 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
239 {
240     int i;
241
242     while ((i = getopt(argc, argv, "f:il:npr:sx")) != -1) {
243         switch (i) {
244         case 'f':
245             if ((f_opt = atoi(optarg)) <= 0)
246                 usage();
247             break;
248         case 'i':
249             i_opt = 1;
250             break;
251         case 'l':
252             if ((l_opt = atoi(optarg)) <= 0)
253                 usage();
254             break;
255         case 'n':
256             n_opt = 1;
257             break;
258         case 'p':
259             p_opt = 1;
260             break;
261         case 'r':
262             if ((r_opt = atoi(optarg)) <= 0)
263                 usage();
264             break;
265         case 's':
266             s_opt = 1;
267             break;
268 #if USE_EXTENDED_COLOR
269         case 'x':
270             x_opt = 1;
271             break;
272 #endif
273         default:
274             usage();
275         }
276     }
277     if (optind < argc)
278         usage();
279     if (r_opt <= 0)
280         r_opt = 1;
281
282     setup_test();
283     if (p_opt) {
284         endwin();
285         for (i = f_opt; i < l_opt; ++i) {
286             my_color_t fg, bg;
287             if (PairContent(i, &fg, &bg) == OK) {
288                 printf("%d: %d %d\n", i, fg, bg);
289             } else {
290                 printf("%d: ? ?\n", i);
291             }
292         }
293     } else {
294         int repeat;
295
296         for (repeat = 0; repeat < r_opt; ++repeat) {
297             run_test();
298             if (i_opt) {
299                 addch('.');
300                 refresh();
301             }
302         }
303
304         if (i_opt) {
305             addch('\n');
306         }
307         printw("DONE: ");
308 #if HAVE_GETTIMEOFDAY
309         gettimeofday(&finish_time, 0);
310         printw("%.03f seconds",
311                seconds(&finish_time)
312                - seconds(&initial_time));
313 #endif
314         finish_test();
315     }
316
317     ExitProgram(EXIT_SUCCESS);
318 }