]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/color_content.c
ncurses 6.1 - patch 20181229
[ncurses.git] / test / color_content.c
1 /****************************************************************************
2  * Copyright (c) 2018 Free Software Foundation, Inc.                        *
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: color_content.c,v 1.2 2018/12/30 00:52:58 tom Exp $
30  */
31
32 #define NEED_TIME_H
33 #include <test.priv.h>
34
35 typedef struct {
36     NCURSES_COLOR_T r;
37     NCURSES_COLOR_T g;
38     NCURSES_COLOR_T b;
39 } MYCOLOR;
40
41 static int i_opt;
42 static int l_opt;
43 static int n_opt;
44 static int r_opt;
45 static int s_opt;
46
47 static MYCOLOR *expected;
48
49 #if HAVE_GETTIMEOFDAY
50 static struct timeval initial_time;
51 static struct timeval finish_time;
52 #endif
53
54 static void
55 failed(const char *msg)
56 {
57     printw("%s", msg);
58     getch();
59     endwin();
60     ExitProgram(EXIT_FAILURE);
61 }
62
63 static NCURSES_COLOR_T
64 random_color(void)
65 {
66     return (NCURSES_COLOR_T) (rand() % 1000);
67 }
68
69 static void
70 setup_test(void)
71 {
72     initscr();
73     cbreak();
74     noecho();
75     scrollok(stdscr, TRUE);
76     if (has_colors()) {
77         start_color();
78         if (!can_change_color())
79             failed("this terminal cannot initialize colors");
80
81         if (!l_opt)
82             l_opt = COLORS;
83         if (l_opt <= 0)
84             failed("color limit must be greater than zero");
85
86         if (!n_opt) {
87             NCURSES_PAIRS_T color;
88
89             expected = typeCalloc(MYCOLOR, l_opt);
90             if (s_opt) {
91                 NCURSES_COLOR_T r;
92                 NCURSES_COLOR_T g;
93                 NCURSES_COLOR_T b;
94                 color = 0;
95                 for (r = 0; r < 1000; ++r) {
96                     for (g = 0; g < 1000; ++g) {
97                         for (b = 0; b < 1000; ++b) {
98                             if (color < l_opt) {
99                                 init_color(color, r, g, b);
100                                 expected[color].r = r;
101                                 expected[color].g = g;
102                                 expected[color].b = b;
103                                 ++color;
104                             } else {
105                                 break;
106                             }
107                         }
108                     }
109                 }
110             } else {
111                 for (color = 1; color < l_opt; ++color) {
112                     expected[color].r = random_color();
113                     expected[color].g = random_color();
114                     expected[color].b = random_color();
115                     init_color(color, expected[color].r, expected[color].g,
116                                expected[color].b);
117                 }
118             }
119         }
120     } else {
121         failed("This demo requires a color terminal");
122     }
123 #if HAVE_GETTIMEOFDAY
124     gettimeofday(&initial_time, 0);
125 #endif
126 }
127
128 static void
129 run_test(void)
130 {
131     NCURSES_PAIRS_T color;
132     bool success = TRUE;
133     for (color = 0; color < l_opt; ++color) {
134         NCURSES_COLOR_T r;
135         NCURSES_COLOR_T g;
136         NCURSES_COLOR_T b;
137         if (color_content(color, &r, &g, &b) == OK) {
138             if (expected != 0) {
139                 if (r != expected[color].r)
140                     success = FALSE;
141                 if (g != expected[color].g)
142                     success = FALSE;
143                 if (b != expected[color].b)
144                     success = FALSE;
145             }
146         }
147     }
148     if (i_opt) {
149         addch(success ? '.' : '?');
150         refresh();
151     }
152 }
153
154 static void
155 finish_test(void)
156 {
157     getch();
158     endwin();
159 }
160
161 #if HAVE_GETTIMEOFDAY
162 static double
163 seconds(struct timeval *mark)
164 {
165     double result = (double) mark->tv_sec;
166     result += ((double) mark->tv_usec / 1e6);
167     return result;
168 }
169 #endif
170
171 static void
172 usage(void)
173 {
174     static const char *msg[] =
175     {
176         "Usage: pair_content [options]"
177         ,""
178         ,"Options:"
179         ," -i       interactive, showing test-progress"
180         ," -l NUM   test NUM color pairs, rather than terminal description"
181         ," -n       do not initialize color pairs"
182         ," -r COUNT repeat for given count"
183         ," -s       initialize pairs sequentially rather than random"
184     };
185     size_t n;
186     for (n = 0; n < SIZEOF(msg); n++)
187         fprintf(stderr, "%s\n", msg[n]);
188     ExitProgram(EXIT_FAILURE);
189 }
190
191 int
192 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
193 {
194     int i;
195     int repeat;
196
197     while ((i = getopt(argc, argv, "il:nr:s")) != -1) {
198         switch (i) {
199         case 'i':
200             i_opt = 1;
201             break;
202         case 'l':
203             if ((l_opt = atoi(optarg)) <= 0)
204                 usage();
205             break;
206         case 'n':
207             n_opt = 1;
208             break;
209         case 'r':
210             if ((r_opt = atoi(optarg)) <= 0)
211                 usage();
212             break;
213         case 's':
214             s_opt = 1;
215             break;
216         default:
217             usage();
218         }
219     }
220     if (optind < argc)
221         usage();
222     if (r_opt <= 0)
223         r_opt = 1;
224
225     setup_test();
226
227     for (repeat = 0; repeat < r_opt; ++repeat) {
228         run_test();
229         if (i_opt) {
230             addch('.');
231             refresh();
232         }
233     }
234
235     if (i_opt) {
236         addch('\n');
237     }
238     printw("DONE: ");
239 #if HAVE_GETTIMEOFDAY
240     gettimeofday(&finish_time, 0);
241     printw("%.03f seconds",
242            seconds(&finish_time)
243            - seconds(&initial_time));
244 #endif
245     finish_test();
246
247     ExitProgram(EXIT_SUCCESS);
248 }