use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.
the class BytecodeGeneratorTest method testFactorial.
@Test
public void testFactorial() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Class stClass = new ClassBuilder("FooObject_testFactorial").method("foo ^100 factorial").build();
Object fooObject = stClass.newInstance();
Method method = fooObject.getClass().getMethod("foo");
Object result = null;
for (int i = 0; i < 10; i++) {
Date start = new Date();
result = method.invoke(fooObject);
Date stop = new Date();
System.out.println("Duration: " + (stop.getTime() - start.getTime()) + " ms");
}
assertEquals(new BigInteger("93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"), result);
}
use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.
the class LiteralsTest method testSymbolLiteral.
@Test
public void testSymbolLiteral() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Class classA = new ClassBuilder("ClassA_testSymbolLiteral").method("foo ^#aap").method("bar ^#aap").build();
Object classAObj = classA.newInstance();
Method method = classAObj.getClass().getMethod("foo");
Object result = method.invoke(classAObj);
assertEquals(Symbol.value("aap"), result);
}
use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.
the class LiteralsTest method testByteArrayLiteral.
@Test
public void testByteArrayLiteral() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Class classA = new ClassBuilder("ClassA_testByteArrayLiteral").method("foo ^#[0 1 127 128 255 0]").method("bar ^#[0 1 127 128 255 0]").method("eq ^self foo == self bar").build();
Object classAObj = classA.newInstance();
Method foo = classAObj.getClass().getMethod("foo");
Method bar = classAObj.getClass().getMethod("bar");
Method eq = classAObj.getClass().getMethod("eq");
Object fooValue = foo.invoke(classAObj);
Object barValue = bar.invoke(classAObj);
Object eqValue = eq.invoke(classAObj);
assertEquals(Boolean.TRUE, eqValue);
assertEquals(fooValue, barValue);
byte[] arr = (byte[]) fooValue;
assertEquals(0, arr[0]);
assertEquals(1, arr[1]);
assertEquals(127, arr[2]);
assertEquals(-128, arr[3]);
assertEquals(-1, arr[4]);
assertEquals(0, arr[5]);
assertEquals(6, arr.length);
}
use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.
the class LoopTest method test_whileFalse.
@Test
public void test_whileFalse() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
Class stClass = new ClassBuilder("FooObject_test_whileFalse").method("foo: sz | sum arr | arr := (1 to: sz) asArray. sum := 0. [sum := sum + arr first. arr := arr tail. arr isEmpty] whileFalse. ^sum").build();
Object fooObject = stClass.newInstance();
Method method = fooObject.getClass().getMethod("foo_", Object.class);
assertEquals(Integer.valueOf(1), method.invoke(fooObject, 1));
assertEquals(Integer.valueOf(21), method.invoke(fooObject, 6));
}
use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.
the class LoopTest method test_BigInt_to_int_do_.
@Test
public void test_BigInt_to_int_do_() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
Class stClass = new ClassBuilder("FooObject_test_BigInt_to_int_do_").method("start Transcript cr; show: 'start'. ^2934758938247592387459832475").method("stop Transcript cr; show: 'stop'. ^7").method("foo | sum | sum := 0. self start to: self stop do: [:i | sum := sum + i]. ^sum").build();
Object fooObject = stClass.newInstance();
Method method = fooObject.getClass().getMethod("foo");
assertEquals(Integer.valueOf(0), method.invoke(fooObject));
}
Aggregations