File: blocks.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 (88 lines) | stat: -rw-r--r-- 2,822 bytes parent folder | download | duplicates (7)
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
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSIndexSet.h>
#import <Foundation/NSLock.h>
#include <Testing.h>
int main()
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSMutableIndexSet *set = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(1,5)];
  [set addIndex:20];
  [set addIndex:25];


  START_SET("NSIndexSet Blocks")
# ifndef __has_feature
# define __has_feature(x) 0
# endif
# if __has_feature(blocks)

  NSMutableIndexSet *referenceSet = nil;
  __block NSMutableIndexSet *newSet = [NSMutableIndexSet indexSet];
  __block BOOL didContainWrongIndex = NO;
  __block NSUInteger lastIndex = NSNotFound;
  NSLock *setLock = [NSLock new];
  void(^enumBlock)(NSUInteger,BOOL*) = ^(NSUInteger idx, BOOL*shouldStop){
      [setLock lock];
      lastIndex = idx;
      [newSet addIndex: idx];
      [setLock unlock];
    };

  // Test forward enumeration:
  [set enumerateIndexesUsingBlock: enumBlock];
  PASS((YES == [set isEqual: newSet]),
    "Can enumerate all indices in an index set using a block");
  PASS((25 == lastIndex),
    "Forward enumeration stops at the last index");

  newSet = [NSMutableIndexSet indexSet];
  didContainWrongIndex = NO;
  lastIndex = NSNotFound;

  // Test reverse enumeration:
  [set enumerateIndexesWithOptions: NSEnumerationReverse
                        usingBlock: enumBlock];
  PASS((YES == [set isEqual: newSet]),
    "Can reverse enumerate all indices in an index set using a block");
  PASS((1 == lastIndex),
    "Reverse enumeration stops at the first index");

  referenceSet = [NSMutableIndexSet indexSetWithIndexesInRange: (NSMakeRange(4,2))];
  [referenceSet addIndex: 20];
  newSet = [NSMutableIndexSet indexSet];
  didContainWrongIndex = NO;
  lastIndex = NSNotFound;

  // Test subrange enumeration:
  [set enumerateIndexesInRange: NSMakeRange(4,20)
                       options: 0
                    usingBlock: enumBlock];
   PASS((YES == [referenceSet isEqual: newSet]), "Can enumerate subranges of an index set");
   PASS((20 == lastIndex), "Subrange enumeration stops at the correct index");

   newSet = [NSMutableIndexSet indexSet];
   lastIndex = NSNotFound;
   referenceSet = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(1,5)];
   [referenceSet addIndex: 20];
   enumBlock = ^(NSUInteger idx, BOOL*shouldStop){
       [setLock lock];
       [newSet addIndex: idx];
       [setLock unlock];
       if (20 == idx)
       {
	 *shouldStop = YES;
       }
     };

  // Test premature termination of enumeration:
  [set enumerateIndexesUsingBlock: enumBlock];
  PASS((YES == [referenceSet isEqual: newSet]), "block can prematurely terminate enumeration");

  [setLock release];
# else
  SKIP("No Blocks support in the compiler.")
# endif
  END_SET("NSIndexSet Blocks")
  [pool release];
  return 0;
}