|
| 1 | +//===--- SwiftNativeNSBase.m - Test _SwiftNativeNS*Base classes -----------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// This file is compiled and run by SwiftNativeNSBase.swift. |
| 14 | + |
| 15 | +#include <Foundation/Foundation.h> |
| 16 | +#include <objc/runtime.h> |
| 17 | + |
| 18 | +static int Errors; |
| 19 | + |
| 20 | +#define expectTrue(expr) \ |
| 21 | + do { \ |
| 22 | + if (!(expr)) { \ |
| 23 | + printf("%s:%d: not true: %s\n", __FILE__, __LINE__, #expr); \ |
| 24 | + Errors++; \ |
| 25 | + } \ |
| 26 | + } while (0) |
| 27 | + |
| 28 | +#define expectFalse(expr) \ |
| 29 | + do { \ |
| 30 | + if (expr) { \ |
| 31 | + printf("%s:%d: not false: %s\n", __FILE__, __LINE__, #expr); \ |
| 32 | + Errors++; \ |
| 33 | + } \ |
| 34 | + } while (0) |
| 35 | + |
| 36 | +#define fail(format, ...) \ |
| 37 | + do { \ |
| 38 | + printf("%s:%d: " format, __FILE__, __LINE__, ##__VA_ARGS__); \ |
| 39 | + Errors++; \ |
| 40 | + } while (0) |
| 41 | + |
| 42 | + |
| 43 | +void TestSwiftNativeNSBase(void) |
| 44 | +{ |
| 45 | + printf("TestSwiftNativeNSBase\n"); |
| 46 | + |
| 47 | + unsigned int classCount; |
| 48 | + Class *classes = objc_copyClassList(&classCount); |
| 49 | + |
| 50 | + NSMutableSet *expectedClasses = |
| 51 | + [NSMutableSet setWithObjects: |
| 52 | + @"_SwiftNativeNSArrayBase", |
| 53 | + @"_SwiftNativeNSDictionaryBase", |
| 54 | + @"_SwiftNativeNSSetBase", |
| 55 | + @"_SwiftNativeNSStringBase", |
| 56 | + @"_SwiftNativeNSEnumeratorBase", |
| 57 | + @"_SwiftNativeNSDataBase", |
| 58 | + @"_SwiftNativeNSCharacterSetBase", |
| 59 | + @"_SwiftNativeNSIndexSetBase", |
| 60 | + nil]; |
| 61 | + |
| 62 | + for (unsigned int i = 0; i < classCount; i++) { |
| 63 | + Class cls = classes[i]; |
| 64 | + NSString *name = @(class_getName(cls)); |
| 65 | + if (! ([name hasPrefix:@"_SwiftNativeNS"] && [name hasSuffix:@"Base"])) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + if (! [expectedClasses containsObject:name]) { |
| 69 | + fail("did not expect class %s\n", name.UTF8String); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // cls is some _SwiftNativeNS*Base class |
| 74 | + [expectedClasses removeObject:name]; |
| 75 | + printf("checking class %s\n", name.UTF8String); |
| 76 | + |
| 77 | + // Check for unwanted C++ cdtors (rdar://18950072) |
| 78 | + expectFalse([cls instancesRespondToSelector:sel_registerName(".cxx_construct")]); |
| 79 | + expectFalse([cls instancesRespondToSelector:sel_registerName(".cxx_destruct")]); |
| 80 | + } |
| 81 | + |
| 82 | + expectTrue(expectedClasses.count == 0); |
| 83 | + |
| 84 | + printf("TestSwiftNativeNSBase: %d error%s\n", |
| 85 | + Errors, Errors == 1 ? "" : "s"); |
| 86 | + exit(Errors ? 1 : 0); |
| 87 | +} |
0 commit comments