source: trunk/abcl/test/src/org/armedbear/lisp/serialization/SerializationTest.java

Last change on this file was 15361, checked in by Mark Evenson, 4 years ago

Serialization of local functions and closures w/ tests (except for compiled closures)

File size: 2.7 KB
Line 
1package org.armedbear.lisp.serialization;
2
3import org.armedbear.lisp.*;
4
5import java.io.*;
6import java.io.ByteArrayInputStream;
7import java.io.ByteArrayOutputStream;
8import java.lang.reflect.Field;
9
10import org.junit.Test;
11
12import static org.armedbear.lisp.Lisp.NIL;
13import static org.armedbear.lisp.Lisp.list;
14import static org.junit.Assert.*;
15
16public 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}
Note: See TracBrowser for help on using the repository browser.