IntPtrTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using NUnit.Framework;
  6. using UnityEngine;
  7. namespace Unity.WebRTC.RuntimeTest
  8. {
  9. class IntPtrTest
  10. {
  11. [Test]
  12. public void AsAnsiStringWithFreeMemThrowException()
  13. {
  14. IntPtr ptr = IntPtr.Zero;
  15. Assert.Throws<ArgumentException>(() => { ptr.AsAnsiStringWithFreeMem(); });
  16. }
  17. [Test]
  18. public void AsAnsiStringWithoutFreeMemThrowException()
  19. {
  20. IntPtr ptr = IntPtr.Zero;
  21. Assert.Throws<ArgumentException>(() => { ptr.AsAnsiStringWithoutFreeMem(); });
  22. }
  23. [Test]
  24. public void AsAnsiStringWithoutFreeMemReturnsString()
  25. {
  26. string source = "test";
  27. IntPtr ptr = Marshal.StringToCoTaskMemAnsi(source);
  28. string dest = ptr.AsAnsiStringWithoutFreeMem();
  29. Assert.IsNotEmpty(dest);
  30. Assert.AreEqual(source, dest);
  31. Marshal.FreeCoTaskMem(ptr);
  32. }
  33. [Test]
  34. public void AsAnsiStringWithFreeMemReturnsString()
  35. {
  36. string source = "test";
  37. IntPtr ptr = Marshal.StringToCoTaskMemAnsi(source);
  38. string dest = ptr.AsAnsiStringWithFreeMem();
  39. Assert.IsNotEmpty(dest);
  40. Assert.AreEqual(source, dest);
  41. }
  42. [Test]
  43. public void AsArrayReturnsByteArray()
  44. {
  45. byte[] source = { 1, 2, 3 };
  46. IntPtr ptr = source.ToPtr();
  47. byte[] dest = ptr.AsArray<byte>(source.Length);
  48. Assert.IsNotEmpty(dest);
  49. CollectionAssert.AreEqual(source, dest);
  50. }
  51. [Test]
  52. public void AsArrayReturnsIntArray()
  53. {
  54. int[] source = {1, 2, 3};
  55. IntPtr ptr = source.ToPtr();
  56. int[] dest = ptr.AsArray<int>(source.Length);
  57. Assert.IsNotEmpty(dest);
  58. CollectionAssert.AreEqual(source, dest);
  59. }
  60. [Test]
  61. public void AsArrayReturnsUintArray()
  62. {
  63. uint[] source = { 1, 2, 3 };
  64. IntPtr ptr = source.ToPtr();
  65. uint[] dest = ptr.AsArray<uint>(source.Length);
  66. Assert.IsNotEmpty(dest);
  67. CollectionAssert.AreEqual(source, dest);
  68. }
  69. [Test]
  70. public void AsArrayReturnsLongArray()
  71. {
  72. long[] source = { 1, 2, 3 };
  73. IntPtr ptr = source.ToPtr();
  74. long[] dest = ptr.AsArray<long>(source.Length);
  75. Assert.IsNotEmpty(dest);
  76. CollectionAssert.AreEqual(source, dest);
  77. }
  78. [Test]
  79. public void AsArrayReturnsUlongArray()
  80. {
  81. ulong[] source = { 1, 2, 3 };
  82. IntPtr ptr = source.ToPtr();
  83. ulong[] dest = ptr.AsArray<ulong>(source.Length);
  84. Assert.IsNotEmpty(dest);
  85. CollectionAssert.AreEqual(source, dest);
  86. }
  87. [Test]
  88. public void AsArrayReturnsFloatArray()
  89. {
  90. double[] source = { 0.1, 0.2, 0.3 };
  91. IntPtr ptr = source.ToPtr();
  92. double[] dest = ptr.AsArray<double>(source.Length);
  93. Assert.IsNotEmpty(dest);
  94. CollectionAssert.AreEqual(source, dest);
  95. }
  96. [Test]
  97. public void AsArrayReturnsDoubleArray()
  98. {
  99. float[] source = { 0.1f, 0.2f, 0.3f };
  100. IntPtr ptr = source.ToPtr();
  101. float[] dest = ptr.AsArray<float>(source.Length);
  102. Assert.IsNotEmpty(dest);
  103. CollectionAssert.AreEqual(source, dest);
  104. }
  105. [Test]
  106. public void AsArrayReturnsBoolArray()
  107. {
  108. bool[] source = { true, false, true };
  109. IntPtr ptr = source.ToPtr();
  110. bool[] dest = ptr.AsArray<bool>(source.Length);
  111. Assert.IsNotEmpty(dest);
  112. CollectionAssert.AreEqual(source, dest);
  113. }
  114. [Test]
  115. public void AsArrayReturnsStringArray()
  116. {
  117. string[] source = { "red", "blue", "yellow" };
  118. IntPtr ptr = source.ToPtr();
  119. string[] dest = ptr.AsArray<string>(source.Length);
  120. Assert.IsNotEmpty(dest);
  121. CollectionAssert.AreEqual(source, dest);
  122. }
  123. }
  124. }