ClassesTest.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2016 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.io.InputStream;
  8. import java.io.IOException;
  9. import java.util.Arrays;
  10. import java.util.Random;
  11. import javapkg.Javapkg;
  12. import javapkg.I;
  13. import javapkg.GoObject;
  14. import javapkg.GoRunnable;
  15. import javapkg.GoSubset;
  16. import javapkg.GoInputStream;
  17. import javapkg.GoArrayList;
  18. public class ClassesTest extends InstrumentationTestCase {
  19. public void testConst() {
  20. assertEquals("const Float", Float.MIN_VALUE, Javapkg.floatMin());
  21. assertEquals("const String", java.util.jar.JarFile.MANIFEST_NAME, Javapkg.manifestName());
  22. assertEquals("const Int", 7, Integer.SIZE, Javapkg.integerBytes());
  23. }
  24. public void testFunction() {
  25. Javapkg.systemCurrentTimeMillis();
  26. }
  27. public void testMethod() {
  28. try {
  29. assertEquals("Integer.decode", 0xff, Javapkg.integerDecode("0xff"));
  30. } catch (Exception e) {
  31. throw new RuntimeException(e);
  32. }
  33. Exception exc = null;
  34. try {
  35. Javapkg.integerDecode("obviously wrong");
  36. } catch (Exception e) {
  37. exc = e;
  38. }
  39. assertNotNull("IntegerDecode Exception", exc);
  40. }
  41. public void testOverloadedMethod() {
  42. try {
  43. assertEquals("Integer.parseInt", 0xc4, Javapkg.integerParseInt("c4", 16));
  44. } catch (Exception e) {
  45. throw new RuntimeException(e);
  46. }
  47. Exception exc = null;
  48. try {
  49. Javapkg.integerParseInt("wrong", 16);
  50. } catch (Exception e) {
  51. exc = e;
  52. }
  53. assertNotNull("integerParseInt Exception", exc);
  54. assertEquals("Integer.valueOf", 42, Javapkg.integerValueOf(42));
  55. }
  56. public void testException() {
  57. Exception exc = null;
  58. try {
  59. Javapkg.provokeRuntimeException();
  60. } catch (Exception e) {
  61. exc = e;
  62. }
  63. assertNotNull("RuntimeException", exc);
  64. }
  65. public void testField() {
  66. GoRunnable r = new GoRunnable();
  67. String f = r.getField();
  68. }
  69. public void testGoObject() {
  70. Runnable r = new GoRunnable();
  71. r.run();
  72. assertEquals("GoRunnable.toString", r.toString(), Javapkg.ToStringPrefix);
  73. Runnable r2 = ((GoRunnable)r).getThis();
  74. assertTrue("GoObject.this", r == r2);
  75. Object o = new GoObject();
  76. assertEquals("GoObject hashCode", 42, o.hashCode());
  77. Object o2 = Javapkg.constructGoObject();
  78. assertEquals("GoObject hashCode", 42, o2.hashCode());
  79. assertTrue("GoObject.toString", o.toString().startsWith(Javapkg.ToStringPrefix));
  80. Javapkg.runRunnable(r);
  81. final boolean[] ran = new boolean[1];
  82. Runnable r3 = new Runnable(){
  83. @Override public void run() {
  84. ran[0] = true;
  85. }
  86. };
  87. Javapkg.runRunnable(r3);
  88. assertTrue("RunRunnable", ran[0]);
  89. assertTrue("RunnableRoundtrip Java", r3 == Javapkg.runnableRoundtrip(r3));
  90. assertTrue("RunnableRoundtrip Go", r == Javapkg.runnableRoundtrip(r));
  91. Runnable r5 = Javapkg.constructGoRunnable();
  92. r5.run();
  93. }
  94. public void testTypedException() {
  95. InputStream is = new GoInputStream();
  96. Exception exc = null;
  97. try {
  98. is.read();
  99. } catch (IOException e) {
  100. exc = e;
  101. }
  102. assertNotNull("IOException", exc);
  103. assertEquals("IOException message", Javapkg.IOExceptionMessage, exc.getMessage());
  104. }
  105. public void testInnerClass() {
  106. Character.Subset s = new Character.Subset(""){};
  107. Character.Subset s2 = new GoSubset("");
  108. Javapkg.callSubset(s);
  109. Javapkg.callSubset(s2);
  110. }
  111. public void testNew() {
  112. Object o = Javapkg.newJavaObject();
  113. assertTrue("new Object()", o != null);
  114. Integer i = Javapkg.newJavaInteger();
  115. assertEquals("new Integer(42)", 42, i.intValue());
  116. }
  117. private static class InterfaceRunnable implements I, Runnable {
  118. @Override public void run() {
  119. }
  120. }
  121. public void testCast() {
  122. Runnable r1 = new GoRunnable();
  123. Runnable r1c = Javapkg.castRunnable((Object)r1);
  124. assertTrue("Casting Go object", r1c != null);
  125. Runnable r2 = new Runnable() {
  126. @Override public void run() {
  127. }
  128. };
  129. Runnable r2c = Javapkg.castRunnable((Object)r2);
  130. assertTrue("Casting Java object", r2c != null);
  131. Runnable r3c = Javapkg.castInterface(new InterfaceRunnable());
  132. assertTrue("Casting Go interface implementation", r3c != null);
  133. Runnable r4c = Javapkg.castRunnable(new Object());
  134. assertTrue("Invalid cast", r4c == null);
  135. }
  136. public void testUnwrap() {
  137. GoArrayList l = new GoArrayList();
  138. Javapkg.unwrapGoArrayList(l);
  139. }
  140. }