File: test08.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (65 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download | duplicates (2)
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
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>


@interface NSString (NumericSort)
- (NSComparisonResult)numericCompare: (NSString *)s;
@end

@implementation NSString (NumericSort)
- (NSComparisonResult)numericCompare: (NSString *)s
{
  return [self compare: s options: NSNumericSearch];
}
@end


int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  NSArray *a1, *a2;
  NSArray *a;
  NSString *s1, *s2;
  
  s1 = @"1";
  s2 = @"2";
  PASS([s1 compare: s2 options: NSNumericSearch] == NSOrderedAscending,
    "1 is less than 2");

  s1 = @"1";
  s2 = @"10";
  PASS([s1 compare: s2 options: NSNumericSearch] == NSOrderedAscending,
    "1 is less than 10");

  s1 = @"1";
  s2 = @"11";
  PASS([s1 compare: s2 options: NSNumericSearch] == NSOrderedAscending,
    "1 is less than 11");
  
  s1 = @"11";
  s2 = @"1";
  PASS([s1 compare: s2 options: NSNumericSearch] == NSOrderedDescending,
    "11 is greater than 1");
  
  s1 = @"11";
  s2 = @"2";
  PASS([s1 compare: s2 options: NSNumericSearch] == NSOrderedDescending,
    "11 is greater than 2");
  
  a1 = [NSArray arrayWithObjects:
    @"2", @"1", @"10", @"11", @"20", @"3", nil];

  a = [NSArray arrayWithObjects:
    @"1", @"10", @"11", @"2", @"20", @"3", nil];
  a2 = [a1 sortedArrayUsingSelector: @selector(compare:)];
  PASS_EQUAL(a2, a, "text sort");

  a = [NSArray arrayWithObjects:
    @"1", @"2", @"3", @"10", @"11", @"20", nil];
  a2 = [a1 sortedArrayUsingSelector: @selector(numericCompare:)];
  PASS_EQUAL(a2, a, "numeric sort");
  
  [pool drain];
  return 0;
}