| 1 |
package org.mozilla.javascript.tests; |
| 2 |
|
| 3 |
import org.junit.After; |
| 4 |
import org.junit.Before; |
| 5 |
import org.junit.Test; |
| 6 |
import org.mozilla.javascript.Context; |
| 7 |
import org.mozilla.javascript.ExternalArrayData; |
| 8 |
import org.mozilla.javascript.Scriptable; |
| 9 |
import org.mozilla.javascript.ScriptableObject; |
| 10 |
import org.mozilla.javascript.tools.shell.Global; |
| 11 |
import org.mozilla.javascript.typedarrays.NativeFloat64Array; |
| 12 |
import org.mozilla.javascript.typedarrays.NativeInt16Array; |
| 13 |
import org.mozilla.javascript.typedarrays.NativeInt32Array; |
| 14 |
|
| 15 |
import java.io.FileReader; |
| 16 |
import java.io.IOException; |
| 17 |
|
| 18 |
import static org.junit.Assert.assertFalse; |
| 19 |
|
| 20 |
public class ExternalArrayTest |
| 21 |
{ |
| 22 |
private Context cx; |
| 23 |
private Scriptable root; |
| 24 |
|
| 25 |
@Before |
| 26 |
public void init() |
| 27 |
{ |
| 28 |
cx = Context.enter(); |
| 29 |
cx.setLanguageVersion(Context.VERSION_1_8); |
| 30 |
cx.setGeneratingDebug(true); |
| 31 |
|
| 32 |
Global global = new Global(cx); |
| 33 |
root = cx.newObject(global); |
| 34 |
} |
| 35 |
|
| 36 |
@After |
| 37 |
public void terminate() |
| 38 |
{ |
| 39 |
Context.exit(); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void testRegularArray() |
| 44 |
{ |
| 45 |
Scriptable a = cx.newArray(root, 10); |
| 46 |
root.put("testArray", root, a); |
| 47 |
root.put("testArrayLength", root, 10); |
| 48 |
root.put("regularArray", root, true); |
| 49 |
runScript("testsrc/jstests/extensions/external-array-test.js", 1); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void testIntArray() |
| 54 |
{ |
| 55 |
ScriptableObject a = (ScriptableObject)cx.newObject(root); |
| 56 |
TestIntArray l = new TestIntArray(10); |
| 57 |
a.setExternalArrayData(l); |
| 58 |
for (int i = 0; i < 10; i++) { |
| 59 |
l.setArrayElement(i, i); |
| 60 |
} |
| 61 |
root.put("testArray", root, a); |
| 62 |
root.put("testArrayLength", root, 10); |
| 63 |
root.put("regularArray", root, false); |
| 64 |
runScript("testsrc/jstests/extensions/external-array-test.js", 1); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void testIntArrayThenRemove() |
| 69 |
{ |
| 70 |
ScriptableObject a = (ScriptableObject)cx.newObject(root); |
| 71 |
|
| 72 |
TestIntArray l = new TestIntArray(10); |
| 73 |
a.setExternalArrayData(l); |
| 74 |
for (int i = 0; i < 10; i++) { |
| 75 |
l.setArrayElement(i, i); |
| 76 |
} |
| 77 |
root.put("testArray", root, a); |
| 78 |
root.put("testArrayLength", root, 10); |
| 79 |
root.put("regularArray", root, false); |
| 80 |
runScript("testsrc/jstests/extensions/external-array-test.js", 1); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
a.delete("stringField"); |
| 85 |
a.delete("intField"); |
| 86 |
a.setExternalArrayData(null); |
| 87 |
for (int i = 0; i < 10; i++) { |
| 88 |
a.put(i, a, i); |
| 89 |
} |
| 90 |
a.defineProperty("length", 10, ScriptableObject.DONTENUM); |
| 91 |
root.put("regularArray", root, true); |
| 92 |
runScript("testsrc/jstests/extensions/external-array-test.js", 1); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void testNativeIntArray() |
| 97 |
{ |
| 98 |
ScriptableObject a = (ScriptableObject)cx.newObject(root); |
| 99 |
NativeInt32Array l = new NativeInt32Array(10); |
| 100 |
a.setExternalArrayData(l); |
| 101 |
|
| 102 |
root.put("testArray", root, a); |
| 103 |
root.put("testArrayLength", root, 10); |
| 104 |
root.put("regularArray", root, false); |
| 105 |
runScript("testsrc/jstests/extensions/external-array-test.js", 1); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void testNativeShortArray() |
| 110 |
{ |
| 111 |
ScriptableObject a = (ScriptableObject)cx.newObject(root); |
| 112 |
NativeInt16Array l = new NativeInt16Array(10); |
| 113 |
a.setExternalArrayData(l); |
| 114 |
|
| 115 |
root.put("testArray", root, a); |
| 116 |
root.put("testArrayLength", root, 10); |
| 117 |
root.put("regularArray", root, false); |
| 118 |
runScript("testsrc/jstests/extensions/external-array-test.js", 1); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void testNativeDoubleArray() |
| 123 |
{ |
| 124 |
ScriptableObject a = (ScriptableObject)cx.newObject(root); |
| 125 |
NativeFloat64Array l = new NativeFloat64Array(10); |
| 126 |
a.setExternalArrayData(l); |
| 127 |
|
| 128 |
root.put("testArray", root, a); |
| 129 |
root.put("testArrayLength", root, 10); |
| 130 |
root.put("regularArray", root, false); |
| 131 |
runScript("testsrc/jstests/extensions/external-array-test.js", 1); |
| 132 |
} |
| 133 |
|
| 134 |
private void runScript(String script, int opt) |
| 135 |
{ |
| 136 |
try { |
| 137 |
cx.setOptimizationLevel(opt); |
| 138 |
FileReader rdr = new FileReader(script); |
| 139 |
|
| 140 |
try { |
| 141 |
cx.evaluateReader(root, rdr, script, 1, null); |
| 142 |
} finally { |
| 143 |
rdr.close(); |
| 144 |
} |
| 145 |
} catch (IOException ioe) { |
| 146 |
assertFalse("I/O Error: " + ioe, true); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
private static class TestIntArray |
| 151 |
implements ExternalArrayData |
| 152 |
{ |
| 153 |
private int[] elements; |
| 154 |
|
| 155 |
public TestIntArray(int length) |
| 156 |
{ |
| 157 |
elements = new int[length]; |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public Object getArrayElement(int index) |
| 162 |
{ |
| 163 |
return elements[index]; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void setArrayElement(int index, Object value) |
| 168 |
{ |
| 169 |
elements[index] = (int)Context.toNumber(value); |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public int getArrayLength() |
| 174 |
{ |
| 175 |
return elements.length; |
| 176 |
} |
| 177 |
} |
| 178 |
} |