LCOV - code coverage report
Current view: top level - gcc/text-art - style.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.9 % 293 284
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 18 18
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Classes for styling text cells (color, URLs).
       2              :    Copyright (C) 2023-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #define INCLUDE_ALGORITHM
      23              : #define INCLUDE_VECTOR
      24              : #include "system.h"
      25              : #include "coretypes.h"
      26              : #include "pretty-print.h"
      27              : #include "intl.h"
      28              : #include "selftest.h"
      29              : #include "text-art/selftests.h"
      30              : #include "text-art/types.h"
      31              : #include "color-macros.h"
      32              : #include "diagnostics/color.h"
      33              : 
      34              : using namespace text_art;
      35              : 
      36              : /* class text_art::style.  */
      37              : 
      38              : style &
      39           24 : style::set_style_url (const char *url)
      40              : {
      41           24 :   m_url.clear ();
      42          672 :   while (*url)
      43          648 :     m_url.push_back (*(url++));
      44           24 :   return *this;
      45              : }
      46              : 
      47              : /* class text_art::style::color.  */
      48              : 
      49              : bool
      50       785184 : style::color::operator== (const style::color &other) const
      51              : {
      52       785184 :   if (m_kind != other.m_kind)
      53              :     return false;
      54       784216 :   switch (m_kind)
      55              :     {
      56            0 :     default:
      57            0 :       gcc_unreachable ();
      58       782866 :     case kind::NAMED:
      59       782866 :       return (u.m_named.m_name == other.u.m_named.m_name
      60       782866 :               && u.m_named.m_bright == other.u.m_named.m_bright);
      61            8 :     case kind::BITS_8:
      62            8 :       return u.m_8bit == other.u.m_8bit;
      63         1342 :     case kind::BITS_24:
      64         1342 :       return (u.m_24bit.r == other.u.m_24bit.r
      65          686 :               && u.m_24bit.g == other.u.m_24bit.g
      66         2028 :               && u.m_24bit.b == other.u.m_24bit.b);
      67              :     }
      68              : }
      69              : 
      70              : static void
      71          516 : ensure_separator (pretty_printer *pp, bool &need_separator)
      72              : {
      73            0 :   if (need_separator)
      74          112 :     pp_string (pp, COLOR_SEPARATOR);
      75          516 :   need_separator = true;
      76            0 : }
      77              : 
      78              : void
      79         1056 : style::color::print_sgr (pretty_printer *pp,
      80              :                          bool fg,
      81              :                          bool &need_separator) const
      82              : {
      83         1056 :   switch (m_kind)
      84              :     {
      85            0 :     default:
      86            0 :       gcc_unreachable ();
      87          808 :     case kind::NAMED:
      88          808 :       {
      89          808 :         static const char * const fg_normal[] = {"", // reset, for DEFAULT
      90              :                                                  COLOR_FG_BLACK,
      91              :                                                  COLOR_FG_RED,
      92              :                                                  COLOR_FG_GREEN,
      93              :                                                  COLOR_FG_YELLOW,
      94              :                                                  COLOR_FG_BLUE,
      95              :                                                  COLOR_FG_MAGENTA,
      96              :                                                  COLOR_FG_CYAN,
      97              :                                                  COLOR_FG_WHITE};
      98          808 :         static const char * const fg_bright[] = {"", // reset, for DEFAULT
      99              :                                                  COLOR_FG_BRIGHT_BLACK,
     100              :                                                  COLOR_FG_BRIGHT_RED,
     101              :                                                  COLOR_FG_BRIGHT_GREEN,
     102              :                                                  COLOR_FG_BRIGHT_YELLOW,
     103              :                                                  COLOR_FG_BRIGHT_BLUE,
     104              :                                                  COLOR_FG_BRIGHT_MAGENTA,
     105              :                                                  COLOR_FG_BRIGHT_CYAN,
     106              :                                                  COLOR_FG_BRIGHT_WHITE};
     107          808 :         static const char * const bg_normal[] = {"", // reset, for DEFAULT
     108              :                                                  COLOR_BG_BLACK,
     109              :                                                  COLOR_BG_RED,
     110              :                                                  COLOR_BG_GREEN,
     111              :                                                  COLOR_BG_YELLOW,
     112              :                                                  COLOR_BG_BLUE,
     113              :                                                  COLOR_BG_MAGENTA,
     114              :                                                  COLOR_BG_CYAN,
     115              :                                                  COLOR_BG_WHITE};
     116          808 :         static const char * const bg_bright[] = {"", // reset, for DEFAULT
     117              :                                                  COLOR_BG_BRIGHT_BLACK,
     118              :                                                  COLOR_BG_BRIGHT_RED,
     119              :                                                  COLOR_BG_BRIGHT_GREEN,
     120              :                                                  COLOR_BG_BRIGHT_YELLOW,
     121              :                                                  COLOR_BG_BRIGHT_BLUE,
     122              :                                                  COLOR_BG_BRIGHT_MAGENTA,
     123              :                                                  COLOR_BG_BRIGHT_CYAN,
     124              :                                                  COLOR_BG_BRIGHT_WHITE};
     125          808 :         STATIC_ASSERT (ARRAY_SIZE (fg_normal) == ARRAY_SIZE (fg_bright));
     126          808 :         STATIC_ASSERT (ARRAY_SIZE (fg_normal) == ARRAY_SIZE (bg_normal));
     127          808 :         STATIC_ASSERT (ARRAY_SIZE (fg_normal) == ARRAY_SIZE (bg_bright));
     128          808 :         gcc_assert ((size_t)u.m_named.m_name < ARRAY_SIZE (fg_normal));
     129          808 :         const char *const *arr;
     130          808 :         if (fg)
     131          436 :           arr = u.m_named.m_bright ? fg_bright : fg_normal;
     132              :         else
     133          372 :           arr = u.m_named.m_bright ? bg_bright : bg_normal;
     134          808 :         const char *str = arr[(size_t)u.m_named.m_name];
     135          808 :         if (strlen (str) > 0)
     136              :           {
     137          236 :             ensure_separator (pp, need_separator);
     138          236 :             pp_string (pp, str);
     139              :           }
     140              :       }
     141              :       break;
     142           48 :     case kind::BITS_8:
     143           48 :       {
     144           48 :         ensure_separator (pp, need_separator);
     145           48 :         pp_printf (pp, "%s;5;%i", fg ? "38" : "48", (int)u.m_8bit);
     146              :       }
     147           48 :       break;
     148          200 :     case kind::BITS_24:
     149          200 :       {
     150          200 :         ensure_separator (pp, need_separator);
     151          400 :         pp_printf (pp, "%s;2;%i;%i;%i",
     152              :                    fg ? "38" : "48",
     153          200 :                    (int)u.m_24bit.r,
     154          200 :                    (int)u.m_24bit.g,
     155          200 :                    (int)u.m_24bit.b);
     156              :       }
     157          200 :       break;
     158              :     }
     159         1056 : }
     160              : 
     161              : /* class text_art::style.  */
     162              : 
     163              : /* See https://www.ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf
     164              :    GRCM - GRAPHIC RENDITION COMBINATION MODE can be "REPLACING" or
     165              :    "CUMULATIVE", which affects whether we need to respecify all attributes
     166              :    at each SGR, or can accumulate them.  Looks like we can't rely on the value
     167              :    of this, so we have to emit a single SGR for all changes, with a "0" reset
     168              :    at the front, forcing it to be effectively replacing.  */
     169              : 
     170              : void
     171       310516 : style::print_changes (pretty_printer *pp,
     172              :                       const style &old_style,
     173              :                       const style &new_style)
     174              : {
     175       310516 :   if (pp_show_color (pp))
     176              :     {
     177          568 :       bool needs_sgr = ((old_style.m_bold != new_style.m_bold)
     178          520 :                         || (old_style.m_underscore != new_style.m_underscore)
     179          512 :                         || (old_style.m_blink != new_style.m_blink)
     180          504 :                         || (old_style.m_fg_color != new_style.m_fg_color)
     181          832 :                         || (old_style.m_bg_color != new_style.m_bg_color));
     182              :       if (needs_sgr)
     183              :         {
     184         1056 :           bool emit_reset = (old_style.m_bold
     185          504 :                              || new_style.m_bold
     186          480 :                              || old_style.m_underscore
     187          476 :                              || new_style.m_underscore
     188          472 :                              || old_style.m_blink
     189          996 :                              || new_style.m_blink);
     190          528 :           bool need_separator = false;
     191              : 
     192          528 :           pp_string (pp, SGR_START);
     193          528 :           if (emit_reset)
     194              :             {
     195           64 :               pp_string (pp, COLOR_NONE);
     196           64 :               need_separator = true;
     197              :             }
     198          528 :           if (new_style.m_bold)
     199              :             {
     200           24 :               gcc_assert (emit_reset);
     201           24 :               ensure_separator (pp, need_separator);
     202           24 :               pp_string (pp, COLOR_BOLD);
     203              :             }
     204          528 :           if (new_style.m_underscore)
     205              :             {
     206            4 :               gcc_assert (emit_reset);
     207            4 :               ensure_separator (pp, need_separator);
     208            4 :               pp_string (pp, COLOR_UNDERSCORE);
     209              :             }
     210          528 :           if (new_style.m_blink)
     211              :             {
     212            4 :               gcc_assert (emit_reset);
     213            4 :               ensure_separator (pp, need_separator);
     214            4 :               pp_string (pp, COLOR_BLINK);
     215              :             }
     216          528 :           new_style.m_fg_color.print_sgr (pp, true, need_separator);
     217          528 :           new_style.m_bg_color.print_sgr (pp, false, need_separator);
     218          528 :           pp_string (pp, SGR_END);
     219              :         }
     220              :     }
     221              : 
     222       310516 :   if (old_style.m_url != new_style.m_url)
     223              :     {
     224           48 :       if (!old_style.m_url.empty ())
     225           24 :         pp_end_url (pp);
     226           48 :       if (pp->supports_urls_p ()
     227           48 :           && !new_style.m_url.empty ())
     228              :         {
     229              :           /* Adapted from pp_begin_url, but encoding the
     230              :              chars to UTF-8 on the fly, rather than converting
     231              :              to a buffer.  */
     232           16 :           pp_string (pp, "\33]8;;");
     233          448 :           for (auto ch : new_style.m_url)
     234          432 :             pp_unicode_character (pp, ch);
     235           16 :           switch (pp->get_url_format ())
     236              :             {
     237            0 :             default:
     238            0 :             case URL_FORMAT_NONE:
     239            0 :               gcc_unreachable ();
     240            8 :             case URL_FORMAT_ST:
     241            8 :               pp_string (pp, "\33\\");
     242            8 :               break;
     243            8 :             case URL_FORMAT_BEL:
     244            8 :               pp_string (pp, "\a");
     245            8 :               break;
     246              :             }
     247              :         }
     248              :     }
     249       310516 : }
     250              : 
     251              : /* Look up the current SGR codes for a color capability NAME
     252              :    (from GCC_COLORS or the defaults), and convert them to
     253              :    a text_art::style.  */
     254              : 
     255              : style
     256        27967 : text_art::get_style_from_color_cap_name (const char *name)
     257              : {
     258        27967 :   const char *sgr_codes = colorize_start (true, name);
     259        27967 :   gcc_assert (sgr_codes);
     260              : 
     261              :   /* Parse the sgr codes.  We expect the resulting styled_string to be
     262              :      empty; we're interested in the final style created during parsing.  */
     263        27967 :   style_manager sm;
     264        27967 :   styled_string styled_str (sm, sgr_codes);
     265        27967 :   return sm.get_style (sm.get_num_styles () - 1);
     266        27967 : }
     267              : 
     268              : /* class text_art::style_manager.  */
     269              : 
     270        39924 : style_manager::style_manager ()
     271              : {
     272              :   // index 0 will be the default style
     273        39924 :   m_styles.push_back (style ());
     274        39924 : }
     275              : 
     276              : style::id_t
     277       281849 : style_manager::get_or_create_id (const style &s)
     278              : {
     279              :   // For now, linear search
     280       281849 :   std::vector<style>::iterator existing
     281       281849 :     (std::find (m_styles.begin (), m_styles.end (), s));
     282              : 
     283              :   /* If found, return index of slot.  */
     284       281849 :   if (existing != m_styles.end ())
     285       213458 :     return std::distance (m_styles.begin (), existing);
     286              : 
     287              :   /* Not found.  */
     288              : 
     289              :   /* styled_str uses 7 bits for style information, so we can only support
     290              :      up to 128 different style combinations.
     291              :      Gracefully fail by turning off styling when this limit is reached.  */
     292        68391 :   if (m_styles.size () >= 127)
     293              :     return 0;
     294              : 
     295        68391 :   m_styles.push_back (s);
     296        68391 :   return m_styles.size () - 1;
     297              : }
     298              : 
     299              : void
     300       341362 : style_manager::print_any_style_changes (pretty_printer *pp,
     301              :                                         style::id_t old_id,
     302              :                                         style::id_t new_id) const
     303              : {
     304       341362 :   gcc_assert (pp);
     305       341362 :   if (old_id == new_id)
     306              :     return;
     307              : 
     308       310292 :   const style &old_style = m_styles[old_id];
     309       310292 :   const style &new_style = m_styles[new_id];
     310       310292 :   gcc_assert (!(old_style == new_style));
     311       310292 :   style::print_changes (pp, old_style, new_style);
     312              : }
     313              : 
     314              : #if CHECKING_P
     315              : 
     316              : namespace selftest {
     317              : 
     318              : void
     319          224 : assert_style_change_streq (const location &loc,
     320              :                            const style &old_style,
     321              :                            const style &new_style,
     322              :                            const char *expected_str)
     323              : {
     324          224 :   pretty_printer pp;
     325          224 :   pp_show_color (&pp) = true;
     326          224 :   style::print_changes (&pp, old_style, new_style);
     327          224 :   ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected_str);
     328          224 : }
     329              : 
     330              : #define ASSERT_STYLE_CHANGE_STREQ(OLD_STYLE, NEW_STYLE, EXPECTED_STR) \
     331              :   SELFTEST_BEGIN_STMT                                                 \
     332              :     assert_style_change_streq ((SELFTEST_LOCATION),                   \
     333              :                                (OLD_STYLE),                           \
     334              :                                (NEW_STYLE),                           \
     335              :                                (EXPECTED_STR));                       \
     336              :   SELFTEST_END_STMT
     337              : 
     338              : static void
     339            4 : test_bold ()
     340              : {
     341            4 :   style_manager sm;
     342            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     343              : 
     344            4 :   style plain;
     345            4 :   ASSERT_EQ (sm.get_or_create_id (plain), 0);
     346            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     347              : 
     348            4 :   style bold;
     349            4 :   bold.m_bold = true;
     350              : 
     351            4 :   ASSERT_EQ (sm.get_or_create_id (bold), 1);
     352            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     353            4 :   ASSERT_EQ (sm.get_or_create_id (bold), 1);
     354            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     355              : 
     356            4 :   ASSERT_STYLE_CHANGE_STREQ (plain, bold, "\33[00;01m\33[K");
     357            4 :   ASSERT_STYLE_CHANGE_STREQ (bold, plain, "\33[00m\33[K");
     358            4 : }
     359              : 
     360              : static void
     361            4 : test_underscore ()
     362              : {
     363            4 :   style_manager sm;
     364            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     365              : 
     366            4 :   style plain;
     367            4 :   ASSERT_EQ (sm.get_or_create_id (plain), 0);
     368            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     369              : 
     370            4 :   style underscore;
     371            4 :   underscore.m_underscore = true;
     372              : 
     373            4 :   ASSERT_EQ (sm.get_or_create_id (underscore), 1);
     374            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     375            4 :   ASSERT_EQ (sm.get_or_create_id (underscore), 1);
     376            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     377              : 
     378            4 :   ASSERT_STYLE_CHANGE_STREQ (plain, underscore, "\33[00;04m\33[K");
     379            4 :   ASSERT_STYLE_CHANGE_STREQ (underscore, plain, "\33[00m\33[K");
     380            4 : }
     381              : 
     382              : static void
     383            4 : test_blink ()
     384              : {
     385            4 :   style_manager sm;
     386            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     387              : 
     388            4 :   style plain;
     389            4 :   ASSERT_EQ (sm.get_or_create_id (plain), 0);
     390            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     391              : 
     392            4 :   style blink;
     393            4 :   blink.m_blink = true;
     394              : 
     395            4 :   ASSERT_EQ (sm.get_or_create_id (blink), 1);
     396            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     397            4 :   ASSERT_EQ (sm.get_or_create_id (blink), 1);
     398            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     399              : 
     400            4 :   ASSERT_STYLE_CHANGE_STREQ (plain, blink, "\33[00;05m\33[K");
     401            4 :   ASSERT_STYLE_CHANGE_STREQ (blink, plain, "\33[00m\33[K");
     402            4 : }
     403              : 
     404              : #define ASSERT_NAMED_COL_STREQ(NAMED_COLOR, FG, BRIGHT, EXPECTED_STR) \
     405              :   SELFTEST_BEGIN_STMT                                                 \
     406              :   {                                                                   \
     407              :     style plain;                                                      \
     408              :     style s;                                                          \
     409              :     if (FG)                                                           \
     410              :       s.m_fg_color = style::color ((NAMED_COLOR), (BRIGHT));          \
     411              :     else                                                              \
     412              :       s.m_bg_color = style::color ((NAMED_COLOR), (BRIGHT));          \
     413              :     assert_style_change_streq ((SELFTEST_LOCATION),                   \
     414              :                                plain,                                 \
     415              :                                s,                                     \
     416              :                                (EXPECTED_STR));                       \
     417              :   }                                                                   \
     418              :   SELFTEST_END_STMT
     419              : 
     420              : static void
     421            4 : test_named_colors ()
     422              : {
     423              :   /* Foreground colors.  */
     424            4 :   {
     425            4 :     const bool fg = true;
     426            4 :     {
     427            4 :       const bool bright = false;
     428            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::DEFAULT, fg, bright, "");
     429            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLACK, fg, bright,
     430              :                               "");
     431            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::RED, fg, bright,
     432              :                               "");
     433            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::GREEN, fg, bright,
     434              :                               "");
     435            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::YELLOW, fg, bright,
     436              :                               "");
     437            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLUE, fg, bright,
     438              :                               "");
     439            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::MAGENTA, fg, bright,
     440              :                               "");
     441            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::CYAN, fg, bright,
     442              :                               "");
     443            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::WHITE, fg, bright,
     444              :                               "");
     445              :     }
     446            4 :     {
     447            4 :       const bool bright = true;
     448            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::DEFAULT, fg, bright,
     449              :                               "");
     450            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLACK, fg, bright,
     451              :                               "");
     452            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::RED, fg, bright,
     453              :                               "");
     454            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::GREEN, fg, bright,
     455              :                               "");
     456            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::YELLOW, fg, bright,
     457              :                               "");
     458            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLUE, fg, bright,
     459              :                               "");
     460            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::MAGENTA, fg, bright,
     461              :                               "");
     462            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::CYAN, fg, bright,
     463              :                               "");
     464            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::WHITE, fg, bright,
     465              :                               "");
     466              :     }
     467              :   }
     468              : 
     469              :   /* Background colors.  */
     470            4 :   {
     471            4 :     const bool fg = false;
     472            4 :     {
     473            4 :       const bool bright = false;
     474            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::DEFAULT, fg, bright, "");
     475            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLACK, fg, bright,
     476              :                               "");
     477            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::RED, fg, bright,
     478              :                               "");
     479            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::GREEN, fg, bright,
     480              :                               "");
     481            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::YELLOW, fg, bright,
     482              :                               "");
     483            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLUE, fg, bright,
     484              :                               "");
     485            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::MAGENTA, fg, bright,
     486              :                               "");
     487            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::CYAN, fg, bright,
     488              :                               "");
     489            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::WHITE, fg, bright,
     490              :                               "");
     491              :     }
     492            4 :     {
     493            4 :       const bool bright = true;
     494            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::DEFAULT, fg, bright,
     495              :                               "");
     496            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLACK, fg, bright,
     497              :                               "");
     498            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::RED, fg, bright,
     499              :                               "");
     500            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::GREEN, fg, bright,
     501              :                               "");
     502            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::YELLOW, fg, bright,
     503              :                               "");
     504            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::BLUE, fg, bright,
     505              :                               "");
     506            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::MAGENTA, fg, bright,
     507              :                               "");
     508            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::CYAN, fg, bright,
     509              :                               "");
     510            4 :       ASSERT_NAMED_COL_STREQ (style::named_color::WHITE, fg, bright,
     511              :                               "");
     512              :     }
     513              :   }
     514            4 : }
     515              : 
     516              : #define ASSERT_8_BIT_COL_STREQ(COL_VAL, FG, EXPECTED_STR) \
     517              :   SELFTEST_BEGIN_STMT                                                 \
     518              :   {                                                                   \
     519              :     style plain;                                                      \
     520              :     style s;                                                          \
     521              :     if (FG)                                                           \
     522              :       s.m_fg_color = style::color (COL_VAL);                          \
     523              :     else                                                              \
     524              :       s.m_bg_color = style::color (COL_VAL);                          \
     525              :     assert_style_change_streq ((SELFTEST_LOCATION),                   \
     526              :                                plain,                                 \
     527              :                                s,                                     \
     528              :                                (EXPECTED_STR));                       \
     529              :   }                                                                   \
     530              :   SELFTEST_END_STMT
     531              : 
     532              : static void
     533            4 : test_8_bit_colors ()
     534              : {
     535              :   /* Foreground colors.  */
     536            4 :   {
     537            4 :     const bool fg = true;
     538              :     /* 0-15: standard and high-intensity standard colors.  */
     539            4 :     ASSERT_8_BIT_COL_STREQ (0, fg, "");
     540            4 :     ASSERT_8_BIT_COL_STREQ (15, fg, "");
     541              :     /* 16-231: 6x6x6 color cube.  */
     542            4 :     ASSERT_8_BIT_COL_STREQ (16, fg, "");
     543            4 :     ASSERT_8_BIT_COL_STREQ (231, fg, "");
     544              :     /* 232-255: grayscale.  */
     545            4 :     ASSERT_8_BIT_COL_STREQ (232, fg, "");
     546            4 :     ASSERT_8_BIT_COL_STREQ (255, fg, "");
     547              :   }
     548              :   /* Background colors.  */
     549            4 :   {
     550            4 :     const bool fg = false;
     551              :     /* 0-15: standard and high-intensity standard colors.  */
     552            4 :     ASSERT_8_BIT_COL_STREQ (0, fg, "");
     553            4 :     ASSERT_8_BIT_COL_STREQ (15, fg, "");
     554              :     /* 16-231: 6x6x6 color cube.  */
     555            4 :     ASSERT_8_BIT_COL_STREQ (16, fg, "");
     556            4 :     ASSERT_8_BIT_COL_STREQ (231, fg, "");
     557              :     /* 232-255: grayscale.  */
     558            4 :     ASSERT_8_BIT_COL_STREQ (232, fg, "");
     559            4 :     ASSERT_8_BIT_COL_STREQ (255, fg, "");
     560              :   }
     561            4 : }
     562              : 
     563              : #define ASSERT_24_BIT_COL_STREQ(R, G, B, FG, EXPECTED_STR)            \
     564              :   SELFTEST_BEGIN_STMT                                                 \
     565              :   {                                                                   \
     566              :     style plain;                                                      \
     567              :     style s;                                                          \
     568              :     if (FG)                                                           \
     569              :       s.m_fg_color = style::color ((R), (G), (B));                    \
     570              :     else                                                              \
     571              :       s.m_bg_color = style::color ((R), (G), (B));                    \
     572              :     assert_style_change_streq ((SELFTEST_LOCATION),                   \
     573              :                                plain,                                 \
     574              :                                s,                                     \
     575              :                                (EXPECTED_STR));                       \
     576              :   }                                                                   \
     577              :   SELFTEST_END_STMT
     578              : 
     579              : static void
     580            4 : test_24_bit_colors ()
     581              : {
     582              :   /* Foreground colors.  */
     583            4 :   {
     584            4 :     const bool fg = true;
     585              :     // #F3FAF2:
     586            4 :     ASSERT_24_BIT_COL_STREQ (0xf3, 0xfa, 0xf2, fg,
     587              :                              "");
     588              :   }
     589              :   /* Background colors.  */
     590            4 :   {
     591            4 :     const bool fg = false;
     592              :     // #FDF7E7
     593            4 :     ASSERT_24_BIT_COL_STREQ (0xfd, 0xf7, 0xe7, fg,
     594              :                              "");
     595              :   }
     596            4 : }
     597              : 
     598              : static void
     599            4 : test_style_combinations ()
     600              : {
     601            4 :   style_manager sm;
     602            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     603              : 
     604            4 :   style plain;
     605            4 :   ASSERT_EQ (sm.get_or_create_id (plain), 0);
     606            4 :   ASSERT_EQ (sm.get_num_styles (), 1);
     607              : 
     608            4 :   style bold;
     609            4 :   bold.m_bold = true;
     610              : 
     611            4 :   ASSERT_EQ (sm.get_or_create_id (bold), 1);
     612            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     613            4 :   ASSERT_EQ (sm.get_or_create_id (bold), 1);
     614            4 :   ASSERT_EQ (sm.get_num_styles (), 2);
     615              : 
     616            4 :   style magenta_on_blue;
     617            4 :   magenta_on_blue.m_fg_color = style::named_color::MAGENTA;
     618            4 :   magenta_on_blue.m_bg_color = style::named_color::BLUE;
     619            4 :   ASSERT_EQ (sm.get_or_create_id (magenta_on_blue), 2);
     620            4 :   ASSERT_EQ (sm.get_num_styles (), 3);
     621            4 :   ASSERT_EQ (sm.get_or_create_id (magenta_on_blue), 2);
     622            4 :   ASSERT_EQ (sm.get_num_styles (), 3);
     623            4 : }
     624              : 
     625              : /* Run all selftests in this file.  */
     626              : 
     627              : void
     628            4 : text_art_style_cc_tests ()
     629              : {
     630            4 :   test_bold ();
     631            4 :   test_underscore ();
     632            4 :   test_blink ();
     633            4 :   test_named_colors ();
     634            4 :   test_8_bit_colors ();
     635            4 :   test_24_bit_colors ();
     636            4 :   test_style_combinations ();
     637            4 : }
     638              : 
     639              : } // namespace selftest
     640              : 
     641              : 
     642              : #endif /* #if CHECKING_P */
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.