Instantly share code, notes, and snippets.

Embed
What would you like to do?
using NUnit.Framework;
using System.Linq;
using UnityEngine;
using UnityEngine.TestTools.Constraints;
using Is = UnityEngine.TestTools.Constraints.Is;
namespace Tests
{
public class AllocatingGCMemoryのテスト
{
static readonly int[] a = new int[] {
1, 2, 3, 4, 5,
6, 7, 8, 9, 0
};
[Test]
public void 合計テスト1()
{
float sum = 0;
Assert.That(() => {
sum = (float)Sum(a);
}, Is.Not.AllocatingGCMemory());
Assert.AreEqual(45, sum);
}
[Test]
public void 合計テスト2()
{
float sum = 0;
Assert.That(() => {
sum = (float)a.Sum();
}, Is.Not.AllocatingGCMemory());
Assert.AreEqual(45, sum);
}
[Test]
public void 配列を定義()
{
Assert.That(() => {
int[] c = new int[] {
1, 2, 3, 4, 5,
6, 7, 8, 9, 0
};
}, Is.Not.AllocatingGCMemory());
}
[Test]
public void キャスト実験_構造体()
{
Vector2Int v2 = new Vector2Int(10, 32);
Vector3Int result;
Assert.That(() => {
result = (Vector3Int)v2;
}, Is.Not.AllocatingGCMemory());
}
[Test]
public void キャストの実験_データ型()
{
int count = 100;
float result;
Assert.That(() => {
result = count;
}, Is.Not.AllocatingGCMemory());
}
[Test]
public void 文字列の結合()
{
string message1 = "hoge";
string message2 = "moge";
string result = string.Empty;
Assert.That(() => {
result = message1 + message2;
}, Is.Not.AllocatingGCMemory());
Assert.AreEqual("hogemoge", result);
}
int Sum(int[] input)
{
int result = 0;
foreach (var i in input)
{
result += i;
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment