1 /****************************************************************************
2 * Copyright 2018-2022,2023 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.22 2023/02/25 16:43:56 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;
60 static TimeType initial_time;
61 static TimeType finish_time;
63 static GCC_NORETURN void
71 failed(const char *msg)
79 #if USE_EXTENDED_COLOR
81 InitPair(int pair, int fg, int bg)
85 rc = init_extended_pair(pair, fg, bg);
87 rc = init_pair((NCURSES_PAIRS_T) pair,
89 (NCURSES_COLOR_T) bg);
95 PairContent(int pair, int *fgp, int *bgp)
99 rc = extended_pair_content(pair, fgp, bgp);
102 if ((rc = pair_content((short) pair, &fg, &bg)) == OK) {
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)
117 return (my_color_t) (rand() % COLORS);
123 setlocale(LC_ALL, "");
127 scrollok(stdscr, TRUE);
136 failed("color-pair limit must be greater than one");
140 size_t need = (size_t) ((l_opt > COLOR_PAIRS)
144 expected = typeCalloc(MYPAIR, need);
149 for (fg = 0; fg < COLORS; ++fg) {
150 for (bg = 0; bg < COLORS; ++bg) {
152 InitPair(pair, fg, bg);
153 expected[pair].fg = (my_color_t) fg;
154 expected[pair].bg = (my_color_t) bg;
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);
170 failed("This demo requires a color terminal");
172 GetClockTime(&initial_time);
180 for (pair = 1; pair < l_opt; ++pair) {
183 if (PairContent(pair, &fg, &bg) == OK) {
185 if (fg != expected[pair].fg)
187 if (bg != expected[pair].bg)
193 addch(success ? '.' : '?');
208 static const char *msg[] =
210 "Usage: pair_content [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"
226 for (n = 0; n < SIZEOF(msg); n++)
227 fprintf(stderr, "%s\n", msg[n]);
228 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
235 main(int argc, char *argv[])
239 while ((ch = getopt(argc, argv, OPTS_COMMON "f:il:npr:sx")) != -1) {
242 if ((f_opt = atoi(optarg)) <= 0)
249 if ((l_opt = atoi(optarg)) <= 0)
259 if ((r_opt = atoi(optarg)) <= 0)
265 #if USE_EXTENDED_COLOR
272 ExitProgram(EXIT_SUCCESS);
274 usage(ch == OPTS_USAGE);
287 for (i = f_opt; i < l_opt; ++i) {
289 if (PairContent(i, &fg, &bg) == OK) {
290 printf("%d: %d %d\n", i, fg, bg);
292 printf("%d: ? ?\n", i);
298 for (repeat = 0; repeat < r_opt; ++repeat) {
310 GetClockTime(&finish_time);
311 printw("%.03f seconds", ElapsedSeconds(&initial_time, &finish_time));
315 finish(EXIT_SUCCESS);