2015-04-28 16:29:48 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-31 12:25:23 +00:00
|
|
|
package go;
|
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
import android.test.suitebuilder.annotation.Suppress;
|
2015-02-24 16:08:16 +00:00
|
|
|
import android.test.AndroidTestCase;
|
2014-12-19 17:59:43 +00:00
|
|
|
import android.test.MoreAsserts;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Random;
|
2014-11-11 14:55:39 +00:00
|
|
|
|
2014-07-31 12:25:23 +00:00
|
|
|
import go.testpkg.Testpkg;
|
|
|
|
|
2015-02-24 16:08:16 +00:00
|
|
|
public class SeqTest extends AndroidTestCase {
|
2015-03-04 22:54:55 +00:00
|
|
|
public SeqTest() {
|
2015-02-24 16:08:16 +00:00
|
|
|
Go.init(this.getContext());
|
2014-11-11 14:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testAdd() {
|
|
|
|
long res = Testpkg.Add(3, 4);
|
|
|
|
assertEquals("Unexpected arithmetic failure", 7, res);
|
|
|
|
}
|
2014-07-31 12:25:23 +00:00
|
|
|
|
2015-05-14 19:46:09 +00:00
|
|
|
public void testBool() {
|
|
|
|
assertTrue(Testpkg.Negate(false));
|
|
|
|
assertFalse(Testpkg.Negate(true));
|
|
|
|
}
|
|
|
|
|
2014-12-11 17:11:02 +00:00
|
|
|
public void testShortString() {
|
|
|
|
String want = "a short string";
|
|
|
|
String got = Testpkg.StrDup(want);
|
|
|
|
assertEquals("Strings should match", want, got);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testLongString() {
|
|
|
|
StringBuilder b = new StringBuilder();
|
|
|
|
for (int i = 0; i < 128*1024; i++) {
|
|
|
|
b.append("0123456789");
|
|
|
|
}
|
|
|
|
String want = b.toString();
|
|
|
|
String got = Testpkg.StrDup(want);
|
|
|
|
assertEquals("Strings should match", want, got);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testUnicode() {
|
|
|
|
String want = "Hello, 世界";
|
|
|
|
String got = Testpkg.StrDup(want);
|
|
|
|
assertEquals("Strings should match", want, got);
|
|
|
|
}
|
|
|
|
|
2014-12-12 13:05:05 +00:00
|
|
|
public void testNilErr() throws Exception {
|
|
|
|
Testpkg.Err(null); // returns nil, no exception
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testErr() {
|
|
|
|
String msg = "Go errors are dropped into the confusing space of exceptions";
|
|
|
|
try {
|
|
|
|
Testpkg.Err(msg);
|
|
|
|
fail("expected non-nil error to be turned into an exception");
|
|
|
|
} catch (Exception e) {
|
|
|
|
assertEquals("messages should match", msg, e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:59:43 +00:00
|
|
|
public void testByteArray() {
|
|
|
|
for (int i = 0; i < 2048; i++) {
|
|
|
|
if (i == 0) {
|
|
|
|
byte[] got = Testpkg.BytesAppend(null, null);
|
|
|
|
assertEquals("Bytes(null+null) should match", (byte[])null, got);
|
|
|
|
got = Testpkg.BytesAppend(new byte[0], new byte[0]);
|
|
|
|
assertEquals("Bytes(empty+empty) should match", (byte[])null, got);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] want = new byte[i];
|
|
|
|
new Random().nextBytes(want);
|
|
|
|
|
|
|
|
byte[] s1 = null;
|
|
|
|
byte[] s2 = null;
|
|
|
|
if (i > 0) {
|
|
|
|
s1 = Arrays.copyOfRange(want, 0, 1);
|
|
|
|
}
|
|
|
|
if (i > 1) {
|
|
|
|
s2 = Arrays.copyOfRange(want, 1, i);
|
|
|
|
}
|
|
|
|
byte[] got = Testpkg.BytesAppend(s1, s2);
|
|
|
|
MoreAsserts.assertEquals("Bytes(len="+i+") should match", want, got);
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 18:07:46 +00:00
|
|
|
|
|
|
|
// Test for golang.org/issue/9486.
|
|
|
|
public void testByteArrayAfterString() {
|
|
|
|
byte[] bytes = new byte[1024];
|
|
|
|
for (int i=0; i < bytes.length; i++) {
|
|
|
|
bytes[i] = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
String stuff = "stuff";
|
|
|
|
byte[] got = Testpkg.AppendToString(stuff, bytes);
|
|
|
|
|
|
|
|
try {
|
|
|
|
byte[] s = stuff.getBytes("UTF-8");
|
|
|
|
byte[] want = new byte[s.length + bytes.length];
|
|
|
|
System.arraycopy(s, 0, want, 0, s.length);
|
|
|
|
System.arraycopy(bytes, 0, want, s.length, bytes.length);
|
|
|
|
MoreAsserts.assertEquals("Bytes should match", want, got);
|
|
|
|
} catch (Exception e) {
|
|
|
|
fail("Cannot perform the test: " + e.toString());
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 17:59:43 +00:00
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
public void testGoRefGC() {
|
|
|
|
Testpkg.S s = Testpkg.New();
|
|
|
|
runGC();
|
|
|
|
long collected = Testpkg.NumSCollected();
|
|
|
|
assertEquals("Only S should be pinned", 0, collected);
|
2014-07-31 12:25:23 +00:00
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
s = null;
|
|
|
|
runGC();
|
|
|
|
collected = Testpkg.NumSCollected();
|
|
|
|
assertEquals("S should be collected", 1, collected);
|
|
|
|
}
|
2014-07-31 12:25:23 +00:00
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
boolean finalizedAnI;
|
2014-07-31 12:25:23 +00:00
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
private class AnI extends Testpkg.I.Stub {
|
2015-01-06 18:50:43 +00:00
|
|
|
public void E() throws Exception {
|
|
|
|
throw new Exception("my exception from E");
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean calledF;
|
2014-11-11 14:55:39 +00:00
|
|
|
public void F() {
|
2015-01-06 18:50:43 +00:00
|
|
|
calledF = true;
|
2014-11-11 14:55:39 +00:00
|
|
|
}
|
2015-01-06 18:50:43 +00:00
|
|
|
|
|
|
|
public Testpkg.I I() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Testpkg.S S() {
|
|
|
|
return Testpkg.New();
|
|
|
|
}
|
|
|
|
|
2015-05-12 13:44:08 +00:00
|
|
|
public String StoString(Testpkg.S s) {
|
|
|
|
return s.String();
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:50:43 +00:00
|
|
|
public long V() {
|
|
|
|
return 1234;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long VE() throws Exception {
|
|
|
|
throw new Exception("my exception from VE");
|
|
|
|
}
|
|
|
|
|
|
|
|
public String name;
|
|
|
|
|
|
|
|
public String String() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
@Override
|
|
|
|
public void finalize() throws Throwable {
|
|
|
|
finalizedAnI = true;
|
|
|
|
super.finalize();
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 18:50:43 +00:00
|
|
|
// TODO(hyangah): add tests for methods that take parameters.
|
|
|
|
|
|
|
|
public void testInterfaceMethodReturnsError() {
|
|
|
|
final AnI obj = new AnI();
|
|
|
|
try {
|
|
|
|
Testpkg.CallE(obj);
|
|
|
|
fail("Expecting exception but none was thrown.");
|
|
|
|
} catch (Exception e) {
|
|
|
|
assertEquals("Error messages should match", "my exception from E", e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testInterfaceMethodVoid() {
|
|
|
|
final AnI obj = new AnI();
|
|
|
|
Testpkg.CallF(obj);
|
|
|
|
assertTrue("Want AnI.F to be called", obj.calledF);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testInterfaceMethodReturnsInterface() {
|
|
|
|
AnI obj = new AnI();
|
|
|
|
obj.name = "testing AnI.I";
|
|
|
|
Testpkg.I i = Testpkg.CallI(obj);
|
|
|
|
assertEquals("Want AnI.I to return itself", i.String(), obj.String());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testInterfaceMethodReturnsStructPointer() {
|
|
|
|
final AnI obj = new AnI();
|
|
|
|
Testpkg.S s = Testpkg.CallS(obj);
|
|
|
|
}
|
|
|
|
|
2015-05-12 13:44:08 +00:00
|
|
|
public void testInterfaceMethodTakesStructPointer() {
|
|
|
|
final AnI obj = new AnI();
|
|
|
|
Testpkg.S s = Testpkg.CallS(obj);
|
|
|
|
String got = obj.StoString(s);
|
|
|
|
String want = s.String();
|
|
|
|
assertEquals("Want AnI.StoString(s) to call s's String", want, got);
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:50:43 +00:00
|
|
|
public void testInterfaceMethodReturnsInt() {
|
|
|
|
final AnI obj = new AnI();
|
|
|
|
assertEquals("Values must match", 1234, Testpkg.CallV(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testInterfaceMethodReturnsIntOrError() {
|
|
|
|
final AnI obj = new AnI();
|
|
|
|
try {
|
|
|
|
long v = Testpkg.CallVE(obj);
|
|
|
|
fail("Expecting exception but none was thrown and got value " + v);
|
|
|
|
} catch (Exception e) {
|
|
|
|
assertEquals("Error messages should match", "my exception from VE", e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2014-07-31 12:25:23 +00:00
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
/* Suppress this test for now; it's flaky or broken. */
|
|
|
|
@Suppress
|
|
|
|
public void testJavaRefGC() {
|
|
|
|
finalizedAnI = false;
|
|
|
|
AnI obj = new AnI();
|
|
|
|
runGC();
|
2015-01-06 18:50:43 +00:00
|
|
|
Testpkg.CallF(obj);
|
|
|
|
assertTrue("want F to be called", obj.calledF);
|
2014-11-11 14:55:39 +00:00
|
|
|
obj = null;
|
|
|
|
runGC();
|
|
|
|
assertTrue("want obj to be collected", finalizedAnI);
|
|
|
|
}
|
2014-07-31 12:25:23 +00:00
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
public void testJavaRefKeep() {
|
|
|
|
finalizedAnI = false;
|
|
|
|
AnI obj = new AnI();
|
|
|
|
Testpkg.Keep(obj);
|
|
|
|
obj = null;
|
|
|
|
runGC();
|
|
|
|
assertFalse("want obj to be kept live by Go", finalizedAnI);
|
|
|
|
}
|
2014-07-31 12:25:23 +00:00
|
|
|
|
2014-11-11 14:55:39 +00:00
|
|
|
private void runGC() {
|
|
|
|
System.gc();
|
|
|
|
System.runFinalization();
|
|
|
|
Testpkg.GC();
|
|
|
|
System.gc();
|
|
|
|
System.runFinalization();
|
|
|
|
}
|
2015-05-12 13:44:08 +00:00
|
|
|
|
|
|
|
public void testUnnamedParams() {
|
|
|
|
final String msg = "1234567";
|
2015-05-13 20:36:42 +00:00
|
|
|
assertEquals("want the length of \"1234567\" passed after unnamed params",
|
2015-05-12 13:44:08 +00:00
|
|
|
7, Testpkg.UnnamedParams(10, 20, msg));
|
|
|
|
}
|
|
|
|
|
2015-05-13 20:36:42 +00:00
|
|
|
public void testPointerToStructAsField() {
|
|
|
|
Testpkg.Node a = Testpkg.NewNode("A");
|
|
|
|
Testpkg.Node b = Testpkg.NewNode("B");
|
|
|
|
a.setNext(b);
|
|
|
|
String got = a.String();
|
|
|
|
assertEquals("want Node A points to Node B", "A:B:<end>", got);
|
|
|
|
}
|
2014-07-31 12:25:23 +00:00
|
|
|
}
|