test_strcasecmp_l.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Test of \c posix_strcasecmp_l() implementation
4  *
5  * Copyright (c) 2015-2020 by the developers. See the LICENSE file for details.
6  */
7 
8 
9 /* ========================================================================== */
10 /* Include headers */
11 
12 #include "posix.h" /* Include this first because of feature test macros */
13 
14 #include <stdio.h>
15 
16 #include "config.h"
17 
18 #include "test.h"
19 #include "test_strcasecmp_l.h"
20 
21 
22 /* ========================================================================== */
23 /*! \addtogroup TEST */
24 /*! @{ */
25 
26 
27 /* ========================================================================== */
28 /*! \brief Test \c posix_strcasecmp_l() implementation
29  *
30  * The following cases are tested using POSIX locale:
31  * - Identical ASCII strings (equal, trivial case)
32  * - Different strings with same length (not equal)
33  * - First string longer (not equal)
34  * - Second string longer (not equal)
35  * - Strings that only differ in case of ASCII letters (equal)
36  * - Unicode strings with non-ASCII characters that only differ in their case
37  * (not equal in POSIX locale)
38  *
39  * \return
40  * - \c EXIT_SUCCESS on success
41  * - \c EXIT_FAILURE on error
42  */
43 
45 {
46  int res = POSIX_EXIT_SUCCESS;
47  posix_locale_t loc_ctype_posix;
48  const char* string1_1 = "x1: 10, y2: 50%, eol";
49  const char* string1_2 = "x1: 10, y2: 50%, eol";
50  const char* string2_1 = "XXX";
51  const char* string2_2 = "YYY";
52  const char* string3_1 = "Longer";
53  const char* string3_2 = "Long";
54  const char* string4_1 = "Long";
55  const char* string4_2 = "Longer";
56  const char* string5_1 = "case";
57  const char* string5_2 = "Case";
58  const char* string6_1 = "B\xC3\xA4ume";
59  const char* string6_2 = "B\xC3\x84ume";
60  int rv;
61  const char* loc;
62 
63  /* Create a locale object with LC_CTYPE == POSIX */
64  loc_ctype_posix = posix_newlocale(POSIX_LC_CTYPE_MASK, "POSIX",
65  (posix_locale_t) 0);
66  if((posix_locale_t) 0 == loc_ctype_posix)
67  {
68  print_error("Creation of locale object failed");
69  res = POSIX_EXIT_FAILURE;
70  }
71 
72  if(!res)
73  {
74  /*
75  * This test the following things:
76  * - Identical strings (trivial case)
77  */
78  rv = posix_strcasecmp_l(string1_1, string1_2, loc_ctype_posix);
79  if(rv)
80  {
81  print_error("Result is not correct (should indicate match)");
82  res = POSIX_EXIT_FAILURE;
83  }
84  if(res)
85  {
86  /* For debugging */
87  fprintf(stderr, TEST_TAB "String 1: \"%s\"\n", string1_1);
88  fprintf(stderr, TEST_TAB "String 2: \"%s\"\n", string1_2);
89  }
90  }
91 
92  if(!res)
93  {
94  /*
95  * This test the following things:
96  * - Different strings with same length
97  */
98  rv = posix_strcasecmp_l(string2_1, string2_2, loc_ctype_posix);
99  if(!rv)
100  {
101  print_error("Result is not correct (should indicate no match)");
102  res = POSIX_EXIT_FAILURE;
103  }
104  if(res)
105  {
106  /* For debugging */
107  fprintf(stderr, TEST_TAB "String 1: \"%s\"\n", string2_1);
108  fprintf(stderr, TEST_TAB "String 2: \"%s\"\n", string2_2);
109  }
110  }
111 
112  if(!res)
113  {
114  /*
115  * This test the following things:
116  * - First string longer
117  */
118  rv = posix_strcasecmp_l(string3_1, string3_2, loc_ctype_posix);
119  if(!rv)
120  {
121  print_error("Result is not correct (should indicate no match)");
122  res = POSIX_EXIT_FAILURE;
123  }
124  if(res)
125  {
126  /* For debugging */
127  fprintf(stderr, TEST_TAB "String 1: \"%s\"\n", string3_1);
128  fprintf(stderr, TEST_TAB "String 2: \"%s\"\n", string3_2);
129  }
130  }
131 
132  if(!res)
133  {
134  /*
135  * This test the following things:
136  * - Second string longer
137  */
138  rv = posix_strcasecmp_l(string4_1, string4_2, loc_ctype_posix);
139  if(!rv)
140  {
141  print_error("Result is not correct (should indicate no match)");
142  res = POSIX_EXIT_FAILURE;
143  }
144  if(res)
145  {
146  /* For debugging */
147  fprintf(stderr, TEST_TAB "String 1: \"%s\"\n", string4_1);
148  fprintf(stderr, TEST_TAB "String 2: \"%s\"\n", string4_2);
149  }
150  }
151 
152  if(!res)
153  {
154  /*
155  * This test the following things:
156  * - Strings that only differ in case of ASCII letters
157  */
158  rv = posix_strcasecmp_l(string5_1, string5_2, loc_ctype_posix);
159  if(rv)
160  {
161  print_error("Result is not correct (should indicate match)");
162  res = POSIX_EXIT_FAILURE;
163  }
164  if(res)
165  {
166  /* For debugging */
167  fprintf(stderr, TEST_TAB "String 1: \"%s\"\n", string5_1);
168  fprintf(stderr, TEST_TAB "String 2: \"%s\"\n", string5_2);
169  }
170  }
171 
172  if(!res)
173  {
174  /*
175  * This test the following things:
176  * - Unicode strings with non-ASCII characters that only differ in their
177  * case
178  */
179  rv = posix_strcasecmp_l(string6_1, string6_2, loc_ctype_posix);
180  if(!rv)
181  {
182  print_error("Result is not correct (should indicate no match)");
183  res = POSIX_EXIT_FAILURE;
184  }
185  if(res)
186  {
187  /* For debugging */
188 #if CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI
189  loc = posix_setlocale(POSIX_LC_CTYPE, "");
190 #else /* CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI */
191  loc = NULL;
192 #endif /* CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI */
193  if(NULL == loc)
194  {
195  print_error("Setting locale for debug messages failed");
196  }
197  else
198  {
199  /* Print Unicode data only if terminal use Unicode locale */
200  if(NULL == strstr(loc, "UTF") && NULL == strstr(loc, "utf"))
201  {
202  print_error(
203  "Debug messages can't be printed with current locale");
204  }
205  else
206  {
207  /* For debugging */
208  fprintf(stderr, TEST_TAB "String 1: \"%s\"\n", string6_1);
209  fprintf(stderr, TEST_TAB "String 2: \"%s\"\n", string6_2);
210  }
211  }
212  }
213  }
214 
215  /* Free memory of locale object */
216  posix_freelocale(loc_ctype_posix);
217 
218  return(res);
219 }
220 
221 
222 /*! @} */
223 
224 /* EOF */
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
test_strcasecmp_l
int test_strcasecmp_l(void)
Test posix_strcasecmp_l() implementation.
Definition: test_strcasecmp_l.c:44
print_error
void print_error(const char *)
Print error message.
Definition: main.cxx:276

Generated at 2024-04-27 using  doxygen