Tests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using NUnit.Framework;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace Ryujinx.Memory.Tests
  5. {
  6. public class Tests
  7. {
  8. private const ulong MemorySize = 0x8000;
  9. private MemoryBlock _memoryBlock;
  10. [SetUp]
  11. public void Setup()
  12. {
  13. _memoryBlock = new MemoryBlock(MemorySize);
  14. }
  15. [TearDown]
  16. public void Teardown()
  17. {
  18. _memoryBlock.Dispose();
  19. }
  20. [Test]
  21. public void Test_Read()
  22. {
  23. Marshal.WriteInt32(_memoryBlock.Pointer, 0x2020, 0x1234abcd);
  24. Assert.AreEqual(_memoryBlock.Read<int>(0x2020), 0x1234abcd);
  25. }
  26. [Test]
  27. public void Test_Write()
  28. {
  29. _memoryBlock.Write(0x2040, 0xbadc0de);
  30. Assert.AreEqual(Marshal.ReadInt32(_memoryBlock.Pointer, 0x2040), 0xbadc0de);
  31. }
  32. [Test]
  33. public void Test_Alias()
  34. {
  35. if (OperatingSystem.IsMacOS())
  36. {
  37. // Memory aliasing tests fail on CI at the moment.
  38. return;
  39. }
  40. using MemoryBlock backing = new MemoryBlock(0x10000, MemoryAllocationFlags.Mirrorable);
  41. using MemoryBlock toAlias = new MemoryBlock(0x10000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  42. toAlias.MapView(backing, 0x1000, 0, 0x4000);
  43. toAlias.UnmapView(backing, 0x3000, 0x1000);
  44. toAlias.Write(0, 0xbadc0de);
  45. Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, 0x1000), 0xbadc0de);
  46. }
  47. [Test]
  48. public void Test_AliasRandom()
  49. {
  50. if (OperatingSystem.IsMacOS())
  51. {
  52. // Memory aliasing tests fail on CI at the moment.
  53. return;
  54. }
  55. using MemoryBlock backing = new MemoryBlock(0x80000, MemoryAllocationFlags.Mirrorable);
  56. using MemoryBlock toAlias = new MemoryBlock(0x80000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  57. Random rng = new Random(123);
  58. for (int i = 0; i < 20000; i++)
  59. {
  60. int srcPage = rng.Next(0, 64);
  61. int dstPage = rng.Next(0, 64);
  62. int pages = rng.Next(1, 65);
  63. if ((rng.Next() & 1) != 0)
  64. {
  65. toAlias.MapView(backing, (ulong)srcPage << 12, (ulong)dstPage << 12, (ulong)pages << 12);
  66. int offset = rng.Next(0, 0x1000 - sizeof(int));
  67. toAlias.Write((ulong)((dstPage << 12) + offset), 0xbadc0de);
  68. Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, (srcPage << 12) + offset), 0xbadc0de);
  69. }
  70. else
  71. {
  72. toAlias.UnmapView(backing, (ulong)dstPage << 12, (ulong)pages << 12);
  73. }
  74. }
  75. }
  76. [Test]
  77. public void Test_AliasMapLeak()
  78. {
  79. if (OperatingSystem.IsMacOS())
  80. {
  81. // Memory aliasing tests fail on CI at the moment.
  82. return;
  83. }
  84. ulong pageSize = 4096;
  85. ulong size = 100000 * pageSize; // The mappings limit on Linux is usually around 65K, so let's make sure we are above that.
  86. using MemoryBlock backing = new MemoryBlock(pageSize, MemoryAllocationFlags.Mirrorable);
  87. using MemoryBlock toAlias = new MemoryBlock(size, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  88. for (ulong offset = 0; offset < size; offset += pageSize)
  89. {
  90. toAlias.MapView(backing, 0, offset, pageSize);
  91. toAlias.Write(offset, 0xbadc0de);
  92. Assert.AreEqual(0xbadc0de, backing.Read<int>(0));
  93. toAlias.UnmapView(backing, offset, pageSize);
  94. }
  95. }
  96. }
  97. }