-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66272bb
commit 18e622f
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...droid/Device_Emulator/UnalignedAccess/Android.Device_Emulator.UnalignedAccess.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<RunAOTCompilation>false</RunAOTCompilation> | ||
<TestRuntime>true</TestRuntime> | ||
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework> | ||
<MainLibraryFileName>Android.Device_Emulator.UnalignedAccess.Test.dll</MainLibraryFileName> | ||
<ExpectedExitCode>42</ExpectedExitCode> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
</ItemGroup> | ||
</Project> |
68 changes: 68 additions & 0 deletions
68
src/tests/FunctionalTests/Android/Device_Emulator/UnalignedAccess/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Buffers.Binary; | ||
|
||
public class Program | ||
{ | ||
[StructLayout(LayoutKind.Sequential, Size=12)] | ||
struct S { | ||
public double d1; | ||
public int i2; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
struct H { | ||
public S s1; | ||
public int i3; | ||
public byte b4; | ||
} | ||
|
||
public static int Main() | ||
{ | ||
Console.WriteLine("Hello, Android!"); | ||
|
||
Console.WriteLine(Marshal.SizeOf(typeof(S))); | ||
var a2 = new S[2]; | ||
a2[1].d1 = 2.12; | ||
Console.WriteLine (a2[1].d1); | ||
var h1 = new H(); | ||
h1.s1.d1 = 4.0; | ||
h1.i3 = 15; | ||
h1.b4 = 42; | ||
unsafe { | ||
fixed (S* parr = &a2[0]) | ||
{ | ||
byte* ptr = (byte*)parr; | ||
ptr += 12; | ||
Console.WriteLine ($"0x{((IntPtr)ptr):x}"); | ||
Console.WriteLine (*(ptr + 0)); | ||
Console.WriteLine (*(ptr + 1)); | ||
Console.WriteLine (*(ptr + 2)); | ||
Console.WriteLine (*(ptr + 3)); | ||
Console.WriteLine (*(ptr + 4)); | ||
Console.WriteLine (*(ptr + 5)); | ||
Console.WriteLine (*(ptr + 6)); | ||
Console.WriteLine (*(ptr + 7)); | ||
Span<byte> tmp = stackalloc byte[8]; | ||
new Span<byte>(ptr, 8).CopyTo(tmp); | ||
double d = BinaryPrimitives.ReadDoubleLittleEndian(tmp); | ||
Console.WriteLine ($"tmp = {d}"); | ||
if (d != 2.12) // if this prints 2.12 - unaligned load happened | ||
return 1; | ||
} | ||
{ | ||
byte* ptr = (byte*)&h1; | ||
Console.WriteLine (*(ptr + 12)); | ||
Console.WriteLine (*(ptr + 13)); | ||
Console.WriteLine (*(ptr + 14)); | ||
Console.WriteLine (*(ptr + 15)); | ||
Console.WriteLine (*(ptr + 16)); | ||
} | ||
} | ||
|
||
return 42; | ||
} | ||
} |