using Xunit;
|
using WIDESEAWCS_S7Simulator.Core.Memory;
|
|
namespace WIDESEAWCS_S7Simulator.UnitTests.Memory
|
{
|
public class DBRegionTests
|
{
|
[Fact]
|
public void Constructor_WithValidParameters_CreatesRegion()
|
{
|
// Arrange & Act
|
var region = new DBRegion(1, 1024);
|
|
// Assert
|
Assert.Equal("DB", region.RegionType);
|
Assert.Equal(1024, region.Size);
|
}
|
|
[Fact]
|
public void Read_ValidDBNumberAndOffset_ReturnsData()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
var testData = new byte[] { 0x12, 0x34, 0x56, 0x78 };
|
region.Write(1, 0, testData);
|
|
// Act
|
var result = region.Read(1, 0, 4);
|
|
// Assert
|
Assert.Equal(testData, result);
|
}
|
|
[Fact]
|
public void Read_InvalidDBNumber_ThrowsArgumentException()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
|
// Act & Assert
|
Assert.Throws<ArgumentException>(() => region.Read(999, 0, 4));
|
}
|
|
[Fact]
|
public void Read_OutOfBounds_ThrowsArgumentOutOfRange()
|
{
|
// Arrange
|
var region = new DBRegion(1, 100);
|
|
// Act & Assert
|
Assert.Throws<ArgumentOutOfRangeException>(() => region.Read(1, 0, 101));
|
}
|
|
[Fact]
|
public void Write_MultipleDBs_StoresSeparately()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
var data1 = new byte[] { 0x11, 0x22, 0x33, 0x44 };
|
var data2 = new byte[] { 0xAA, 0xBB, 0xCC, 0xDD };
|
|
// Act
|
region.Write(1, 0, data1);
|
region.Write(2, 0, data2);
|
var result1 = region.Read(1, 0, 4);
|
var result2 = region.Read(2, 0, 4);
|
|
// Assert
|
Assert.Equal(data1, result1);
|
Assert.Equal(data2, result2);
|
}
|
|
[Fact]
|
public void Clear_AllBlocks_ZerosAllMemory()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
region.Write(1, 0, new byte[] { 0xFF, 0xFF, 0xFF });
|
region.Write(2, 0, new byte[] { 0xAA, 0xBB, 0xCC });
|
|
// Act
|
region.Clear();
|
var result1 = region.Read(1, 0, 3);
|
var result2 = region.Read(2, 0, 3);
|
|
// Assert
|
Assert.Equal(new byte[] { 0, 0, 0 }, result1);
|
Assert.Equal(new byte[] { 0, 0, 0 }, result2);
|
}
|
|
[Fact]
|
public void ReadInt_ValidOffset_ReturnsCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
region.Write(1, 0, new byte[] { 0x12, 0x34 }); // S7 Int: 0x1234 = 4660
|
|
// Act
|
var result = region.ReadInt(1, 0);
|
|
// Assert
|
Assert.Equal(4660, result);
|
}
|
|
[Fact]
|
public void WriteInt_ValidOffset_WritesCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
|
// Act
|
region.WriteInt(1, 0, 4660); // 0x1234
|
var result = region.Read(1, 0, 2);
|
|
// Assert
|
Assert.Equal(new byte[] { 0x12, 0x34 }, result);
|
}
|
|
[Fact]
|
public void ReadDInt_ValidOffset_ReturnsCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
region.Write(1, 0, new byte[] { 0x00, 0x01, 0x00, 0x00 }); // S7 DInt: 65536
|
|
// Act
|
var result = region.ReadDInt(1, 0);
|
|
// Assert
|
Assert.Equal(65536, result);
|
}
|
|
[Fact]
|
public void WriteDInt_ValidOffset_WritesCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
|
// Act
|
region.WriteDInt(1, 0, 65536); // 0x00010000
|
var result = region.Read(1, 0, 4);
|
|
// Assert
|
Assert.Equal(new byte[] { 0x00, 0x01, 0x00, 0x00 }, result);
|
}
|
|
[Fact]
|
public void ReadReal_ValidOffset_ReturnsCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
region.Write(1, 0, new byte[] { 0x41, 0x20, 0x00, 0x00 }); // IEEE 754: 10.0f
|
|
// Act
|
var result = region.ReadReal(1, 0);
|
|
// Assert
|
Assert.Equal(10.0f, result);
|
}
|
|
[Fact]
|
public void WriteReal_ValidOffset_WritesCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
|
// Act
|
region.WriteReal(1, 0, 10.0f);
|
var result = region.Read(1, 0, 4);
|
|
// Assert
|
Assert.Equal(new byte[] { 0x41, 0x20, 0x00, 0x00 }, result);
|
}
|
|
[Fact]
|
public void ReadBool_ValidOffset_ReturnsCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
region.Write(1, 0, new byte[] { 0xFF }); // 所有位为1
|
|
// Act
|
var result = region.ReadBool(1, 0, 3);
|
|
// Assert
|
Assert.True(result);
|
}
|
|
[Fact]
|
public void WriteBool_ValidOffset_SetsCorrectValue()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
|
// Act
|
region.WriteBool(1, 0, 3, true);
|
var result = region.ReadBool(1, 0, 3);
|
|
// Assert
|
Assert.True(result);
|
}
|
|
[Fact]
|
public void ReadString_WriteAndReadString_ReturnsOriginalString()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
var testString = "Hello";
|
|
// Act
|
region.WriteString(1, 0, testString, 10);
|
var result = region.ReadString(1, 0, 10);
|
|
// Assert
|
Assert.Equal(testString, result);
|
}
|
|
[Fact]
|
public void ReadString_EmptyString_ReturnsEmptyString()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
|
// Act
|
region.WriteString(1, 0, "", 10);
|
var result = region.ReadString(1, 0, 10);
|
|
// Assert
|
Assert.Equal("", result);
|
}
|
|
[Fact]
|
public void ReadString_StringWithMaxLength_RespectsMaxLength()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
var testString = "ABC";
|
|
// Act
|
region.WriteString(1, 0, testString, 5);
|
var result = region.ReadString(1, 0, 5);
|
|
// Assert
|
Assert.Equal(testString, result);
|
}
|
|
[Fact]
|
public void Dispose_ClearsBlocksDictionary()
|
{
|
// Arrange
|
var region = new DBRegion(1, 1024);
|
region.Write(1, 0, new byte[] { 0x12, 0x34 });
|
region.Write(2, 0, new byte[] { 0x56, 0x78 });
|
|
// Act
|
region.Dispose();
|
|
// Assert - trying to access after dispose should handle gracefully
|
// The implementation should clear the dictionary
|
Assert.True(true); // If no exception thrown, test passes
|
}
|
}
|
}
|