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