Skip to content

Instantly share code, notes, and snippets.

@takuma104
Created November 11, 2008 11:38
Show Gist options
  • Save takuma104/23807 to your computer and use it in GitHub Desktop.
Save takuma104/23807 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
static const char* chk(int n)
{
return n ? "passed" : "failed";
}
static void test1(NSString *v1, NSString *v2, NSString *v3)
{
int r1 = [v1 compare:v2 options:NSNumericSearch];
int r2 = [v2 compare:v2 options:NSNumericSearch];
int r3 = [v2 compare:v3 options:NSNumericSearch];
NSLog(@"(%s) [@\"%@\" compare:@\"%@\" options:NSNumericSearch] = %d", chk(r1==-1), v1, v2, r1);
NSLog(@"(%s) [@\"%@\" compare:@\"%@\" options:NSNumericSearch] = %d", chk(r2== 0), v2, v2, r2);
NSLog(@"(%s) [@\"%@\" compare:@\"%@\" options:NSNumericSearch] = %d", chk(r3==-1), v2, v3, r3);
}
int main(int argc, char *argv[])
{
test1( @"1", @"2", @"3");
test1( @"999999999", @"1000000000", @"1000000001");
test1(@"2147483646", @"2147483647", @"2147483648"); // int32_t max
test1(@"4294967294", @"4294967295", @"4294967296"); // uint32_t max
test1(@"9223372036854775806", @"9223372036854775807", @"9223372036854775808"); // int64_t max
test1(@"999999999999999999999999999999", @"1000000000000000000000000000000", @"1000000000000000000000000000001");
}
/* result: (Simulator)
2008-11-11 20:34:36.395 comptest[11989:20b] (passed) [@"1" compare:@"2" options:NSNumericSearch] = -1
2008-11-11 20:34:36.397 comptest[11989:20b] (passed) [@"2" compare:@"2" options:NSNumericSearch] = 0
2008-11-11 20:34:36.399 comptest[11989:20b] (passed) [@"2" compare:@"3" options:NSNumericSearch] = -1
2008-11-11 20:34:36.400 comptest[11989:20b] (passed) [@"999999999" compare:@"1000000000" options:NSNumericSearch] = -1
2008-11-11 20:34:36.401 comptest[11989:20b] (passed) [@"1000000000" compare:@"1000000000" options:NSNumericSearch] = 0
2008-11-11 20:34:36.402 comptest[11989:20b] (passed) [@"1000000000" compare:@"1000000001" options:NSNumericSearch] = -1
2008-11-11 20:34:36.403 comptest[11989:20b] (passed) [@"2147483646" compare:@"2147483647" options:NSNumericSearch] = -1
2008-11-11 20:34:36.403 comptest[11989:20b] (passed) [@"2147483647" compare:@"2147483647" options:NSNumericSearch] = 0
2008-11-11 20:34:36.404 comptest[11989:20b] (passed) [@"2147483647" compare:@"2147483648" options:NSNumericSearch] = -1
2008-11-11 20:34:36.405 comptest[11989:20b] (passed) [@"4294967294" compare:@"4294967295" options:NSNumericSearch] = -1
2008-11-11 20:34:36.405 comptest[11989:20b] (passed) [@"4294967295" compare:@"4294967295" options:NSNumericSearch] = 0
2008-11-11 20:34:36.407 comptest[11989:20b] (passed) [@"4294967295" compare:@"4294967296" options:NSNumericSearch] = -1
2008-11-11 20:34:36.409 comptest[11989:20b] (passed) [@"9223372036854775806" compare:@"9223372036854775807" options:NSNumericSearch] = -1
2008-11-11 20:34:36.410 comptest[11989:20b] (passed) [@"9223372036854775807" compare:@"9223372036854775807" options:NSNumericSearch] = 0
2008-11-11 20:34:36.436 comptest[11989:20b] (passed) [@"9223372036854775807" compare:@"9223372036854775808" options:NSNumericSearch] = -1
2008-11-11 20:34:36.437 comptest[11989:20b] (passed) [@"999999999999999999999999999999" compare:@"1000000000000000000000000000000" options:NSNumericSearch] = -1
2008-11-11 20:34:36.438 comptest[11989:20b] (passed) [@"1000000000000000000000000000000" compare:@"1000000000000000000000000000000" options:NSNumericSearch] = 0
2008-11-11 20:34:36.438 comptest[11989:20b] (passed) [@"1000000000000000000000000000000" compare:@"1000000000000000000000000000001" options:NSNumericSearch] = -1
*/
/* result: iPod touch (2.1)
2008-11-11 20:36:39.481 comptest[1135:20b] (passed) [@"1" compare:@"2" options:NSNumericSearch] = -1
2008-11-11 20:36:39.497 comptest[1135:20b] (passed) [@"2" compare:@"2" options:NSNumericSearch] = 0
2008-11-11 20:36:39.509 comptest[1135:20b] (passed) [@"2" compare:@"3" options:NSNumericSearch] = -1
2008-11-11 20:36:39.520 comptest[1135:20b] (passed) [@"999999999" compare:@"1000000000" options:NSNumericSearch] = -1
2008-11-11 20:36:39.531 comptest[1135:20b] (passed) [@"1000000000" compare:@"1000000000" options:NSNumericSearch] = 0
2008-11-11 20:36:39.542 comptest[1135:20b] (passed) [@"1000000000" compare:@"1000000001" options:NSNumericSearch] = -1
2008-11-11 20:36:39.553 comptest[1135:20b] (passed) [@"2147483646" compare:@"2147483647" options:NSNumericSearch] = -1
2008-11-11 20:36:39.564 comptest[1135:20b] (passed) [@"2147483647" compare:@"2147483647" options:NSNumericSearch] = 0
2008-11-11 20:36:39.575 comptest[1135:20b] (passed) [@"2147483647" compare:@"2147483648" options:NSNumericSearch] = -1
2008-11-11 20:36:39.586 comptest[1135:20b] (passed) [@"4294967294" compare:@"4294967295" options:NSNumericSearch] = -1
2008-11-11 20:36:39.597 comptest[1135:20b] (passed) [@"4294967295" compare:@"4294967295" options:NSNumericSearch] = 0
2008-11-11 20:36:39.607 comptest[1135:20b] (passed) [@"4294967295" compare:@"4294967296" options:NSNumericSearch] = -1
2008-11-11 20:36:39.618 comptest[1135:20b] (passed) [@"9223372036854775806" compare:@"9223372036854775807" options:NSNumericSearch] = -1
2008-11-11 20:36:39.629 comptest[1135:20b] (passed) [@"9223372036854775807" compare:@"9223372036854775807" options:NSNumericSearch] = 0
2008-11-11 20:36:39.640 comptest[1135:20b] (passed) [@"9223372036854775807" compare:@"9223372036854775808" options:NSNumericSearch] = -1
2008-11-11 20:36:39.651 comptest[1135:20b] (passed) [@"999999999999999999999999999999" compare:@"1000000000000000000000000000000" options:NSNumericSearch] = -1
2008-11-11 20:36:39.662 comptest[1135:20b] (passed) [@"1000000000000000000000000000000" compare:@"1000000000000000000000000000000" options:NSNumericSearch] = 0
2008-11-11 20:36:39.673 comptest[1135:20b] (passed) [@"1000000000000000000000000000000" compare:@"1000000000000000000000000000001" options:NSNumericSearch] = -1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment