summaryrefslogtreecommitdiff
path: root/gcc/testsuite/obj-c++.dg/torture/strings/const-cfstring-1.mm
blob: 2983c43c445a0e43b4c70c5459e2f669f26ae2b4 (plain)
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
/* Test the -fconstant-cfstrings option for constructing
   compile-time immutable CFStrings, and their interoperation
   with both Cocoa and CoreFoundation.  This will only work
   on MacOS X 10.1.2 and later.  */
/* Developed by Ziemowit Laski <zlaski@apple.com>.  */

/* So far, CFString is darwin-only.  */
/* { dg-do run { target *-*-darwin* } } */
/* { dg-skip-if "NeXT only" { *-*-* } { "-fgnu-runtime" } { "" } } */
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
/* { dg-options "-mconstant-cfstrings -framework Cocoa" } */
/* Darwin10's linker emits a warning that the constant strings are incompatible with writable ones.
   well, we don't implement writable ones at this juncture.  */
/* { dg-options "-mconstant-cfstrings -framework Cocoa -Wl,-w" { target *-*-darwin[123]* } } */

#import <Foundation/NSString.h>
#import <CoreFoundation/CFString.h>
#include <stdlib.h>

void printOut(NSString *str) {
  NSLog(@"The value of str is: %@", str);
}

CFStringRef s0a = CFSTR("Compile-time string literal");
CFStringRef s0b = CFSTR("Compile-time string literal");

void checkNSRange(NSRange r) {
  if (r.location != 6 || r.length != 5) {
    printOut(@"Range check failed");
    abort();
  }
}

void checkCFRange(CFRange r) {
  if (r.location != 6 || r.length != 5) {
    printOut(@"Range check failed");
    abort();
  }
}

int main(void) {
  const NSString *s1 = @"Compile-time string literal";
  CFStringRef s2 = CFSTR("Compile-time string literal");

  checkNSRange([@"Hello World" rangeOfString:@"World"]);
  checkNSRange([(id)CFSTR("Hello World") rangeOfString:@"World"]);
  checkNSRange([@"Hello World" rangeOfString:(id)CFSTR("World")]);
  checkNSRange([(id)CFSTR("Hello World") rangeOfString:(id)CFSTR("World")]);

  checkCFRange(CFStringFind((CFStringRef)@"Hello World", (CFStringRef)@"World", 0));
  checkCFRange(CFStringFind(CFSTR("Hello World"), (CFStringRef)@"World", 0));
  checkCFRange(CFStringFind((CFStringRef)@"Hello World", CFSTR("World"), 0));
  checkCFRange(CFStringFind(CFSTR("Hello World"), CFSTR("World"), 0));

  /* Check for string uniquing.  */
  if (s0a != s0b || s0a != s2 || s1 != (id)s2) {
    NSLog(@"String uniquing failed");
    abort ();
  }

  return 0;
}