File: basic.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 (90 lines) | stat: -rw-r--r-- 3,071 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
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
#import <Foundation/NSException.h>
#import <Foundation/NSAutoreleasePool.h>
#import "ObjectTesting.h"

static void
handler(NSException *e)
{
  PASS (YES == [[e reason] isEqual: @"Terminate"],
    "uncaught exceptionhandler called as expected");
  abort();
}

@interface      MyClass : NSObject
+ (void) testAbc;
@end
@implementation MyClass
+ (void) simulateProblem
{
  [NSException raise: NSGenericException format: @"In MyClass"];
}
+ (void) testAbc
{
  [self simulateProblem];
}
@end

int main()
{
  NSException *obj;
  NSMutableArray *testObjs = [[NSMutableArray alloc] init];
  NSAutoreleasePool   *arp = [NSAutoreleasePool new];

  test_alloc_only(@"NSException"); 
  obj = [NSException exceptionWithName: NSGenericException
                                reason: nil
                              userInfo: nil];
  PASS((obj != nil), "can create an exception");
  PASS(([[obj name] isEqualToString: NSGenericException]), "name works");
  obj = [NSException exceptionWithName: NSGenericException
                                reason: nil
                              userInfo: nil];
  [testObjs addObject: obj];
  test_NSObject(@"NSException", testObjs);
  
  NS_DURING
    [MyClass testAbc];
  NS_HANDLER
    {
      NSArray   *addresses = [localException callStackReturnAddresses];
      NSArray   *a = [localException callStackSymbols];
      NSString  *s = nil;
      BOOL	ok = YES;

      PASS([addresses count] > 0, "call stack addresses is not empty");
      PASS([addresses count] == [a count], "addresses and symbols match");

NSLog(@"Got %@", a);
      testHopeful = YES;
      PASS([a count] > 0
	&& [(s = [a objectAtIndex: 0]) rangeOfString: @"NSException"].length > 0
	&& [s rangeOfString: @"raise"].length > 0,
	"Exception raised at start of stack")
      PASS([a count] > 1
	&& [(s = [a objectAtIndex: 1]) rangeOfString: @"MyClass"].length > 0
	&& [s rangeOfString: @"simulateProblem"].length > 0,
	"simulateProblem is where exception was raised")
      if (NO == testPassed) ok = NO;
      PASS([a count] > 2
	&& [(s = [a objectAtIndex: 2]) rangeOfString: @"MyClass"].length > 0
	&& [s rangeOfString: @"testAbc"].length > 0,
	"testAbc called simulateProblem to raise exception")
      if (NO == testPassed) ok = NO;

      PASS(ok, "working callStackSymbols ... if this has failed it is probably due to a lack of support for objective-c method names (local symbols) in the backtrace_symbols() function of your libc. If so, you might lobby your operating system provider for a fix.");
      testHopeful = NO;
    }
  NS_ENDHANDLER

  PASS(NSGetUncaughtExceptionHandler() == 0, "default handler is null");
  NSSetUncaughtExceptionHandler(handler);
  PASS(NSGetUncaughtExceptionHandler() == handler, "setting handler works");

  fprintf(stderr, "We expect a single FAIL without any explanation as\n"
    "the test is terminated by an uncaught exception ...\n");
  [NSException raise: NSGenericException format: @"Terminate"];
  PASS(NO, "shouldn't get here ... exception should have terminated process");

  [arp release]; arp = nil;
  return 0;
}