Skip to content

Commit

Permalink
Added test cased for fixed array buffes.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rs-mt committed Jul 23, 2020
1 parent 6c68ec6 commit fd4fba4
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ Src/ILGPU.Tests/CompareFloatOperations.cs
Src/ILGPU.Tests/CompareIntOperations.cs
Src/ILGPU.Tests/ConvertFloatOperations.cs
Src/ILGPU.Tests/ConvertIntOperations.cs
Src/ILGPU.Tests/FixedBuffers.cs
Src/ILGPU.Tests/MemoryBufferOperations.cs
Src/ILGPU.Tests/UnaryIntOperations.cs

Expand Down
1 change: 1 addition & 0 deletions Src/ILGPU.Tests/Configurations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DebugTests: Debug, Release, O2
DisassemblerTests: Debug, Release, O2
EnumValues: Debug, Release, O2
ExchangeBufferOperations: Debug, Release, O2
FixedBuffers: Debug, Release, O2
KernelEntryPoints: Debug, Release, O2
MemoryBufferOperations : Debug, Release, O2
MemoryCacheOperations : Debug, Release, O2
Expand Down
118 changes: 118 additions & 0 deletions Src/ILGPU.Tests/FixedBuffers.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ include file="Generic/ConfigurationBase.tt" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using Xunit;
using Xunit.Abstractions;

#pragma warning disable CA1815 // Override equals and operator equals on value types
#pragma warning disable CA1051 // Do not declare visible instance fields
#pragma warning disable CA2231 // Overload operator equals on overriding value type Equals
#pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not
// override Object.GetHashCode()

namespace ILGPU.Tests
{
<# foreach (var type in IntTypes) { #>
public unsafe struct FixedBufferStruct<#= type.Name #> :
IXunitSerializable,
IEquatable<FixedBufferStruct<#= type.Name #>>
{
public fixed <#= type.Type #> Data[FixedBuffers.Length];

public FixedBufferStruct<#= type.Name #>(<#= type.Type #> data)
{
for (int i = 0; i < FixedBuffers.Length; ++i)
Data[i] = data;
}

public void Deserialize(IXunitSerializationInfo info)
{
for (int i = 0; i < FixedBuffers.Length; ++i)
Data[i] = info.GetValue<<#= type.Type #>>(nameof(Data) + i);
}

public void Serialize(IXunitSerializationInfo info)
{
for (int i = 0; i < FixedBuffers.Length; ++i)
info.AddValue(nameof(Data) + i, Data[i]);
}

public bool Equals(FixedBufferStruct<#= type.Name #> buffer)
{
for (int i = 0; i < FixedBuffers.Length; ++i)
{
if (Data[i] != buffer.Data[i])
return false;
}
return true;
}

public override bool Equals(object obj) =>
obj is FixedBufferStruct<#= type.Name #> fixedStruct &&
Equals(fixedStruct);
}

<# } #>

public unsafe abstract class FixedBuffers : TestBase
{
public const int Length = 9;

protected FixedBuffers(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }
<# foreach (var type in IntTypes) { #>

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AdjustBuffer(
ref FixedBufferStruct<#= type.Name #> value,
<#= type.Type #> scalarValue)
{
for (int i = 0; i < Length; ++i)
value.Data[i] += scalarValue;
}

internal static void FixedBuffer<#= type.Name #>Kernel(
Index1 index,
ArrayView<<#= type.Type #>> data,
ArrayView<<#= type.Type #>> data2,
FixedBufferStruct<#= type.Name #> value,
<#= type.Type #> scalarValue)
{
data[index] = value.Data[index];
AdjustBuffer(ref value, scalarValue);
data2[index] = value.Data[index];
}

[Fact]
[KernelMethod(nameof(FixedBuffer<#= type.Name #>Kernel))]
public void FixedBuffer<#= type.Name #>()
{
using var buffer1 = Accelerator.Allocate<<#= type.Type #>>(Length);
using var buffer2 = Accelerator.Allocate<<#= type.Type #>>(Length);

<#= type.Type #> scalarValue = 2;
var fixedBufferData1 = new FixedBufferStruct<#= type.Name #>(scalarValue);
Execute(Length, buffer1.View, buffer2.View, fixedBufferData1, scalarValue);

var expected1 = Enumerable.Repeat(scalarValue, Length).ToArray();
var expected2 = Enumerable.Repeat(
(<#= type.Type #>)(scalarValue + scalarValue),
Length).ToArray();
Verify(buffer1, expected1);
Verify(buffer2, expected2);
}
<# } #>
}
}

#pragma warning restore CA2231 // Overload operator equals on overriding value type Equals
#pragma warning restore CA1051 // Do not declare visible instance fields
#pragma warning restore CA1815 // Override equals and operator equals on value types
#pragma warning restore CS0659 // Type overrides Object.Equals(object o) but does not
// override Object.GetHashCode()
14 changes: 14 additions & 0 deletions Src/ILGPU.Tests/ILGPU.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<AutoGen>True</AutoGen>
<DependentUpon>ConvertIntOperations.tt</DependentUpon>
</None>
<None Include="FixedBuffers.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>FixedBuffers.tt</DependentUpon>
</None>
<None Include="Generic\ConfigurationBase.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand Down Expand Up @@ -124,6 +129,11 @@
<AutoGen>True</AutoGen>
<DependentUpon>ConvertIntOperations.tt</DependentUpon>
</Compile>
<Compile Update="FixedBuffers.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>FixedBuffers.tt</DependentUpon>
</Compile>
<Compile Update="Generic\ConfigurationBase.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand Down Expand Up @@ -170,6 +180,10 @@
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>ConvertIntOperations.cs</LastGenOutput>
</None>
<None Update="FixedBuffers.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>FixedBuffers.cs</LastGenOutput>
</None>
<None Update="Generic\ConfigurationBase.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>ConfigurationBase.cs</LastGenOutput>
Expand Down

0 comments on commit fd4fba4

Please sign in to comment.