| OLD | NEW |
| (Empty) |
| 1 package go; | |
| 2 | |
| 3 import android.test.suitebuilder.annotation.Suppress; | |
| 4 import android.test.AndroidTestCase; | |
| 5 import android.test.MoreAsserts; | |
| 6 import java.util.Arrays; | |
| 7 import java.util.Random; | |
| 8 | |
| 9 import go.testpkg.Testpkg; | |
| 10 | |
| 11 public class SeqTest extends AndroidTestCase { | |
| 12 protected void setUp() throws Exception { | |
| 13 super.setUp(); | |
| 14 Go.init(this.getContext()); | |
| 15 } | |
| 16 | |
| 17 public void testAdd() { | |
| 18 long res = Testpkg.Add(3, 4); | |
| 19 assertEquals("Unexpected arithmetic failure", 7, res); | |
| 20 } | |
| 21 | |
| 22 public void testShortString() { | |
| 23 String want = "a short string"; | |
| 24 String got = Testpkg.StrDup(want); | |
| 25 assertEquals("Strings should match", want, got); | |
| 26 } | |
| 27 | |
| 28 public void testLongString() { | |
| 29 StringBuilder b = new StringBuilder(); | |
| 30 for (int i = 0; i < 128*1024; i++) { | |
| 31 b.append("0123456789"); | |
| 32 } | |
| 33 String want = b.toString(); | |
| 34 String got = Testpkg.StrDup(want); | |
| 35 assertEquals("Strings should match", want, got); | |
| 36 } | |
| 37 | |
| 38 public void testUnicode() { | |
| 39 String want = "Hello, 世界"; | |
| 40 String got = Testpkg.StrDup(want); | |
| 41 assertEquals("Strings should match", want, got); | |
| 42 } | |
| 43 | |
| 44 public void testNilErr() throws Exception { | |
| 45 Testpkg.Err(null); // returns nil, no exception | |
| 46 } | |
| 47 | |
| 48 public void testErr() { | |
| 49 String msg = "Go errors are dropped into the confusing space of exceptions"; | |
| 50 try { | |
| 51 Testpkg.Err(msg); | |
| 52 fail("expected non-nil error to be turned into an exception"); | |
| 53 } catch (Exception e) { | |
| 54 assertEquals("messages should match", msg, e.getMessage()); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 public void testByteArray() { | |
| 59 for (int i = 0; i < 2048; i++) { | |
| 60 if (i == 0) { | |
| 61 byte[] got = Testpkg.BytesAppend(null, null); | |
| 62 assertEquals("Bytes(null+null) should match", (byte[])null, got); | |
| 63 got = Testpkg.BytesAppend(new byte[0], new byte[0]); | |
| 64 assertEquals("Bytes(empty+empty) should match", (byte[])null, got); | |
| 65 continue; | |
| 66 } | |
| 67 | |
| 68 byte[] want = new byte[i]; | |
| 69 new Random().nextBytes(want); | |
| 70 | |
| 71 byte[] s1 = null; | |
| 72 byte[] s2 = null; | |
| 73 if (i > 0) { | |
| 74 s1 = Arrays.copyOfRange(want, 0, 1); | |
| 75 } | |
| 76 if (i > 1) { | |
| 77 s2 = Arrays.copyOfRange(want, 1, i); | |
| 78 } | |
| 79 byte[] got = Testpkg.BytesAppend(s1, s2); | |
| 80 MoreAsserts.assertEquals("Bytes(len="+i+") should match", want, got); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 // Test for golang.org/issue/9486. | |
| 85 public void testByteArrayAfterString() { | |
| 86 byte[] bytes = new byte[1024]; | |
| 87 for (int i=0; i < bytes.length; i++) { | |
| 88 bytes[i] = 8; | |
| 89 } | |
| 90 | |
| 91 String stuff = "stuff"; | |
| 92 byte[] got = Testpkg.AppendToString(stuff, bytes); | |
| 93 | |
| 94 try { | |
| 95 byte[] s = stuff.getBytes("UTF-8"); | |
| 96 byte[] want = new byte[s.length + bytes.length]; | |
| 97 System.arraycopy(s, 0, want, 0, s.length); | |
| 98 System.arraycopy(bytes, 0, want, s.length, bytes.length); | |
| 99 MoreAsserts.assertEquals("Bytes should match", want, got); | |
| 100 } catch (Exception e) { | |
| 101 fail("Cannot perform the test: " + e.toString()); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 public void testGoRefGC() { | |
| 106 Testpkg.S s = Testpkg.New(); | |
| 107 runGC(); | |
| 108 long collected = Testpkg.NumSCollected(); | |
| 109 assertEquals("Only S should be pinned", 0, collected); | |
| 110 | |
| 111 s = null; | |
| 112 runGC(); | |
| 113 collected = Testpkg.NumSCollected(); | |
| 114 assertEquals("S should be collected", 1, collected); | |
| 115 } | |
| 116 | |
| 117 boolean finalizedAnI; | |
| 118 | |
| 119 private class AnI extends Testpkg.I.Stub { | |
| 120 public void E() throws Exception { | |
| 121 throw new Exception("my exception from E"); | |
| 122 } | |
| 123 | |
| 124 boolean calledF; | |
| 125 public void F() { | |
| 126 calledF = true; | |
| 127 } | |
| 128 | |
| 129 public Testpkg.I I() { | |
| 130 return this; | |
| 131 } | |
| 132 | |
| 133 public Testpkg.S S() { | |
| 134 return Testpkg.New(); | |
| 135 } | |
| 136 | |
| 137 public long V() { | |
| 138 return 1234; | |
| 139 } | |
| 140 | |
| 141 public long VE() throws Exception { | |
| 142 throw new Exception("my exception from VE"); | |
| 143 } | |
| 144 | |
| 145 public String name; | |
| 146 | |
| 147 public String String() { | |
| 148 return name; | |
| 149 } | |
| 150 | |
| 151 @Override | |
| 152 public void finalize() throws Throwable { | |
| 153 finalizedAnI = true; | |
| 154 super.finalize(); | |
| 155 } | |
| 156 } | |
| 157 // TODO(hyangah): add tests for methods that take parameters. | |
| 158 | |
| 159 public void testInterfaceMethodReturnsError() { | |
| 160 final AnI obj = new AnI(); | |
| 161 try { | |
| 162 Testpkg.CallE(obj); | |
| 163 fail("Expecting exception but none was thrown."); | |
| 164 } catch (Exception e) { | |
| 165 assertEquals("Error messages should match", "my exception from E", e.getMe
ssage()); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 public void testInterfaceMethodVoid() { | |
| 170 final AnI obj = new AnI(); | |
| 171 Testpkg.CallF(obj); | |
| 172 assertTrue("Want AnI.F to be called", obj.calledF); | |
| 173 } | |
| 174 | |
| 175 public void testInterfaceMethodReturnsInterface() { | |
| 176 AnI obj = new AnI(); | |
| 177 obj.name = "testing AnI.I"; | |
| 178 Testpkg.I i = Testpkg.CallI(obj); | |
| 179 assertEquals("Want AnI.I to return itself", i.String(), obj.String()); | |
| 180 } | |
| 181 | |
| 182 public void testInterfaceMethodReturnsStructPointer() { | |
| 183 final AnI obj = new AnI(); | |
| 184 Testpkg.S s = Testpkg.CallS(obj); | |
| 185 } | |
| 186 | |
| 187 public void testInterfaceMethodReturnsInt() { | |
| 188 final AnI obj = new AnI(); | |
| 189 assertEquals("Values must match", 1234, Testpkg.CallV(obj)); | |
| 190 } | |
| 191 | |
| 192 public void testInterfaceMethodReturnsIntOrError() { | |
| 193 final AnI obj = new AnI(); | |
| 194 try { | |
| 195 long v = Testpkg.CallVE(obj); | |
| 196 fail("Expecting exception but none was thrown and got value " + v); | |
| 197 } catch (Exception e) { | |
| 198 assertEquals("Error messages should match", "my exception from VE", e.getM
essage()); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 /* Suppress this test for now; it's flaky or broken. */ | |
| 203 @Suppress | |
| 204 public void testJavaRefGC() { | |
| 205 finalizedAnI = false; | |
| 206 AnI obj = new AnI(); | |
| 207 runGC(); | |
| 208 Testpkg.CallF(obj); | |
| 209 assertTrue("want F to be called", obj.calledF); | |
| 210 obj = null; | |
| 211 runGC(); | |
| 212 assertTrue("want obj to be collected", finalizedAnI); | |
| 213 } | |
| 214 | |
| 215 public void testJavaRefKeep() { | |
| 216 finalizedAnI = false; | |
| 217 AnI obj = new AnI(); | |
| 218 Testpkg.Keep(obj); | |
| 219 obj = null; | |
| 220 runGC(); | |
| 221 assertFalse("want obj to be kept live by Go", finalizedAnI); | |
| 222 } | |
| 223 | |
| 224 private void runGC() { | |
| 225 System.gc(); | |
| 226 System.runFinalization(); | |
| 227 Testpkg.GC(); | |
| 228 System.gc(); | |
| 229 System.runFinalization(); | |
| 230 } | |
| 231 } | |
| OLD | NEW |