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 (184 lines) | stat: -rw-r--r-- 5,817 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
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
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>

@interface CustomString : NSString
{
  unichar *characters;
  NSUInteger length;
}
@end

@implementation CustomString

- (id) initWithBytesNoCopy: (void *)c
		    length: (NSUInteger)l
		  encoding: (NSStringEncoding)encoding
	      freeWhenDone: (BOOL)freeWhenDone
{
  if (l > 0)
    {
      if (encoding == NSUnicodeStringEncoding)
	{
	  characters = malloc(l);
	  memcpy(characters, c, l);
	}
      else
	{
	  NSString	*s;

	  s = [[NSString alloc] initWithBytesNoCopy: c
					     length: l
					   encoding: encoding
				       freeWhenDone: freeWhenDone];
	  if (s == nil) {RELEASE(self); return nil;}
	  l = [s length] * sizeof(unichar);
	  characters = malloc(l);
	  [s getCharacters: characters];
	  [s release];
	}
    }
  length = l / sizeof(unichar);
  return self;
}

- (void) dealloc
{
  if (characters)
    {
      free(characters);
      characters = NULL;
    }
  [super dealloc];
}

- (NSUInteger) length
{
  return length;
}

- (unichar) characterAtIndex: (NSUInteger)index
{
  return characters[index];
}
@end


int main()
{
  ENTER_POOL
  unichar	u0 = 'a';
  unichar	u1 = 0xfe66;
  NSMutableString *testObj,*base,*ext,*want, *str1, *str2;
  unichar chars[3];

  test_alloc(@"NSMutableString");
  testObj = AUTORELEASE([[NSMutableString alloc] initWithCString:"Hello\n"]);
  test_NSCoding([NSArray arrayWithObject: testObj]);
  test_keyed_NSCoding([NSArray arrayWithObject: testObj]);
  test_NSCopying(@"NSString",@"NSMutableString",
                 [NSArray arrayWithObject: testObj],NO,NO); 
  test_NSMutableCopying(@"NSString",@"NSMutableString",
                        [NSArray arrayWithObject: testObj]);
 
  base = [NSMutableString stringWithCString:"hello"];
  ext = [@"\"\\UFE66???\"" propertyList];
  want = [@"\"hello\\UFE66???\"" propertyList];
  [base appendString:ext];
  PASS([base length] == 9 && [ext length] == 4
    && [want length] == 9 && [base isEqual:want],
    "We can append a unicode string to a C string");

  PASS([AUTORELEASE([[NSMutableString alloc] initWithCharacters: &u0 length: 1])
    isKindOfClass: [NSMutableString class]],
    "initWithCharacters:length: creates mutable string for ascii");

  PASS([AUTORELEASE([[NSMutableString alloc] initWithCharacters: &u1 length: 1])
    isKindOfClass: [NSMutableString class]],
    "initWithCharacters:length: creates mutable string for unicode");

  PASS_RUNS([[NSMutableString stringWithString: @"foo"]
		  			appendString: @"bar"];,
		"can append to string from NSMutableString +stringWithString:");

  testObj = AUTORELEASE([@"hello" mutableCopy]);
  [testObj replaceCharactersInRange: NSMakeRange(1,1) withString: @"a"];
  PASS([testObj isEqual: @"hallo"],
    "replaceCharactersInRange:withString: works in middle of string");
  [testObj replaceCharactersInRange: NSMakeRange(4,1) withString: @"y"];
  PASS([testObj isEqual: @"hally"],
    "replaceCharactersInRange:withString: works at end of string");

  [testObj setString: @"hello"];
  [testObj replaceCharactersInRange: NSMakeRange(1,1)
			 withString: [CustomString stringWithCString: "a"]];
  PASS([testObj isEqual: @"hallo"],
    "custom string replacement works in middle of string");
  [testObj replaceCharactersInRange: NSMakeRange(4,1)
			 withString: [CustomString stringWithCString: "y"]];
  PASS([testObj isEqual: @"hally"],
    "custom string replacement works at end of string");

  chars[0] = '\"';
  chars[1] = 1;
  str1 = [NSMutableString stringWithCharacters: chars length: 2];
  [str1 replaceOccurrencesOfString: @"\""
                       withString: @"\\\""
                          options: 0
                            range: NSMakeRange(0, [str1 length])];

  chars[0] = '\\';
  chars[1] = '\"';
  chars[2] = 1;
  str2 = [NSMutableString stringWithCharacters: chars length: 3];
  NSLog(@"string 1 %@ string 2 %@", str1, str2);
  PASS([str1 isEqual: str2],
    "string occurrences replacement works");
  
  [str1 setString: @"{Message} one"];
  [str1 replaceOccurrencesOfString: @"{Message}"
                        withString: @"The quick brown fox"
                           options: NSLiteralSearch
                             range: NSMakeRange(0, [str1 length])];
  PASS_EQUAL(str1, @"The quick brown fox one", "replace at start of string")

  [str1 setString: @"two {Message}"];
  [str1 replaceOccurrencesOfString: @"{Message}"
                        withString: @"The quick brown fox"
                           options: NSLiteralSearch
                             range: NSMakeRange(0, [str1 length])];
  PASS_EQUAL(str1, @"two The quick brown fox", "replace at end of string")

  [str1 setString: @"{Message}"];
  [str1 replaceOccurrencesOfString: @"{Message}"
                        withString: @"The quick brown fox"
                           options: NSLiteralSearch
                             range: NSMakeRange(0, [str1 length])];
  PASS_EQUAL(str1, @"The quick brown fox", "replace entire string works")

  /* Test for normalisation of combining characters.  The literal here has
   * a space combined with multiple repeated combining-tilde characters,
   * and that needs to match a single space plus combining tilde.
   */
  {
    NSString	*text; 
    NSString	*find;
    NSString	*str;
    NSRange	r;
    unichar	u;

    text = [NSString stringWithUTF8String: "Text Message ̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃̃"];

    find = [NSString stringWithUTF8String: " ̃"];

    r = [text rangeOfString: find];
    PASS(12 == r.location, "find combining-tilde")

    str = [text stringByReplacingOccurrencesOfString: find
					  withString: @""];
    PASS_EQUAL(str, @"Text Message", "remove combining-tilde")
  }

  LEAVE_POOL
  return 0;
}