|
| 1 | +#import "Testing.h" |
| 2 | +#import <Foundation/NSAutoreleasePool.h> |
| 3 | +#import <Foundation/NSBundle.h> |
| 4 | +#import <Foundation/NSCondition.h> |
| 5 | +#import <Foundation/NSDate.h> |
| 6 | +#import <Foundation/NSFileManager.h> |
| 7 | +#import <Foundation/NSPathUtilities.h> |
| 8 | +#import <Foundation/NSThread.h> |
| 9 | +#import <Foundation/NSString.h> |
| 10 | + |
| 11 | +static NSString *bundlePath = nil; |
| 12 | +static NSCondition *condition = nil; |
| 13 | +static int remainingThreads = 0; |
| 14 | +static int invalidResultCount = 0; |
| 15 | +static int localizedResultCount = 0; |
| 16 | +static int fallbackResultCount = 0; |
| 17 | + |
| 18 | +static const int workerCount = 8; |
| 19 | +static const int iterationsPerWorker = 2500; |
| 20 | + |
| 21 | +@interface LocalizationWorker: NSObject |
| 22 | ++ (void) run: (id)unused; |
| 23 | +@end |
| 24 | + |
| 25 | +@implementation LocalizationWorker |
| 26 | + |
| 27 | ++ (void) run: (id)unused |
| 28 | +{ |
| 29 | + NSAutoreleasePool *arp = [NSAutoreleasePool new]; |
| 30 | + int localInvalid = 0; |
| 31 | + int localLocalized = 0; |
| 32 | + int localFallback = 0; |
| 33 | + int i; |
| 34 | + |
| 35 | + for (i = 0; i < iterationsPerWorker; i++) |
| 36 | + { |
| 37 | + NSBundle *bundle = [[NSBundle alloc] initWithPath: bundlePath]; |
| 38 | + NSString *str; |
| 39 | + |
| 40 | + if (nil == bundle) |
| 41 | + { |
| 42 | + localInvalid++; |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + str = [bundle localizedStringForKey: @"test" |
| 47 | + value: @"test" |
| 48 | + table: nil]; |
| 49 | + if ([str isEqualToString: @"42"]) |
| 50 | + { |
| 51 | + localLocalized++; |
| 52 | + } |
| 53 | + else if ([str isEqualToString: @"test"]) |
| 54 | + { |
| 55 | + localFallback++; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + localInvalid++; |
| 60 | + } |
| 61 | + |
| 62 | + str = [bundle localizedStringForKey: @"test" |
| 63 | + value: @"test" |
| 64 | + table: @"Localizable.strings"]; |
| 65 | + if ([str isEqualToString: @"42"]) |
| 66 | + { |
| 67 | + localLocalized++; |
| 68 | + } |
| 69 | + else if ([str isEqualToString: @"test"]) |
| 70 | + { |
| 71 | + localFallback++; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + localInvalid++; |
| 76 | + } |
| 77 | + |
| 78 | + [bundle release]; |
| 79 | + } |
| 80 | + |
| 81 | + [condition lock]; |
| 82 | + invalidResultCount += localInvalid; |
| 83 | + localizedResultCount += localLocalized; |
| 84 | + fallbackResultCount += localFallback; |
| 85 | + remainingThreads--; |
| 86 | + [condition signal]; |
| 87 | + [condition unlock]; |
| 88 | + |
| 89 | + [arp release]; |
| 90 | +} |
| 91 | + |
| 92 | +@end |
| 93 | + |
| 94 | +int main() |
| 95 | +{ |
| 96 | + NSAutoreleasePool *arp = [NSAutoreleasePool new]; |
| 97 | + NSString *path; |
| 98 | + NSBundle *bundle; |
| 99 | + NSDate *timeout; |
| 100 | + int i; |
| 101 | + |
| 102 | + START_SET("NSBundle localizedStringForKey concurrency") |
| 103 | + |
| 104 | + path = [[[[[NSFileManager defaultManager] currentDirectoryPath] |
| 105 | + stringByStandardizingPath] stringByAppendingPathComponent: @"Resources"] |
| 106 | + stringByAppendingPathComponent: @"TestBundle.bundle"]; |
| 107 | + bundle = [NSBundle bundleWithPath: path]; |
| 108 | + if (nil == bundle) |
| 109 | + { |
| 110 | + SKIP("test bundle not available"); |
| 111 | + } |
| 112 | + |
| 113 | + bundlePath = [path copy]; |
| 114 | + condition = [NSCondition new]; |
| 115 | + remainingThreads = workerCount; |
| 116 | + invalidResultCount = 0; |
| 117 | + localizedResultCount = 0; |
| 118 | + fallbackResultCount = 0; |
| 119 | + |
| 120 | + for (i = 0; i < workerCount; i++) |
| 121 | + { |
| 122 | + [NSThread detachNewThreadSelector: @selector(run:) |
| 123 | + toTarget: [LocalizationWorker class] |
| 124 | + withObject: nil]; |
| 125 | + } |
| 126 | + |
| 127 | + timeout = [NSDate dateWithTimeIntervalSinceNow: 60.0]; |
| 128 | + [condition lock]; |
| 129 | + while (remainingThreads > 0) |
| 130 | + { |
| 131 | + if (NO == [condition waitUntilDate: timeout]) |
| 132 | + { |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + PASS(remainingThreads == 0, "all worker threads completed"); |
| 137 | + [condition unlock]; |
| 138 | + |
| 139 | + PASS(invalidResultCount == 0, |
| 140 | + "localizedStringForKey returned only expected values"); |
| 141 | + PASS(localizedResultCount > 0, "localized lookups succeeded"); |
| 142 | + PASS((localizedResultCount + fallbackResultCount) |
| 143 | + == (workerCount * iterationsPerWorker * 2), |
| 144 | + "all lookups accounted for"); |
| 145 | + |
| 146 | + [condition release]; |
| 147 | + condition = nil; |
| 148 | + [bundlePath release]; |
| 149 | + bundlePath = nil; |
| 150 | + |
| 151 | + END_SET("NSBundle localizedStringForKey concurrency") |
| 152 | + |
| 153 | + [arp release]; |
| 154 | + return 0; |
| 155 | +} |
0 commit comments