1 30 package org.objectweb.asm.jbfc; 31 32 import java.io.ByteArrayInputStream ; 33 import java.io.ByteArrayOutputStream ; 34 import java.io.InputStream ; 35 import java.io.PrintStream ; 36 import java.io.StringReader ; 37 import java.lang.reflect.InvocationTargetException ; 38 import java.lang.reflect.Method ; 39 40 import junit.framework.TestCase; 41 42 import org.objectweb.asm.ClassWriter; 43 44 50 public class BFCompilerTest extends TestCase { 51 private BFCompiler bc; 52 53 private ClassWriter cw; 54 55 protected void setUp() throws Exception { 56 super.setUp(); 57 bc = new BFCompiler(); 58 cw = new ClassWriter(true); 59 } 60 61 public void testCompileHelloWorld() throws Throwable { 62 assertEquals("Hello World!\n", 63 execute("Hello", 64 ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]" 65 + "<.#>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[" 66 + "<++++>-]<+.[-]++++++++++.", 67 "")); 68 } 69 70 public void testCompileEcho() throws Throwable { 71 assertEquals("AAA", execute("Echo", ",+[-.,+]", "AAA")); 72 } 73 74 public void testCompileYaPi() throws Throwable { 75 assertEquals("3.1415926\n", execute("YaPi", 76 ">+++++[<+++++++++>-]>>>>>>\r\n\r\n+++++ +++ (7 " 77 + "digits)\r\n\r\n[<<+>++++++++++>-]<<+>>+++<[->>+" 78 + "<-[>>>]>[[<+>-]>+>>]<<<<<]>[-]>[-]>[<+>-]<[>+<[" 79 + "-\r\n>>>>>>>+<<<<<<<]>[->+>>>>>>+<<<<<<<]>>>>++" 80 + ">>-]>[-]<<<[<<<<<<<]<[->>>>>[>>>>>>>]<\r\n<<<<<" 81 + "<[>>>>[-]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<<[<<++" 82 + "++++++++>>-]>[<<<<[>+>>+<<<-\r\n]>>>[<<<+>>>-]>" 83 + "-]<<<<[>>++>+<<<-]>>->[<<<+>>>-]>[-]<<<[->>+<-[" 84 + ">>>]>[[<+>-]>+>>]<\r\n<<<<]>[-]<<<<<<<<<]>+>>>>" 85 + ">>->>>>[<<<<<<<<+>>>>>>>>-]<<<<<<<[-]++++++++++" 86 + "<[->>+<-\r\n[>>>]>[[<+>-]>+>>]<<<<<]>[-]>[>>>>>" 87 + "+<<<<<-]>[<+>>+<-]>[<+>-]<<<+<+>>[-[-[-[-[-[-\r" 88 + "\n[-[-[-<->[-<+<->>[<<+>>[-]]]]]]]]]]]]<[+++++[" 89 + "<<<<++++++++>>>>>++++++++<-]>+<<<<-\r\n>>[>+>-<" 90 + "<<<<+++++++++>>>-]<<<<[>>>>>>+<<<<<<-]<[>>>>>>>" 91 + ".<<<<<<<<[+.[-]]>>]>[<]<+\r\n>>>[<.>-]<[-]>>>>>" 92 + "[-]<[>>[<<<<<<<+>>>>>>>-]<<-]]>>[-]>+<<<<[-]<]+" 93 + "+++++++++.", 94 "")); 95 } 96 97 public void testCompileTest1() throws Throwable { 98 assertEquals("H\n", execute("Test1", 99 "[]++++++++++[>++++++++++++++++++>+++++++>+<<<-]A;?@![#>>" 100 + "+<<]>[>++<[-]]>.>.", 101 "")); 102 } 103 104 private String execute(String name, String code, String input) 105 throws Throwable 106 { 107 bc.compile(new StringReader (code), name, name, cw); 108 109 113 120 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 121 InputStream is = System.in; 122 PrintStream os = System.out; 123 System.setIn(new ByteArrayInputStream (input.getBytes())); 124 System.setOut(new PrintStream (bos)); 125 126 try { 127 TestClassLoader cl = new TestClassLoader(getClass().getClassLoader(), 128 name, 129 cw.toByteArray()); 130 Class c = cl.loadClass(name); 131 Method m = c.getDeclaredMethod("main", 132 new Class [] { String [].class }); 133 m.invoke(null, new Object [] { new String [0] }); 134 135 } catch (InvocationTargetException ex) { 136 throw ex.getCause(); 137 138 } finally { 139 System.setIn(is); 140 System.setOut(os); 141 142 } 143 return new String (bos.toByteArray(), "ASCII"); 144 } 145 146 private static final class TestClassLoader extends ClassLoader { 147 private final String className; 148 149 private final ClassLoader cl; 150 151 private final byte[] bytecode; 152 153 public TestClassLoader(ClassLoader cl, String className, byte[] bytecode) 154 { 155 super(); 156 this.cl = cl; 157 this.className = className; 158 this.bytecode = bytecode; 159 } 160 161 public Class loadClass(String name) throws ClassNotFoundException { 162 if (className.equals(name)) { 163 return super.defineClass(className, 164 bytecode, 165 0, 166 bytecode.length); 167 } 168 return cl.loadClass(name); 169 } 170 171 } 172 173 } 174 | Popular Tags |