github.com/F4RD1N/gomobile@v1.0.1/bind/java/SeqTest.java (about) 1 // Copyright 2014 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 package go; 6 7 import android.test.InstrumentationTestCase; 8 import android.test.MoreAsserts; 9 10 import java.util.Arrays; 11 import java.util.Random; 12 13 import testpkg.*; 14 import secondpkg.Secondpkg; 15 16 public class SeqTest extends InstrumentationTestCase { 17 public SeqTest() { 18 } 19 20 public void testConst() { 21 assertEquals("const String", "a string", Testpkg.AString); 22 assertEquals("const Int", 7, Testpkg.AnInt); 23 assertEquals("const Bool", true, Testpkg.ABool); 24 assertEquals("const Float", 0.12345, Testpkg.AFloat, 0.0001); 25 26 assertEquals("const MinInt32", -1<<31, Testpkg.MinInt32); 27 assertEquals("const MaxInt32", (1<<31) - 1, Testpkg.MaxInt32); 28 assertEquals("const MinInt64", -1L<<63, Testpkg.MinInt64); 29 assertEquals("const MaxInt64", (1L<<63) - 1, Testpkg.MaxInt64); 30 assertEquals("const SmallestNonzeroFloat64", 4.940656458412465441765687928682213723651e-324, Testpkg.SmallestNonzeroFloat64, 1e-323); 31 assertEquals("const MaxFloat64", 1.797693134862315708145274237317043567981e+308, Testpkg.MaxFloat64, 0.0001); 32 assertEquals("const SmallestNonzeroFloat32", 1.401298464324817070923729583289916131280e-45, Testpkg.SmallestNonzeroFloat32, 1e-44); 33 assertEquals("const MaxFloat32", 3.40282346638528859811704183484516925440e+38, Testpkg.MaxFloat32, 0.0001); 34 assertEquals("const Log2E", 1/0.693147180559945309417232121458176568075500134360255254120680009, Testpkg.Log2E, 0.0001); 35 } 36 37 public void testRefMap() { 38 // Ensure that the RefMap.live count is kept in sync 39 // even a particular reference number is removed and 40 // added again 41 Seq.RefMap m = new Seq.RefMap(); 42 Seq.Ref r = new Seq.Ref(1, null); 43 m.put(r.refnum, r); 44 m.remove(r.refnum); 45 m.put(r.refnum, r); 46 // Force the RefMap to grow, to activate the sanity 47 // checking of the live count in RefMap.grow. 48 for (int i = 2; i < 24; i++) { 49 m.put(i, new Seq.Ref(i, null)); 50 } 51 } 52 53 public void testVar() { 54 assertEquals("var StringVar", "a string var", Testpkg.getStringVar()); 55 56 String newStringVar = "a new string var"; 57 Testpkg.setStringVar(newStringVar); 58 assertEquals("var StringVar", newStringVar, Testpkg.getStringVar()); 59 60 assertEquals("var IntVar", 77, Testpkg.getIntVar()); 61 62 long newIntVar = 777; 63 Testpkg.setIntVar(newIntVar); 64 assertEquals("var IntVar", newIntVar, Testpkg.getIntVar()); 65 66 S s0 = Testpkg.getStructVar(); 67 assertEquals("var StructVar", "a struct var", s0.string()); 68 S s1 = Testpkg.new_(); 69 Testpkg.setStructVar(s1); 70 assertEquals("var StructVar", s1.string(), Testpkg.getStructVar().string()); 71 72 AnI obj = new AnI(); 73 obj.name = "this is an I"; 74 Testpkg.setInterfaceVar(obj); 75 assertEquals("var InterfaceVar", obj.string(), Testpkg.getInterfaceVar().string()); 76 } 77 78 public void testAssets() { 79 // Make sure that a valid context is set before reading assets 80 Seq.setContext(getInstrumentation().getContext()); 81 String want = "Hello, Assets.\n"; 82 String got = Testpkg.readAsset(); 83 assertEquals("Asset read", want, got); 84 } 85 86 public void testAdd() { 87 long res = Testpkg.add(3, 4); 88 assertEquals("Unexpected arithmetic failure", 7, res); 89 } 90 91 public void testBool() { 92 assertTrue(Testpkg.negate(false)); 93 assertFalse(Testpkg.negate(true)); 94 } 95 96 public void testShortString() { 97 String want = "a short string"; 98 String got = Testpkg.strDup(want); 99 assertEquals("Strings should match", want, got); 100 101 want = ""; 102 got = Testpkg.strDup(want); 103 assertEquals("Strings should match (empty string)", want, got); 104 105 got = Testpkg.strDup(null); 106 assertEquals("Strings should match (null string)", want, got); 107 } 108 109 public void testLongString() { 110 StringBuilder b = new StringBuilder(); 111 for (int i = 0; i < 128*1024; i++) { 112 b.append("0123456789"); 113 } 114 String want = b.toString(); 115 String got = Testpkg.strDup(want); 116 assertEquals("Strings should match", want, got); 117 } 118 119 public void testUnicode() { 120 String[] tests = new String[]{ 121 "abcxyz09{}", 122 "Hello, 世界", 123 "\uffff\uD800\uDC00\uD800\uDC01\uD808\uDF45\uDBFF\uDFFF", 124 // From Go std lib tests in unicode/utf16/utf16_test.go 125 "\u0001\u0002\u0003\u0004", 126 "\uffff\ud800\udc00\ud800\udc01\ud808\udf45\udbff\udfff", 127 "\ud800a", 128 "\udfff" 129 }; 130 String[] wants = new String[]{ 131 "abcxyz09{}", 132 "Hello, 世界", 133 "\uffff\uD800\uDC00\uD800\uDC01\uD808\uDF45\uDBFF\uDFFF", 134 "\u0001\u0002\u0003\u0004", 135 "\uffff\ud800\udc00\ud800\udc01\ud808\udf45\udbff\udfff", 136 "\ufffda", 137 "\ufffd" 138 }; 139 for (int i = 0; i < tests.length; i++) { 140 String got = Testpkg.strDup(tests[i]); 141 String want = wants[i]; 142 assertEquals("Strings should match", want, got); 143 } 144 } 145 146 public void testNilErr() throws Exception { 147 Testpkg.err(null); // returns nil, no exception 148 } 149 150 public void testErr() { 151 String msg = "Go errors are dropped into the confusing space of exceptions"; 152 try { 153 Testpkg.err(msg); 154 fail("expected non-nil error to be turned into an exception"); 155 } catch (Exception e) { 156 assertEquals("messages should match", msg, e.getMessage()); 157 } 158 } 159 160 public void testByteArray() { 161 for (int i = 0; i < 2048; i++) { 162 if (i == 0) { 163 byte[] got = Testpkg.bytesAppend(null, null); 164 assertEquals("Bytes(null+null) should match", (byte[])null, got); 165 got = Testpkg.bytesAppend(new byte[0], new byte[0]); 166 assertEquals("Bytes(empty+empty) should match", (byte[])null, got); 167 continue; 168 } 169 170 byte[] want = new byte[i]; 171 new Random().nextBytes(want); 172 173 byte[] s1 = null; 174 byte[] s2 = null; 175 if (i > 0) { 176 s1 = Arrays.copyOfRange(want, 0, 1); 177 } 178 if (i > 1) { 179 s2 = Arrays.copyOfRange(want, 1, i); 180 } 181 byte[] got = Testpkg.bytesAppend(s1, s2); 182 MoreAsserts.assertEquals("Bytes(len="+i+") should match", want, got); 183 } 184 } 185 186 // Test for golang.org/issue/9486. 187 public void testByteArrayAfterString() { 188 byte[] bytes = new byte[1024]; 189 for (int i=0; i < bytes.length; i++) { 190 bytes[i] = 8; 191 } 192 193 String stuff = "stuff"; 194 byte[] got = Testpkg.appendToString(stuff, bytes); 195 196 try { 197 byte[] s = stuff.getBytes("UTF-8"); 198 byte[] want = new byte[s.length + bytes.length]; 199 System.arraycopy(s, 0, want, 0, s.length); 200 System.arraycopy(bytes, 0, want, s.length, bytes.length); 201 MoreAsserts.assertEquals("Bytes should match", want, got); 202 } catch (Exception e) { 203 fail("Cannot perform the test: " + e.toString()); 204 } 205 } 206 207 public void testGoRefGC() { 208 S s = Testpkg.new_(); 209 runGC(); 210 long collected = Testpkg.numSCollected(); 211 assertEquals("Only S should be pinned", 0, collected); 212 213 s = null; 214 runGC(); 215 collected = Testpkg.numSCollected(); 216 assertEquals("S should be collected", 1, collected); 217 } 218 219 private class AnI implements I { 220 public void e() throws Exception { 221 throw new Exception("my exception from E"); 222 } 223 224 boolean calledF; 225 public void f() { 226 calledF = true; 227 } 228 229 public I i() { 230 return this; 231 } 232 233 public S s() { 234 return Testpkg.new_(); 235 } 236 237 public String stoString(S s) { 238 return s.string(); 239 } 240 241 public long v() { 242 return 1234; 243 } 244 245 public long ve() throws Exception { 246 throw new Exception("my exception from VE"); 247 } 248 249 public String name; 250 251 public String string() { 252 return name; 253 } 254 255 } 256 257 // TODO(hyangah): add tests for methods that take parameters. 258 259 public void testInterfaceMethodReturnsError() { 260 final AnI obj = new AnI(); 261 try { 262 Testpkg.callE(obj); 263 fail("Expecting exception but none was thrown."); 264 } catch (Exception e) { 265 assertEquals("Error messages should match", "my exception from E", e.getMessage()); 266 } 267 } 268 269 public void testInterfaceMethodVoid() { 270 final AnI obj = new AnI(); 271 Testpkg.callF(obj); 272 assertTrue("Want AnI.F to be called", obj.calledF); 273 } 274 275 public void testInterfaceMethodReturnsInterface() { 276 AnI obj = new AnI(); 277 obj.name = "testing AnI.I"; 278 I i = Testpkg.callI(obj); 279 assertEquals("Want AnI.I to return itself", i.string(), obj.string()); 280 281 runGC(); 282 283 i = Testpkg.callI(obj); 284 assertEquals("Want AnI.I to return itself", i.string(), obj.string()); 285 } 286 287 public void testInterfaceMethodReturnsStructPointer() { 288 final AnI obj = new AnI(); 289 for (int i = 0; i < 5; i++) { 290 S s = Testpkg.callS(obj); 291 runGC(); 292 } 293 } 294 295 public void testInterfaceMethodTakesStructPointer() { 296 final AnI obj = new AnI(); 297 S s = Testpkg.callS(obj); 298 String got = obj.stoString(s); 299 String want = s.string(); 300 assertEquals("Want AnI.StoString(s) to call s's String", want, got); 301 } 302 303 public void testInterfaceMethodReturnsInt() { 304 final AnI obj = new AnI(); 305 assertEquals("Values must match", 1234, Testpkg.callV(obj)); 306 } 307 308 public void testInterfaceMethodReturnsIntOrError() { 309 final AnI obj = new AnI(); 310 try { 311 long v = Testpkg.callVE(obj); 312 fail("Expecting exception but none was thrown and got value " + v); 313 } catch (Exception e) { 314 assertEquals("Error messages should match", "my exception from VE", e.getMessage()); 315 } 316 } 317 318 boolean finalizedAnI; 319 320 private class AnI_Traced extends AnI { 321 @Override 322 public void finalize() throws Throwable { 323 finalizedAnI = true; 324 super.finalize(); 325 } 326 } 327 328 public void testJavaRefKeep() { 329 finalizedAnI = false; 330 AnI obj = new AnI_Traced(); 331 Testpkg.callF(obj); 332 assertTrue("want F to be called", obj.calledF); 333 Testpkg.callF(obj); 334 obj = null; 335 int attempts = 0; 336 while (true) { 337 runGC(); 338 if (finalizedAnI) 339 break; 340 attempts++; 341 try { 342 Thread.sleep(100); 343 } catch (InterruptedException e) { 344 throw new RuntimeException(e); 345 } 346 if (attempts >= 10) 347 fail("want obj not to be kept by Go; tried " + attempts + " garbage collections."); 348 } 349 350 finalizedAnI = false; 351 obj = new AnI_Traced(); 352 Testpkg.keep(obj); 353 obj = null; 354 runGC(); 355 assertFalse("want obj to be kept live by Go", finalizedAnI); 356 } 357 358 private int countI = 0; 359 360 private class CountI implements I { 361 public void f() { countI++; } 362 363 public void e() throws Exception {} 364 public I i() { return null; } 365 public S s() { return null; } 366 public String stoString(S s) { return ""; } 367 public long v() { return 0; } 368 public long ve() throws Exception { return 0; } 369 public String string() { return ""; } 370 } 371 372 public void testGoRefMapGrow() { 373 CountI obj = new CountI(); 374 Testpkg.keep(obj); 375 376 // Push active references beyond base map size. 377 for (int i = 0; i < 24; i++) { 378 CountI o = new CountI(); 379 Testpkg.callF(o); 380 if (i%3==0) { 381 Testpkg.keep(o); 382 } 383 } 384 runGC(); 385 for (int i = 0; i < 128; i++) { 386 Testpkg.callF(new CountI()); 387 } 388 389 Testpkg.callF(obj); // original object needs to work. 390 391 assertEquals(countI, 1+24+128); 392 } 393 394 private void runGC() { 395 System.gc(); 396 System.runFinalization(); 397 Testpkg.gc(); 398 System.gc(); 399 System.runFinalization(); 400 } 401 402 public void testUnnamedParams() { 403 final String msg = "1234567"; 404 assertEquals("want the length of \"1234567\" passed after unnamed params", 405 7, Testpkg.unnamedParams(10, 20, msg)); 406 } 407 408 public void testPointerToStructAsField() { 409 Node a = Testpkg.newNode("A"); 410 Node b = Testpkg.newNode("B"); 411 a.setNext(b); 412 String got = a.string(); 413 assertEquals("want Node A points to Node B", "A:B:<end>", got); 414 } 415 416 public void testImplementsInterface() { 417 Interface intf = Testpkg.newConcrete(); 418 } 419 420 public void testErrorField() { 421 Node n = Testpkg.newNode("ErrTest"); 422 Exception want = new Exception("an error message"); 423 n.setErr(want); 424 Exception got = n.getErr(); 425 assertTrue("want back the error we set", want == got); 426 String msg = Testpkg.errorMessage(want); 427 assertEquals("the error message must match", want.getMessage(), msg); 428 } 429 430 public void testErrorDup() { 431 Exception err = Testpkg.getGlobalErr(); 432 assertTrue("the Go error instance must preserve its identity", Testpkg.isGlobalErr(err)); 433 assertEquals("the Go error message must be preserved", "global err", err.getMessage()); 434 } 435 436 //test if we have JNI local reference table overflow error 437 public void testLocalReferenceOverflow() { 438 Testpkg.callWithCallback(new GoCallback() { 439 440 @Override 441 public void varUpdate() { 442 //do nothing 443 } 444 }); 445 } 446 447 public void testNullReferences() { 448 assertTrue(Testpkg.callWithNull(null, new NullTest() { 449 public NullTest null_() { 450 return null; 451 } 452 })); 453 assertEquals("Go nil interface is null", null, Testpkg.newNullInterface()); 454 assertEquals("Go nil struct pointer is null", null, Testpkg.newNullStruct()); 455 456 Issue20330 nullArger = new Issue20330(); 457 assertTrue(nullArger.callWithNull(null)); 458 } 459 460 public void testPassByteArray() { 461 Testpkg.passByteArray(new B() { 462 @Override public void b(byte[] b) { 463 byte[] want = new byte[]{1, 2, 3, 4}; 464 MoreAsserts.assertEquals("bytes should match", want, b); 465 } 466 }); 467 } 468 469 public void testReader() { 470 byte[] b = new byte[8]; 471 try { 472 long n = Testpkg.readIntoByteArray(b); 473 assertEquals("wrote to the entire byte array", b.length, n); 474 byte[] want = new byte[b.length]; 475 for (int i = 0; i < want.length; i++) 476 want[i] = (byte)i; 477 MoreAsserts.assertEquals("bytes should match", want, b); 478 } catch (Exception e) { 479 fail("Failed to write: " + e.toString()); 480 } 481 } 482 483 public void testGoroutineCallback() { 484 Testpkg.goroutineCallback(new Receiver() { 485 @Override public void hello(String msg) { 486 } 487 }); 488 } 489 490 public void testImportedPkg() { 491 Testpkg.callImportedI(new secondpkg.I() { 492 @Override public long f(long i) { 493 return i; 494 } 495 }); 496 assertEquals("imported string should match", Secondpkg.HelloString, Secondpkg.hello()); 497 secondpkg.I i = Testpkg.newImportedI(); 498 secondpkg.S s = Testpkg.newImportedS(); 499 i = Testpkg.getImportedVarI(); 500 s = Testpkg.getImportedVarS(); 501 assertEquals("numbers should match", 8, i.f(8)); 502 assertEquals("numbers should match", 8, s.f(8)); 503 Testpkg.setImportedVarI(i); 504 Testpkg.setImportedVarS(s); 505 ImportedFields fields = Testpkg.newImportedFields(); 506 i = fields.getI(); 507 s = fields.getS(); 508 fields.setI(i); 509 fields.setS(s); 510 Testpkg.withImportedI(i); 511 Testpkg.withImportedS(s); 512 513 secondpkg.IF f = new AnI(); 514 f = Testpkg.new_(); 515 secondpkg.Ser ser = Testpkg.newSer(); 516 } 517 518 public void testRoundtripEquality() { 519 I want = new AnI(); 520 assertTrue("java object passed through Go should not be wrapped", want == Testpkg.iDup(want)); 521 InterfaceDupper idup = new InterfaceDupper(){ 522 @Override public Interface iDup(Interface i) { 523 return i; 524 } 525 }; 526 assertTrue("Go interface passed through Java should not be wrapped", Testpkg.callIDupper(idup)); 527 ConcreteDupper cdup = new ConcreteDupper(){ 528 @Override public Concrete cDup(Concrete c) { 529 return c; 530 } 531 }; 532 assertTrue("Go struct passed through Java should not be wrapped", Testpkg.callCDupper(cdup)); 533 } 534 535 public void testConstructor() { 536 Interface i = new Concrete(); 537 i.f(); 538 539 S2 s = new S2(1, 2); 540 assertEquals("new S2().sum", 3.0, s.sum()); 541 assertEquals("new S2().tryTwoStrings", "gostring", s.tryTwoStrings("go", "string")); 542 543 new S3(); 544 545 S4 s4 = new S4(123); 546 assertEquals("Constructor argument", 123, s4.getI()); 547 548 s4 = new S4(123.456); 549 assertEquals("Overloaded constructor argument", 123, s4.getI()); 550 551 s4 = new S4(false); 552 assertEquals("Exceptional constructor", 0, s4.getI()); 553 554 try { 555 s4 = new S4(true); 556 fail("Constructor error wasn't caught"); 557 } catch (Exception e) { 558 } 559 } 560 561 public void testEmptyError() { 562 try { 563 Testpkg.emptyError(); 564 fail("Empty error wasn't caught"); 565 } catch (Exception e) { 566 } 567 EmptyErrorer empty = new EmptyErrorer() { 568 @Override public void emptyError() throws Exception { 569 throw new Exception(""); 570 } 571 }; 572 try { 573 Testpkg.callEmptyError(empty); 574 fail("Empty exception wasn't caught"); 575 } catch (Exception e) { 576 } 577 } 578 579 public void testInitCaller() { 580 Testpkg.init(); 581 582 InitCaller initer = Testpkg.newInitCaller(); 583 initer.init(); 584 } 585 586 public void testSIGPIPE() { 587 Testpkg.testSIGPIPE(); 588 } 589 590 public void testTags() { 591 assertEquals("Constant from a tagged file", 42, Testpkg.TaggedConst); 592 } 593 594 public void testClassNameWithPackageName() { 595 testpkg.Testpkg_ o = new secondpkg.Secondpkg_(); 596 secondpkg.Secondpkg_ o2 = Secondpkg.newSecondpkg(); 597 o2.m(); 598 o2.setV("hi"); 599 assertEquals(o2.getV(), "hi"); 600 Testpkg.clashingParameterFromOtherPackage(o2); 601 } 602 }