X-Git-Url: https://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=test%2Fpair_content.c;h=2b759a82466d486b14908f82f417b6a76578738e;hb=46503c9c062e855bb9bcdc61aab11844ed8e5e20;hp=4e1d0b882e4037509a07336525e1f67614db50ff;hpb=bfe3845eb1a2ff02a740e917b537e939ec4e44cb;p=ncurses.git diff --git a/test/pair_content.c b/test/pair_content.c index 4e1d0b88..2b759a82 100644 --- a/test/pair_content.c +++ b/test/pair_content.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2018 Free Software Foundation, Inc. * + * Copyright 2018-2022,2023 Thomas E. Dickey * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -26,29 +26,46 @@ * authorization. * ****************************************************************************/ /* - * $Id: pair_content.c,v 1.2 2018/12/30 00:54:48 tom Exp $ + * $Id: pair_content.c,v 1.22 2023/02/25 16:43:56 tom Exp $ */ #define NEED_TIME_H #include +#if USE_EXTENDED_COLOR +typedef int my_color_t; +#else +typedef NCURSES_COLOR_T my_color_t; +#endif + typedef struct { - NCURSES_COLOR_T fg; - NCURSES_COLOR_T bg; + my_color_t fg; + my_color_t bg; } MYPAIR; +static int f_opt; static int i_opt; static int l_opt; static int n_opt; +static int p_opt; static int r_opt; static int s_opt; +#if USE_EXTENDED_COLOR +static int x_opt; +#endif + static MYPAIR *expected; -#if HAVE_GETTIMEOFDAY -static struct timeval initial_time; -static struct timeval finish_time; -#endif +static TimeType initial_time; +static TimeType finish_time; + +static GCC_NORETURN void +finish(int code) +{ + free(expected); + ExitProgram(code); +} static void failed(const char *msg) @@ -56,18 +73,54 @@ failed(const char *msg) printw("%s", msg); getch(); endwin(); - ExitProgram(EXIT_FAILURE); + finish(EXIT_FAILURE); } -static NCURSES_COLOR_T +#if USE_EXTENDED_COLOR +static int +InitPair(int pair, int fg, int bg) +{ + int rc; + if (x_opt) { + rc = init_extended_pair(pair, fg, bg); + } else { + rc = init_pair((NCURSES_PAIRS_T) pair, + (NCURSES_COLOR_T) fg, + (NCURSES_COLOR_T) bg); + } + return rc; +} + +static int +PairContent(int pair, int *fgp, int *bgp) +{ + int rc; + if (x_opt) { + rc = extended_pair_content(pair, fgp, bgp); + } else { + short fg, bg; + if ((rc = pair_content((short) pair, &fg, &bg)) == OK) { + *fgp = fg; + *bgp = bg; + } + } + return rc; +} +#else +#define InitPair(pair,fg,bg) init_pair((NCURSES_COLOR_T)pair,(NCURSES_COLOR_T)fg,(NCURSES_COLOR_T)bg) +#define PairContent(pair,fgp,bgp) pair_content((NCURSES_PAIRS_T)pair,fgp,bgp) +#endif + +static my_color_t random_color(void) { - return (NCURSES_COLOR_T) (rand() % COLORS); + return (my_color_t) (rand() % COLORS); } static void setup_test(void) { + setlocale(LC_ALL, ""); initscr(); cbreak(); noecho(); @@ -75,25 +128,30 @@ setup_test(void) if (has_colors()) { start_color(); + if (!f_opt) + f_opt = 1; if (!l_opt) l_opt = COLOR_PAIRS; if (l_opt <= 1) failed("color-pair limit must be greater than one"); if (!n_opt) { - NCURSES_PAIRS_T pair; + int pair; + size_t need = (size_t) ((l_opt > COLOR_PAIRS) + ? l_opt + : COLOR_PAIRS) + 1; - expected = typeCalloc(MYPAIR, l_opt); + expected = typeCalloc(MYPAIR, need); if (s_opt) { - NCURSES_COLOR_T fg; - NCURSES_COLOR_T bg; - pair = 1; + my_color_t fg; + my_color_t bg; + pair = f_opt; for (fg = 0; fg < COLORS; ++fg) { for (bg = 0; bg < COLORS; ++bg) { if (pair < l_opt) { - init_pair(pair, fg, bg); - expected[pair].fg = fg; - expected[pair].bg = bg; + InitPair(pair, fg, bg); + expected[pair].fg = (my_color_t) fg; + expected[pair].bg = (my_color_t) bg; ++pair; } else { break; @@ -101,30 +159,28 @@ setup_test(void) } } } else { - for (pair = 1; (int) pair < l_opt; ++pair) { + for (pair = f_opt; pair < l_opt; ++pair) { expected[pair].fg = random_color(); expected[pair].bg = random_color(); - init_pair(pair, expected[pair].fg, expected[pair].bg); + InitPair(pair, expected[pair].fg, expected[pair].bg); } } } } else { failed("This demo requires a color terminal"); } -#if HAVE_GETTIMEOFDAY - gettimeofday(&initial_time, 0); -#endif + GetClockTime(&initial_time); } static void run_test(void) { - NCURSES_PAIRS_T pair; + int pair; bool success = TRUE; - for (pair = 1; (int) pair < l_opt; ++pair) { - NCURSES_COLOR_T fg; - NCURSES_COLOR_T bg; - if (pair_content(pair, &fg, &bg) == OK) { + for (pair = 1; pair < l_opt; ++pair) { + my_color_t fg; + my_color_t bg; + if (PairContent(pair, &fg, &bg) == OK) { if (expected != 0) { if (fg != expected[pair].fg) success = FALSE; @@ -146,91 +202,115 @@ finish_test(void) endwin(); } -#if HAVE_GETTIMEOFDAY -static double -seconds(struct timeval *mark) -{ - double result = (double) mark->tv_sec; - result += ((double) mark->tv_usec / 1e6); - return result; -} -#endif - static void -usage(void) +usage(int ok) { static const char *msg[] = { "Usage: pair_content [options]" ,"" + ,USAGE_COMMON ,"Options:" + ," -f PAIR first color pair to test (default: 1)" ," -i interactive, showing test-progress" - ," -l NUM test NUM color pairs, rather than terminal description" + ," -l PAIR last color pair to test (default: max_pairs-1)" ," -n do not initialize color pairs" + ," -p print data for color pairs instead of testing" ," -r COUNT repeat for given count" ," -s initialize pairs sequentially rather than random" +#if USE_EXTENDED_COLOR + ," -x use extended color pairs/values" +#endif }; size_t n; for (n = 0; n < SIZEOF(msg); n++) fprintf(stderr, "%s\n", msg[n]); - ExitProgram(EXIT_FAILURE); + ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); } +/* *INDENT-OFF* */ +VERSION_COMMON() +/* *INDENT-ON* */ int -main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) +main(int argc, char *argv[]) { - int i; - int repeat; + int ch; - while ((i = getopt(argc, argv, "il:nr:s")) != -1) { - switch (i) { + while ((ch = getopt(argc, argv, OPTS_COMMON "f:il:npr:sx")) != -1) { + switch (ch) { + case 'f': + if ((f_opt = atoi(optarg)) <= 0) + usage(FALSE); + break; case 'i': i_opt = 1; break; case 'l': if ((l_opt = atoi(optarg)) <= 0) - usage(); + usage(FALSE); break; case 'n': n_opt = 1; break; + case 'p': + p_opt = 1; + break; case 'r': if ((r_opt = atoi(optarg)) <= 0) - usage(); + usage(FALSE); break; case 's': s_opt = 1; break; +#if USE_EXTENDED_COLOR + case 'x': + x_opt = 1; + break; +#endif + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); default: - usage(); + usage(ch == OPTS_USAGE); + /* NOTREACHED */ } } if (optind < argc) - usage(); + usage(FALSE); if (r_opt <= 0) r_opt = 1; setup_test(); + if (p_opt) { + int i; + endwin(); + for (i = f_opt; i < l_opt; ++i) { + my_color_t fg, bg; + if (PairContent(i, &fg, &bg) == OK) { + printf("%d: %d %d\n", i, fg, bg); + } else { + printf("%d: ? ?\n", i); + } + } + } else { + int repeat; - for (repeat = 0; repeat < r_opt; ++repeat) { - run_test(); - if (i_opt) { - addch('.'); - refresh(); + for (repeat = 0; repeat < r_opt; ++repeat) { + run_test(); + if (i_opt) { + addch('.'); + refresh(); + } } - } - if (i_opt) { - addch('\n'); + if (i_opt) { + addch('\n'); + } + printw("DONE: "); + GetClockTime(&finish_time); + printw("%.03f seconds", ElapsedSeconds(&initial_time, &finish_time)); + finish_test(); } - printw("DONE: "); -#if HAVE_GETTIMEOFDAY - gettimeofday(&finish_time, 0); - printw("%.03f seconds", - seconds(&finish_time) - - seconds(&initial_time)); -#endif - finish_test(); - ExitProgram(EXIT_SUCCESS); + finish(EXIT_SUCCESS); }