1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System.IO;
9 using Antmicro.Migrant;
10 using Antmicro.Renode.Core;
11 using Antmicro.Renode.Peripherals.Python;
12 using Antmicro.Renode.Utilities;
13 using IronPython.Runtime;
14 using NUnit.Framework;
15 
16 namespace Antmicro.Renode.UnitTests.PythonPeripherals
17 {
18     [TestFixture]
19     public class SerializationTests
20     {
21         [Test]
ShouldSerializeSimplePyDev()22         public void ShouldSerializeSimplePyDev()
23         {
24             var source = @"
25 if request.isInit:
26 	num = 4
27 	str = 'napis'
28 if request.isRead:
29 	request.value = num + len(str)
30 ";
31             var pyDev = new PythonPeripheral(100, true, script: source);
32             var copy = Serializer.DeepClone(pyDev);
33             Assert.AreEqual(9, copy.ReadByte(0));
34         }
35 
36         [Test]
ShouldSerializePyDevWithListAndDictionary()37         public void ShouldSerializePyDevWithListAndDictionary()
38         {
39             var source = @"
40 if request.isInit:
41 	some_list = [ 'a', 666 ]
42 	some_dict = { 'lion': 'yellow', 'kitty': 'red' }
43 if request.isRead:
44 	if request.offset == 0:
45 		request.value = some_list[1]
46 	if request.offset == 4:
47 		request.value = len(some_dict['kitty'])
48 ";
49             var pyDev = new PythonPeripheral(100, true, script: source);
50             var serializer = new Serializer();
51             serializer.ForObject<PythonDictionary>().SetSurrogate(x => new PythonDictionarySurrogate(x));
52             serializer.ForSurrogate<PythonDictionarySurrogate>().SetObject(x => x.Restore());
53             var mStream = new MemoryStream();
54             serializer.Serialize(pyDev, mStream);
55             mStream.Seek(0, SeekOrigin.Begin);
56             var copy = serializer.Deserialize<PythonPeripheral>(mStream);
57             Assert.AreEqual(666, copy.ReadDoubleWord(0));
58             Assert.AreEqual(3, copy.ReadDoubleWord(4));
59         }
60 
61         [Test]
ShouldSerializePyDevWithModuleImport()62         public void ShouldSerializePyDevWithModuleImport()
63         {
64             var source = @"
65 import time
66 
67 if request.isRead:
68 	request.value = time.localtime().tm_hour
69 ";
70             var pyDev = new PythonPeripheral(100, true, script: source);
71             var copy = Serializer.DeepClone(pyDev);
72             Assert.AreEqual(CustomDateTime.Now.Hour, copy.ReadDoubleWord(0), 1);
73         }
74 
75         [Test]
ShouldSerializeMachineWithSimplePyDev()76         public void ShouldSerializeMachineWithSimplePyDev()
77         {
78             var source = @"
79 if request.isInit:
80     num = 4
81     str = 'napis'
82 if request.isRead:
83     request.value = num + len(str)
84 ";
85             var pyDev = new PythonPeripheral(100, true, script: source);
86             var sysbus = machine.SystemBus;
87             sysbus.Register(pyDev, new Antmicro.Renode.Peripherals.Bus.BusRangeRegistration(0x100, 0x10));
88 
89             Assert.AreEqual(9, sysbus.ReadByte(0x100));
90             machine = Serializer.DeepClone(machine);
91             sysbus = machine.SystemBus;
92             Assert.AreEqual(9, sysbus.ReadByte(0x100));
93         }
94 
95         [SetUp]
SetUp()96         public void SetUp()
97         {
98             EmulationManager.Instance.Clear();
99             machine = new Machine();
100         }
101 
102         private IMachine machine;
103     }
104 }
105