Tests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // Memory aliasing tests fail on CI at the moment.
  34. [Platform(Exclude = "MacOsX")]
  35. public void Test_Alias()
  36. {
  37. using MemoryBlock backing = new MemoryBlock(0x10000, MemoryAllocationFlags.Mirrorable);
  38. using MemoryBlock toAlias = new MemoryBlock(0x10000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  39. toAlias.MapView(backing, 0x1000, 0, 0x4000);
  40. toAlias.UnmapView(backing, 0x3000, 0x1000);
  41. toAlias.Write(0, 0xbadc0de);
  42. Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, 0x1000), 0xbadc0de);
  43. }
  44. [Test]
  45. // Memory aliasing tests fail on CI at the moment.
  46. [Platform(Exclude = "MacOsX")]
  47. public void Test_AliasRandom()
  48. {
  49. using MemoryBlock backing = new MemoryBlock(0x80000, MemoryAllocationFlags.Mirrorable);
  50. using MemoryBlock toAlias = new MemoryBlock(0x80000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  51. Random rng = new Random(123);
  52. for (int i = 0; i < 20000; i++)
  53. {
  54. int srcPage = rng.Next(0, 64);
  55. int dstPage = rng.Next(0, 64);
  56. int pages = rng.Next(1, 65);
  57. if ((rng.Next() & 1) != 0)
  58. {
  59. toAlias.MapView(backing, (ulong)srcPage << 12, (ulong)dstPage << 12, (ulong)pages << 12);
  60. int offset = rng.Next(0, 0x1000 - sizeof(int));
  61. toAlias.Write((ulong)((dstPage << 12) + offset), 0xbadc0de);
  62. Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, (srcPage << 12) + offset), 0xbadc0de);
  63. }
  64. else
  65. {
  66. toAlias.UnmapView(backing, (ulong)dstPage << 12, (ulong)pages << 12);
  67. }
  68. }
  69. }
  70. [Test]
  71. // Memory aliasing tests fail on CI at the moment.
  72. [Platform(Exclude = "MacOsX")]
  73. public void Test_AliasMapLeak()
  74. {
  75. ulong pageSize = 4096;
  76. ulong size = 100000 * pageSize; // The mappings limit on Linux is usually around 65K, so let's make sure we are above that.
  77. using MemoryBlock backing = new MemoryBlock(pageSize, MemoryAllocationFlags.Mirrorable);
  78. using MemoryBlock toAlias = new MemoryBlock(size, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
  79. for (ulong offset = 0; offset < size; offset += pageSize)
  80. {
  81. toAlias.MapView(backing, 0, offset, pageSize);
  82. toAlias.Write(offset, 0xbadc0de);
  83. Assert.AreEqual(0xbadc0de, backing.Read<int>(0));
  84. toAlias.UnmapView(backing, offset, pageSize);
  85. }
  86. }
  87. }
  88. }