github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/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.util.Log;
     8  import android.test.suitebuilder.annotation.Suppress;
     9  import android.test.AndroidTestCase;
    10  import android.test.MoreAsserts;
    11  import java.util.Arrays;
    12  import java.util.Random;
    13  
    14  import go.testpkg.Testpkg;
    15  
    16  public class SeqTest extends AndroidTestCase {
    17    public SeqTest() {
    18    }
    19  
    20    public void testAssets() {
    21      String want = "Hello, Assets.\n";
    22      String got = Testpkg.ReadAsset();
    23      assertEquals("Asset read", want, got);
    24    }
    25  
    26    public void testAdd() {
    27      long res = Testpkg.Add(3, 4);
    28      assertEquals("Unexpected arithmetic failure", 7, res);
    29    }
    30  
    31    public void testBool() {
    32      assertTrue(Testpkg.Negate(false));
    33      assertFalse(Testpkg.Negate(true));
    34    }
    35  
    36    public void testShortString() {
    37      String want = "a short string";
    38      String got = Testpkg.StrDup(want);
    39      assertEquals("Strings should match", want, got);
    40    }
    41  
    42    public void testLongString() {
    43      StringBuilder b = new StringBuilder();
    44      for (int i = 0; i < 128*1024; i++) {
    45        b.append("0123456789");
    46      }
    47      String want = b.toString();
    48      String got = Testpkg.StrDup(want);
    49      assertEquals("Strings should match", want, got);
    50    }
    51  
    52    public void testUnicode() {
    53      String want = "Hello, 世界";
    54      String got = Testpkg.StrDup(want);
    55      assertEquals("Strings should match", want, got);
    56    }
    57  
    58    public void testNilErr() throws Exception {
    59      Testpkg.Err(null); // returns nil, no exception
    60    }
    61  
    62    public void testErr() {
    63      String msg = "Go errors are dropped into the confusing space of exceptions";
    64      try {
    65        Testpkg.Err(msg);
    66        fail("expected non-nil error to be turned into an exception");
    67      } catch (Exception e) {
    68        assertEquals("messages should match", msg, e.getMessage());
    69      }
    70    }
    71  
    72    public void testByteArray() {
    73      for (int i = 0; i < 2048; i++) {
    74        if (i == 0) {
    75          byte[] got = Testpkg.BytesAppend(null, null);
    76          assertEquals("Bytes(null+null) should match", (byte[])null, got);
    77          got = Testpkg.BytesAppend(new byte[0], new byte[0]);
    78          assertEquals("Bytes(empty+empty) should match", (byte[])null, got);
    79          continue;
    80        }
    81  
    82        byte[] want = new byte[i];
    83        new Random().nextBytes(want);
    84  
    85        byte[] s1 = null;
    86        byte[] s2 = null;
    87        if (i > 0) {
    88          s1 = Arrays.copyOfRange(want, 0, 1);
    89        }
    90        if (i > 1) {
    91          s2 = Arrays.copyOfRange(want, 1, i);
    92        }
    93        byte[] got = Testpkg.BytesAppend(s1, s2);
    94        MoreAsserts.assertEquals("Bytes(len="+i+") should match", want, got);
    95      }
    96    }
    97  
    98    // Test for golang.org/issue/9486.
    99    public void testByteArrayAfterString() {
   100      byte[] bytes = new byte[1024];
   101      for (int i=0; i < bytes.length; i++) {
   102             bytes[i] = 8;
   103      }
   104  
   105      String stuff = "stuff";
   106      byte[] got = Testpkg.AppendToString(stuff, bytes);
   107  
   108      try {
   109        byte[] s = stuff.getBytes("UTF-8");
   110        byte[] want = new byte[s.length + bytes.length];
   111        System.arraycopy(s, 0, want, 0, s.length);
   112        System.arraycopy(bytes, 0, want, s.length, bytes.length);
   113        MoreAsserts.assertEquals("Bytes should match", want, got);
   114      } catch (Exception e) {
   115        fail("Cannot perform the test: " + e.toString());
   116      }
   117    }
   118  
   119    public void testGoRefGC() {
   120      Testpkg.S s = Testpkg.New();
   121      runGC();
   122      long collected = Testpkg.NumSCollected();
   123      assertEquals("Only S should be pinned", 0, collected);
   124  
   125      s = null;
   126      runGC();
   127      collected = Testpkg.NumSCollected();
   128      assertEquals("S should be collected", 1, collected);
   129    }
   130  
   131    private class AnI extends Testpkg.I.Stub {
   132      public void E() throws Exception {
   133        throw new Exception("my exception from E");
   134      }
   135  
   136      boolean calledF;
   137      public void F() {
   138        calledF = true;
   139      }
   140  
   141      public Testpkg.I I() {
   142        return this;
   143      }
   144  
   145      public Testpkg.S S() {
   146        return Testpkg.New();
   147      }
   148  
   149      public String StoString(Testpkg.S s) {
   150        return s.String();
   151      }
   152  
   153      public long V() {
   154        return 1234;
   155      }
   156  
   157      public long VE() throws Exception {
   158        throw new Exception("my exception from VE");
   159      }
   160  
   161      public String name;
   162  
   163      public String String() {
   164        return name;
   165      }
   166  
   167    }
   168  
   169    // TODO(hyangah): add tests for methods that take parameters.
   170  
   171    public void testInterfaceMethodReturnsError() {
   172      final AnI obj = new AnI();
   173      try {
   174        Testpkg.CallE(obj);
   175        fail("Expecting exception but none was thrown.");
   176      } catch (Exception e) {
   177        assertEquals("Error messages should match", "my exception from E", e.getMessage());
   178      }
   179    }
   180  
   181    public void testInterfaceMethodVoid() {
   182      final AnI obj = new AnI();
   183      Testpkg.CallF(obj);
   184      assertTrue("Want AnI.F to be called", obj.calledF);
   185    }
   186  
   187    public void testInterfaceMethodReturnsInterface() {
   188      AnI obj = new AnI();
   189      obj.name = "testing AnI.I";
   190      Testpkg.I i = Testpkg.CallI(obj);
   191      assertEquals("Want AnI.I to return itself", i.String(), obj.String());
   192  
   193      runGC();
   194  
   195      i = Testpkg.CallI(obj);
   196      assertEquals("Want AnI.I to return itself", i.String(), obj.String());
   197    }
   198  
   199    public void testInterfaceMethodReturnsStructPointer() {
   200      final AnI obj = new AnI();
   201      for (int i = 0; i < 5; i++) {
   202      	Testpkg.S s = Testpkg.CallS(obj);
   203  	runGC();
   204      }
   205    }
   206  
   207    public void testInterfaceMethodTakesStructPointer() {
   208      final AnI obj = new AnI();
   209      Testpkg.S s = Testpkg.CallS(obj);
   210      String got = obj.StoString(s);
   211      String want = s.String();
   212      assertEquals("Want AnI.StoString(s) to call s's String", want, got);
   213    }
   214  
   215    public void testInterfaceMethodReturnsInt() {
   216      final AnI obj = new AnI();
   217      assertEquals("Values must match", 1234, Testpkg.CallV(obj));
   218    }
   219  
   220    public void testInterfaceMethodReturnsIntOrError() {
   221      final AnI obj = new AnI();
   222      try {
   223        long v = Testpkg.CallVE(obj);
   224        fail("Expecting exception but none was thrown and got value " + v);
   225      } catch (Exception e) {
   226        assertEquals("Error messages should match", "my exception from VE", e.getMessage());
   227      }
   228    }
   229  
   230    boolean finalizedAnI;
   231  
   232    private class AnI_Traced extends AnI {
   233      @Override
   234      public void finalize() throws Throwable {
   235        finalizedAnI = true;
   236        super.finalize();
   237      }
   238    }
   239  
   240    public void testJavaRefGC() {
   241      finalizedAnI = false;
   242      AnI obj = new AnI_Traced();
   243      Testpkg.CallF(obj);
   244      assertTrue("want F to be called", obj.calledF);
   245      Testpkg.CallF(obj);
   246      obj = null;
   247      runGC();
   248      assertTrue("want obj to be collected", finalizedAnI);
   249    }
   250  
   251    public void testJavaRefKeep() {
   252      finalizedAnI = false;
   253      AnI obj = new AnI_Traced();
   254      Testpkg.CallF(obj);
   255      Testpkg.CallF(obj);
   256      obj = null;
   257      runGC();
   258      assertTrue("want obj not to be kept by Go", finalizedAnI);
   259  
   260      finalizedAnI = false;
   261      obj = new AnI_Traced();
   262      Testpkg.Keep(obj);
   263      obj = null;
   264      runGC();
   265      assertFalse("want obj to be kept live by Go", finalizedAnI);
   266    }
   267  
   268    private void runGC() {
   269      System.gc();
   270      System.runFinalization();
   271      Testpkg.GC();
   272      System.gc();
   273      System.runFinalization();
   274    }
   275  
   276    public void testUnnamedParams() {
   277      final String msg = "1234567";
   278      assertEquals("want the length of \"1234567\" passed after unnamed params",
   279  		    7, Testpkg.UnnamedParams(10, 20, msg));
   280    }
   281  
   282    public void testPointerToStructAsField() {
   283      Testpkg.Node a = Testpkg.NewNode("A");
   284      Testpkg.Node b = Testpkg.NewNode("B");
   285      a.setNext(b);
   286      String got = a.String();
   287      assertEquals("want Node A points to Node B", "A:B:<end>", got);
   288    }
   289  
   290    public void testErrorField() {
   291      final String want = "an error message";
   292      Testpkg.Node n = Testpkg.NewNode("ErrTest");
   293      n.setErr(want);
   294      String got = n.getErr();
   295      assertEquals("want back the error message we set", want, got);
   296    }
   297  }