1/*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20//
21//  CollectionChecking.m
22//  Copyright (c) 2010-2011 Apple Inc. All rights reserved.
23//
24
25#import <Foundation/Foundation.h>
26#import "BlackBoxTest.h"
27
28@interface CollectionCheckingBlock : BlackBoxTest
29{
30    __strong void *_testBlock;
31    BOOL _gotCallback;
32}
33@end
34
35@interface SubzoneBlockCollectionCheck : CollectionCheckingBlock
36@end
37
38@interface LargeBlockCollectionCheck : CollectionCheckingBlock
39@end
40
41@implementation CollectionCheckingBlock
42
43+ (BOOL)isConcreteTestCase
44{
45    return self != [CollectionCheckingBlock class];
46}
47
48- (__strong void *)allocateBlock
49{
50}
51
52- (void)testFinished
53{
54    _testBlock = nil;
55    auto_zone_disable_collection_checking([self auto_zone]);
56    [super testFinished];
57}
58
59- (void)processOutputLine:(NSString *)line
60{
61    if ([line rangeOfString:@"was not collected"].location != NSNotFound) {
62    } else {
63        [super processOutputLine:line];
64    }
65}
66
67- (void)outputComplete
68{
69    if (_gotCallback) {
70        [self passed];
71    } else {
72        [self fail:@"collection checking did not call callback with block"];
73    }
74    [super outputComplete];
75}
76
77- (id)callback
78{
79    return nil;
80}
81
82- (void)performTest
83{
84    _testBlock = [self allocateBlock];
85    malloc_zone_enable_discharge_checking([self auto_zone]);
86    malloc_zone_discharge([self auto_zone], _testBlock);
87    //auto_zone_enumerate_uncollected([self auto_zone], NULL);
88    malloc_zone_enumerate_discharged_pointers([self auto_zone], ^(void *block, void *info){
89        if (block == _testBlock) {
90            _gotCallback = YES;
91        }
92    });
93    [self testFinished];
94}
95@end
96
97@implementation SubzoneBlockCollectionCheck
98- (__strong void *)allocateBlock
99{
100    return NSAllocateObject([NSObject class], 0, NULL);
101}
102@end
103
104@implementation LargeBlockCollectionCheck
105- (__strong void *)allocateBlock
106{
107    return NSAllocateObject([NSObject class], 1024*1024, NULL);
108}
109@end
110