File: locale.m

package info (click to toggle)
gnustep-base 1.24.9-3.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 26,844 kB
  • sloc: objc: 975,047; ansic: 31,533; makefile: 135; cpp: 122; sh: 102; xml: 32
file content (277 lines) | stat: -rw-r--r-- 10,204 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSLocale.h>

#if	defined(GS_USE_ICU)
#define	NSLOCALE_SUPPORTED	GS_USE_ICU
#else
#define	NSLOCALE_SUPPORTED	1 /* Assume Apple support */
#endif

static void testBasic(void)
{
  NSComparisonResult compRes;
  NSRange range;

  range = [@"ababa" rangeOfString: @"b"
			  options: 0
			    range: NSMakeRange(2, 3)
			   locale: [[[NSLocale alloc] initWithLocaleIdentifier: @"en_US"] autorelease]];

  PASS(NSEqualRanges(NSMakeRange(3,1), range), "expected {3,1}, got {%d, %d}", (int)range.location,
       (int)range.length);


  compRes = [@"hello" compare: @"Hello"];		
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);

  compRes = [@"H" compare: @"i"];		
  PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);
  
  compRes = [@"h" compare: @"H"];
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);

  compRes = [@"hello" compare: @"Hello" options: 0 range: NSMakeRange(0, 5) locale: nil];
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);
  
  compRes = [@"h" compare: @"H" options: 0 range: NSMakeRange(0, 1) locale: nil];
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);

  compRes = [@"hello" compare: @"H" options: 0 range: NSMakeRange(0, 1) locale: nil];
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);

  compRes = [@"a" compare: @"Z"];
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);

  compRes = [@"z" compare: @"A"];
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);
  
  compRes = [@"a" localizedCompare: @"Z"];
  PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);
  
  compRes = [@"z" localizedCompare: @"A"];
  PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);

  
  // These two may be considered implementation details
  {
    BOOL wasHopeful = testHopeful;
    testHopeful = YES;
    
    compRes = [@"hello" compare: @"Hello" options: 0 range: NSMakeRange(0, 5)
			 locale: [[[NSLocale alloc] initWithLocaleIdentifier: @"en_US_POSIX"] autorelease]];
    PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);	
    
    
    compRes = [@"hello" compare: @"Hello" options: 0 range: NSMakeRange(0, 5)
			 locale: [NSLocale systemLocale]];
    PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);

    testHopeful = wasHopeful;
  }

  compRes = [@"hello" compare: @"Hello" options: 0 range: NSMakeRange(0, 5)
		       locale: [[[NSLocale alloc] initWithLocaleIdentifier: @"en_US"] autorelease]];
  PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);

  compRes = [@"hello" localizedCompare: @"Hello"];
  PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);

  compRes = [@"H" localizedCompare: @"i"];
  PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);
}

static void testEszett(void)
{
  NSComparisonResult compRes;
  NSRange range;
    
  const unichar EszettChar = 0x00df;
  NSString *EszettStr = [[[NSString alloc] initWithCharacters: &EszettChar
						       length: 1] autorelease];

  NSString *EszettPrefixStr = [EszettStr stringByAppendingString: @"abcdef"];
  NSString *EszettSuffixStr = [@"abcdef" stringByAppendingString: EszettStr];
  NSString *EszettPrefixSuffixStr = [NSString stringWithFormat: @"%@abcdef%@", EszettStr, EszettStr];
    
  // test compare:
    
  compRes = [EszettStr compare: @"Ss"
		       options: NSCaseInsensitiveSearch
			 range: NSMakeRange(0, 1)];

  PASS(compRes == 0, "Ss compares equal to Eszett character with"
       " NSCaseInsensitiveSearch. got %d", (int)compRes);
    
  compRes = [EszettStr compare: @"S"
		       options: 0
			 range: NSMakeRange(0, 1)];

  PASS(compRes == 1, "Eszett compare: S is NSOrderedDescending. got %d", (int)compRes);
    
  compRes = [EszettStr compare: @"s"
		       options: 0
			 range: NSMakeRange(0, 1)];

  PASS(compRes == 1, "Eszett compare: s is NSOrderedDescending. got %d", (int)compRes);
    
  // test rangeOfString:
    
  range = [EszettPrefixStr rangeOfString: @"sS"
				 options: NSCaseInsensitiveSearch
				   range: NSMakeRange(0, 7)];
    
  PASS(NSEqualRanges(range, NSMakeRange(0, 1)), "with NSCaseInsensitiveSearch range of sS"
       " in <Eszett>abcdef is {0,1}. got {%d,%d}", (int)range.location, (int)range.length);
    
  range = [EszettPrefixStr rangeOfString: @"sS"
				 options: 0
				   range: NSMakeRange(0, 7)];
    
  PASS(NSEqualRanges(range, NSMakeRange(NSNotFound, 0)), "without NSCaseInsensitiveSearch, "
       "range of sS in <Eszett>abcdef is {NSNotFound, 0}. got {%d,%d}",
       (int)range.location, (int)range.length);
    
  range = [EszettPrefixStr rangeOfString: @"sS"
				 options: NSCaseInsensitiveSearch | NSAnchoredSearch | NSBackwardsSearch
				   range: NSMakeRange(0, 7)];
    
  PASS(NSEqualRanges(range, NSMakeRange(NSNotFound, 0)), "for anchored backwards search, "
       "range of sS in <Eszett>abcdef is {NSNotFound, 0}. got {%d,%d}",
       (int)range.location, (int)range.length);

  range = [EszettPrefixStr rangeOfString: @"sS"
				 options: NSCaseInsensitiveSearch | NSAnchoredSearch
				   range: NSMakeRange(0, 7)];
    
  PASS(NSEqualRanges(range, NSMakeRange(0, 1)), "for anchored forwards search, "
       "range of sS in <Eszett>abcdef is {0, 1}. got {%d,%d}",
       (int)range.location, (int)range.length);
    
  range = [EszettSuffixStr rangeOfString: @"sS"
				 options: NSCaseInsensitiveSearch | NSAnchoredSearch | NSBackwardsSearch
				   range: NSMakeRange(0, 7)];

  PASS(NSEqualRanges(range, NSMakeRange(6, 1)), "for anchored backwards search, "
       "range of sS in abcdef<Eszett> is {6, 1}. got {%d,%d}",
       (int)range.location, (int)range.length);
    
  range = [EszettPrefixSuffixStr rangeOfString: @"sS"
				       options: NSCaseInsensitiveSearch 
					 range: NSMakeRange(0, 8)];
    
  PASS(NSEqualRanges(range, NSMakeRange(0, 1)), "for forward search, "
       "range of sS in <Eszett>abcdef<Eszett> is {0, 1}. got {%d,%d}",
       (int)range.location, (int)range.length);    

  range = [EszettPrefixSuffixStr rangeOfString: @"sS"
				       options: NSCaseInsensitiveSearch | NSBackwardsSearch
					 range: NSMakeRange(0, 8)];
    
  PASS(NSEqualRanges(range, NSMakeRange(7, 1)), "for backward search, "
       "range of sS in <Eszett>abcdef<Eszett> is {7, 1}. got {%d,%d}",
       (int)range.location, (int)range.length); 
}

static void testLithuanian(void)
{
  // "in Lithuanian, "y" is sorted between "i" and "k"."
  //   -- http://userguide.icu-project.org/collation

  NSComparisonResult compRes;

  compRes = [@"y" compare: @"k" options: 0 range: NSMakeRange(0,1) locale: nil];

  PASS(compRes == NSOrderedDescending, "y>k in nil locale. comparison result: %d",
       (int)compRes);

  compRes = [@"y" compare: @"k" options: 0 range: NSMakeRange(0,1) locale: 
		[[[NSLocale alloc] initWithLocaleIdentifier: @"lt"] autorelease]];
  
  PASS(compRes == NSOrderedAscending, "y<k in 'lt' locale. comparison result: %d",
       (int)compRes);
}

static void testDiacritics(void)
{
	NSComparisonResult compRes;

	const unichar eAcuteChar = 0x00e9;
	NSString *eAcuteStr = [[[NSString alloc] initWithCharacters: &eAcuteChar
														 length: 1] autorelease];
	const unichar EAcuteChar = 0x00c9;
	NSString *EAcuteStr = [[[NSString alloc] initWithCharacters: &EAcuteChar
														 length: 1] autorelease];

	
	compRes = [@"e" compare: eAcuteStr];
	PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);

	compRes = [@"e" compare: EAcuteStr];
	PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);
	
	compRes = [@"e" localizedCompare: eAcuteStr];
	PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);
	
	compRes = [@"e" localizedCompare: EAcuteStr];
	PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);
	
	compRes = [eAcuteStr compare: EAcuteStr];
	PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);	
	
	compRes = [eAcuteStr localizedCompare: EAcuteStr];
	PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);

	// test NSCaseInsensitiveSearch
	
	compRes = [@"e" compare: eAcuteStr options: NSCaseInsensitiveSearch];
	PASS(compRes == NSOrderedAscending, "expected -1 got %d", (int)compRes);

	compRes = [eAcuteStr compare: EAcuteStr options: NSCaseInsensitiveSearch];
	PASS(compRes == NSOrderedSame, "expected 0 got %d", (int)compRes);	

	// test NSDiacriticInsensitiveSearch
	
	compRes = [@"e" compare: eAcuteStr options: NSDiacriticInsensitiveSearch];
	PASS(compRes == NSOrderedSame, "expected 0 got %d", (int)compRes);
	
	compRes = [@"e" compare: EAcuteStr options: NSDiacriticInsensitiveSearch];
	PASS(compRes == NSOrderedDescending, "expected 1 got %d", (int)compRes);

	// test NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch
	
	compRes = [@"e" compare: eAcuteStr options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
	PASS(compRes == NSOrderedSame, "expected 0 got %d", (int)compRes);
	
	compRes = [@"e" compare: EAcuteStr options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
	PASS(compRes == NSOrderedSame, "expected 0 got %d", (int)compRes);
	
	compRes = [@"E" compare: eAcuteStr options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
	PASS(compRes == NSOrderedSame, "expected 0 got %d", (int)compRes);
	
	compRes = [@"E" compare: EAcuteStr options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
	PASS(compRes == NSOrderedSame, "expected 0 got %d", (int)compRes);
	
	compRes = [EAcuteStr compare: eAcuteStr options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
	PASS(compRes == NSOrderedSame, "expected 0 got %d", (int)compRes);
}

int main()
{
  START_SET("NSString + locale")
  
  if (!NSLOCALE_SUPPORTED)
    SKIP("NSLocale not supported\nThe ICU library was not available when GNUstep-base was built")

  testHopeful = YES;
  
  testBasic();
  testEszett();
  testLithuanian();
  testDiacritics();

  END_SET("NSString + locale")

  return 0;
}