b486678290
Library -Artifacts
453 lines
14 KiB
Plaintext
453 lines
14 KiB
Plaintext
<#/*THIS IS A T4 FILE - see t4_text_templating.md for what it is and how to run codegen*/#>
|
|
<#@ template debug="True" #>
|
|
<#@ output extension=".gen.cs" encoding="utf-8" #>
|
|
<#@ assembly name="System.Core" #>
|
|
<#@ import namespace="System.Globalization" #>
|
|
|
|
//------------------------------------------------------------------------------
|
|
// <auto-generated>
|
|
// This code was generated by a tool.
|
|
//
|
|
// TextTransform Samples/Packages/com.unity.collections/Unity.Collections.Tests/FixedListTests.tt
|
|
//
|
|
// Changes to this file may cause incorrect behavior and will be lost if
|
|
// the code is regenerated.
|
|
// </auto-generated>
|
|
//------------------------------------------------------------------------------
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Threading;
|
|
using NUnit.Framework;
|
|
using Unity.Collections;
|
|
using Unity.Collections.LowLevel.Unsafe;
|
|
using Unity.Collections.Tests;
|
|
#if !UNITY_DOTSRUNTIME // DOTS-Runtime doesn't support UnityEngine
|
|
using UnityEngine;
|
|
#endif
|
|
|
|
internal class FixedListTests : CollectionsTestFixture
|
|
{
|
|
struct NonComparableStruct
|
|
{
|
|
public int a;
|
|
}
|
|
|
|
struct DescendingComparer<T> : IComparer<T> where T : IComparable<T>
|
|
{
|
|
public int Compare(T x, T y) => y.CompareTo(x);
|
|
}
|
|
|
|
<#
|
|
{
|
|
var SIZES = new int[] {32, 64, 128};
|
|
for (var size = 0; size < 3; ++size)
|
|
{
|
|
var BYTES = SIZES[size];
|
|
var TYPENAME = String.Format("FixedList{0}Bytes", BYTES);
|
|
#>
|
|
[Test]
|
|
[IgnoreInPortableTests("Crashes in IL2CPP on unsupported feature.")]
|
|
public void <#=TYPENAME #>DebugView()
|
|
{
|
|
var list = new <#=TYPENAME #><NonComparableStruct>();
|
|
CollectionAssert.IsEmpty(new <#=TYPENAME #>DebugView<NonComparableStruct>(list).Items);
|
|
|
|
var reference = new []
|
|
{
|
|
new NonComparableStruct{ a = 123 },
|
|
new NonComparableStruct{ a = 234 },
|
|
new NonComparableStruct{ a = 345 },
|
|
};
|
|
|
|
list.Add(reference[0]);
|
|
list.Add(reference[1]);
|
|
list.Add(reference[2]);
|
|
|
|
CollectionAssert.AreEqual(reference, new <#=TYPENAME #>DebugView<NonComparableStruct>(list).Items);
|
|
}
|
|
|
|
<#
|
|
}
|
|
}
|
|
{
|
|
var TYPES = new string[] {"byte", "int", "float"};
|
|
var TYPESIZES = new int[] {1, 4, 4};
|
|
var SIZES = new int[] {32, 64, 128};
|
|
for (var type = 0; type < 3; ++type)
|
|
{
|
|
for (var size = 0; size < 3; ++size)
|
|
{
|
|
var BYTES = SIZES[size];
|
|
var TYPE = TYPES[type];
|
|
var TYPESIZE = TYPESIZES[type];
|
|
var MAXLENGTH = BYTES - 2;
|
|
var TYPENAME = String.Format("FixedList{0}Bytes", BYTES);
|
|
var TYPENAME_IDENTIFIER = String.Format("FixedList{0}{1}_", BYTES, new CultureInfo("en-US").TextInfo.ToTitleCase(TYPE));
|
|
var EXPECTEDCAPACITY = (BYTES - 2) / TYPESIZE;
|
|
var BUFFERTYPE = String.Format("FixedBytes{0}", MAXLENGTH);
|
|
#>
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #><#=BUFFERTYPE#>ToNativeArrayWorksGeneric()
|
|
{
|
|
var list = new FixedList<<#=TYPE#>,<#=BUFFERTYPE#>>();
|
|
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY#>; ++i)
|
|
list.Add((<#=TYPE#>)(i * 123 + 234));
|
|
|
|
using(var array = list.ToNativeArray(Allocator.Temp))
|
|
{
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY#>; ++i)
|
|
Assert.AreEqual((<#=TYPE#>)(i * 123 + 234), array[i]);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>ToNativeArrayWorks()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>();
|
|
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY#>; ++i)
|
|
list.Add((<#=TYPE#>)(i * 123 + 234));
|
|
|
|
using(var array = list.ToNativeArray(Allocator.Temp))
|
|
{
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY#>; ++i)
|
|
Assert.AreEqual((<#=TYPE#>)(i * 123 + 234), array[i]);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericHasExpectedLayout()
|
|
{
|
|
var actual = new <#=TYPENAME #><<#=TYPE#>>();
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY #>; ++i)
|
|
actual.Add((<#=TYPE #>)i);
|
|
unsafe
|
|
{
|
|
var e = stackalloc byte[<#=BYTES #>];
|
|
e[0] = (byte)((<#=EXPECTEDCAPACITY #> >> 0) & 0xFF);
|
|
e[1] = (byte)((<#=EXPECTEDCAPACITY #> >> 8) & 0xFF);
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY #>; ++i)
|
|
{
|
|
var s = (<#=TYPE #>)i;
|
|
UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<<#=TYPE#>>() + sizeof(<#=TYPE #>) * i, &s, sizeof(<#=TYPE #>));
|
|
}
|
|
Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.length, <#=BYTES #>));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericHasExpectedCapacity()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>();
|
|
var expectedCapacity = list.Capacity;
|
|
for(int i = 0; i < expectedCapacity; ++i)
|
|
list.Add((<#=TYPE #>)i);
|
|
Assert.Throws<IndexOutOfRangeException> (() => { list.Add((<#=TYPE #>)expectedCapacity); });
|
|
}
|
|
<#
|
|
if (EXPECTEDCAPACITY >= 5)
|
|
{
|
|
#>
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericInsertRangeWithBeginEnd()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>() { 0, 3, 4 };
|
|
list.InsertRangeWithBeginEnd(1,3);
|
|
list[1] = 1;
|
|
list[2] = 2;
|
|
for(var i = 0; i < 5; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericRemoveRange()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>() { 0, 3, 3, 1, 2 };
|
|
list.RemoveRange(1, 2);
|
|
for(var i = 0; i < 3; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericInsert()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>() { 0, 3, 4 };
|
|
list.Insert(1,1);
|
|
list.Insert(2,2);
|
|
for(var i = 0; i < 5; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericRemoveAt()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>() { 0, 3, 3, 1, 2 };
|
|
list.RemoveAt(1);
|
|
list.RemoveAt(1);
|
|
for(var i = 0; i < 3; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericRemove()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>() { 0, 3, 3, 1, 2 };
|
|
Assert.True(list.Remove((<#=TYPE#>)3));
|
|
Assert.True(list.Remove((<#=TYPE#>)3));
|
|
for(var i = 0; i < 3; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericRemoveSwapBack()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>() { 0, 3, 3, 2, 1 };
|
|
Assert.True(list.RemoveSwapBack((<#=TYPE#>)3));
|
|
Assert.True(list.RemoveSwapBack((<#=TYPE#>)3));
|
|
for(var i = 0; i < 3; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericSort()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>();
|
|
for(var i = 0; i < 5; ++i)
|
|
list.Add((<#=TYPE #>)(4-i));
|
|
list.Sort();
|
|
for(var i = 0; i < 5; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>GenericSortCustomComparer()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>();
|
|
for(var i = 0; i < 5; ++i)
|
|
list.Add((<#=TYPE #>)(i));
|
|
list.Sort(new DescendingComparer<<#=TYPE#>>());
|
|
for(var i = 0; i < 5; ++i)
|
|
Assert.AreEqual(4-i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public unsafe void <#=TYPENAME_IDENTIFIER #>IndexOf()
|
|
{
|
|
var list = new <#=TYPENAME #><<#=TYPE#>>() { 123, 178 };
|
|
bool r0 = false, r1 = false, r2 = false;
|
|
|
|
GCAllocRecorder.ValidateNoGCAllocs(() =>
|
|
{
|
|
r0 = -1 != list.IndexOf((<#=TYPE#>)145);
|
|
r1 = list.Contains((<#=TYPE#>)123);
|
|
r2 = list.Contains((<#=TYPE#>)178);
|
|
});
|
|
|
|
Assert.False(r0);
|
|
Assert.True(r1);
|
|
Assert.True(r2);
|
|
}
|
|
|
|
<#
|
|
}
|
|
}
|
|
for (var size = 0; size < 3; ++size)
|
|
{
|
|
var BYTES = SIZES[size];
|
|
var TYPE = TYPES[type];
|
|
var TYPESIZE = TYPESIZES[type];
|
|
var MAXLENGTH = BYTES - 2;
|
|
var TYPENAME = String.Format("FixedList{0}Bytes<{1}>", BYTES, TYPE);
|
|
var TYPENAME_IDENTIFIER = String.Format("FixedList{0}{1}_", BYTES, new CultureInfo("en-US").TextInfo.ToTitleCase(TYPE));
|
|
var EXPECTEDCAPACITY = (BYTES - 2) / TYPESIZE;
|
|
#>
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>HasExpectedLayout()
|
|
{
|
|
var actual = new <#=TYPENAME #>();
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY #>; ++i)
|
|
actual.Add((<#=TYPE #>)i);
|
|
unsafe
|
|
{
|
|
var e = stackalloc byte[<#=BYTES #>];
|
|
e[0] = (byte)((<#=EXPECTEDCAPACITY #> >> 0) & 0xFF);
|
|
e[1] = (byte)((<#=EXPECTEDCAPACITY #> >> 8) & 0xFF);
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY #>; ++i)
|
|
{
|
|
var s = (<#=TYPE #>)i;
|
|
UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<<#=TYPE#>>() + sizeof(<#=TYPE #>) * i, &s, sizeof(<#=TYPE #>));
|
|
}
|
|
Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.length, <#=BYTES #>));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>HasExpectedCapacity()
|
|
{
|
|
var list = new <#=TYPENAME #>();
|
|
var expectedCapacity = list.Capacity;
|
|
for(int i = 0; i < expectedCapacity; ++i)
|
|
list.Add((<#=TYPE #>)i);
|
|
Assert.Throws<IndexOutOfRangeException> (() => { list.Add((<#=TYPE #>)expectedCapacity); });
|
|
}
|
|
|
|
#if !UNITY_DOTSRUNTIME // DOTS-Runtime doesn't support UnityEngine
|
|
class ScriptableObject<#=TYPENAME_IDENTIFIER#> : UnityEngine.ScriptableObject
|
|
{
|
|
public <#=TYPENAME#> List;
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>Serializes()
|
|
{
|
|
var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObject<#=TYPENAME_IDENTIFIER#> >();
|
|
for(int i = 0; i < a.List.Capacity; ++i)
|
|
a.List.Add((<#=TYPE #>)i);
|
|
var b = UnityEngine.Object.Instantiate(a);
|
|
CollectionAssert.AreEqual(a.List, b.List);
|
|
}
|
|
#endif
|
|
|
|
<#
|
|
if (EXPECTEDCAPACITY >= 5)
|
|
{
|
|
#>
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>InsertRangeWithBeginEnd()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 4 };
|
|
list.InsertRangeWithBeginEnd(1,3);
|
|
list[1] = 1;
|
|
list[2] = 2;
|
|
for(var i = 0; i < 5; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
private static void Expected(ref <#=TYPENAME #> container, int expectedLength, int[] expected)
|
|
{
|
|
Assert.AreEqual(expectedLength == 0, container.IsEmpty);
|
|
Assert.AreEqual(container.Length, expectedLength);
|
|
for (var i = 0; i < container.Length; ++i)
|
|
{
|
|
Assert.AreEqual(expected[i], container[i]);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>RemoveAt()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 3, 1, 2 };
|
|
list.RemoveAt(1);
|
|
list.RemoveAt(1);
|
|
Expected(ref list, 3, new int[] { 0, 1, 2 });
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>Remove()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 3, 1, 2 };
|
|
Assert.True(list.Remove((<#=TYPE#>)3));
|
|
Assert.True(list.Remove((<#=TYPE#>)3));
|
|
Expected(ref list, 3, new int[] { 0, 1, 2 });
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>RemoveSwapBack()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 3, 2, 1 };
|
|
Assert.True(list.RemoveSwapBack((<#=TYPE#>)3));
|
|
Assert.True(list.RemoveSwapBack((<#=TYPE#>)3));
|
|
Expected(ref list, 3, new int[] { 0, 1, 2 });
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>RemoveRange()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 3, 1, 2 };
|
|
list.RemoveRange(1, 2);
|
|
Expected(ref list, 3, new int[] { 0, 1, 2 });
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>RemoveAtSwapBack()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 3, 1, 2 };
|
|
list.RemoveAtSwapBack(1);
|
|
list.RemoveAtSwapBack(1);
|
|
Expected(ref list, 3, new int[] { 0, 1, 3 });
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>RemoveRangeSwapBack()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 3, 1, 2 };
|
|
list.RemoveRangeSwapBack(1, 2);
|
|
Expected(ref list, 3, new int[] { 0, 1, 2 });
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>Insert()
|
|
{
|
|
var list = new <#=TYPENAME #>() { 0, 3, 4 };
|
|
list.Insert(1,1);
|
|
list.Insert(2,2);
|
|
for(var i = 0; i < 5; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>Sort()
|
|
{
|
|
var list = new <#=TYPENAME #>();
|
|
for(var i = 0; i < 5; ++i)
|
|
list.Add((<#=TYPE #>)(4-i));
|
|
list.Sort();
|
|
for(var i = 0; i < 5; ++i)
|
|
Assert.AreEqual(i, list[i]);
|
|
}
|
|
|
|
<#
|
|
}
|
|
|
|
foreach (var OTHERBYTES in SIZES)
|
|
{
|
|
if (OTHERBYTES != BYTES)
|
|
{
|
|
var OTHERCAPACITY = (OTHERBYTES - 2) / TYPESIZE;
|
|
var OTHERTYPENAME = String.Format("FixedList{0}Bytes<{1}>", OTHERBYTES, TYPE);
|
|
var OTHERTYPENAME_IDENTIFIER = String.Format("_FixedList{0}{1}", OTHERBYTES, new CultureInfo("en-US").TextInfo.ToTitleCase(TYPE));
|
|
#>
|
|
[Test]
|
|
public void <#=TYPENAME_IDENTIFIER #>To<#=OTHERTYPENAME_IDENTIFIER #>()
|
|
{
|
|
var a = new <#=TYPENAME #>();
|
|
for(var i = 0; i < <#=EXPECTEDCAPACITY #>; ++i)
|
|
a.Add((<#=TYPE #>)i);
|
|
<#
|
|
if (EXPECTEDCAPACITY <= OTHERCAPACITY)
|
|
{
|
|
WriteLine(" var b = new {0}(a);", OTHERTYPENAME);
|
|
WriteLine(" for(var i = 0; i < {0}; ++i)", EXPECTEDCAPACITY);
|
|
WriteLine(" Assert.AreEqual(({0})i, b[i]);", TYPE);
|
|
}
|
|
else
|
|
WriteLine(
|
|
" Assert.Throws<IndexOutOfRangeException> (() => {{ var b = new {0}(a); }} );",
|
|
OTHERTYPENAME);
|
|
#>
|
|
}
|
|
<#
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#>
|
|
|
|
}
|