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 NUnit.Framework; 9 using Antmicro.Renode.Peripherals; 10 using Antmicro.Renode.Core.Structure; 11 using Antmicro.Renode.Core; 12 using Antmicro.Renode.Peripherals.Bus; 13 using System.Linq; 14 using Antmicro.Renode.Exceptions; 15 16 namespace Antmicro.Renode.UnitTests 17 { 18 [TestFixture] 19 public class NullRegistrationPointPeripheralContainerTests 20 { 21 [Test] ShouldRegisterPeripheral()22 public void ShouldRegisterPeripheral() 23 { 24 container.Register(peripheral, registrationPoint); 25 Assert.IsTrue(machine.IsRegistered(peripheral)); 26 } 27 28 [Test] ShouldThrowWhenSecondPeripheral()29 public void ShouldThrowWhenSecondPeripheral() 30 { 31 container.Register(peripheral2, NullRegistrationPoint.Instance); 32 Assert.Throws<RegistrationException>(() => 33 container.Register(peripheral, NullRegistrationPoint.Instance)); 34 } 35 36 [Test] ShouldUnregisterPeripheral()37 public void ShouldUnregisterPeripheral() 38 { 39 container.Register(peripheral, registrationPoint); 40 container.Unregister(peripheral); 41 Assert.IsFalse(machine.IsRegistered(peripheral)); 42 } 43 44 [Test] ShouldThrowWhenUnregisteringNotRegisteredPeripheral()45 public void ShouldThrowWhenUnregisteringNotRegisteredPeripheral() 46 { 47 container.Register(peripheral2, NullRegistrationPoint.Instance); 48 Assert.Throws<RegistrationException>(() => 49 container.Unregister(peripheral)); 50 } 51 52 [Test] ShouldThrowWhenUnregisteringFromEmptyContainers()53 public void ShouldThrowWhenUnregisteringFromEmptyContainers() 54 { 55 Assert.Throws<RegistrationException>(() => 56 container.Unregister(peripheral)); 57 } 58 59 [Test] ShouldGetRegistrationPoints()60 public void ShouldGetRegistrationPoints() 61 { 62 container.Register(peripheral, registrationPoint); 63 Assert.AreEqual(1, container.GetRegistrationPoints(peripheral).Count()); 64 Assert.AreSame(NullRegistrationPoint.Instance, container.GetRegistrationPoints(peripheral).First()); 65 } 66 67 [Test] ShouldGetEmptyRegistrationPoints()68 public void ShouldGetEmptyRegistrationPoints() 69 { 70 Assert.IsEmpty(container.GetRegistrationPoints(peripheral)); 71 container.Register(peripheral, registrationPoint); 72 container.Unregister(peripheral); 73 Assert.IsEmpty(container.GetRegistrationPoints(peripheral)); 74 } 75 76 [Test] ShouldGetRegisteredPeripheralAsChildren()77 public void ShouldGetRegisteredPeripheralAsChildren() 78 { 79 container.Register(peripheral, registrationPoint); 80 Assert.AreEqual(1, container.GetRegistrationPoints(peripheral).Count()); 81 Assert.AreSame(peripheral, container.Children.First().Peripheral); 82 Assert.AreSame(NullRegistrationPoint.Instance, container.Children.First().RegistrationPoint); 83 } 84 85 [Test] ShouldGetEmptyChildren()86 public void ShouldGetEmptyChildren() 87 { 88 Assert.IsEmpty(container.Children); 89 container.Register(peripheral, registrationPoint); 90 container.Unregister(peripheral); 91 Assert.IsEmpty(container.Children); 92 } 93 94 [Test] ShouldRegister2ndAfterUnregistering()95 public void ShouldRegister2ndAfterUnregistering() 96 { 97 container.Register(peripheral, registrationPoint); 98 container.Unregister(peripheral); 99 container.Register(peripheral2, registrationPoint); 100 Assert.IsFalse(machine.IsRegistered(peripheral)); 101 Assert.IsTrue(machine.IsRegistered(peripheral2)); 102 } 103 104 105 [SetUp] SetUp()106 public void SetUp() 107 { 108 var sysbusRegistrationPoint = new BusRangeRegistration(1337, 666); 109 machine = new Machine(); 110 peripheral = new PeripheralMock(); 111 peripheral2 = new PeripheralMock(); 112 container = new NullRegistrationPointPeripheralContainerMock(machine); 113 registrationPoint = NullRegistrationPoint.Instance; 114 machine.SystemBus.Register(container, sysbusRegistrationPoint); 115 } 116 117 private IMachine machine; 118 private PeripheralMock peripheral; 119 private PeripheralMock peripheral2; 120 private NullRegistrationPointPeripheralContainerMock container; 121 private NullRegistrationPoint registrationPoint; 122 123 private class NullRegistrationPointPeripheralContainerMock : 124 NullRegistrationPointPeripheralContainer<PeripheralMock>, 125 IDoubleWordPeripheral 126 { NullRegistrationPointPeripheralContainerMock(IMachine machine)127 public NullRegistrationPointPeripheralContainerMock(IMachine machine) : base(machine) {} Reset()128 public override void Reset(){} WriteDoubleWord(long offset, uint value)129 public void WriteDoubleWord(long offset, uint value){} ReadDoubleWord(long offset)130 public uint ReadDoubleWord(long offset) 131 { 132 return 1337; 133 } 134 } 135 136 private class PeripheralMock : IPeripheral 137 { Reset()138 public void Reset(){} 139 } 140 } 141 } 142