Browse Source

TextVariationHandler can now clear its previous history

- Uses XorShift style bit mixing on hashes to avoid collisions when input and section are identical
- Added tests to verify basic text variation is working as required.
master
desktop-maesty/steve 6 years ago
parent
commit
3c517c2107
  1. 14
      Assets/Fungus/Scripts/Utils/TextVariationHandler.cs
  2. 8
      Assets/Tests/StringSubstitution/Editor.meta
  3. 114
      Assets/Tests/StringSubstitution/Editor/FungusTextVariationSelectionTests.cs
  4. 11
      Assets/Tests/StringSubstitution/Editor/FungusTextVariationSelectionTests.cs.meta

14
Assets/Fungus/Scripts/Utils/TextVariationHandler.cs

@ -70,6 +70,11 @@ namespace Fungus
static Dictionary<int, int> hashedSections = new Dictionary<int, int>();
static public void ClearHistory()
{
hashedSections.Clear();
}
/// <summary>
/// Simple parser to extract depth matched [].
///
@ -190,7 +195,14 @@ namespace Fungus
//fetched hashed value
int index = -1;
int key = input.GetHashCode() ^ curSection.entire.GetHashCode() ^ parentHash;
//as input and entire can be the same thing we need to shuffle these bits
//we use some xorshift style mixing
int inputHash = input.GetHashCode();
inputHash ^= inputHash << 13;
int curSecHash = curSection.entire.GetHashCode();
curSecHash ^= curSecHash >> 17;
int key = inputHash ^ curSecHash ^ parentHash;
int foundVal = 0;
if (hashedSections.TryGetValue(key, out foundVal))

8
Assets/Tests/StringSubstitution/Editor.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b0ab687196033764d88aa5c2e32c035b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

114
Assets/Tests/StringSubstitution/Editor/FungusTextVariationSelectionTests.cs

@ -0,0 +1,114 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
public class FungusTextVariationSelectionTests
{
[Test]
public void SimpleSequenceSelection()
{
Fungus.TextVariationHandler.ClearHistory();
string startingText = @"This is test [a|b|c]";
string startingTextA = @"This is test a";
string startingTextB = @"This is test b";
string startingTextC = @"This is test c";
string res = string.Empty;
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextA);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextB);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextC);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextC);
}
[Test]
public void SimpleCycleSelection()
{
Fungus.TextVariationHandler.ClearHistory();
string startingText = @"This is test [&a|b|c]";
string startingTextA = @"This is test a";
string startingTextB = @"This is test b";
string startingTextC = @"This is test c";
string res = string.Empty;
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextA);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextB);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextC);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextA);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextB);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextC);
}
[Test]
public void SimpleOnceSelection()
{
Fungus.TextVariationHandler.ClearHistory();
string startingText = @"This is test [!a|b|c]";
string startingTextA = @"This is test a";
string startingTextB = @"This is test b";
string startingTextC = @"This is test c";
string startingTextD = @"This is test ";
string res = string.Empty;
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextA);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextB);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextC);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextD);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextD);
}
[Test]
public void NestedSelection()
{
Fungus.TextVariationHandler.ClearHistory();
string startingText = @"This is test [a||sub [~a|b]|[!b|[~c|d]]]";
string startingTextA = @"This is test a";
string startingTextBlank = @"This is test ";
string startingTextSubA = @"This is test sub a";
string startingTextSubB = @"This is test sub b";
string startingTextB = @"This is test b";
string startingTextC = @"This is test c";
string startingTextD = @"This is test d";
string res = string.Empty;
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextA);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextBlank);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
if(res != startingTextSubA && res != startingTextSubB)
{
Assert.Fail();
}
res = Fungus.TextVariationHandler.SelectVariations(startingText);
Assert.AreEqual(res, startingTextB);
res = Fungus.TextVariationHandler.SelectVariations(startingText);
if (res != startingTextC && res != startingTextD)
{
Assert.Fail();
}
}
}

11
Assets/Tests/StringSubstitution/Editor/FungusTextVariationSelectionTests.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8769bf7410785704f95413bb0865079c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save