LCOV - code coverage report
Current view: top level - glib/gio/tests - fileattributematcher.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 37 37 100.0 %
Date: 2024-04-23 05:16:05 Functions: 4 4 100.0 %
Branches: 6 6 100.0 %

           Branch data     Line data    Source code
       1                 :            : #include <gio/gio.h>
       2                 :            : 
       3                 :            : static void
       4                 :          1 : test_exact (void)
       5                 :            : {
       6                 :          1 :   const char *exact_matches[] = {
       7                 :            :     "*",
       8                 :            :     "a::*",
       9                 :            :     "a::*,b::*",
      10                 :            :     "a::a,a::b",
      11                 :            :     "a::a,a::b,b::*"
      12                 :            :   };
      13                 :            : 
      14                 :            :   GFileAttributeMatcher *matcher;
      15                 :            :   char *s;
      16                 :            :   guint i;
      17                 :            : 
      18         [ +  + ]:          6 :   for (i = 0; i < G_N_ELEMENTS (exact_matches); i++)
      19                 :            :     {
      20                 :          5 :       matcher = g_file_attribute_matcher_new (exact_matches[i]);
      21                 :          5 :       s = g_file_attribute_matcher_to_string (matcher);
      22                 :          5 :       g_assert_cmpstr (exact_matches[i], ==, s);
      23                 :          5 :       g_free (s);
      24                 :          5 :       g_file_attribute_matcher_unref (matcher);
      25                 :            :     }
      26                 :          1 : }
      27                 :            : 
      28                 :            : static void
      29                 :          1 : test_equality (void)
      30                 :            : {
      31                 :            :   struct {
      32                 :            :     const char *expected;
      33                 :            :     const char *actual;
      34                 :          1 :   } equals[] = {
      35                 :            :     /* star makes everything else go away */
      36                 :            :     { "*", "*,*" },
      37                 :            :     { "*", "*,a::*" },
      38                 :            :     { "*", "*,a::b" },
      39                 :            :     { "*", "a::*,*" },
      40                 :            :     { "*", "a::b,*" },
      41                 :            :     { "*", "a::b,*,a::*" },
      42                 :            :     /* a::* makes a::<anything> go away */
      43                 :            :     { "a::*", "a::*,a::*" },
      44                 :            :     { "a::*", "a::*,a::b" },
      45                 :            :     { "a::*", "a::b,a::*" },
      46                 :            :     { "a::*", "a::b,a::*,a::c" },
      47                 :            :     /* a::b does not allow duplicates */
      48                 :            :     { "a::b", "a::b,a::b" },
      49                 :            :     { "a::b,a::c", "a::b,a::c,a::b" },
      50                 :            :     /* stuff gets ordered in registration order */
      51                 :            :     { "a::b,a::c", "a::c,a::b" },
      52                 :            :     { "a::*,b::*", "b::*,a::*" },
      53                 :            :   };
      54                 :            : 
      55                 :            :   GFileAttributeMatcher *matcher;
      56                 :            :   char *s;
      57                 :            :   guint i;
      58                 :            : 
      59         [ +  + ]:         15 :   for (i = 0; i < G_N_ELEMENTS (equals); i++)
      60                 :            :     {
      61                 :         14 :       matcher = g_file_attribute_matcher_new (equals[i].actual);
      62                 :         14 :       s = g_file_attribute_matcher_to_string (matcher);
      63                 :         14 :       g_assert_cmpstr (equals[i].expected, ==, s);
      64                 :         14 :       g_free (s);
      65                 :         14 :       g_file_attribute_matcher_unref (matcher);
      66                 :            :     }
      67                 :          1 : }
      68                 :            : 
      69                 :            : static void
      70                 :          1 : test_subtract (void)
      71                 :            : {
      72                 :            :   struct {
      73                 :            :     const char *attributes;
      74                 :            :     const char *subtract;
      75                 :            :     const char *result;
      76                 :          1 :   } subtractions[] = {
      77                 :            :     /* * subtracts everything */
      78                 :            :     { "*", "*", NULL },
      79                 :            :     { "a::*", "*", NULL },
      80                 :            :     { "a::b", "*", NULL },
      81                 :            :     { "a::b,a::c", "*", NULL },
      82                 :            :     { "a::*,b::*", "*", NULL },
      83                 :            :     { "a::*,b::c", "*", NULL },
      84                 :            :     { "a::b,b::*", "*", NULL },
      85                 :            :     { "a::b,b::c", "*", NULL },
      86                 :            :     { "a::b,a::c,b::*", "*", NULL },
      87                 :            :     { "a::b,a::c,b::c", "*", NULL },
      88                 :            :     /* a::* subtracts all a's */
      89                 :            :     { "*", "a::*", "*" },
      90                 :            :     { "a::*", "a::*", NULL },
      91                 :            :     { "a::b", "a::*", NULL },
      92                 :            :     { "a::b,a::c", "a::*", NULL },
      93                 :            :     { "a::*,b::*", "a::*", "b::*" },
      94                 :            :     { "a::*,b::c", "a::*", "b::c" },
      95                 :            :     { "a::b,b::*", "a::*", "b::*" },
      96                 :            :     { "a::b,b::c", "a::*", "b::c" },
      97                 :            :     { "a::b,a::c,b::*", "a::*", "b::*" },
      98                 :            :     { "a::b,a::c,b::c", "a::*", "b::c" },
      99                 :            :     /* a::b subtracts exactly that */
     100                 :            :     { "*", "a::b", "*" },
     101                 :            :     { "a::*", "a::b", "a::*" },
     102                 :            :     { "a::b", "a::b", NULL },
     103                 :            :     { "a::b,a::c", "a::b", "a::c" },
     104                 :            :     { "a::*,b::*", "a::b", "a::*,b::*" },
     105                 :            :     { "a::*,b::c", "a::b", "a::*,b::c" },
     106                 :            :     { "a::b,b::*", "a::b", "b::*" },
     107                 :            :     { "a::b,b::c", "a::b", "b::c" },
     108                 :            :     { "a::b,a::c,b::*", "a::b", "a::c,b::*" },
     109                 :            :     { "a::b,a::c,b::c", "a::b", "a::c,b::c" },
     110                 :            :     /* a::b,b::* subtracts both of those */
     111                 :            :     { "*", "a::b,b::*", "*" },
     112                 :            :     { "a::*", "a::b,b::*", "a::*" },
     113                 :            :     { "a::b", "a::b,b::*", NULL },
     114                 :            :     { "a::b,a::c", "a::b,b::*", "a::c" },
     115                 :            :     { "a::*,b::*", "a::b,b::*", "a::*" },
     116                 :            :     { "a::*,b::c", "a::b,b::*", "a::*" },
     117                 :            :     { "a::b,b::*", "a::b,b::*", NULL },
     118                 :            :     { "a::b,b::c", "a::b,b::*", NULL },
     119                 :            :     { "a::b,a::c,b::*", "a::b,b::*", "a::c" },
     120                 :            :     { "a::b,a::c,b::c", "a::b,b::*", "a::c" },
     121                 :            :     /* a::b,b::c should work, too */
     122                 :            :     { "*", "a::b,b::c", "*" },
     123                 :            :     { "a::*", "a::b,b::c", "a::*" },
     124                 :            :     { "a::b", "a::b,b::c", NULL },
     125                 :            :     { "a::b,a::c", "a::b,b::c", "a::c" },
     126                 :            :     { "a::*,b::*", "a::b,b::c", "a::*,b::*" },
     127                 :            :     { "a::*,b::c", "a::b,b::c", "a::*" },
     128                 :            :     { "a::b,b::*", "a::b,b::c", "b::*" },
     129                 :            :     { "a::b,b::c", "a::b,b::c", NULL },
     130                 :            :     { "a::b,a::c,b::*", "a::b,b::c", "a::c,b::*" },
     131                 :            :     { "a::b,a::c,b::c", "a::b,b::c", "a::c" },
     132                 :            :   };
     133                 :            : 
     134                 :            :   GFileAttributeMatcher *matcher, *subtract, *result;
     135                 :            :   char *s;
     136                 :            :   guint i;
     137                 :            : 
     138         [ +  + ]:         51 :   for (i = 0; i < G_N_ELEMENTS (subtractions); i++)
     139                 :            :     {
     140                 :         50 :       matcher = g_file_attribute_matcher_new (subtractions[i].attributes);
     141                 :         50 :       subtract = g_file_attribute_matcher_new (subtractions[i].subtract);
     142                 :         50 :       result = g_file_attribute_matcher_subtract (matcher, subtract);
     143                 :         50 :       s = g_file_attribute_matcher_to_string (result);
     144                 :         50 :       g_assert_cmpstr (subtractions[i].result, ==, s);
     145                 :         50 :       g_free (s);
     146                 :         50 :       g_file_attribute_matcher_unref (matcher);
     147                 :         50 :       g_file_attribute_matcher_unref (subtract);
     148                 :         50 :       g_file_attribute_matcher_unref (result);
     149                 :            :     }
     150                 :          1 : }
     151                 :            : 
     152                 :            : int
     153                 :          1 : main (int argc, char *argv[])
     154                 :            : {
     155                 :          1 :   g_test_init (&argc, &argv, NULL);
     156                 :            : 
     157                 :          1 :   g_test_add_func ("/fileattributematcher/exact", test_exact);
     158                 :          1 :   g_test_add_func ("/fileattributematcher/equality", test_equality);
     159                 :          1 :   g_test_add_func ("/fileattributematcher/subtract", test_subtract);
     160                 :            : 
     161                 :          1 :   return g_test_run ();
     162                 :            : }

Generated by: LCOV version 1.14