github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/bind/objc/SeqTest.m (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 #import <Foundation/Foundation.h> 8 #import "GoTestpkg.h" 9 10 #define ERROR(...) \ 11 do { \ 12 NSLog(__VA_ARGS__); \ 13 err = 1; \ 14 } while (0); 15 16 static int err = 0; 17 18 void testConst() { 19 if (![GoTestpkgAString isEqualToString:@"a string"]) { 20 ERROR(@"GoTestpkgAString = %@, want 'a string'", GoTestpkgAString); 21 } 22 if (GoTestpkgAnInt != 7) { 23 ERROR(@"GoTestpkgAnInt = %lld, want 7", GoTestpkgAnInt); 24 } 25 if (ABS(GoTestpkgAFloat - 0.12345) > 0.0001) { 26 ERROR(@"GoTestpkgAFloat = %f, want 0.12345", GoTestpkgAFloat); 27 } 28 if (GoTestpkgABool != YES) { 29 ERROR(@"GoTestpkgABool = %@, want YES", GoTestpkgAFloat ? @"YES" : @"NO"); 30 } 31 32 if (GoTestpkgMinInt32 != INT32_MIN) { 33 ERROR(@"GoTestpkgMinInt32 = %d, want %d", GoTestpkgMinInt32, INT32_MIN); 34 } 35 if (GoTestpkgMaxInt32 != INT32_MAX) { 36 ERROR(@"GoTestpkgMaxInt32 = %d, want %d", GoTestpkgMaxInt32, INT32_MAX); 37 } 38 if (GoTestpkgMinInt64 != INT64_MIN) { 39 ERROR(@"GoTestpkgMinInt64 = %lld, want %lld", GoTestpkgMinInt64, INT64_MIN); 40 } 41 if (GoTestpkgMaxInt64 != INT64_MAX) { 42 ERROR(@"GoTestpkgMaxInt64 = %lld, want %lld", GoTestpkgMaxInt64, INT64_MAX); 43 } 44 if (ABS(GoTestpkgSmallestNonzeroFloat64 - 45 4.940656458412465441765687928682213723651e-324) > 1e-323) { 46 ERROR(@"GoTestpkgSmallestNonzeroFloat64 = %f, want %f", 47 GoTestpkgSmallestNonzeroFloat64, 48 4.940656458412465441765687928682213723651e-324); 49 } 50 if (ABS(GoTestpkgMaxFloat64 - 51 1.797693134862315708145274237317043567981e+308) > 0.0001) { 52 ERROR(@"GoTestpkgMaxFloat64 = %f, want %f", GoTestpkgMaxFloat64, 53 1.797693134862315708145274237317043567981e+308); 54 } 55 if (ABS(GoTestpkgSmallestNonzeroFloat32 - 56 1.401298464324817070923729583289916131280e-45) > 1e-44) { 57 ERROR(@"GoTestpkgSmallestNonzeroFloat32 = %f, want %f", 58 GoTestpkgSmallestNonzeroFloat32, 59 1.401298464324817070923729583289916131280e-45); 60 } 61 if (ABS(GoTestpkgMaxFloat32 - 3.40282346638528859811704183484516925440e+38) > 62 0.0001) { 63 ERROR(@"GoTestpkgMaxFloat32 = %f, want %f", GoTestpkgMaxFloat32, 64 3.40282346638528859811704183484516925440e+38); 65 } 66 if (ABS(GoTestpkgLog2E - 67 1 / 0.693147180559945309417232121458176568075500134360255254120680009) > 68 0.0001) { 69 ERROR( 70 @"GoTestpkgLog2E = %f, want %f", GoTestpkgLog2E, 71 1 / 0.693147180559945309417232121458176568075500134360255254120680009); 72 } 73 } 74 75 void testHello(NSString *input) { 76 NSString *got = GoTestpkgHello(input); 77 NSString *want = [NSString stringWithFormat:@"Hello, %@!", input]; 78 if (!got) { 79 ERROR(@"GoTestpkgHello(%@)= NULL, want %@", input, want); 80 return; 81 } 82 if (![got isEqualToString:want]) { 83 ERROR(@"want %@\nGoTestpkgHello(%@)= %@", want, input, got); 84 } 85 } 86 87 void testString() { 88 NSString *input = @""; 89 NSString *got = GoTestpkgEcho(input); 90 if (!got || ![got isEqualToString:input]) { 91 ERROR(@"want %@\nGoTestpkgEcho(%@)= %@", input, input, got); 92 } 93 94 input = @"FOO"; 95 got = GoTestpkgEcho(input); 96 if (!got || ![got isEqualToString:input]) { 97 ERROR(@"want %@\nGoTestpkgEcho(%@)= %@", input, input, got); 98 } 99 } 100 101 void testBytesAppend(NSString *a, NSString *b) { 102 NSData *data_a = [a dataUsingEncoding:NSUTF8StringEncoding]; 103 NSData *data_b = [b dataUsingEncoding:NSUTF8StringEncoding]; 104 NSData *gotData = GoTestpkgBytesAppend(data_a, data_b); 105 NSString *got = 106 [[NSString alloc] initWithData:gotData encoding:NSUTF8StringEncoding]; 107 NSString *want = [a stringByAppendingString:b]; 108 if (![got isEqualToString:want]) { 109 ERROR(@"want %@\nGoTestpkgBytesAppend(%@, %@) = %@", want, a, b, got); 110 } 111 } 112 113 void testReturnsError() { 114 NSString *value; 115 NSError *error; 116 GoTestpkgReturnsError(TRUE, &value, &error); 117 NSString *got = [error.userInfo valueForKey:NSLocalizedDescriptionKey]; 118 NSString *want = @"Error"; 119 if (![got isEqualToString:want]) { 120 ERROR(@"want %@\nGoTestpkgReturnsError(TRUE) = (%@, %@)", want, value, got); 121 } 122 } 123 124 void testStruct() { 125 GoTestpkgS *s = GoTestpkgNewS(10.0, 100.0); 126 if (!s) { 127 ERROR(@"GoTestpkgNewS returned NULL"); 128 } 129 130 double x = [s x]; 131 double y = [s y]; 132 double sum = [s sum]; 133 if (x != 10.0 || y != 100.0 || sum != 110.0) { 134 ERROR(@"GoTestpkgS(10.0, 100.0).X=%f Y=%f SUM=%f; want 10, 100, 110", x, y, 135 sum); 136 } 137 138 double sum2 = GoTestpkgCallSSum(s); 139 if (sum != sum2) { 140 ERROR(@"GoTestpkgCallSSum(s)=%f; want %f as returned by s.Sum", sum2, sum); 141 } 142 143 [s setX:7]; 144 [s setY:70]; 145 x = [s x]; 146 y = [s y]; 147 sum = [s sum]; 148 if (x != 7 || y != 70 || sum != 77) { 149 ERROR(@"GoTestpkgS(7, 70).X=%f Y=%f SUM=%f; want 7, 70, 77", x, y, sum); 150 } 151 152 NSString *first = @"trytwotested"; 153 NSString *second = @"test"; 154 NSString *got = [s tryTwoStrings:first second:second]; 155 NSString *want = [first stringByAppendingString:second]; 156 if (![got isEqualToString:want]) { 157 ERROR(@"GoTestpkgS_TryTwoStrings(%@, %@)= %@; want %@", first, second, got, 158 want); 159 } 160 161 GoTestpkgGC(); 162 } 163 164 // Objective-C implementation of testpkg.I. 165 @interface Number : NSObject <GoTestpkgI> { 166 } 167 @property int32_t value; 168 169 // TODO(hyangah): error:error is not good. 170 - (BOOL)error:(BOOL)e error:(NSError **)error; 171 - (int64_t)times:(int32_t)v; 172 @end 173 174 // numI is incremented when the first numI objective-C implementation is 175 // deallocated. 176 static int numI = 0; 177 178 @implementation Number { 179 } 180 @synthesize value; 181 182 - (BOOL)stringError:(NSString *)s 183 ret0_:(NSString **)ret0_ 184 error:(NSError **)error { 185 if ([s isEqualTo:@"number"]) { 186 if (ret0_ != NULL) { 187 *ret0_ = @"OK"; 188 } 189 return true; 190 } 191 return false; 192 } 193 194 - (BOOL)error:(BOOL)triggerError error:(NSError **)error { 195 if (!triggerError) { 196 return YES; 197 } 198 if (error != NULL) { 199 *error = [NSError errorWithDomain:@"SeqTest" code:1 userInfo:NULL]; 200 } 201 return NO; 202 } 203 204 - (int64_t)times:(int32_t)v { 205 return v * value; 206 } 207 208 - (void)dealloc { 209 if (self.value == 0) { 210 numI++; 211 } 212 } 213 @end 214 215 void testInterface() { 216 // Test Go object implementing testpkg.I is handled correctly. 217 id<GoTestpkgI> goObj = GoTestpkgNewI(); 218 int64_t got = [goObj times:10]; 219 if (got != 100) { 220 ERROR(@"GoTestpkgNewI().times(10) = %lld; want %d", got, 100); 221 } 222 int32_t key = -1; 223 GoTestpkgRegisterI(key, goObj); 224 int64_t got2 = GoTestpkgMultiply(key, 10); 225 if (got != got2) { 226 ERROR(@"GoTestpkgMultiply(10 * 10) = %lld; want %lld", got2, got); 227 } 228 GoTestpkgUnregisterI(key); 229 230 // Test Objective-C objects implementing testpkg.I is handled correctly. 231 @autoreleasepool { 232 for (int32_t i = 0; i < 10; i++) { 233 Number *num = [[Number alloc] init]; 234 num.value = i; 235 GoTestpkgRegisterI(i, num); 236 } 237 GoTestpkgGC(); 238 } 239 240 // Registered Objective-C objects are pinned on Go side which must 241 // prevent deallocation from Objective-C. 242 for (int32_t i = 0; i < 10; i++) { 243 int64_t got = GoTestpkgMultiply(i, 2); 244 if (got != i * 2) { 245 ERROR(@"GoTestpkgMultiply(%d, 2) = %lld; want %d", i, got, i * 2); 246 return; 247 } 248 GoTestpkgUnregisterI(i); 249 GoTestpkgGC(); 250 } 251 // Unregistered all Objective-C objects. 252 } 253 254 void testIssue12307() { 255 Number *num = [[Number alloc] init]; 256 num.value = 1024; 257 NSError *error; 258 if (GoTestpkgCallIError(num, YES, &error) == YES) { 259 ERROR(@"GoTestpkgCallIError(Number, YES) succeeded; want error"); 260 } 261 NSError *error2; 262 if (GoTestpkgCallIError(num, NO, &error2) == NO) { 263 ERROR(@"GoTestpkgCallIError(Number, NO) failed(%@); want success", error2); 264 } 265 } 266 267 void testIssue12403() { 268 Number *num = [[Number alloc] init]; 269 num.value = 1024; 270 271 NSString *ret; 272 NSError *error; 273 if (GoTestpkgCallIStringError(num, @"alphabet", &ret, &error) == YES) { 274 ERROR( 275 @"GoTestpkgCallIStringError(Number, 'alphabet') succeeded; want error"); 276 } 277 NSError *error2; 278 if (GoTestpkgCallIStringError(num, @"number", &ret, &error2) == NO) { 279 ERROR( 280 @"GoTestpkgCallIStringError(Number, 'number') failed(%@); want success", 281 error2); 282 } else if (![ret isEqualTo:@"OK"]) { 283 ERROR(@"GoTestpkgCallIStringError(Number, 'number') returned unexpected " 284 @"results %@", 285 ret); 286 } 287 } 288 289 void testVar() { 290 NSString *s = GoTestpkg.stringVar; 291 if (![s isEqualToString:@"a string var"]) { 292 ERROR(@"GoTestpkg.StringVar = %@, want 'a string var'", s); 293 } 294 s = @"a new string var"; 295 GoTestpkg.stringVar = s; 296 NSString *s2 = GoTestpkg.stringVar; 297 if (![s2 isEqualToString:s]) { 298 ERROR(@"GoTestpkg.stringVar = %@, want %@", s2, s); 299 } 300 301 int64_t i = GoTestpkg.intVar; 302 if (i != 77) { 303 ERROR(@"GoTestpkg.intVar = %lld, want 77", i); 304 } 305 GoTestpkg.intVar = 777; 306 i = GoTestpkg.intVar; 307 if (i != 777) { 308 ERROR(@"GoTestpkg.intVar = %lld, want 777", i); 309 } 310 [GoTestpkg setIntVar:7777]; 311 i = [GoTestpkg intVar]; 312 if (i != 7777) { 313 ERROR(@"GoTestpkg.intVar = %lld, want 7777", i); 314 } 315 316 GoTestpkgNode *n0 = GoTestpkg.structVar; 317 if (![n0.v isEqualToString:@"a struct var"]) { 318 ERROR(@"GoTestpkg.structVar = %@, want 'a struct var'", n0.v); 319 } 320 GoTestpkgNode *n1 = GoTestpkgNewNode(@"a new struct var"); 321 GoTestpkg.structVar = n1; 322 GoTestpkgNode *n2 = GoTestpkg.structVar; 323 if (![n2.v isEqualToString:@"a new struct var"]) { 324 ERROR(@"GoTestpkg.StructVar = %@, want 'a new struct var'", n2.v); 325 } 326 327 Number *num = [[Number alloc] init]; 328 num.value = 12345; 329 GoTestpkg.interfaceVar = num; 330 id<GoTestpkgI> iface = GoTestpkg.interfaceVar; 331 int64_t x = [iface times:10]; 332 int64_t y = [num times:10]; 333 if (x != y) { 334 ERROR(@"GoTestpkg.InterfaceVar Times 10 = %lld, want %lld", x, y); 335 } 336 } 337 338 // Invokes functions and object methods defined in Testpkg.h. 339 // 340 // TODO(hyangah): apply testing framework (e.g. XCTestCase) 341 // and test through xcodebuild. 342 int main(void) { 343 @autoreleasepool { 344 GoTestpkgHi(); 345 346 GoTestpkgInt(42); 347 348 int64_t sum = GoTestpkgSum(31, 21); 349 if (sum != 52) { 350 ERROR(@"GoTestpkgSum(31, 21) = %lld, want 52\n", sum); 351 } 352 353 testHello(@"세계"); // korean, utf-8, world. 354 355 testString(); 356 357 unichar t[] = { 358 0xD83D, 0xDCA9, 359 }; // utf-16, pile of poo. 360 testHello([NSString stringWithCharacters:t length:2]); 361 362 testBytesAppend(@"Foo", @"Bar"); 363 364 testStruct(); 365 int numS = GoTestpkgCollectS( 366 1, 10); // within 10 seconds, collect the S used in testStruct. 367 if (numS != 1) { 368 ERROR(@"%d S objects were collected; S used in testStruct is supposed to " 369 @"be collected.", 370 numS); 371 } 372 373 @autoreleasepool { 374 testInterface(); 375 } 376 if (numI != 1) { 377 ERROR(@"%d I objects were collected; I used in testInterface is supposed " 378 @"to be collected.", 379 numI); 380 } 381 382 testConst(); 383 384 testIssue12307(); 385 386 testVar(); 387 } 388 389 fprintf(stderr, "%s\n", err ? "FAIL" : "PASS"); 390 return err; 391 }