| 1 | package org.armedbear.lisp.serialization; |
|---|
| 2 | |
|---|
| 3 | import org.armedbear.lisp.*; |
|---|
| 4 | |
|---|
| 5 | import java.io.*; |
|---|
| 6 | import java.io.ByteArrayInputStream; |
|---|
| 7 | import java.io.ByteArrayOutputStream; |
|---|
| 8 | import java.lang.reflect.Field; |
|---|
| 9 | |
|---|
| 10 | import org.junit.Test; |
|---|
| 11 | |
|---|
| 12 | import static org.armedbear.lisp.Lisp.NIL; |
|---|
| 13 | import static org.armedbear.lisp.Lisp.list; |
|---|
| 14 | import static org.junit.Assert.*; |
|---|
| 15 | |
|---|
| 16 | public class SerializationTest { |
|---|
| 17 | |
|---|
| 18 | @Test |
|---|
| 19 | public void testSerializationOfBuiltInFunction() throws Exception { |
|---|
| 20 | Field declaredField = Primitives.class.getDeclaredField("CONS"); |
|---|
| 21 | declaredField.setAccessible(true); |
|---|
| 22 | Object consFunction = declaredField.get(null); |
|---|
| 23 | |
|---|
| 24 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|---|
| 25 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
|---|
| 26 | oos.writeObject(consFunction); |
|---|
| 27 | |
|---|
| 28 | ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); |
|---|
| 29 | Object readObject = ois.readObject(); |
|---|
| 30 | assertEquals(readObject, consFunction); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | @Test |
|---|
| 34 | public void testSerializationOfLocalFunction() throws Exception { |
|---|
| 35 | LispObject lambda_expression = |
|---|
| 36 | new Cons(Symbol.LAMBDA, new Cons(NIL, NIL)); |
|---|
| 37 | LispObject lambda_name = |
|---|
| 38 | list(Symbol.FLET, new Symbol("test")); |
|---|
| 39 | Closure closure = |
|---|
| 40 | new Closure(lambda_name, lambda_expression, new Environment()); |
|---|
| 41 | |
|---|
| 42 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|---|
| 43 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
|---|
| 44 | oos.writeObject(closure); |
|---|
| 45 | |
|---|
| 46 | ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); |
|---|
| 47 | Object readObject = ois.readObject(); |
|---|
| 48 | assertTrue(readObject instanceof Closure); |
|---|
| 49 | assertEquals(NIL, ((Closure) readObject).execute()); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | @Test |
|---|
| 53 | public void testSerializationOfLocalClosure() throws Exception { |
|---|
| 54 | Symbol symbol = new Symbol("test"); |
|---|
| 55 | LispObject lambda_expression = |
|---|
| 56 | new Cons(Symbol.LAMBDA, new Cons(NIL, new Cons(symbol))); |
|---|
| 57 | LispObject lambda_name = |
|---|
| 58 | list(Symbol.FLET, new Symbol("test")); |
|---|
| 59 | Environment env = new Environment(); |
|---|
| 60 | env.bind(symbol, new SimpleString("OK")); |
|---|
| 61 | Closure closure = |
|---|
| 62 | new Closure(lambda_name, lambda_expression, env); |
|---|
| 63 | |
|---|
| 64 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|---|
| 65 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
|---|
| 66 | oos.writeObject(closure); |
|---|
| 67 | |
|---|
| 68 | ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); |
|---|
| 69 | Object readObject = ois.readObject(); |
|---|
| 70 | assertTrue(readObject instanceof Closure); |
|---|
| 71 | assertEquals("OK", ((Closure) readObject).execute().toString()); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | } |
|---|