LibHac/tests/LibHac.Tests/BufferStructTests.cs

87 lines
2.1 KiB
C#
Raw Permalink Normal View History

2019-11-16 01:38:55 +01:00
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using LibHac.Common;
using Xunit;
2021-11-14 20:08:57 +01:00
namespace LibHac.Tests;
public class BufferStructTests
2019-11-16 01:38:55 +01:00
{
2021-11-14 20:08:57 +01:00
[Fact]
public static void BufferIndexer()
2019-11-16 01:38:55 +01:00
{
2021-11-14 20:08:57 +01:00
var buffer = new Buffer16();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
buffer[0] = 5;
buffer[1] = 6;
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Assert.Equal(5, buffer[0]);
Assert.Equal(6, buffer[1]);
}
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
[Fact]
public static void CastBufferToByteSpan()
{
var buffer = new Buffer16();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Span<byte> byteSpan = buffer.Bytes;
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Assert.Equal(16, byteSpan.Length);
Assert.True(Unsafe.AreSame(ref Unsafe.As<Buffer16, byte>(ref buffer), ref byteSpan[0]));
}
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
[Fact]
public static void CastBufferToByteSpanImplicit()
{
var buffer = new Buffer16();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Span<byte> byteSpan = buffer;
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Assert.Equal(16, byteSpan.Length);
Assert.True(Unsafe.AreSame(ref Unsafe.As<Buffer16, byte>(ref buffer), ref byteSpan[0]));
}
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
[Fact]
public static void CastBufferToReadOnlyByteSpanImplicit()
{
var buffer = new Buffer16();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
ReadOnlySpan<byte> byteSpan = buffer;
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Assert.Equal(16, byteSpan.Length);
2023-12-10 07:30:19 +01:00
Assert.True(Unsafe.AreSame(ref Unsafe.As<Buffer16, byte>(ref buffer), ref Unsafe.AsRef(in byteSpan[0])));
2021-11-14 20:08:57 +01:00
}
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
[Fact]
public static void CastBufferToSpan()
{
var buffer = new Buffer16();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Span<ulong> ulongSpan = buffer.AsSpan<ulong>();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Assert.Equal(2, ulongSpan.Length);
Assert.True(Unsafe.AreSame(ref Unsafe.As<Buffer16, ulong>(ref buffer), ref ulongSpan[0]));
}
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
[Fact]
public static void CastBufferToStruct()
{
var buffer = new Buffer16();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
ref ulong ulongSpan = ref buffer.As<ulong>();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Assert.True(Unsafe.AreSame(ref Unsafe.As<Buffer16, ulong>(ref buffer), ref ulongSpan));
}
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
[Fact]
public static void CastBufferToLargerStruct()
{
var buffer = new Buffer16();
2019-11-16 01:38:55 +01:00
2021-11-14 20:08:57 +01:00
Assert.Throws<ArgumentException>(() => buffer.As<Struct32Bytes>());
2019-11-16 01:38:55 +01:00
}
2021-11-14 20:08:57 +01:00
[StructLayout(LayoutKind.Sequential, Size = 32)]
private struct Struct32Bytes { }
}