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//  UnregisteredThread.m
22//  Copyright (c) 2008-2001 Apple Inc. All rights reserved.
23//
24
25#import <objc/objc-auto.h>
26#import "BlackBoxTest.h"
27#import "WhiteBoxTest.h"
28
29@interface UnregisteredThread : BlackBoxTest
30{
31    pthread_t _thread;
32    BOOL _sawWarning;
33}
34@end
35
36@interface RegisteredThread : BlackBoxTest
37{
38    pthread_t _thread;
39}
40@end
41
42static void *registerTest(UnregisteredThread *tester)
43{
44    @synchronized (tester) {
45        auto_zone_register_thread(objc_collectableZone());
46        auto_zone_assert_thread_registered(objc_collectableZone());
47    }
48    return NULL;
49}
50
51static void *unregisterTest(UnregisteredThread *tester)
52{
53    @synchronized (tester) {
54        auto_zone_assert_thread_registered(objc_collectableZone());
55    }
56    return NULL;
57}
58
59
60@implementation UnregisteredThread
61
62- (void)processOutputLine:(NSString *)line
63{
64    NSString *expectedString = @"error: GC operation on unregistered thread. Thread registered implicitly. Break on auto_zone_thread_registration_error() to debug.";
65    NSRange r = [line rangeOfString:expectedString];
66    if (r.location == NSNotFound) {
67        [super processOutputLine:line];
68    } else {
69        _sawWarning = YES;
70    }
71}
72
73- (void)performTest
74{
75    if (pthread_create(&_thread, NULL, (void *(*)(void *))unregisterTest, self) != 0) {
76        [self fail:@"failed to create thread\n"];
77        return;
78    }
79    pthread_join(_thread, NULL);
80    [self testFinished];
81}
82
83- (void)outputComplete
84{
85    if (_sawWarning)
86        [self passed];
87    else
88        [self fail:@"did not see thread registration warning"];
89    [super outputComplete];
90}
91
92@end
93
94@implementation RegisteredThread
95
96- (void)performTest
97{
98    if (pthread_create(&_thread, NULL, (void *(*)(void *))registerTest, self) != 0) {
99        [self fail:@"failed to create thread\n"];
100        return;
101    }
102    pthread_join(_thread, NULL);
103    [self passed];
104    [self testFinished];
105}
106
107@end
108
109