Skip to content

Commit

Permalink
more updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-IN committed Apr 8, 2022
1 parent 4628531 commit d7b06d9
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 196 deletions.
14 changes: 8 additions & 6 deletions AssemblyToProcess/SomeBinaryLiterals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ public static unsafe class SomeBinaryLiterals {

public static readonly byte* BinNonLiteral;

[ValueLiteral(0, new byte[] {1, 2, 3})]
[ValueLiteral(0, new byte[] { 1, 2, 3 })]
public static readonly byte* BinLiteral1;

[ValueLiteral(4, new byte[] {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1})]
[ValueLiteral(4, new byte[] { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 })]
public static readonly byte* BinLiteral2;

[ValueLiteral(0, 0x04030201)]
public static readonly IntPtr BinLiteral3;

[ValueLiteral(4, new byte[] {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1})]
[ValueLiteral(4, new byte[] { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 })]
public static byte* BinLiteral4;

[ValueLiteral(3, 0x01020304u)]
public static readonly (IntPtr, int) BinLiteral5;

[ValueLiteral(3, new byte[] {1, 2, 3})]
[ValueLiteral(3, new byte[] { 1, 2, 3 })]
public static readonly ReadOnlyData<byte> BinLiteral6;

[Base64Literal(0, "AQ==")]
Expand Down Expand Up @@ -50,10 +50,12 @@ public static unsafe class SomeBinaryLiterals {
public static readonly byte* BinLiteral14; // [ 0xAB, 0xCD, 0xEF, 0x01 ]

[EncodedLiteral(0, 28591, "This is ISO-8559-1.\0")]
public static readonly (IntPtr,long) BinLiteral15;
public static readonly (IntPtr, long) BinLiteral15;

#if false // Support for UTF-7 is disabled. See https://aka.ms/dotnet-warnings/SYSLIB0001 for more information.
[EncodedLiteral(0, 65000, "This is UTF-7.\0")]
public static readonly (IntPtr,int) BinLiteral16;
public static readonly (IntPtr, int) BinLiteral16;
#endif

[Base64Literal(0,
"MBu0Iclx+", "7ftAdzDqZ", "ds5T3wNAI", "rqYK5fQ8n", "1IxPA4g6q",
Expand Down
2 changes: 1 addition & 1 deletion AssemblyToProcess/SomeUtf8StringLiterals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static unsafe class SomeUtf8StringLiterals {
public static byte* Utf8Literal2Dupe;

[Utf8Literal("Utf8Literal2 String")]
public static ReadOnlyData<byte> Utf8Literal2ReadOnlyData;
public static ReadOnlyData<byte> Utf8Literal2ReadOnlyData;

[Utf8Literal(null)]
public static readonly byte* Utf8Literal3;
Expand Down
119 changes: 61 additions & 58 deletions Embedded.Fody.Tests/BinaryLiteralWeaverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@
using System.Reflection;
using System.Text;
using Embedded;
using JetBrains.Annotations;
using Xunit;
using Xunit.Abstractions;

[PublicAPI]
[Collection("WeaverTests")]
public unsafe class BinaryLiteralWeaverTests : WeaverTests {

public BinaryLiteralWeaverTests(ITestOutputHelper outputHelper, TestResultFixture fixture)
: base(outputHelper, fixture) {
}
: base(outputHelper, fixture) { }

[Fact]
public void ValidateBinLiteralsResource() {
var ums = (UnmanagedMemoryStream) TestResult.Assembly.GetManifestResourceStream("RDATA")!;
var ums = (UnmanagedMemoryStream)TestResult.Assembly.GetManifestResourceStream("RDATA")!;
Assert.NotNull(ums);
var bufSize = ums.Length;
Assert.NotEqual(0, bufSize);
var buffer = new byte[bufSize];
Assert.Equal(bufSize, ums.Read(buffer, 0, checked((int) bufSize)));
Assert.Equal(bufSize, ums.Read(buffer, 0, checked((int)bufSize)));
//OutputHelper.WriteLine(BitConverter.ToString(buffer));
}

[Fact]
public void ValidateBinLiteral1() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral1))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral1))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(1, v[0]);
Assert.Equal(2, v[1]);
Assert.Equal(3, v[2]);
Expand All @@ -51,8 +52,8 @@ public void ValidateBinLiteral1() {
[Fact]
public void ValidateBinLiteral2() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral2))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral2))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(15, v[0]);
Assert.Equal(14, v[1]);
Assert.Equal(13, v[2]);
Expand All @@ -75,9 +76,9 @@ public void ValidateBinLiteral2() {
[Fact]
public void ValidateBinLiteral3() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (IntPtr) t.GetField(nameof(SomeBinaryLiterals.BinLiteral3))!.GetValue(null)!;
var p = (byte*) v;
Assert.NotEqual((IntPtr) 0, v);
var v = (IntPtr)t.GetField(nameof(SomeBinaryLiterals.BinLiteral3))!.GetValue(null)!;
var p = (byte*)v;
Assert.NotEqual((IntPtr)0, v);
Assert.Equal(1, p[0]);
Assert.Equal(2, p[1]);
Assert.Equal(3, p[2]);
Expand All @@ -88,8 +89,8 @@ public void ValidateBinLiteral3() {
[Fact]
public void ValidateBinLiteral4() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral4))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral4))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(15, v[0]);
Assert.Equal(14, v[1]);
Assert.Equal(13, v[2]);
Expand All @@ -111,10 +112,10 @@ public void ValidateBinLiteral4() {
[Fact]
public void ValidateBinLiteral5() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var (v, l) = ((IntPtr, int)) t.GetField(nameof(SomeBinaryLiterals.BinLiteral5))!.GetValue(null)!;
Assert.NotEqual((IntPtr) 0, v);
var (v, l) = ((IntPtr, int))t.GetField(nameof(SomeBinaryLiterals.BinLiteral5))!.GetValue(null)!;
Assert.NotEqual((IntPtr)0, v);
Assert.Equal(4, l);
var p = (byte*) v;
var p = (byte*)v;
Assert.Equal(4, p[0]);
Assert.Equal(3, p[1]);
Assert.Equal(2, p[2]);
Expand All @@ -124,12 +125,12 @@ public void ValidateBinLiteral5() {
[Fact]
public void ValidateBinLiteral6() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (ReadOnlyData<byte>) t.GetField(nameof(SomeBinaryLiterals.BinLiteral6))!.GetValue(null)!;
var v = (ReadOnlyData<byte>)t.GetField(nameof(SomeBinaryLiterals.BinLiteral6))!.GetValue(null)!;
var x = v.Pointer;
Assert.NotEqual((IntPtr) 0, x);
var p = (byte*) v;
Assert.Equal(x, (IntPtr) p);
Assert.Equal(x, (IntPtr) v.TypedPointer);
Assert.NotEqual((IntPtr)0, x);
var p = (byte*)v;
Assert.Equal(x, (IntPtr)p);
Assert.Equal(x, (IntPtr)v.TypedPointer);
Assert.Equal(1, p[0]);
Assert.Equal(2, p[1]);
Assert.Equal(3, p[2]);
Expand All @@ -138,70 +139,70 @@ public void ValidateBinLiteral6() {
[Fact]
public void ValidateBinLiteral7() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral7))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral7))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(0x01, v[0]);
}

[Fact]
public void ValidateBinLiteral8() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (ushort*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral8))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (ushort*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral8))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(0xAA55, v[0]);
}

[Fact]
public void ValidateBinLiteral9() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral9))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral9))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(0x12, v[0]);
Assert.Equal(0x34, v[1]);
}

[Fact]
public void ValidateBinLiteral10() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral10))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral10))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(0x12, v[0]);
Assert.Equal(0x34, v[1]);
}

[Fact]
public void ValidateBinLiteral11() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral11))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral11))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(0x12, v[0]);
Assert.Equal(0x34, v[1]);
}

[Fact]
public void ValidateBinLiteral12() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral12))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral12))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(0xAB, v[0]);
Assert.Equal(0xCD, v[1]);
}

[Fact]
public void ValidateBinLiteral13() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var (v, l) = ((IntPtr, int)) t.GetField(nameof(SomeBinaryLiterals.BinLiteral13))!.GetValue(null)!;
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var p = (byte*) v;
var (v, l) = ((IntPtr, int))t.GetField(nameof(SomeBinaryLiterals.BinLiteral13))!.GetValue(null)!;
Assert.NotEqual((IntPtr)0, (IntPtr)v);
var p = (byte*)v;
Assert.Equal(0xAB, p[0]);
Assert.Equal(0xCD, p[1]);
}

[Fact]
public void ValidateBinLiteral14() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral14))!.GetValue(null)!);
Assert.NotEqual((IntPtr) 0, (IntPtr) v);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinLiteral14))!.GetValue(null)!);
Assert.NotEqual((IntPtr)0, (IntPtr)v);
Assert.Equal(0xAB, v[0]);
Assert.Equal(0xCD, v[1]);
Assert.Equal(0xEF, v[2]);
Expand All @@ -211,57 +212,59 @@ public void ValidateBinLiteral14() {
[Fact]
public unsafe void ValidateBinLiteral15() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var (v, l) = ((IntPtr, long)) t.GetField(nameof(SomeBinaryLiterals.BinLiteral15))!.GetValue(null)!;
Assert.NotEqual((IntPtr) 0, v);
var (v, l) = ((IntPtr, long))t.GetField(nameof(SomeBinaryLiterals.BinLiteral15))!.GetValue(null)!;
Assert.NotEqual((IntPtr)0, v);
Assert.Equal(20, l);
var s = Encoding.GetEncoding(28591).GetString((byte*) v, (int) l);
var s = Encoding.GetEncoding(28591).GetString((byte*)v, (int)l);
Assert.Equal("This is ISO-8559-1.\0", s);
}

/*
[Fact]
public unsafe void ValidateBinLiteral16() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var (v, l) = ((IntPtr, int)) t.GetField(nameof(SomeBinaryLiterals.BinLiteral16))!.GetValue(null)!;
Assert.NotEqual((IntPtr) 0, v);
var (v, l) = ((IntPtr, int))t.GetField(nameof(SomeBinaryLiterals.BinLiteral16))!.GetValue(null)!;
Assert.NotEqual((IntPtr)0, v);
Assert.Equal(19, l);
var s = Encoding.GetEncoding(65000).GetString((byte*) v, l);
var s = Encoding.GetEncoding(65000).GetString((byte*)v, l);
Assert.Equal("This is UTF-7.\0", s);
}
*/

[Fact]
public unsafe void ValidateBinLiteral17() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (ReadOnlyData<byte>) t.GetField(nameof(SomeBinaryLiterals.BinLiteral17))!.GetValue(null)!;
Assert.NotEqual((IntPtr) 0, v.Pointer);
var v = (ReadOnlyData<byte>)t.GetField(nameof(SomeBinaryLiterals.BinLiteral17))!.GetValue(null)!;
Assert.NotEqual((IntPtr)0, v.Pointer);
Assert.Equal(64, v.Length);
// SHA3-512 of "Test"
Assert.Equal(new byte[] {
0x30, 0x1b, 0xb4, 0x21, 0xc9, 0x71, 0xfb, 0xb7, 0xed, 0x01, 0xdc, 0xc3, 0xa9, 0x97, 0x6c, 0xe5,
0x3d, 0xf0, 0x34, 0x02, 0x2b, 0xa9, 0x82, 0xb9, 0x7d, 0x0f, 0x27, 0xd4, 0x8c, 0x4f, 0x03, 0x88,
0x3a, 0xab, 0xf7, 0xc6, 0xbc, 0x77, 0x8a, 0xa7, 0xc3, 0x83, 0x06, 0x2f, 0x68, 0x23, 0x04, 0x5a,
0x6d, 0x41, 0xb8, 0xa7, 0x20, 0xaf, 0xbb, 0x8a, 0x96, 0x07, 0x69, 0x0f, 0x89, 0xfb, 0xe1, 0xa7
}, ((ReadOnlySpan<byte>) v).ToArray());
}, ((ReadOnlySpan<byte>)v).ToArray());
}

[Fact]
public unsafe void ValidateReadOnlyData() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v6 = (ReadOnlyData<byte>) t.GetField(nameof(SomeBinaryLiterals.BinLiteral6))!.GetValue(null)!;
Assert.NotEqual((IntPtr) 0, v6.Pointer);
var v17 = (ReadOnlyData<byte>) t.GetField(nameof(SomeBinaryLiterals.BinLiteral17))!.GetValue(null)!;
Assert.NotEqual((IntPtr) 0, v17.Pointer);
var v6 = (ReadOnlyData<byte>)t.GetField(nameof(SomeBinaryLiterals.BinLiteral6))!.GetValue(null)!;
Assert.NotEqual((IntPtr)0, v6.Pointer);
var v17 = (ReadOnlyData<byte>)t.GetField(nameof(SomeBinaryLiterals.BinLiteral17))!.GetValue(null)!;
Assert.NotEqual((IntPtr)0, v17.Pointer);
Assert.NotEqual(v6.Pointer, v17.Pointer);
Assert.True(v6 != v17);
Assert.False(v6 == v17);
var v6str = v6.ToString();
Assert.NotNull(v6str);
Assert.NotEmpty(v6str);
var s = (ReadOnlySpan<byte>) v17;
var s = (ReadOnlySpan<byte>)v17;
Assert.Equal(s.Length, v17.Length);
Assert.Equal(s[0], v17[0]);
Assert.Equal(v17[0], v17[0L]);
var ti = ((IntPtr p, int l)) v17;
var tl = ((IntPtr p, long l)) v17;
var ti = ((IntPtr p, int l))v17;
var tl = ((IntPtr p, long l))v17;
var rc = v17.Recast<sbyte>();
var us = v17.Recast<ushort>();
Assert.Equal(v17.Pointer, ti.p);
Expand All @@ -281,10 +284,10 @@ public unsafe void ValidateReadOnlyData() {
[Fact]
public void ValidateNonBinLiterals() {
var t = TestResult.Assembly.GetType(nameof(SomeBinaryLiterals))!;
var v = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.NonBinLiteral))!.GetValue(null)!);
Assert.Equal((IntPtr) 0, (IntPtr) v);
var x = (byte*) Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinNonLiteral))!.GetValue(null)!);
Assert.Equal((IntPtr) 0, (IntPtr) x);
var v = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.NonBinLiteral))!.GetValue(null)!);
Assert.Equal((IntPtr)0, (IntPtr)v);
var x = (byte*)Pointer.Unbox(t.GetField(nameof(SomeBinaryLiterals.BinNonLiteral))!.GetValue(null)!);
Assert.Equal((IntPtr)0, (IntPtr)x);
}

}
15 changes: 5 additions & 10 deletions Embedded.Fody.Tests/Embedded.Fody.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<DisableFody>true</DisableFody>
<RootNamespace />
<LangVersion>8</LangVersion>
<LangVersion>10</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FodyHelpers" Version="6.6.0" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="All" />
<PackageReference Include="coverlet.collector" Version="3.1.2" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Embedded.Fody.Tests/FodyWeaverTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using JetBrains.Annotations;
using Xunit;
using Xunit.Abstractions;

[PublicAPI]
[Collection("WeaverTests")]
public class FodyWeaverTests : WeaverTests {

Expand Down
2 changes: 2 additions & 0 deletions Embedded.Fody.Tests/TestResultFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Fody;
using JetBrains.Annotations;

[PublicAPI]
public class TestResultFixture {

public TestResult TestResult;
Expand Down
2 changes: 2 additions & 0 deletions Embedded.Fody.Tests/Utf8StringWeaverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Reflection;
using System.Text;
using Embedded;
using JetBrains.Annotations;
using Xunit;
using Xunit.Abstractions;

[PublicAPI]
[Collection("WeaverTests")]
public unsafe class Utf8StringWeaverTests : WeaverTests {

Expand Down
Loading

0 comments on commit d7b06d9

Please sign in to comment.