File: enumerateLinesUsingBlock.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 (89 lines) | stat: -rw-r--r-- 2,461 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
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>

#import "Testing.h"

#if defined(__has_extension) && __has_extension(blocks)

BOOL testEnumerateSimpleLines() {
  NSString *testString = @"First line\nSecond line\nThird line";
  NSMutableArray *collectedLines = [NSMutableArray array];
  NSArray *expectedLines = @[@"First line", @"Second line", @"Third line"];

  [testString enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
    [collectedLines addObject: line];
  }];

  return [collectedLines isEqualToArray: expectedLines];
}

BOOL testEnumerateCRLFLines() {
  NSString *testString = @"First line\r\nSecond line\r\nThird line";
  NSMutableArray *collectedLines = [NSMutableArray array];
  NSArray *expectedLines = @[@"First line", @"Second line", @"Third line"];

  [testString enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
    [collectedLines addObject: line];
  }];

  return [collectedLines isEqualToArray: expectedLines];
}

BOOL testStopEarly() {
  NSString *testString = @"First line\nSecond line\nThird line";
  __block int lineCount = 0;

  [testString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    lineCount++;
    if ([line isEqualToString:@"Second line"]) {
      *stop = YES;
    }
  }];

  return lineCount == 2; // Should stop after the second line
}

BOOL testEmptyString() {
  NSString *testString = @"";
  __block BOOL blockCalled = NO;

  [testString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    blockCalled = YES;
  }];

  return !blockCalled; // Block should not be called
}

BOOL testSingleLineNoBreaks() {
  NSString *testString = @"Single line without line breaks";
  __block NSString *receivedLine = nil;

  [testString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    receivedLine = line;
  }];

  return [receivedLine isEqualToString:testString];
}

int main() {
  NSAutoreleasePool *arp = [NSAutoreleasePool new];

  PASS(testEnumerateSimpleLines(), "Should enumerate all lines correctly.");
  PASS(testEnumerateCRLFLines(), "Should enumerate all CRLF lines correctly.");
  PASS(testStopEarly(), "Should stop enumeration early as directed.");
  PASS(testEmptyString(), "Should not call block for empty string.");
  PASS(testSingleLineNoBreaks(), "Should handle single line without line breaks correctly.");

  [arp release];
  return 0;
}

#else

int main (int argc, const char * argv[])
{
  return 0;
}

#endif