SeqTest.java 17 KB

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