github.com/champo/mobile@v0.0.0-20190107162257-dc0771356504/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 testJavaRefGC() { 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 runGC(); 336 assertTrue("want obj to be collected", finalizedAnI); 337 } 338 339 public void testJavaRefKeep() { 340 finalizedAnI = false; 341 AnI obj = new AnI_Traced(); 342 Testpkg.callF(obj); 343 Testpkg.callF(obj); 344 obj = null; 345 runGC(); 346 assertTrue("want obj not to be kept by Go", finalizedAnI); 347 348 finalizedAnI = false; 349 obj = new AnI_Traced(); 350 Testpkg.keep(obj); 351 obj = null; 352 runGC(); 353 assertFalse("want obj to be kept live by Go", finalizedAnI); 354 } 355 356 private int countI = 0; 357 358 private class CountI implements I { 359 public void f() { countI++; } 360 361 public void e() throws Exception {} 362 public I i() { return null; } 363 public S s() { return null; } 364 public String stoString(S s) { return ""; } 365 public long v() { return 0; } 366 public long ve() throws Exception { return 0; } 367 public String string() { return ""; } 368 } 369 370 public void testGoRefMapGrow() { 371 CountI obj = new CountI(); 372 Testpkg.keep(obj); 373 374 // Push active references beyond base map size. 375 for (int i = 0; i < 24; i++) { 376 CountI o = new CountI(); 377 Testpkg.callF(o); 378 if (i%3==0) { 379 Testpkg.keep(o); 380 } 381 } 382 runGC(); 383 for (int i = 0; i < 128; i++) { 384 Testpkg.callF(new CountI()); 385 } 386 387 Testpkg.callF(obj); // original object needs to work. 388 389 assertEquals(countI, 1+24+128); 390 } 391 392 private void runGC() { 393 System.gc(); 394 System.runFinalization(); 395 Testpkg.gc(); 396 System.gc(); 397 System.runFinalization(); 398 } 399 400 public void testUnnamedParams() { 401 final String msg = "1234567"; 402 assertEquals("want the length of \"1234567\" passed after unnamed params", 403 7, Testpkg.unnamedParams(10, 20, msg)); 404 } 405 406 public void testPointerToStructAsField() { 407 Node a = Testpkg.newNode("A"); 408 Node b = Testpkg.newNode("B"); 409 a.setNext(b); 410 String got = a.string(); 411 assertEquals("want Node A points to Node B", "A:B:<end>", got); 412 } 413 414 public void testImplementsInterface() { 415 Interface intf = Testpkg.newConcrete(); 416 } 417 418 public void testErrorField() { 419 Node n = Testpkg.newNode("ErrTest"); 420 Exception want = new Exception("an error message"); 421 n.setErr(want); 422 Exception got = n.getErr(); 423 assertTrue("want back the error we set", want == got); 424 String msg = Testpkg.errorMessage(want); 425 assertEquals("the error message must match", want.getMessage(), msg); 426 } 427 428 public void testErrorDup() { 429 Exception err = Testpkg.getGlobalErr(); 430 assertTrue("the Go error instance must preserve its identity", Testpkg.isGlobalErr(err)); 431 assertEquals("the Go error message must be preserved", "global err", err.getMessage()); 432 } 433 434 //test if we have JNI local reference table overflow error 435 public void testLocalReferenceOverflow() { 436 Testpkg.callWithCallback(new GoCallback() { 437 438 @Override 439 public void varUpdate() { 440 //do nothing 441 } 442 }); 443 } 444 445 public void testNullReferences() { 446 assertTrue(Testpkg.callWithNull(null, new NullTest() { 447 public NullTest null_() { 448 return null; 449 } 450 })); 451 assertEquals("Go nil interface is null", null, Testpkg.newNullInterface()); 452 assertEquals("Go nil struct pointer is null", null, Testpkg.newNullStruct()); 453 454 Issue20330 nullArger = new Issue20330(); 455 assertTrue(nullArger.callWithNull(null)); 456 } 457 458 public void testPassByteArray() { 459 Testpkg.passByteArray(new B() { 460 @Override public void b(byte[] b) { 461 byte[] want = new byte[]{1, 2, 3, 4}; 462 MoreAsserts.assertEquals("bytes should match", want, b); 463 } 464 }); 465 } 466 467 public void testReader() { 468 byte[] b = new byte[8]; 469 try { 470 long n = Testpkg.readIntoByteArray(b); 471 assertEquals("wrote to the entire byte array", b.length, n); 472 byte[] want = new byte[b.length]; 473 for (int i = 0; i < want.length; i++) 474 want[i] = (byte)i; 475 MoreAsserts.assertEquals("bytes should match", want, b); 476 } catch (Exception e) { 477 fail("Failed to write: " + e.toString()); 478 } 479 } 480 481 public void testGoroutineCallback() { 482 Testpkg.goroutineCallback(new Receiver() { 483 @Override public void hello(String msg) { 484 } 485 }); 486 } 487 488 public void testImportedPkg() { 489 Testpkg.callImportedI(new secondpkg.I() { 490 @Override public long f(long i) { 491 return i; 492 } 493 }); 494 assertEquals("imported string should match", Secondpkg.HelloString, Secondpkg.hello()); 495 secondpkg.I i = Testpkg.newImportedI(); 496 secondpkg.S s = Testpkg.newImportedS(); 497 i = Testpkg.getImportedVarI(); 498 s = Testpkg.getImportedVarS(); 499 assertEquals("numbers should match", 8, i.f(8)); 500 assertEquals("numbers should match", 8, s.f(8)); 501 Testpkg.setImportedVarI(i); 502 Testpkg.setImportedVarS(s); 503 ImportedFields fields = Testpkg.newImportedFields(); 504 i = fields.getI(); 505 s = fields.getS(); 506 fields.setI(i); 507 fields.setS(s); 508 Testpkg.withImportedI(i); 509 Testpkg.withImportedS(s); 510 511 secondpkg.IF f = new AnI(); 512 f = Testpkg.new_(); 513 secondpkg.Ser ser = Testpkg.newSer(); 514 } 515 516 public void testRoundtripEquality() { 517 I want = new AnI(); 518 assertTrue("java object passed through Go should not be wrapped", want == Testpkg.iDup(want)); 519 InterfaceDupper idup = new InterfaceDupper(){ 520 @Override public Interface iDup(Interface i) { 521 return i; 522 } 523 }; 524 assertTrue("Go interface passed through Java should not be wrapped", Testpkg.callIDupper(idup)); 525 ConcreteDupper cdup = new ConcreteDupper(){ 526 @Override public Concrete cDup(Concrete c) { 527 return c; 528 } 529 }; 530 assertTrue("Go struct passed through Java should not be wrapped", Testpkg.callCDupper(cdup)); 531 } 532 533 public void testConstructor() { 534 Interface i = new Concrete(); 535 i.f(); 536 537 S2 s = new S2(1, 2); 538 assertEquals("new S2().sum", 3.0, s.sum()); 539 assertEquals("new S2().tryTwoStrings", "gostring", s.tryTwoStrings("go", "string")); 540 541 new S3(); 542 543 S4 s4 = new S4(123); 544 assertEquals("Constructor argument", 123, s4.getI()); 545 546 s4 = new S4(123.456); 547 assertEquals("Overloaded constructor argument", 123, s4.getI()); 548 549 s4 = new S4(false); 550 assertEquals("Exceptional constructor", 0, s4.getI()); 551 552 try { 553 s4 = new S4(true); 554 fail("Constructor error wasn't caught"); 555 } catch (Exception e) { 556 } 557 } 558 559 public void testEmptyError() { 560 try { 561 Testpkg.emptyError(); 562 fail("Empty error wasn't caught"); 563 } catch (Exception e) { 564 } 565 EmptyErrorer empty = new EmptyErrorer() { 566 @Override public void emptyError() throws Exception { 567 throw new Exception(""); 568 } 569 }; 570 try { 571 Testpkg.callEmptyError(empty); 572 fail("Empty exception wasn't caught"); 573 } catch (Exception e) { 574 } 575 } 576 577 public void testInitCaller() { 578 Testpkg.init(); 579 580 InitCaller initer = Testpkg.newInitCaller(); 581 initer.init(); 582 } 583 584 public void testSIGPIPE() { 585 Testpkg.testSIGPIPE(); 586 } 587 588 public void testTags() { 589 assertEquals("Constant from a tagged file", 42, Testpkg.TaggedConst); 590 } 591 592 public void testClassNameWithPackageName() { 593 testpkg.Testpkg_ o = new secondpkg.Secondpkg_(); 594 secondpkg.Secondpkg_ o2 = Secondpkg.newSecondpkg(); 595 o2.m(); 596 o2.setV("hi"); 597 assertEquals(o2.getV(), "hi"); 598 Testpkg.clashingParameterFromOtherPackage(o2); 599 } 600 }