public void CreateFromStringListTest() { var r = new[] { "5", "5", "5", "5" }; var h = SafeHGlobalHandle.CreateFromStringList(r, StringListPackMethod.Concatenated, CharSet.Ansi, 7); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(16)); Assert.That(h.ToStringEnum(CharSet.Ansi, 7), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items); h = SafeHGlobalHandle.CreateFromStringList(r, StringListPackMethod.Concatenated, CharSet.Unicode, 7); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(25)); Assert.That(h.ToStringEnum(CharSet.Unicode, 7), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items); h = SafeHGlobalHandle.CreateFromStringList(r); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(18)); Assert.That(h.ToString(-1), Is.EqualTo("5")); Assert.That(h.ToStringEnum(), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items); h = SafeHGlobalHandle.CreateFromStringList(r, StringListPackMethod.Packed, CharSet.Ansi, 7); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(7 + IntPtr.Size + r.Length * (2 + IntPtr.Size))); Assert.That(h.ToStringEnum(4, CharSet.Ansi, 7), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items); h = SafeHGlobalHandle.CreateFromStringList(r, StringListPackMethod.Packed, CharSet.Unicode, 7); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(7 + IntPtr.Size + r.Length * (4 + IntPtr.Size))); Assert.That(h.ToStringEnum(4, CharSet.Unicode, 7), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items); h = SafeHGlobalHandle.CreateFromStringList(r, StringListPackMethod.Packed); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(IntPtr.Size + r.Length * (4 + IntPtr.Size))); Assert.That(h.ToStringEnum(4, CharSet.Unicode), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items); h = SafeHGlobalHandle.CreateFromStringList(null); Assert.That(h.Size, Is.EqualTo(Extensions.StringHelper.GetCharSize())); }
public void ToStringEnumTest() { var rs = new[] { "str1", "str2", "str3" }; using (var sa = SafeHGlobalHandle.CreateFromStringList(rs, StringListPackMethod.Concatenated, CharSet.Ansi, i)) { var ptr = sa.DangerousGetHandle(); Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); var se = ptr.ToStringEnum(CharSet.Ansi, i, sa.Size); Assert.That(se, Is.EquivalentTo(rs)); Assert.That(() => ptr.ToStringEnum(CharSet.Ansi, i, sa.Size - 5).ToArray(), Throws.TypeOf <InsufficientMemoryException>()); Assert.That(() => ptr.ToStringEnum(CharSet.Ansi, i, sa.Size - 1).ToArray(), Throws.TypeOf <InsufficientMemoryException>()); } using (var sa = SafeHGlobalHandle.CreateFromStringList(rs, StringListPackMethod.Concatenated, CharSet.Unicode, i)) { var ptr = sa.DangerousGetHandle(); Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); var se = ptr.ToStringEnum(CharSet.Unicode, i, sa.Size); Assert.That(se, Is.EquivalentTo(rs)); Assert.That(() => ptr.ToStringEnum(CharSet.Unicode, i, sa.Size - 5).ToArray(), Throws.TypeOf <InsufficientMemoryException>()); Assert.That(() => ptr.ToStringEnum(CharSet.Unicode, i, sa.Size - 1).ToArray(), Throws.TypeOf <InsufficientMemoryException>()); } using (var sa = SafeHGlobalHandle.CreateFromStringList(null, StringListPackMethod.Concatenated, CharSet.Unicode, i)) { var ptr = sa.DangerousGetHandle(); Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); var se = ptr.ToStringEnum(CharSet.Unicode, i, sa.Size); Assert.That(se, Is.Empty); Assert.That(() => ptr.ToStringEnum(CharSet.Unicode, i, i).ToArray(), Throws.TypeOf <InsufficientMemoryException>()); Assert.That(() => ptr.ToStringEnum(CharSet.Unicode, i, sa.Size - 1).ToArray(), Throws.TypeOf <InsufficientMemoryException>()); } Assert.That(IntPtr.Zero.ToStringEnum(CharSet.Unicode, i), Is.Empty); }
public void ToStringEnumTest1() { var rs = new[] { "str1", "str2", null, "", "str3" }; using (var sa = SafeHGlobalHandle.CreateFromStringList(rs, StringListPackMethod.Packed, CharSet.Ansi, i)) { var ptr = sa.DangerousGetHandle(); Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); var se = ptr.ToStringEnum(rs.Length, CharSet.Ansi, i); Assert.That(se, Is.EquivalentTo(rs)); } using (var sa = SafeHGlobalHandle.CreateFromStringList(rs, StringListPackMethod.Packed, CharSet.Unicode, i)) { var ptr = sa.DangerousGetHandle(); Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); var se = ptr.ToStringEnum(rs.Length, CharSet.Unicode, i); Assert.That(se, Is.EquivalentTo(rs)); } using (var sa = SafeHGlobalHandle.CreateFromStringList(null, StringListPackMethod.Packed, CharSet.Unicode, i)) { var ptr = sa.DangerousGetHandle(); Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); var se = ptr.ToStringEnum(0, CharSet.Unicode, i); Assert.That(se, Is.Empty); } Assert.That(IntPtr.Zero.ToStringEnum(0, CharSet.Unicode, i), Is.Empty); }
public void RtlMoveMemoryTest() { var strings = new[] { "One", "Two", "Three" }; using (var src = SafeHGlobalHandle.CreateFromStringList(strings)) using (var dest = new SafeHGlobalHandle(src.Size)) { Assert.That(() => RtlZeroMemory(dest, dest.Size), Throws.Nothing); Assert.That(() => RtlMoveMemory(dest, src, src.Size), Throws.Nothing); Assert.That(dest.ToStringEnum(), Is.EquivalentTo(strings)); } }