KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > jbfc > BFCompilerTest


1 /***
2  * ASM examples: examples showing how ASM can be used
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.jbfc;
31
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.ByteArrayOutputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.PrintStream JavaDoc;
36 import java.io.StringReader JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39
40 import junit.framework.TestCase;
41
42 import org.objectweb.asm.ClassWriter;
43
44 /**
45  * A naive implementation of compiler for Brain**** language.
46  * http://www.muppetlabs.com/~breadbox/bf/ *
47  *
48  * @author Eugene Kuleshov
49  */

50 public class BFCompilerTest extends TestCase {
51     private BFCompiler bc;
52
53     private ClassWriter cw;
54
55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57         bc = new BFCompiler();
58         cw = new ClassWriter(true);
59     }
60
61     public void testCompileHelloWorld() throws Throwable JavaDoc {
62         assertEquals("Hello World!\n",
63                 execute("Hello",
64                         ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]"
65                                 + "<.#>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++["
66                                 + "<++++>-]<+.[-]++++++++++.",
67                         ""));
68     }
69
70     public void testCompileEcho() throws Throwable JavaDoc {
71         assertEquals("AAA", execute("Echo", ",+[-.,+]", "AAA"));
72     }
73
74     public void testCompileYaPi() throws Throwable JavaDoc {
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 JavaDoc {
98         assertEquals("H\n", execute("Test1",
99                 "[]++++++++++[>++++++++++++++++++>+++++++>+<<<-]A;?@![#>>"
100                         + "+<<]>[>++<[-]]>.>.",
101                 ""));
102     }
103
104     private String JavaDoc execute(String JavaDoc name, String JavaDoc code, String JavaDoc input)
105             throws Throwable JavaDoc
106     {
107         bc.compile(new StringReader JavaDoc(code), name, name, cw);
108
109         // ClassReader cr = new ClassReader(cw.toByteArray());
110
// cr.accept(new TraceClassVisitor(null, new PrintWriter(System.err)),
111
// true);
112

113         // File tmp = File.createTempFile(name, ".class");
114
// System.err.println(tmp.getAbsolutePath());
115
// FileOutputStream fos = new FileOutputStream(tmp);
116
// fos.write(cw.toByteArray());
117
// fos.flush();
118
// fos.close();
119

120         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
121         InputStream JavaDoc is = System.in;
122         PrintStream JavaDoc os = System.out;
123         System.setIn(new ByteArrayInputStream JavaDoc(input.getBytes()));
124         System.setOut(new PrintStream JavaDoc(bos));
125
126         try {
127             TestClassLoader cl = new TestClassLoader(getClass().getClassLoader(),
128                     name,
129                     cw.toByteArray());
130             Class JavaDoc c = cl.loadClass(name);
131             Method JavaDoc m = c.getDeclaredMethod("main",
132                     new Class JavaDoc[] { String JavaDoc[].class });
133             m.invoke(null, new Object JavaDoc[] { new String JavaDoc[0] });
134
135         } catch (InvocationTargetException JavaDoc ex) {
136             throw ex.getCause();
137
138         } finally {
139             System.setIn(is);
140             System.setOut(os);
141
142         }
143         return new String JavaDoc(bos.toByteArray(), "ASCII");
144     }
145
146     private static final class TestClassLoader extends ClassLoader JavaDoc {
147         private final String JavaDoc className;
148
149         private final ClassLoader JavaDoc cl;
150
151         private final byte[] bytecode;
152
153         public TestClassLoader(ClassLoader JavaDoc cl, String JavaDoc className, byte[] bytecode)
154         {
155             super();
156             this.cl = cl;
157             this.className = className;
158             this.bytecode = bytecode;
159         }
160
161         public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
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