1 | #import <Foundation/Foundation.h> |
2 | #include <unistd.h> |
3 | #import "my-base.h" |
4 | |
5 | @interface MyString : MyBase { |
6 | NSString *str; |
7 | NSDate *date; |
8 | BOOL _desc_pauses; |
9 | } |
10 | |
11 | @property(retain) NSString * str_property; |
12 | @property BOOL descriptionPauses; |
13 | |
14 | - (id)initWithNSString:(NSString *)string; |
15 | @end |
16 | |
17 | @implementation MyString |
18 | @synthesize descriptionPauses = _desc_pauses; |
19 | @synthesize str_property = str; |
20 | |
21 | - (id)initWithNSString:(NSString *)string |
22 | { |
23 | if (self = [super init]) |
24 | { |
25 | str = [NSString stringWithString:string]; |
26 | date = [NSDate date]; |
27 | } |
28 | self.descriptionPauses = NO; |
29 | return self; |
30 | } |
31 | |
32 | - (void)dealloc |
33 | { |
34 | [date release]; |
35 | [str release]; |
36 | [super dealloc]; |
37 | } |
38 | |
39 | - (NSString *)description |
40 | { |
41 | // Set a breakpoint on '-[MyString description]' and test expressions: |
42 | // expression (char *)sel_getName(_cmd) |
43 | if (self.descriptionPauses) // Break here for description test |
44 | { |
45 | printf ("\nAbout to sleep.\n" ); |
46 | usleep(useconds: 100000); |
47 | } |
48 | |
49 | return [str stringByAppendingFormat:@" with timestamp: %@" , date]; |
50 | } |
51 | @end |
52 | |
53 | int |
54 | Test_Selector () |
55 | { |
56 | SEL sel = @selector(length); |
57 | printf("sel = %p\n" , sel); |
58 | // Expressions to test here for selector: |
59 | // expression (char *)sel_getName(sel) |
60 | // The expression above should return "sel" as it should be just |
61 | // a uniqued C string pointer. We were seeing the result pointer being |
62 | // truncated with recent LLDBs. |
63 | return 0; // Break here for selector: tests |
64 | } |
65 | |
66 | int |
67 | Test_NSString (const char *program) |
68 | { |
69 | NSString *str = [NSString stringWithFormat:@"Hello from '%s'" , program]; |
70 | NSLog(@"NSString instance: %@" , str); |
71 | printf("str = '%s'\n" , [str cStringUsingEncoding: [NSString defaultCStringEncoding]]); |
72 | printf("[str length] = %zu\n" , (size_t)[str length]); |
73 | printf("[str description] = %s\n" , [[str description] UTF8String]); |
74 | id str_id = str; |
75 | // Expressions to test here for NSString: |
76 | // expression (char *)sel_getName(sel) |
77 | // expression [str length] |
78 | // expression [str_id length] |
79 | // expression [str description] |
80 | // expression [str_id description] |
81 | // expression str.length |
82 | // expression str.description |
83 | // expression str = @"new" |
84 | // expression str = [NSString stringWithFormat: @"%cew", 'N'] |
85 | return 0; // Break here for NSString tests |
86 | } |
87 | |
88 | NSString *my_global_str = NULL; |
89 | |
90 | void |
91 | Test_MyString (const char *program) |
92 | { |
93 | my_global_str = @"This is a global string" ; |
94 | NSString *str = [NSString stringWithFormat:@"Hello from '%s'" , program]; |
95 | MyString *my = [[MyString alloc] initWithNSString:str]; |
96 | NSLog(@"MyString instance: %@" , [my description]); |
97 | my.descriptionPauses = YES; // Set break point at this line. Test 'expression -o -- my'. |
98 | NSLog(@"MyString instance: %@" , [my description]); |
99 | } |
100 | |
101 | int |
102 | Test_NSArray () |
103 | { |
104 | NSMutableArray *nil_mutable_array = nil; |
105 | NSArray *array1 = [NSArray arrayWithObjects: @"array1 object1" , @"array1 object2" , @"array1 object3" , nil]; |
106 | NSArray *array2 = [NSArray arrayWithObjects: array1, @"array2 object2" , @"array2 object3" , nil]; |
107 | // Expressions to test here for NSArray: |
108 | // expression [nil_mutable_array count] |
109 | // expression [array1 count] |
110 | // expression array1.count |
111 | // expression [array2 count] |
112 | // expression array2.count |
113 | id obj; |
114 | // After each object at index call, use expression and validate object |
115 | obj = [array1 objectAtIndex: 0]; // Break here for NSArray tests |
116 | obj = [array1 objectAtIndex: 1]; |
117 | obj = [array1 objectAtIndex: 2]; |
118 | |
119 | obj = [array2 objectAtIndex: 0]; |
120 | obj = [array2 objectAtIndex: 1]; |
121 | obj = [array2 objectAtIndex: 2]; |
122 | NSUInteger count = [nil_mutable_array count]; |
123 | return 0; |
124 | } |
125 | |
126 | |
127 | int main (int argc, char const *argv[]) |
128 | { |
129 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
130 | Test_Selector(); |
131 | Test_NSArray (); |
132 | Test_NSString (program: argv[0]); |
133 | Test_MyString (program: argv[0]); |
134 | |
135 | printf("sizeof(id) = %zu\n" , sizeof(id)); |
136 | printf("sizeof(Class) = %zu\n" , sizeof(Class)); |
137 | printf("sizeof(SEL) = %zu\n" , sizeof(SEL)); |
138 | |
139 | [pool release]; |
140 | return 0; |
141 | } |
142 | |