github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/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 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 testVar() {
    38      assertEquals("var StringVar", "a string var", Testpkg.getStringVar());
    39  
    40      String newStringVar = "a new string var";
    41      Testpkg.setStringVar(newStringVar);
    42      assertEquals("var StringVar", newStringVar, Testpkg.getStringVar());
    43  
    44      assertEquals("var IntVar", 77, Testpkg.getIntVar());
    45  
    46      long newIntVar = 777;
    47      Testpkg.setIntVar(newIntVar);
    48      assertEquals("var IntVar", newIntVar, Testpkg.getIntVar());
    49  
    50      Testpkg.S s0 = Testpkg.getStructVar();
    51      assertEquals("var StructVar", "a struct var", s0.String());
    52      Testpkg.S s1 = Testpkg.New();
    53      Testpkg.setStructVar(s1);
    54      assertEquals("var StructVar", s1.String(), Testpkg.getStructVar().String());
    55  
    56      // TODO(hyangah): handle nil return value (translate to null)
    57  
    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  
    64    public void testAssets() {
    65      String want = "Hello, Assets.\n";
    66      String got = Testpkg.ReadAsset();
    67      assertEquals("Asset read", want, got);
    68    }
    69  
    70    public void testAdd() {
    71      long res = Testpkg.Add(3, 4);
    72      assertEquals("Unexpected arithmetic failure", 7, res);
    73    }
    74  
    75    public void testBool() {
    76      assertTrue(Testpkg.Negate(false));
    77      assertFalse(Testpkg.Negate(true));
    78    }
    79  
    80    public void testShortString() {
    81      String want = "a short string";
    82      String got = Testpkg.StrDup(want);
    83      assertEquals("Strings should match", want, got);
    84  
    85      want = "";
    86      got = Testpkg.StrDup(want);
    87      assertEquals("Strings should match (empty string)", want, got);
    88  
    89      got = Testpkg.StrDup(null);
    90      assertEquals("Strings should match (null string)", want, got);
    91    }
    92  
    93    public void testLongString() {
    94      StringBuilder b = new StringBuilder();
    95      for (int i = 0; i < 128*1024; i++) {
    96        b.append("0123456789");
    97      }
    98      String want = b.toString();
    99      String got = Testpkg.StrDup(want);
   100      assertEquals("Strings should match", want, got);
   101    }
   102  
   103    public void testUnicode() {
   104      String want = "Hello, 世界";
   105      String got = Testpkg.StrDup(want);
   106      assertEquals("Strings should match", want, got);
   107    }
   108  
   109    public void testNilErr() throws Exception {
   110      Testpkg.Err(null); // returns nil, no exception
   111    }
   112  
   113    public void testErr() {
   114      String msg = "Go errors are dropped into the confusing space of exceptions";
   115      try {
   116        Testpkg.Err(msg);
   117        fail("expected non-nil error to be turned into an exception");
   118      } catch (Exception e) {
   119        assertEquals("messages should match", msg, e.getMessage());
   120      }
   121    }
   122  
   123    public void testByteArray() {
   124      for (int i = 0; i < 2048; i++) {
   125        if (i == 0) {
   126          byte[] got = Testpkg.BytesAppend(null, null);
   127          assertEquals("Bytes(null+null) should match", (byte[])null, got);
   128          got = Testpkg.BytesAppend(new byte[0], new byte[0]);
   129          assertEquals("Bytes(empty+empty) should match", (byte[])null, got);
   130          continue;
   131        }
   132  
   133        byte[] want = new byte[i];
   134        new Random().nextBytes(want);
   135  
   136        byte[] s1 = null;
   137        byte[] s2 = null;
   138        if (i > 0) {
   139          s1 = Arrays.copyOfRange(want, 0, 1);
   140        }
   141        if (i > 1) {
   142          s2 = Arrays.copyOfRange(want, 1, i);
   143        }
   144        byte[] got = Testpkg.BytesAppend(s1, s2);
   145        MoreAsserts.assertEquals("Bytes(len="+i+") should match", want, got);
   146      }
   147    }
   148  
   149    // Test for golang.org/issue/9486.
   150    public void testByteArrayAfterString() {
   151      byte[] bytes = new byte[1024];
   152      for (int i=0; i < bytes.length; i++) {
   153             bytes[i] = 8;
   154      }
   155  
   156      String stuff = "stuff";
   157      byte[] got = Testpkg.AppendToString(stuff, bytes);
   158  
   159      try {
   160        byte[] s = stuff.getBytes("UTF-8");
   161        byte[] want = new byte[s.length + bytes.length];
   162        System.arraycopy(s, 0, want, 0, s.length);
   163        System.arraycopy(bytes, 0, want, s.length, bytes.length);
   164        MoreAsserts.assertEquals("Bytes should match", want, got);
   165      } catch (Exception e) {
   166        fail("Cannot perform the test: " + e.toString());
   167      }
   168    }
   169  
   170    public void testGoRefGC() {
   171      Testpkg.S s = Testpkg.New();
   172      runGC();
   173      long collected = Testpkg.NumSCollected();
   174      assertEquals("Only S should be pinned", 0, collected);
   175  
   176      s = null;
   177      runGC();
   178      collected = Testpkg.NumSCollected();
   179      assertEquals("S should be collected", 1, collected);
   180    }
   181  
   182    private class AnI extends Testpkg.I.Stub {
   183      public void E() throws Exception {
   184        throw new Exception("my exception from E");
   185      }
   186  
   187      boolean calledF;
   188      public void F() {
   189        calledF = true;
   190      }
   191  
   192      public Testpkg.I I() {
   193        return this;
   194      }
   195  
   196      public Testpkg.S S() {
   197        return Testpkg.New();
   198      }
   199  
   200      public String StoString(Testpkg.S s) {
   201        return s.String();
   202      }
   203  
   204      public long V() {
   205        return 1234;
   206      }
   207  
   208      public long VE() throws Exception {
   209        throw new Exception("my exception from VE");
   210      }
   211  
   212      public String name;
   213  
   214      public String String() {
   215        return name;
   216      }
   217  
   218    }
   219  
   220    // TODO(hyangah): add tests for methods that take parameters.
   221  
   222    public void testInterfaceMethodReturnsError() {
   223      final AnI obj = new AnI();
   224      try {
   225        Testpkg.CallE(obj);
   226        fail("Expecting exception but none was thrown.");
   227      } catch (Exception e) {
   228        assertEquals("Error messages should match", "my exception from E", e.getMessage());
   229      }
   230    }
   231  
   232    public void testInterfaceMethodVoid() {
   233      final AnI obj = new AnI();
   234      Testpkg.CallF(obj);
   235      assertTrue("Want AnI.F to be called", obj.calledF);
   236    }
   237  
   238    public void testInterfaceMethodReturnsInterface() {
   239      AnI obj = new AnI();
   240      obj.name = "testing AnI.I";
   241      Testpkg.I i = Testpkg.CallI(obj);
   242      assertEquals("Want AnI.I to return itself", i.String(), obj.String());
   243  
   244      runGC();
   245  
   246      i = Testpkg.CallI(obj);
   247      assertEquals("Want AnI.I to return itself", i.String(), obj.String());
   248    }
   249  
   250    public void testInterfaceMethodReturnsStructPointer() {
   251      final AnI obj = new AnI();
   252      for (int i = 0; i < 5; i++) {
   253      	Testpkg.S s = Testpkg.CallS(obj);
   254  	runGC();
   255      }
   256    }
   257  
   258    public void testInterfaceMethodTakesStructPointer() {
   259      final AnI obj = new AnI();
   260      Testpkg.S s = Testpkg.CallS(obj);
   261      String got = obj.StoString(s);
   262      String want = s.String();
   263      assertEquals("Want AnI.StoString(s) to call s's String", want, got);
   264    }
   265  
   266    public void testInterfaceMethodReturnsInt() {
   267      final AnI obj = new AnI();
   268      assertEquals("Values must match", 1234, Testpkg.CallV(obj));
   269    }
   270  
   271    public void testInterfaceMethodReturnsIntOrError() {
   272      final AnI obj = new AnI();
   273      try {
   274        long v = Testpkg.CallVE(obj);
   275        fail("Expecting exception but none was thrown and got value " + v);
   276      } catch (Exception e) {
   277        assertEquals("Error messages should match", "my exception from VE", e.getMessage());
   278      }
   279    }
   280  
   281    boolean finalizedAnI;
   282  
   283    private class AnI_Traced extends AnI {
   284      @Override
   285      public void finalize() throws Throwable {
   286        finalizedAnI = true;
   287        super.finalize();
   288      }
   289    }
   290  
   291    public void testJavaRefGC() {
   292      finalizedAnI = false;
   293      AnI obj = new AnI_Traced();
   294      Testpkg.CallF(obj);
   295      assertTrue("want F to be called", obj.calledF);
   296      Testpkg.CallF(obj);
   297      obj = null;
   298      runGC();
   299      assertTrue("want obj to be collected", finalizedAnI);
   300    }
   301  
   302    public void testJavaRefKeep() {
   303      finalizedAnI = false;
   304      AnI obj = new AnI_Traced();
   305      Testpkg.CallF(obj);
   306      Testpkg.CallF(obj);
   307      obj = null;
   308      runGC();
   309      assertTrue("want obj not to be kept by Go", finalizedAnI);
   310  
   311      finalizedAnI = false;
   312      obj = new AnI_Traced();
   313      Testpkg.Keep(obj);
   314      obj = null;
   315      runGC();
   316      assertFalse("want obj to be kept live by Go", finalizedAnI);
   317    }
   318  
   319    private int countI = 0;
   320  
   321    private class CountI extends Testpkg.I.Stub {
   322      public void F() { countI++; }
   323  
   324      public void E() throws Exception {}
   325      public Testpkg.I I() { return null; }
   326      public Testpkg.S S() { return null; }
   327      public String StoString(Testpkg.S s) { return ""; }
   328      public long V() { return 0; }
   329      public long VE() throws Exception { return 0; }
   330      public String String() { return ""; }
   331    }
   332  
   333    public void testGoRefMapGrow() {
   334      CountI obj = new CountI();
   335      Testpkg.Keep(obj);
   336  
   337      // Push active references beyond base map size.
   338      for (int i = 0; i < 24; i++) {
   339        CountI o = new CountI();
   340        Testpkg.CallF(o);
   341        if (i%3==0) {
   342          Testpkg.Keep(o);
   343        }
   344      }
   345      runGC();
   346      for (int i = 0; i < 128; i++) {
   347        Testpkg.CallF(new CountI());
   348      }
   349  
   350      Testpkg.CallF(obj); // original object needs to work.
   351  
   352      assertEquals(countI, 1+24+128);
   353    }
   354  
   355    private void runGC() {
   356      System.gc();
   357      System.runFinalization();
   358      Testpkg.GC();
   359      System.gc();
   360      System.runFinalization();
   361    }
   362  
   363    public void testUnnamedParams() {
   364      final String msg = "1234567";
   365      assertEquals("want the length of \"1234567\" passed after unnamed params",
   366  		    7, Testpkg.UnnamedParams(10, 20, msg));
   367    }
   368  
   369    public void testPointerToStructAsField() {
   370      Testpkg.Node a = Testpkg.NewNode("A");
   371      Testpkg.Node b = Testpkg.NewNode("B");
   372      a.setNext(b);
   373      String got = a.String();
   374      assertEquals("want Node A points to Node B", "A:B:<end>", got);
   375    }
   376  
   377    public void testErrorField() {
   378      final String want = "an error message";
   379      Testpkg.Node n = Testpkg.NewNode("ErrTest");
   380      n.setErr(want);
   381      String got = n.getErr();
   382      assertEquals("want back the error message we set", want, got);
   383    }
   384  }