LAL  7.4.1.1-da0a4ae
LALStringTest.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Karl Wette
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 2 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 with program; see the file COPYING. If not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  */
19 
20 #include <string.h>
21 #include <ctype.h>
22 
23 #include <lal/LALString.h>
24 #include <lal/XLALError.h>
25 #include <lal/LALMalloc.h>
26 
27 static inline int sign(int s) {
28  if (s < 0) return -1;
29  if (s > 0) return 1;
30  return 0;
31 }
32 
33 #define STRCMPCHK(XLALCMP, XLALS1, XLALS2, CCMP, CS1, CS2) do { \
34  int xlalsign = sign(XLALCMP(XLALS1, XLALS2)); \
35  int csign = sign(CCMP(CS1, CS2)); \
36  XLAL_CHECK_MAIN( xlalsign == csign, XLAL_EFAILED, \
37  "Failed string comparison test: sign(%s('%s','%s')) = %i, sign(%s('%s','%s')) = %i\n", \
38  #XLALCMP, XLALS1, XLALS2, xlalsign, #CCMP, CS1, CS2, csign ); \
39  } while(0)
40 
41 #define STRNCMPCHK(N, XLALCMP, XLALS1, XLALS2, CCMP, CS1, CS2) do { \
42  int xlalsign = sign(XLALCMP(XLALS1, XLALS2, N)); \
43  int csign = sign(CCMP(CS1, CS2, N)); \
44  XLAL_CHECK_MAIN( xlalsign == csign, XLAL_EFAILED, \
45  "Failed string comparison test: sign(%s('%s','%s',%zu)) = %i, sign(%s('%s','%s',%zu)) = %i\n", \
46  #XLALCMP, XLALS1, XLALS2, N, xlalsign, #CCMP, CS1, CS2, N, csign ); \
47  } while(0)
48 
49 int main( void )
50 {
51 
52  {
53  char *s = NULL;
54  XLAL_CHECK( strcmp( (s = XLALStringAppendFmt(s, "Hi %s!", "there")), "Hi there!" ) == 0, XLAL_EFAILED );
55  XLAL_CHECK( strcmp( (s = XLALStringAppendFmt(s, " %i + %i = %i?", 2, 2, 4)), "Hi there! 2 + 2 = 4?" ) == 0, XLAL_EFAILED );
56  XLALFree( s );
57  }
58 
59  STRCMPCHK( XLALStringCaseCompare, "0gS1BGPuCG", "aMYKHogF0r", strcmp, "0gs1bgpucg", "amykhogf0r" );
60  STRCMPCHK( XLALStringCaseCompare, "1XJ7G7dIjZ", "ERB8R8FljJ", strcmp, "1xj7g7dijz", "erb8r8fljj" );
61  STRCMPCHK( XLALStringCaseCompare, "8n4EwTImL", "kmuxDObvfI29", strcmp, "8n4ewtiml", "kmuxdobvfi29" );
62  STRCMPCHK( XLALStringCaseCompare, "bekJxzChXC", "O3doTwMI7C", strcmp, "bekjxzchxc", "o3dotwmi7c" );
63  STRCMPCHK( XLALStringCaseCompare, "f42TpVwldV", "F5qNN", strcmp, "f42tpvwldv", "f5qnn" );
64  STRCMPCHK( XLALStringCaseCompare, "FPpc9", "w0uwzFMYnd", strcmp, "fppc9", "w0uwzfmynd" );
65  STRCMPCHK( XLALStringCaseCompare, "J4io4HKa62", "s3erTmariX", strcmp, "j4io4hka62", "s3ertmarix" );
66  STRCMPCHK( XLALStringCaseCompare, "kdi7f2", "aSiM7gvIm", strcmp, "kdi7f2", "asim7gvim" );
67  STRCMPCHK( XLALStringCaseCompare, "nwWd7qaipf", "ZnDeR", strcmp, "nwwd7qaipf", "znder" );
68  STRCMPCHK( XLALStringCaseCompare, "oVTTSi", "tG1", strcmp, "ovttsi", "tg1" );
69  STRCMPCHK( XLALStringCaseCompare, "t6fv04Bjc5", "6tnnoIDZHY", strcmp, "t6fv04bjc5", "6tnnoidzhy" );
70  STRCMPCHK( XLALStringCaseCompare, "tW8Ng63utwwS", "x47v59r9e", strcmp, "tw8ng63utwws", "x47v59r9e" );
71  STRCMPCHK( XLALStringCaseCompare, "yiMB0wmdkq", "X1BpBeOFwy", strcmp, "yimb0wmdkq", "x1bpbeofwy" );
72  STRCMPCHK( XLALStringCaseCompare, "zaNDp", "Kd0H0AVtaA", strcmp, "zandp", "kd0h0avtaa" );
73  STRCMPCHK( XLALStringCaseCompare, "ztZDUBu4rviQ", "gzM8YDLnzedn", strcmp, "ztzdubu4rviq", "gzm8ydlnzedn" );
74 
75  for (size_t n = 0; n < 12; ++n) {
76 
77  STRNCMPCHK( n, XLALStringNCaseCompare, "0gS1BGPuCG", "aMYKHogF0r", strncmp, "0gs1bgpucg", "amykhogf0r" );
78  STRNCMPCHK( n, XLALStringNCaseCompare, "1XJ7G7dIjZ", "ERB8R8FljJ", strncmp, "1xj7g7dijz", "erb8r8fljj" );
79  STRNCMPCHK( n, XLALStringNCaseCompare, "8n4EwTImL", "kmuxDObvfI29", strncmp, "8n4ewtiml", "kmuxdobvfi29" );
80  STRNCMPCHK( n, XLALStringNCaseCompare, "bekJxzChXC", "O3doTwMI7C", strncmp, "bekjxzchxc", "o3dotwmi7c" );
81  STRNCMPCHK( n, XLALStringNCaseCompare, "f42TpVwldV", "F5qNN", strncmp, "f42tpvwldv", "f5qnn" );
82  STRNCMPCHK( n, XLALStringNCaseCompare, "FPpc9", "w0uwzFMYnd", strncmp, "fppc9", "w0uwzfmynd" );
83  STRNCMPCHK( n, XLALStringNCaseCompare, "J4io4HKa62", "s3erTmariX", strncmp, "j4io4hka62", "s3ertmarix" );
84  STRNCMPCHK( n, XLALStringNCaseCompare, "kdi7f2", "aSiM7gvIm", strncmp, "kdi7f2", "asim7gvim" );
85  STRNCMPCHK( n, XLALStringNCaseCompare, "nwWd7qaipf", "ZnDeR", strncmp, "nwwd7qaipf", "znder" );
86  STRNCMPCHK( n, XLALStringNCaseCompare, "oVTTSi", "tG1", strncmp, "ovttsi", "tg1" );
87  STRNCMPCHK( n, XLALStringNCaseCompare, "t6fv04Bjc5", "6tnnoIDZHY", strncmp, "t6fv04bjc5", "6tnnoidzhy" );
88  STRNCMPCHK( n, XLALStringNCaseCompare, "tW8Ng63utwwS", "x47v59r9e", strncmp, "tw8ng63utwws", "x47v59r9e" );
89  STRNCMPCHK( n, XLALStringNCaseCompare, "yiMB0wmdkq", "X1BpBeOFwy", strncmp, "yimb0wmdkq", "x1bpbeofwy" );
90  STRNCMPCHK( n, XLALStringNCaseCompare, "zaNDp", "Kd0H0AVtaA", strncmp, "zandp", "kd0h0avtaa" );
91  STRNCMPCHK( n, XLALStringNCaseCompare, "ztZDUBu4rviQ", "gzM8YDLnzedn", strncmp, "ztzdubu4rviq", "gzm8ydlnzedn" );
92 
93  }
94 
95  {
96  char s[] = "abc,def-,ghij k-lmn, ,-";
97  char *p = s;
98  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 0), "abc" ) == 0, XLAL_EFAILED );
99  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 0), "def" ) == 0, XLAL_EFAILED );
100  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 0), "ghij k" ) == 0, XLAL_EFAILED );
101  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 0), "lmn" ) == 0, XLAL_EFAILED );
102  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 0), " " ) == 0, XLAL_EFAILED );
103  XLAL_CHECK_MAIN( XLALStringToken(&p, ",-", 0) == NULL, XLAL_EFAILED );
104  }
105 
106  {
107  char s[] = "abc,def-,ghij k-lmn, ,-";
108  char *p = s;
109  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), "abc" ) == 0, XLAL_EFAILED );
110  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), "def" ) == 0, XLAL_EFAILED );
111  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), "" ) == 0, XLAL_EFAILED );
112  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), "ghij k" ) == 0, XLAL_EFAILED );
113  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), "lmn" ) == 0, XLAL_EFAILED );
114  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), " " ) == 0, XLAL_EFAILED );
115  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), "" ) == 0, XLAL_EFAILED );
116  XLAL_CHECK_MAIN( strcmp( XLALStringToken(&p, ",-", 1), "" ) == 0, XLAL_EFAILED );
117  XLAL_CHECK_MAIN( XLALStringToken(&p, ",-", 1) == NULL, XLAL_EFAILED );
118  }
119 
120  {
121  char s[256];
122  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
123  XLAL_CHECK_MAIN( strcmp( XLALStringStripChars( s, isupper ), "a0!bd1$2f^&3i*4(j5l$6m %op&qs7&t8,..9/?0" ) == 0, XLAL_EFAILED );
124  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
125  XLAL_CHECK_MAIN( strcmp( XLALStringStripChars( s, islower ), "0!C1$E2G^&H3*4(5K$6N %&R7&8V,..9VW/X?0YZ" ) == 0, XLAL_EFAILED );
126  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
127  XLAL_CHECK_MAIN( strcmp( XLALStringStripChars( s, isalnum ), "!$^&*($ %&&,../?" ) == 0, XLAL_EFAILED );
128  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
129  XLAL_CHECK_MAIN( strcmp( XLALStringStripChars( s, ispunct ), "a0bCd1E2fGH3i4j5Kl6mN opqRs7t8V9VWX0YZ" ) == 0, XLAL_EFAILED );
130  }
131 
132  {
133  char s[256];
134  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
135  XLAL_CHECK_MAIN( strcmp( XLALStringKeepChars( s, isupper ), "CEGHKNRVVWXYZ" ) == 0, XLAL_EFAILED );
136  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
137  XLAL_CHECK_MAIN( strcmp( XLALStringKeepChars( s, islower ), "abdfijlmopqst" ) == 0, XLAL_EFAILED );
138  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
139  XLAL_CHECK_MAIN( strcmp( XLALStringKeepChars( s, isalnum ), "a0bCd1E2fGH3i4j5Kl6mNopqRs7t8V9VWX0YZ" ) == 0, XLAL_EFAILED );
140  strcpy( s, "a0!bCd1$E2fG^&H3i*4(j5Kl$6mN %op&qRs7&t8V,..9VW/X?0YZ" );
141  XLAL_CHECK_MAIN( strcmp( XLALStringKeepChars( s, ispunct ), "!$^&*($%&&,../?" ) == 0, XLAL_EFAILED );
142  }
143 
144  {
145  char s[] = "abcbdeacd";
146  XLAL_CHECK_MAIN( strcmp( XLALStringReplaceChar(s, 'a', 'b'), "bbcbdebcd" ) == 0, XLAL_EFAILED );
147  XLAL_CHECK_MAIN( strcmp( XLALStringReplaceChar(s, 'b', 'c'), "ccccdeccd" ) == 0, XLAL_EFAILED );
148  XLAL_CHECK_MAIN( strcmp( XLALStringReplaceChar(s, 'c', 'd'), "dddddeddd" ) == 0, XLAL_EFAILED );
149  XLAL_CHECK_MAIN( strcmp( XLALStringReplaceChar(s, 'd', 'e'), "eeeeeeeee" ) == 0, XLAL_EFAILED );
150  }
151 
153 
154  return 0;
155 
156 }
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
char * XLALStringAppendFmt(char *s, const char *fmt,...)
Append the formatted string 'fmt' to the string 's', which is reallocated with XLALRealloc() to the r...
Definition: LALString.c:69
#define STRCMPCHK(XLALCMP, XLALS1, XLALS2, CCMP, CS1, CS2)
Definition: LALStringTest.c:33
#define STRNCMPCHK(N, XLALCMP, XLALS1, XLALS2, CCMP, CS1, CS2)
Definition: LALStringTest.c:41
static int sign(int s)
Definition: LALStringTest.c:27
int main(void)
Definition: LALStringTest.c:49
#define XLALFree(p)
Definition: LALMalloc.h:47
char * XLALStringStripChars(char *s, int(*f)(int))
Return the string 's' with all characters for which 'f()' is true removed.
Definition: LALString.c:354
char * XLALStringReplaceChar(char *s, const int from, const int to)
Return the string 's' with all characters 'from' replaced with 'to'.
Definition: LALString.c:382
int XLALStringCaseCompare(const char *s1, const char *s2)
Compare two strings, ignoring case and without using locale-dependent functions.
Definition: LALString.c:210
char * XLALStringKeepChars(char *s, int(*f)(int))
Return the string 's' with all characters for which 'f()' is false removed.
Definition: LALString.c:368
int XLALStringNCaseCompare(const char *s1, const char *s2, size_t n)
Compare the first N characters of two strings, ignoring case and without using locale-dependent funct...
Definition: LALString.c:219
char * XLALStringToken(char **s, const char *delim, int empty)
Return the next token delimited by any character in 'delim' from the string 's', which is updated to ...
Definition: LALString.c:292
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
Definition: XLALError.h:810
#define XLAL_CHECK_MAIN(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a C main() routine.
Definition: XLALError.h:885
@ XLAL_EFAILED
Generic failure.
Definition: XLALError.h:418