@Test public void testIterate() throws RecognitionException, IOException { VM vm = new VM( "" + "ENTRY { " + " address " + " author " + " title " + " type " + "} {} { label } " + "FUNCTION {test}{ cite$ } " + "READ " + "ITERATE { test }"); Vector<BibtexEntry> v = new Vector<>(); v.add(t1BibtexEntry()); v.add(TestVM.bibtexString2BibtexEntry("@article{test, title=\"BLA\"}")); vm.run(v); Assert.assertEquals(2, vm.getStack().size()); String s1 = (String) vm.getStack().pop(); String s2 = (String) vm.getStack().pop(); if (s1.equals("canh05")) { Assert.assertEquals("test", s2); } else { Assert.assertEquals("canh05", s2); Assert.assertEquals("test", s1); } }
@Test public void testHypthenatedName() throws RecognitionException, IOException { VM vm = new VM(new File("src/test/resources/net/sf/jabref/bst/abbrv.bst")); Vector<BibtexEntry> v = new Vector<>(); v.add(TestVM.bibtexString2BibtexEntry("@article{canh05, author = \"Jean-Paul Sartre\" }")); Assert.assertTrue(vm.run(v).contains("J.-P. Sartre")); }
private static long headerSize(BasicType type) { if (Universe.elementTypeShouldBeAligned(type)) { return alignObjectSize(headerSizeInBytes()) / VM.getVM().getHeapWordSize(); } else { return headerSizeInBytes() / VM.getVM().getHeapWordSize(); } }
private void setup(VM vm) { this.vm = vm; // find a 1-to-1 mixer VMMixer[] mixers = vm.getMixers(); for (int i = 0; i < mixers.length; i++) { if (mixers[i].numInputs() == 2 && mixers[i].getRatio(0) == mixers[i].getRatio(1)) { this.mixer = mixers[i]; } } assert mixer != null : "Could not find 1-to-1 mixer in VM"; // find store int numStores = vm.getStores().length; assert numStores > 0 : "No storage cells in VM"; if (numStores > 1) { System.err.println( "WARNING: BasicEngine only deals with 1 store, will ignore stores 2-" + numStores); } this.store = vm.getStores()[0]; // initialize list of free cells this.free = new boolean[store.getSize()]; for (int i = 0; i < free.length; i++) { free[i] = true; } // set default precision this.precision = DEFAULT_PRECISION; }
/** * <br> * This function performs following checks on allPartitionRegion and bucket2Node region of * Partition Region</br><br> * 1. allPartitionRegion should not be null</br><br> * 2. Size of allPartitionRegion should be no. of regions + 1.</br><br> * 3. Bucket2Node should not be null and size should = no. of regions</br> <br> * 4. Name of the Bucket2Node should be PartitionedRegionHelper.BUCKET_2_NODE_TABLE_PREFIX + * pr.getName().</br> */ private void validateMultiplePartitionedRegions( VM vm0, VM vm1, VM vm2, VM vm3, int startIndexForRegion, int endIndexForRegion) throws Throwable { int AsyncInvocationArrSize = 4; AsyncInvocation[] async = new AsyncInvocation[AsyncInvocationArrSize]; async[0] = vm0.invokeAsync( validateMultiplePartitionRegion(prPrefix, startIndexForRegion, endIndexForRegion)); async[1] = vm1.invokeAsync( validateMultiplePartitionRegion(prPrefix, startIndexForRegion, endIndexForRegion)); async[2] = vm2.invokeAsync( validateMultiplePartitionRegion(prPrefix, startIndexForRegion, endIndexForRegion)); async[3] = vm3.invokeAsync( validateMultiplePartitionRegion(prPrefix, startIndexForRegion, endIndexForRegion)); for (int count = 0; count < AsyncInvocationArrSize; count++) { DistributedTestCase.join(async[count], 30 * 1000, getLogWriter()); } for (int count = 0; count < AsyncInvocationArrSize; count++) { if (async[count].exceptionOccurred()) { fail("exception during " + count, async[count].getException()); } } }
// sha3_memSizeQuadraticCost32_zeroSize @Ignore // TODO #POC9 @Test // contract call quadratic memory use public void test10() { int expectedGas = 313; DataWord key1 = new DataWord(999); DataWord value1 = new DataWord(3); // Set contract into Database String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681"; String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6"; String code = "600061040020600055"; byte[] contractAddrB = Hex.decode(contractAddr); byte[] callerAddrB = Hex.decode(callerAddr); byte[] codeB = Hex.decode(code); byte[] codeKey = HashUtil.sha3(codeB); AccountState accountState = new AccountState(SystemProperties.getDefault()); accountState.setCodeHash(codeKey); ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl(); pi.setOwnerAddress(contractAddrB); Repository repository = pi.getRepository(); repository.createAccount(callerAddrB); repository.addBalance( callerAddrB, new BigInteger( "115792089237316195423570985008687907853269984665640564039457584007913129639935")); repository.createAccount(contractAddrB); repository.saveCode(contractAddrB, codeB); repository.addStorageRow(contractAddrB, key1, value1); // Play the program VM vm = new VM(); Program program = new Program(codeB, pi); try { while (!program.isStopped()) vm.step(program); } catch (RuntimeException e) { program.setRuntimeFailure(e); } System.out.println(); System.out.println("============ Results ============"); BigInteger balance = repository.getBalance(callerAddrB); System.out.println("*** Used gas: " + program.getResult().getGasUsed()); System.out.println("*** Contract Balance: " + balance); // todo: assert caller balance after contract exec repository.close(); assertEquals(expectedGas, program.getResult().getGasUsed()); }
@Test public void testNumNames() throws RecognitionException { VM vm = new VM("FUNCTION {test} { \"Johnny Foo and Mary Bar\" num.names$ }" + "EXECUTE {test}"); List<BibEntry> v = new ArrayList<>(); vm.run(v); Assert.assertEquals(2, vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); }
@Test public void testVMChrToIntIntToChr() throws RecognitionException { VM vm = new VM("FUNCTION {test} { \"H\" chr.to.int$ int.to.chr$ }" + "EXECUTE {test}"); List<BibEntry> v = new ArrayList<>(); vm.run(v); Assert.assertEquals("H", vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); }
@Test public void testVMIntToStr() throws RecognitionException, IOException { VM vm = new VM("FUNCTION {test} { #3 int.to.str$ #9999 int.to.str$}" + "EXECUTE {test}"); Vector<BibtexEntry> v = new Vector<>(); vm.run(v); Assert.assertEquals("9999", vm.getStack().pop()); Assert.assertEquals("3", vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); }
private void initOtherId() { VM vm = getOtherVm(); vm.invoke( new CacheSerializableRunnable("Connect") { public void run2() throws CacheException { getCache(); } }); this.otherId = (DistributedMember) vm.invoke(ProxyDUnitTest.class, "getVMDistributedMember"); }
@Test public void testBuildIn() throws RecognitionException, IOException { VM vm = new VM("EXECUTE {global.max$}"); Vector<BibtexEntry> v = new Vector<>(); vm.run(v); Assert.assertEquals(Integer.MAX_VALUE, vm.getStack().pop()); Assert.assertTrue(vm.getStack().empty()); }
@Test public void testQuote() throws RecognitionException, IOException { VM vm = new VM("FUNCTION {a}{ quote$ quote$ * } EXECUTE {a}"); Vector<BibtexEntry> v = new Vector<>(); vm.run(v); Assert.assertEquals("\"\"", vm.getStack().pop()); }
@Test public void testFormatName() throws RecognitionException, IOException { { VM vm = new VM( "FUNCTION {format}{ \"Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin\" #1 \"{vv~}{ll}{, jj}{, f}?\" format.name$ }" + "EXECUTE {format}"); Vector<BibtexEntry> v = new Vector<>(); vm.run(v); Assert.assertEquals("de~la Vall{\\'e}e~Poussin, C.~L. X.~J?", vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); } VM vm = new VM( "ENTRY { author } { } { label } " + "FUNCTION {presort} { cite$ 'sort.key$ := } " + "ITERATE { presort } " + "READ " + "SORT " + "FUNCTION {format}{ author #2 \"{vv~}{ll}{, jj}{, f}?\" format.name$ }" + "ITERATE {format}"); Vector<BibtexEntry> v = new Vector<>(); v.add(t1BibtexEntry()); v.add( TestVM.bibtexString2BibtexEntry( "@book{test, author=\"Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin\"}")); vm.run(v); Assert.assertEquals("de~la Vall{\\'e}e~Poussin, C.~L. X.~J?", vm.getStack().pop()); Assert.assertEquals("Annabi, H?", vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); }
@Test // CREATE magic public void test4() { /** * #The code will run ------------------ * * <p>contract A: 77045e71a7a2c50903d88e564cd72fab11e82051 ----------- * * <p>a = 0x7f60c860005461012c6020540000000000000000000000000000000000000000 b = * 0x0060005460206000f20000000000000000000000000000000000000000000000 create(100, 0 41) * * <p>contract B: (the contract to be created the addr will be defined to: * 8e45367623a2865132d9bf875d5cfa31b9a0cd94) ----------- a = 200 b = 300 */ // Set contract into Database byte[] caller_addr_bytes = Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"); byte[] contractA_addr_bytes = Hex.decode("77045e71a7a2c50903d88e564cd72fab11e82051"); byte[] codeA = Hex.decode( "7f7f60c860005461012c602054000000000000" + "00000000000000000000000000006000547e60" + "005460206000f2000000000000000000000000" + "0000000000000000000000602054602960006064f0"); ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl(); pi.setOwnerAddress(contractA_addr_bytes); Repository repository = pi.getRepository(); repository.createAccount(contractA_addr_bytes); repository.saveCode(contractA_addr_bytes, codeA); repository.createAccount(caller_addr_bytes); // ****************** // // Play the program // // ****************** // VM vm = new VM(); Program program = new Program(codeA, pi); try { while (!program.isStopped()) vm.step(program); } catch (RuntimeException e) { program.setRuntimeFailure(e); } logger.info("============ Results ============"); System.out.println("*** Used gas: " + program.getResult().getGasUsed()); // TODO: check that the value pushed after exec is the new address repository.close(); }
@Test public void testAbbrv() throws RecognitionException, IOException { VM vm = new VM(new File("src/test/resources/net/sf/jabref/bst/abbrv.bst")); Vector<BibtexEntry> v = new Vector<>(); v.add(t1BibtexEntry()); String expected = "\\begin{thebibliography}{1}\\bibitem{canh05}K.~Crowston, H.~Annabi, J.~Howison, and C.~Masango.\\newblock Effective work practices for floss development: A model and propositions.\\newblock In {\\em Hawaii International Conference On System Sciences (HICSS)}, 2005.\\end{thebibliography}"; Assert.assertEquals(expected.replaceAll("\\s", ""), vm.run(v).replaceAll("\\s", "")); }
@Test public void testVMArithmetic() throws RecognitionException { VM vm = new VM("FUNCTION {test} { " + "#1 #1 + #5 #2 - }" + "EXECUTE {test}"); List<BibEntry> v = new ArrayList<>(); vm.run(v); Assert.assertEquals(3, vm.getStack().pop()); Assert.assertEquals(2, vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); }
private long lengthOffsetInBytes() { if (lengthOffsetInBytes != 0) { return lengthOffsetInBytes; } if (VM.getVM().isCompressedOopsEnabled()) { lengthOffsetInBytes = typeSize - VM.getVM().getIntSize(); } else { lengthOffsetInBytes = typeSize; } return lengthOffsetInBytes; }
public byte[] getResourceData(String name) { Assert.that(VM.isHosted() || VM.getCurrentIsolate().getLeafSuite() == suite); try { byte[] bytes = classPath.getBytes(name); ResourceFile resourceFile = new ResourceFile(name, bytes); suite.installResource(resourceFile); return bytes; } catch (IOException e) { return null; } }
private static long headerSizeInBytes() { if (headerSize != 0) { return headerSize; } if (VM.getVM().isCompressedOopsEnabled()) { headerSize = typeSize; } else { headerSize = VM.getVM().alignUp(typeSize + VM.getVM().getIntSize(), VM.getVM().getHeapWordSize()); } return headerSize; }
@Test public void testVMExecuteSimple() throws RecognitionException { VM vm = new VM( "INTEGERS { variable.a } " + "FUNCTION {init.state.consts}{ #5 'variable.a := } " + "EXECUTE {init.state.consts}"); List<BibEntry> v = new ArrayList<>(); vm.run(v); Assert.assertEquals(Integer.valueOf(5), vm.getIntegers().get("variable.a")); }
@Test public void testFormatName() throws RecognitionException { VM vm = new VM( "FUNCTION {format}{ \"Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin\" #1 \"{vv~}{ll}{, jj}{, f}?\" format.name$ }" + "EXECUTE {format}"); List<BibEntry> v = new ArrayList<>(); vm.run(v); Assert.assertEquals("de~la Vall{\\'e}e~Poussin, C.~L. X.~J?", vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); }
@Test public void testVMSwap() throws RecognitionException, IOException { VM vm = new VM("FUNCTION {a}{ #3 \"Hallo\" swap$ } EXECUTE { a }"); Vector<BibtexEntry> v = new Vector<>(); vm.run(v); Assert.assertEquals(2, vm.getStack().size()); Assert.assertEquals(3, vm.getStack().pop()); Assert.assertEquals("Hallo", vm.getStack().pop()); }
private void doCreateOtherVm() { VM vm = getOtherVm(); vm.invoke( new CacheSerializableRunnable("create root") { public void run2() throws CacheException { AttributesFactory af = new AttributesFactory(); af.setDataPolicy(DataPolicy.REPLICATE); af.setScope(Scope.DISTRIBUTED_ACK); createRootRegion("ProxyDUnitTest", af.create()); } }); }
/** * This function performs destroy(key) operations in multiple Partiton Regions. The range of keys * to be destroyed is from 100 to 200. Each Vm destroys different set of the keys. */ private void destroyInMultiplePartitionedRegion( VM vm0, VM vm1, VM vm2, VM vm3, int startIndexForRegion, int endIndexForRegion) throws Throwable { int AsyncInvocationArrSize = 4; AsyncInvocation[] async = new AsyncInvocation[AsyncInvocationArrSize]; int delta = (endIndexForDestroy - startIndexForDestroy) / 4; async[0] = vm0.invokeAsync( destroyInMultiplePartitionRegion( prPrefix, startIndexForDestroy, startIndexForDestroy + 1 * delta, startIndexForRegion, endIndexForRegion)); async[1] = vm1.invokeAsync( destroyInMultiplePartitionRegion( prPrefix, startIndexForDestroy + 1 * delta, startIndexForDestroy + 2 * delta, startIndexForRegion, endIndexForRegion)); async[2] = vm2.invokeAsync( destroyInMultiplePartitionRegion( prPrefix, startIndexForDestroy + 2 * delta, startIndexForDestroy + 3 * delta, startIndexForRegion, endIndexForRegion)); async[3] = vm3.invokeAsync( destroyInMultiplePartitionRegion( prPrefix, startIndexForDestroy + 3 * delta, endIndexForDestroy, startIndexForRegion, endIndexForRegion)); /** main thread is waiting for the other threads to complete */ for (int count = 0; count < AsyncInvocationArrSize; count++) { DistributedTestCase.join(async[count], 30 * 1000, getLogWriter()); } for (int count = 0; count < AsyncInvocationArrSize; count++) { if (async[count].exceptionOccurred()) { fail("exception during " + count, async[count].getException()); } } }
@Test public void testVMArithmetic2() throws RecognitionException { VM vm = new VM("FUNCTION {test} { " + "#1 \"HELLO\" + #5 #2 - }" + "EXECUTE {test}"); List<BibEntry> v = new ArrayList<>(); try { vm.run(v); Assert.fail(); } catch (VMException ignored) { // Ignored } }
@Test public void testTextLength() throws RecognitionException, IOException { VM vm = new VM( "FUNCTION {test} {" + " \"hello world\" text.length$ " + " \"Hello {W}orld\" text.length$ " + " \"\" text.length$ " + " \"{A}{D}/{Cycle}\" text.length$ " + " \"{\\This is one character}\" text.length$ " + " \"{\\This {is} {one} {c{h}}aracter as well}\" text.length$ " + " \"{\\And this too\" text.length$ " + " \"These are {\\11}\" text.length$ " + "} " + "EXECUTE {test} "); Vector<BibtexEntry> v = new Vector<>(); vm.run(v); Assert.assertEquals(11, vm.getStack().pop()); Assert.assertEquals(1, vm.getStack().pop()); Assert.assertEquals(1, vm.getStack().pop()); Assert.assertEquals(1, vm.getStack().pop()); Assert.assertEquals(8, vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().pop()); Assert.assertEquals(11, vm.getStack().pop()); Assert.assertEquals(11, vm.getStack().pop()); Assert.assertEquals(0, vm.getStack().size()); }
/** {@inheritDoc} */ public void load(Klass klass) { Assert.that(VM.isHosted() || VM.getCurrentIsolate().getLeafSuite() == suite); int state = klass.getState(); if (state < Klass.STATE_LOADED) { if (klass.isArray()) { load(klass.getComponentType()); } else { lastClassNameStack.push(klass.getName()); ClassFile classFile = getClassFile(klass); load(classFile); lastClassNameStack.pop(); } } }
@Test public void testVariables() throws RecognitionException, IOException { VM vm = new VM( " STRINGS { t } " + " FUNCTION {not} { { #0 } { #1 } if$ } " + " FUNCTION {n.dashify} { \"HELLO-WORLD\" 't := t empty$ not } " + " EXECUTE {n.dashify} "); vm.run(new Vector<BibtexEntry>()); Assert.assertEquals(VM.TRUE, vm.getStack().pop()); }
/** * This function performs get() operations in multiple Partition Regions. Each Vm gets keys from 0 * to 400. */ private void getInMultiplePartitionedRegion( VM vm0, VM vm1, VM vm2, VM vm3, int startIndexForRegion, int endIndexForRegion) throws Throwable { int AsyncInvocationArrSize = 4; AsyncInvocation[] async = new AsyncInvocation[AsyncInvocationArrSize]; async[0] = vm0.invokeAsync( getInMultiplePartitionRegion( prPrefix, startIndexForKey, endIndexForKey, startIndexForRegion, endIndexForRegion)); async[1] = vm1.invokeAsync( getInMultiplePartitionRegion( prPrefix, startIndexForKey, endIndexForKey, startIndexForRegion, endIndexForRegion)); async[2] = vm2.invokeAsync( getInMultiplePartitionRegion( prPrefix, startIndexForKey, endIndexForKey, startIndexForRegion, endIndexForRegion)); async[3] = vm3.invokeAsync( getInMultiplePartitionRegion( prPrefix, startIndexForKey, endIndexForKey, startIndexForRegion, endIndexForRegion)); /** main thread is waiting for the other threads to complete */ for (int count = 0; count < AsyncInvocationArrSize; count++) { DistributedTestCase.join(async[count], 30 * 1000, getLogWriter()); } for (int count = 0; count < AsyncInvocationArrSize; count++) { if (async[count].exceptionOccurred()) { fail( "Failed due to exception: " + async[count].getException(), async[count].getException()); } } }
@Test public void testVMFunction1() throws RecognitionException, IOException { VM vm = new VM("FUNCTION {init.state.consts}{ #0 'before.all := } "); Vector<BibtexEntry> v = new Vector<>(); vm.run(v); Assert.assertEquals(38, vm.getFunctions().size()); Assert.assertTrue(vm.getFunctions().get("init.state.consts") instanceof StackFunction); StackFunction fun = (StackFunction) vm.getFunctions().get("init.state.consts"); Assert.assertEquals(3, fun.getTree().getChildCount()); }