(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-string-desc.c
       1  /* Test of string descriptors.
       2     Copyright (C) 2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  /* Written by Bruno Haible <bruno@clisp.org>, 2023.  */
      18  
      19  #include <config.h>
      20  
      21  #include "string-desc.h"
      22  
      23  #include <fcntl.h>
      24  #include <stdlib.h>
      25  #include <string.h>
      26  #include <unistd.h>
      27  
      28  #include "macros.h"
      29  
      30  int
      31  main (int argc, char *argv[])
      32  {
      33    ASSERT (argc > 1);
      34    int fd3 = open (argv[1], O_RDWR | O_TRUNC | O_CREAT, 0600);
      35    ASSERT (fd3 >= 0);
      36  
      37    string_desc_t s0 = string_desc_new_empty ();
      38    string_desc_t s1 = string_desc_from_c ("Hello world!");
      39    string_desc_t s2 = string_desc_new_addr (21, "The\0quick\0brown\0\0fox");
      40  
      41    /* Test string_desc_length.  */
      42    ASSERT (string_desc_length (s0) == 0);
      43    ASSERT (string_desc_length (s1) == 12);
      44    ASSERT (string_desc_length (s2) == 21);
      45  
      46    /* Test string_desc_char_at.  */
      47    ASSERT (string_desc_char_at (s1, 0) == 'H');
      48    ASSERT (string_desc_char_at (s1, 11) == '!');
      49    ASSERT (string_desc_char_at (s2, 0) == 'T');
      50    ASSERT (string_desc_char_at (s2, 1) == 'h');
      51    ASSERT (string_desc_char_at (s2, 2) == 'e');
      52    ASSERT (string_desc_char_at (s2, 3) == '\0');
      53    ASSERT (string_desc_char_at (s2, 4) == 'q');
      54    ASSERT (string_desc_char_at (s2, 15) == '\0');
      55    ASSERT (string_desc_char_at (s2, 16) == '\0');
      56  
      57    /* Test string_desc_data.  */
      58    (void) string_desc_data (s0);
      59    ASSERT (memcmp (string_desc_data (s1), "Hello world!", 12) == 0);
      60    ASSERT (memcmp (string_desc_data (s2), "The\0quick\0brown\0\0fox", 21) == 0);
      61  
      62    /* Test string_desc_is_empty.  */
      63    ASSERT (string_desc_is_empty (s0));
      64    ASSERT (!string_desc_is_empty (s1));
      65    ASSERT (!string_desc_is_empty (s2));
      66  
      67    /* Test string_desc_startswith.  */
      68    ASSERT (string_desc_startswith (s1, s0));
      69    ASSERT (!string_desc_startswith (s0, s1));
      70    ASSERT (!string_desc_startswith (s1, s2));
      71    ASSERT (!string_desc_startswith (s2, s1));
      72    ASSERT (string_desc_startswith (s2, string_desc_from_c ("The")));
      73    ASSERT (string_desc_startswith (s2, string_desc_new_addr (9, "The\0quick")));
      74    ASSERT (!string_desc_startswith (s2, string_desc_new_addr (9, "The\0quirk")));
      75  
      76    /* Test string_desc_endswith.  */
      77    ASSERT (string_desc_endswith (s1, s0));
      78    ASSERT (!string_desc_endswith (s0, s1));
      79    ASSERT (!string_desc_endswith (s1, s2));
      80    ASSERT (!string_desc_endswith (s2, s1));
      81    ASSERT (!string_desc_endswith (s2, string_desc_from_c ("fox")));
      82    ASSERT (string_desc_endswith (s2, string_desc_new_addr (4, "fox")));
      83    ASSERT (string_desc_endswith (s2, string_desc_new_addr (6, "\0\0fox")));
      84    ASSERT (!string_desc_endswith (s2, string_desc_new_addr (5, "\0\0ox")));
      85  
      86    /* Test string_desc_cmp.  */
      87    ASSERT (string_desc_cmp (s0, s0) == 0);
      88    ASSERT (string_desc_cmp (s0, s1) < 0);
      89    ASSERT (string_desc_cmp (s0, s2) < 0);
      90    ASSERT (string_desc_cmp (s1, s0) > 0);
      91    ASSERT (string_desc_cmp (s1, s1) == 0);
      92    ASSERT (string_desc_cmp (s1, s2) < 0);
      93    ASSERT (string_desc_cmp (s2, s0) > 0);
      94    ASSERT (string_desc_cmp (s2, s1) > 0);
      95    ASSERT (string_desc_cmp (s2, s2) == 0);
      96  
      97    /* Test string_desc_index.  */
      98    ASSERT (string_desc_index (s0, 'o') == -1);
      99    ASSERT (string_desc_index (s2, 'o') == 12);
     100  
     101    /* Test string_desc_last_index.  */
     102    ASSERT (string_desc_last_index (s0, 'o') == -1);
     103    ASSERT (string_desc_last_index (s2, 'o') == 18);
     104  
     105    /* Test string_desc_contains.  */
     106    ASSERT (string_desc_contains (s0, string_desc_from_c ("ll")) == -1);
     107    ASSERT (string_desc_contains (s1, string_desc_from_c ("ll")) == 2);
     108    ASSERT (string_desc_contains (s1, string_desc_new_addr (1, "")) == -1);
     109    ASSERT (string_desc_contains (s2, string_desc_new_addr (1, "")) == 3);
     110    ASSERT (string_desc_contains (s1, string_desc_new_addr (2, "\0")) == -1);
     111    ASSERT (string_desc_contains (s2, string_desc_new_addr (2, "\0")) == 15);
     112  
     113    /* Test string_desc_substring.  */
     114    ASSERT (string_desc_cmp (string_desc_substring (s1, 2, 5),
     115                             string_desc_from_c ("llo")) == 0);
     116  
     117    /* Test string_desc_write.  */
     118    ASSERT (string_desc_write (fd3, s0) == 0);
     119    ASSERT (string_desc_write (fd3, s1) == 0);
     120    ASSERT (string_desc_write (fd3, s2) == 0);
     121  
     122    /* Test string_desc_fwrite.  */
     123    ASSERT (string_desc_fwrite (stdout, s0) == 0);
     124    ASSERT (string_desc_fwrite (stdout, s1) == 0);
     125    ASSERT (string_desc_fwrite (stdout, s2) == 0);
     126  
     127    /* Test string_desc_new, string_desc_set_char_at, string_desc_fill.  */
     128    string_desc_t s4;
     129    ASSERT (string_desc_new (&s4, 5) == 0);
     130    string_desc_set_char_at (s4, 0, 'H');
     131    string_desc_set_char_at (s4, 4, 'o');
     132    string_desc_set_char_at (s4, 1, 'e');
     133    string_desc_fill (s4, 2, 4, 'l');
     134    ASSERT (string_desc_length (s4) == 5);
     135    ASSERT (string_desc_startswith (s1, s4));
     136  
     137    /* Test string_desc_new_filled, string_desc_set_char_at.  */
     138    string_desc_t s5;
     139    ASSERT (string_desc_new_filled (&s5, 5, 'l') == 0);
     140    string_desc_set_char_at (s5, 0, 'H');
     141    string_desc_set_char_at (s5, 4, 'o');
     142    string_desc_set_char_at (s5, 1, 'e');
     143    ASSERT (string_desc_length (s5) == 5);
     144    ASSERT (string_desc_startswith (s1, s5));
     145  
     146    /* Test string_desc_equals.  */
     147    ASSERT (!string_desc_equals (s1, s5));
     148    ASSERT (string_desc_equals (s4, s5));
     149  
     150    /* Test string_desc_copy, string_desc_free.  */
     151    {
     152      string_desc_t s6;
     153      ASSERT (string_desc_copy (&s6, s0) == 0);
     154      ASSERT (string_desc_is_empty (s6));
     155      string_desc_free (s6);
     156    }
     157    {
     158      string_desc_t s6;
     159      ASSERT (string_desc_copy (&s6, s2) == 0);
     160      ASSERT (string_desc_equals (s6, s2));
     161      string_desc_free (s6);
     162    }
     163  
     164    /* Test string_desc_overwrite.  */
     165    {
     166      string_desc_t s7;
     167      ASSERT (string_desc_copy (&s7, s2) == 0);
     168      string_desc_overwrite (s7, 4, s1);
     169      ASSERT (string_desc_equals (s7, string_desc_new_addr (21, "The\0Hello world!\0fox")));
     170    }
     171  
     172    /* Test string_desc_concat.  */
     173    {
     174      string_desc_t s8;
     175      ASSERT (string_desc_concat (&s8, 3, string_desc_new_addr (10, "The\0quick"),
     176                                          string_desc_new_addr (7, "brown\0"),
     177                                          string_desc_new_addr (4, "fox"),
     178                                          string_desc_new_addr (7, "unused")) == 0);
     179      ASSERT (string_desc_equals (s8, s2));
     180      string_desc_free (s8);
     181    }
     182  
     183    /* Test string_desc_c.  */
     184    {
     185      char *ptr = string_desc_c (s2);
     186      ASSERT (ptr != NULL);
     187      ASSERT (memcmp (ptr, "The\0quick\0brown\0\0fox\0", 22) == 0);
     188      free (ptr);
     189    }
     190  
     191    close (fd3);
     192  
     193    return 0;
     194  }