An easy to use Unity 3D library for creating illustrated Interactive Fiction games and more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
11 KiB

Collection (#787) Add concept of Collection to Fungus. Add Collection Demo, Physics Cast Demo. They are their own type, that are addressed in a generic way. Internally using object boxing and type comparisons. A number of refactorings and additions along with this to make it work more seamlessly. Variable operators defined by Variable itself Conditional and Looping commands refactored, to avoid more duplication in the new looping commands, such as ForEach AnyVariableData removes duplication from SetVariable and Condition logic, provides a clearer method of adding additional variable types that result in immediate support in those commands PlayMode Tests and Utils added Additional Variable Types, such as Quaternion, Matrix4x4 Physics casts Commands that use collections * Initial Fungus Collection work with support for int and IntegerVariable * While and Condition related changes to allow for new command Loop Range * Collection Get changes and ForEach rough * Added Collection Commands Reordered Elif logic in Condition * More collection types and commands * Variable reorg to allow for new types to be added more simply and centrally * Added more Fungus Vars for Common Unity types VariableList Adapter can delegate to the variable itself for custom drawing Variable IsArthithemticSupported queries for each op rather than all, more fine grain support for types like color and quaterion Event handlers, such as physics related, now have optional fungus variable to store the data they are given by unity in fungus variable for use in the block VariableInfo consolidates more info into the typeActionLookup table, updates to support new fungus var types * Many basic collection operations added Common Collection types Physics Overlap command * Custom variable drawing moved to VariableInfo.cs rather than child variable itself Added Physics cast and overlap commands * Move all Editor scripts into Editor AsmDef folder * LoopRange also registers as command name For * Condition base class commenting improved and refactored repeated looping style child class elements into base * More descriptive CollectionBase Command class names * Fungus Collection interface and comments refined * Rename ICollection to IFungusCollection Cleanup formatting * Collection example scene * Added Collection Tests * Format collection and test files Added CopyFrom to IFungusCollection * Added FungusCollection initial summaries * Added CollectionRandom commands -added simple use to CollectionLoop demo scene * Added Unique to FungusCollection -Updated tests to include unique Added Collection Physics demo, shows use of returned array of all cast intersecting GameObjects * CollectionPhysics tracks mousepos with visual Added Fungus Command to get mouse position * Variable custom draw moved to its own editor class, no longer part of typeinfo Variable generation run * Add playmode test for variable type sets Add playmode test for flow control * Update doco and formatting for Collection classes Refactor parts of Conditional classes and AnyVariable Move Property commands into Category 'Property' * Update Collection comments, formatting and Update internal to public * Add License header to added variable types
5 years ago
// This code is part of the Fungus library (https://github.com/snozbot/fungus)
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Fungus.Tests
{
[TestFixture]
/// <summary>
/// It should be no surprise that all of these work, but it is worth confirming there isn't
/// a silly mistake somewhere
/// </summary>
public class CollectionTests
{
private Fungus.GameObjectCollection goCol;
private Fungus.IntCollection intColA, intColB;
[Test]
public void AddRemove()
{
Assert.NotNull(intColA);
Assert.NotNull(intColB);
const int Items = 10;
const int ItemIndexToRemove = 3;
const int ValueToRemove = 1;
const int ValueToAddUnique = ValueToRemove;
const int ValueToAddDups = ValueToAddUnique;
const int MinBVal = 7;
const int SizeAAfterBRem = Items - (Items - MinBVal);
//add items ensure indicies match
for (int i = 0; i < Items; i++)
{
//added at expected index
Assert.AreEqual(i, intColA.Add(i));
//value at index matches expected value
Assert.AreEqual(i, intColA.Get(i));
}
//ensure count matches items added
Assert.AreEqual(Items, intColA.Count);
var valAtRemoveIndex = intColA.GetSafe(ItemIndexToRemove);
intColA.RemoveAt(ItemIndexToRemove);
Assert.AreEqual(Items - 1, intColA.Count);
intColA.Insert(ItemIndexToRemove, valAtRemoveIndex);
Assert.AreEqual(valAtRemoveIndex, intColA.GetSafe(ItemIndexToRemove));
intColA.Remove(ValueToRemove);
Assert.AreEqual(Items - 1, intColA.Count);
//multiple calls to add unique to get back the value we removed and ensure unique works
for (int i = 0; i < Items; i++)
{
intColA.AddUnique(ValueToAddUnique);
}
Assert.AreEqual(Items, intColA.Count);
//now add a bunch of dups, so we can remove all of them
for (int i = 0; i < Items; i++)
{
intColA.Add(ValueToAddDups);
}
intColA.RemoveAll(ValueToAddDups);
Assert.AreEqual(Items - 1, intColA.Count);
//put it back
intColA.Add(ValueToAddDups);
//now add a bunch of items to the colB and remove all of them from colA
for (int i = MinBVal; i < Items; i++)
{
intColB.Add(i);
}
intColA.RemoveAll(intColB);
Assert.AreEqual(intColA.Count, SizeAAfterBRem);// all of b should be gone
intColA.Add(intColB.Get(0));
intColA.AddUnique(intColB);//ensure intColB[0] doesn't double
Assert.AreEqual(intColA.Count, Items);// all of b should be back in there now
intColA.Add(intColB);
intColA.Unique();
Assert.IsTrue(intColA.Count == Items);
intColA.RemoveAll(intColA);//should be equiv to clear
intColB.Clear();
Assert.AreEqual(intColA.Count, 0);
Assert.AreEqual(intColB.Count, 0);
intColA.Clear();
intColB.Clear();
}
[Test]
public void Compat()
{
Assert.IsTrue(intColA.IsElementCompatible(7));
Assert.IsTrue(intColA.IsCollectionCompatible(intColB));
Assert.IsTrue(intColA.IsCollectionCompatible(new int[] { 1 }));
Assert.IsTrue(intColA.IsCollectionCompatible(new List<int>()));
Assert.IsTrue(intColA.IsCollectionCompatible(new List<Fungus.IntegerVariable>()));
Assert.IsFalse(intColA.IsElementCompatible(Vector3.up));
Assert.IsFalse(intColA.IsCollectionCompatible(goCol));
Assert.IsFalse(intColA.IsCollectionCompatible(new Color[] { Color.white }));
Assert.IsFalse(intColA.IsCollectionCompatible(new List<Material>()));
Assert.IsFalse(intColA.IsCollectionCompatible(new List<Fungus.StringVariable>()));
}
[Test]
public void Copy()
{
const int Items = 10;
for (int i = 0; i < Items; i++)
{
intColA.Add(i);
}
intColB.CopyFrom(intColA);
for (int i = 0; i < intColA.Count; i++)
{
Assert.AreEqual(intColA[i], intColB[i]);
}
//test enumerator
var enumer = intColA.GetEnumerator();
var index = 0;
while (enumer.MoveNext())
{
Assert.AreEqual(enumer.Current, intColB[index++]);
}
intColB.Clear();
intColB.Add(intColA);
for (int i = 0; i < intColA.Count; i++)
{
Assert.AreEqual(intColA[i], intColB[i]);
}
intColB.Clear();
int[] copyDest = new int[intColA.Count];
intColA.CopyTo(copyDest, 0);
for (int i = 0; i < intColA.Count; i++)
{
Assert.AreEqual(intColA[i], copyDest[i]);
}
//now back again
intColA.Clear();
intColA.CopyFrom(copyDest);
for (int i = 0; i < intColA.Count; i++)
{
Assert.AreEqual(intColA[i], copyDest[i]);
}
//now lists
intColA.Clear();
intColA.CopyFrom(copyDest.ToList());
for (int i = 0; i < intColA.Count; i++)
{
Assert.AreEqual(intColA[i], copyDest[i]);
}
intColA.Clear();
intColB.Clear();
}
[OneTimeTearDown]
public void DestroyTestObjects()
{
Object.DestroyImmediate(intColA.gameObject);
Object.DestroyImmediate(intColB.gameObject);
Object.DestroyImmediate(goCol.gameObject);
}
[Test]
public void Exclusive()
{
const int Items = 10;
const int Step = 5;
for (int i = 0; i < Items; i++)
{
intColA.Add(i);
intColB.Add(i + Step);
}
intColA.Exclusive(intColB);
for (int i = 0; i < intColA.Count; i++)
{
Assert.IsTrue(intColA.GetSafe(i) < Step || intColA.GetSafe(i) >= Items);
}
Assert.AreEqual(intColA.Count, Items);
intColA.Clear();
intColB.Clear();
}
[Test]
public void Finds()
{
const int Items = 10;
const int TargetItemValue = 5;
const int TargetItemIndex = 5;
const int BItems = 3;
for (int i = 0; i < Items; i++)
{
intColA.Add(i);
}
//add a dup, test first and last, remove dup
intColA.Add(TargetItemValue);
Assert.AreEqual(intColA.IndexOf(TargetItemValue), TargetItemIndex);
Assert.IsTrue(intColA.Contains(TargetItemValue));
Assert.AreEqual(intColA.Occurrences(TargetItemValue), 2);
Assert.AreEqual(intColA.LastIndexOf(TargetItemValue), intColA.Count - 1);
intColA.RemoveAt(intColA.Count - 1);
//any of, put some in colb
for (int i = 0; i < BItems; i++)
{
intColB.Add(i);
}
Assert.IsTrue(intColA.ContainsAnyOf(intColB));
//clear be, put all of in col b
intColB.Clear();
intColB.Add(intColA);
//contain ordered
Assert.IsTrue(intColA.ContainsAllOfOrdered(intColB));
//shuffle
EnsureShuffledDifferent(intColB);
//contain ordered must fail
Assert.IsFalse(intColA.ContainsAllOfOrdered(intColB));
//contain all of must true
Assert.IsTrue(intColA.ContainsAllOf(intColB));
intColA.Clear();
intColB.Clear();
}
[Test]
public void GetSet()
{
const int ValueA = 1, ValueB = 2;
intColA.Add(ValueA);
Assert.AreEqual(ValueA, intColA[0]);
Assert.AreEqual(ValueA, intColA.Get(0));
Assert.AreEqual(ValueA, intColA.GetSafe(0));
intColA.Set(0, ValueB);
Assert.AreEqual(ValueB, intColA[0]);
intColA[0] = ValueA;
Assert.AreEqual(ValueA, intColA[0]);
intColA.Clear();
intColB.Clear();
}
[OneTimeSetUp]
public void InitTestObjects()
{
intColA = new GameObject().AddComponent<Fungus.IntCollection>();
intColB = new GameObject().AddComponent<Fungus.IntCollection>();
goCol = new GameObject().AddComponent<Fungus.GameObjectCollection>();
}
[Test]
public void Intersection()
{
const int Items = 10;
const int Step = 5;
for (int i = 0; i < Items; i++)
{
intColA.Add(i);
intColB.Add(i + Step);
}
intColA.Intersection(intColB);
for (int i = 0; i < intColA.Count; i++)
{
Assert.IsTrue(intColA.GetSafe(i) >= Step || intColA.GetSafe(i) < Items);
}
Assert.AreEqual(intColA.Count, Step);
intColA.Clear();
intColB.Clear();
}
[Test]
public void Size()
{
intColA.Capacity = 10;
var startCap = intColA.Capacity;
var startCount = intColA.Count;
intColA.Capacity *= 10;
Assert.Greater(intColA.Capacity, startCap);
startCap = intColA.Capacity;
Assert.AreEqual(startCount, intColA.Count);
intColA.Reserve(intColA.Capacity * 10);
Assert.Greater(intColA.Capacity, startCap);
intColA.Resize(startCount + 10);
Assert.AreEqual(startCount + 10, intColA.Count);
intColA.Clear();
intColB.Clear();
}
[Test]
public void Sort()
{
const int Items = 10;
for (int i = 0; i < Items; i++)
{
intColA.Add(i);
}
//other tests ensure that all actually works
EnsureShuffledDifferent(intColA);
intColA.Sort();
for (int i = 0; i < Items; i++)
{
Assert.AreEqual(i, intColA[i]);
}
//are they now mirrored
intColA.Reverse();
for (int i = 0; i < Items; i++)
{
Assert.AreEqual(i, intColA[Items - i - 1]);
}
intColA.Clear();
intColB.Clear();
}
private void EnsureShuffledDifferent(Fungus.IntCollection col)
{
var startval = col.GetSafe(0);
col.Shuffle();
//don't let shuffle result in the same seq
if (col.GetSafe(0) == startval)
{
col[0] = col[col.Count - 1];
col[col.Count - 1] = startval;
}
}
}
}