1 /****************************************************************************
2 * Copyright 2018-2020,2022 Thomas E. Dickey *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: pair_content.c,v 1.15 2022/04/16 18:27:24 tom Exp $
33 #include <test.priv.h>
35 #if USE_EXTENDED_COLOR
36 typedef int my_color_t;
38 typedef NCURSES_COLOR_T my_color_t;
54 #if USE_EXTENDED_COLOR
58 static MYPAIR *expected;
61 static struct timeval initial_time;
62 static struct timeval finish_time;
66 failed(const char *msg)
71 ExitProgram(EXIT_FAILURE);
74 #if USE_EXTENDED_COLOR
76 InitPair(int pair, int fg, int bg)
80 rc = init_extended_pair(pair, fg, bg);
82 rc = init_pair((NCURSES_PAIRS_T) pair,
84 (NCURSES_COLOR_T) bg);
90 PairContent(int pair, int *fgp, int *bgp)
94 rc = extended_pair_content(pair, fgp, bgp);
97 if ((rc = pair_content((short) pair, &fg, &bg)) == OK) {
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)
112 return (my_color_t) (rand() % COLORS);
118 setlocale(LC_ALL, "");
122 scrollok(stdscr, TRUE);
131 failed("color-pair limit must be greater than one");
135 size_t need = (size_t) ((l_opt > COLOR_PAIRS)
139 expected = typeCalloc(MYPAIR, need);
144 for (fg = 0; fg < COLORS; ++fg) {
145 for (bg = 0; bg < COLORS; ++bg) {
147 InitPair(pair, fg, bg);
148 expected[pair].fg = (my_color_t) fg;
149 expected[pair].bg = (my_color_t) bg;
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);
165 failed("This demo requires a color terminal");
167 #if HAVE_GETTIMEOFDAY
168 gettimeofday(&initial_time, 0);
177 for (pair = 1; pair < l_opt; ++pair) {
180 if (PairContent(pair, &fg, &bg) == OK) {
182 if (fg != expected[pair].fg)
184 if (bg != expected[pair].bg)
190 addch(success ? '.' : '?');
202 #if HAVE_GETTIMEOFDAY
204 seconds(struct timeval *mark)
206 double result = (double) mark->tv_sec;
207 result += ((double) mark->tv_usec / 1e6);
215 static const char *msg[] =
217 "Usage: pair_content [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"
232 for (n = 0; n < SIZEOF(msg); n++)
233 fprintf(stderr, "%s\n", msg[n]);
234 ExitProgram(EXIT_FAILURE);
238 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
242 while ((i = getopt(argc, argv, "f:il:npr:sx")) != -1) {
245 if ((f_opt = atoi(optarg)) <= 0)
252 if ((l_opt = atoi(optarg)) <= 0)
262 if ((r_opt = atoi(optarg)) <= 0)
268 #if USE_EXTENDED_COLOR
285 for (i = f_opt; i < l_opt; ++i) {
287 if (PairContent(i, &fg, &bg) == OK) {
288 printf("%d: %d %d\n", i, fg, bg);
290 printf("%d: ? ?\n", i);
296 for (repeat = 0; repeat < r_opt; ++repeat) {
308 #if HAVE_GETTIMEOFDAY
309 gettimeofday(&finish_time, 0);
310 printw("%.03f seconds",
311 seconds(&finish_time)
312 - seconds(&initial_time));
317 ExitProgram(EXIT_SUCCESS);