Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / gnulib-tests / test-term-ostream.c
1 /* Test for the term-ostream API.  */
2
3 #include <config.h>
4
5 #include "term-ostream.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9
10 int
11 main ()
12 {
13   static struct { const char *name; term_color_t c; int r; int g; int b; }
14          colors[] =
15     {
16       { "black",   -2,   0,   0,   0 },
17       { "blue",    -2,   0,   0, 255 },
18       { "green",   -2,   0, 255,   0 },
19       { "cyan",    -2,   0, 255, 255 },
20       { "red",     -2, 255,   0,   0 },
21       { "magenta", -2, 255,   0, 255 },
22       { "yellow",  -2, 255, 255,   0 },
23       { "white",   -2, 255, 255, 255 },
24       { "default", COLOR_DEFAULT }
25     };
26   term_ostream_t stream;
27   int i, row, col;
28
29   stream = term_ostream_create (1, "stdout");
30
31   for (i = 0; i < 8; i++)
32     colors[i].c =
33       term_ostream_rgb_to_color (stream, colors[i].r, colors[i].g, colors[i].b);
34
35   ostream_write_str (stream, "Colors (foreground/background):\n");
36   ostream_write_str (stream, "       ");
37   for (col = 0; col <= 8; col++)
38     {
39       const char *name = colors[col].name;
40       ostream_write_str (stream, "|");
41       ostream_write_str (stream, name);
42       ostream_write_mem (stream, "        ", 7 - strlen (name));
43     }
44   ostream_write_str (stream, "\n");
45   for (row = 0; row <= 8; row++)
46     {
47       const char *name = colors[row].name;
48       ostream_write_str (stream, name);
49       ostream_write_mem (stream, "        ", 7 - strlen (name));
50       for (col = 0; col <= 8; col++)
51         {
52           term_color_t row_color = colors[row].c;
53           term_color_t col_color = colors[col].c;
54
55           ostream_write_str (stream, "|");
56           term_ostream_set_color (stream, row_color);
57           term_ostream_set_bgcolor (stream, col_color);
58           if (!(term_ostream_get_color (stream) == row_color
59                 && term_ostream_get_bgcolor (stream) == col_color))
60             abort ();
61           ostream_write_str (stream, " Words ");
62           term_ostream_set_color (stream, COLOR_DEFAULT);
63           term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
64           if (!(term_ostream_get_color (stream) == COLOR_DEFAULT
65                 && term_ostream_get_bgcolor (stream) == COLOR_DEFAULT))
66             abort ();
67         }
68       ostream_write_str (stream, "\n");
69     }
70   ostream_write_str (stream, "\n");
71
72   ostream_write_str (stream, "Colors (hue/saturation):\n");
73   /* Hue from 0 to 1.  */
74   for (row = 0; row <= 17; row++)
75     {
76       for (col = 0; col <= 64; col++)
77         {
78           int r = 255;
79           int b = (int) (255.0f / 64.0f * col + 0.5f);
80           int g = b + (int) (row / 17.0f * (r - b) + 0.5f);
81           term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
82           term_ostream_set_bgcolor (stream, c);
83           ostream_write_str (stream, " ");
84           term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
85         }
86       ostream_write_str (stream, "\n");
87     }
88   /* Hue from 1 to 2.  */
89   for (row = 17; row >= 0; row--)
90     {
91       for (col = 0; col <= 64; col++)
92         {
93           int g = 255;
94           int b = (int) (255.0f / 64.0f * col + 0.5f);
95           int r = b + (int) (row / 17.0f * (g - b) + 0.5f);
96           term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
97           term_ostream_set_bgcolor (stream, c);
98           ostream_write_str (stream, " ");
99           term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
100         }
101       ostream_write_str (stream, "\n");
102     }
103   /* Hue from 2 to 3.  */
104   for (row = 0; row <= 17; row++)
105     {
106       for (col = 0; col <= 64; col++)
107         {
108           int g = 255;
109           int r = (int) (255.0f / 64.0f * col + 0.5f);
110           int b = r + (int) (row / 17.0f * (g - r) + 0.5f);
111           term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
112           term_ostream_set_bgcolor (stream, c);
113           ostream_write_str (stream, " ");
114           term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
115         }
116       ostream_write_str (stream, "\n");
117     }
118   /* Hue from 3 to 4.  */
119   for (row = 17; row >= 0; row--)
120     {
121       for (col = 0; col <= 64; col++)
122         {
123           int b = 255;
124           int r = (int) (255.0f / 64.0f * col + 0.5f);
125           int g = r + (int) (row / 17.0f * (b - r) + 0.5f);
126           term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
127           term_ostream_set_bgcolor (stream, c);
128           ostream_write_str (stream, " ");
129           term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
130         }
131       ostream_write_str (stream, "\n");
132     }
133   /* Hue from 4 to 5.  */
134   for (row = 0; row <= 17; row++)
135     {
136       for (col = 0; col <= 64; col++)
137         {
138           int b = 255;
139           int g = (int) (255.0f / 64.0f * col + 0.5f);
140           int r = g + (int) (row / 17.0f * (b - g) + 0.5f);
141           term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
142           term_ostream_set_bgcolor (stream, c);
143           ostream_write_str (stream, " ");
144           term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
145         }
146       ostream_write_str (stream, "\n");
147     }
148   /* Hue from 5 to 6.  */
149   for (row = 17; row >= 0; row--)
150     {
151       for (col = 0; col <= 64; col++)
152         {
153           int r = 255;
154           int g = (int) (255.0f / 64.0f * col + 0.5f);
155           int b = g + (int) (row / 17.0f * (r - g) + 0.5f);
156           term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
157           term_ostream_set_bgcolor (stream, c);
158           ostream_write_str (stream, " ");
159           term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
160         }
161       ostream_write_str (stream, "\n");
162     }
163   ostream_write_str (stream, "\n");
164
165   ostream_write_str (stream, "Weights:\n");
166   term_ostream_set_weight (stream, WEIGHT_NORMAL);
167   if (term_ostream_get_weight (stream) != WEIGHT_NORMAL)
168     abort ();
169   ostream_write_str (stream, "normal, ");
170   term_ostream_set_weight (stream, WEIGHT_BOLD);
171   if (term_ostream_get_weight (stream) != WEIGHT_BOLD)
172     abort ();
173   ostream_write_str (stream, "bold, ");
174   term_ostream_set_weight (stream, WEIGHT_DEFAULT);
175   if (term_ostream_get_weight (stream) != WEIGHT_DEFAULT)
176     abort ();
177   ostream_write_str (stream, "default \n");
178   ostream_write_str (stream, "\n");
179
180   ostream_write_str (stream, "Postures:\n");
181   term_ostream_set_posture (stream, POSTURE_NORMAL);
182   if (term_ostream_get_posture (stream) != POSTURE_NORMAL)
183     abort ();
184   ostream_write_str (stream, "normal, ");
185   term_ostream_set_posture (stream, POSTURE_ITALIC);
186   if (term_ostream_get_posture (stream) != POSTURE_ITALIC)
187     abort ();
188   ostream_write_str (stream, "italic, ");
189   term_ostream_set_posture (stream, POSTURE_DEFAULT);
190   if (term_ostream_get_posture (stream) != POSTURE_DEFAULT)
191     abort ();
192   ostream_write_str (stream, "default \n");
193   ostream_write_str (stream, "\n");
194
195   ostream_write_str (stream, "Text decorations:\n");
196   term_ostream_set_underline (stream, UNDERLINE_OFF);
197   if (term_ostream_get_underline (stream) != UNDERLINE_OFF)
198     abort ();
199   ostream_write_str (stream, "normal, ");
200   term_ostream_set_underline (stream, UNDERLINE_ON);
201   if (term_ostream_get_underline (stream) != UNDERLINE_ON)
202     abort ();
203   ostream_write_str (stream, "underlined, ");
204   term_ostream_set_underline (stream, UNDERLINE_DEFAULT);
205   if (term_ostream_get_underline (stream) != UNDERLINE_DEFAULT)
206     abort ();
207   ostream_write_str (stream, "default \n");
208   ostream_write_str (stream, "\n");
209
210   ostream_free (stream);
211
212   return 0;
213 }