-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the WeightedSelectMethodTests. - All WeightedSelectMethods now support weights less than or equal to 0. - IWeightedSelectMethod now takes a TemporaryArray<float> as an argument instead of a float[]. - Fixed a fatal bug that prevented BinaryWeightedSelectMethod from working.
- Loading branch information
Showing
10 changed files
with
294 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Assets/MackySoft/MackySoft.Choice/Tests/Editor/ItemEnumerableGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace MackySoft.Choice.Tests { | ||
|
||
public class Item { | ||
|
||
public int id; | ||
|
||
public override string ToString () { | ||
return "Index: " + id.ToString(); | ||
} | ||
|
||
} | ||
|
||
public struct ItemEntry { | ||
|
||
public Item item; | ||
public float weight; | ||
|
||
public override string ToString () { | ||
return $"{{ {item}: Weight {weight} }}"; | ||
} | ||
|
||
} | ||
|
||
public class ItemEnumerableGenerator { | ||
|
||
public static IEnumerable<ItemEntry> GenerateEnumerable (int count) { | ||
for (int i = 0;count > i;i++) { | ||
yield return new ItemEntry { | ||
item = new Item { id = i }, | ||
weight = Random.Range(0,10) | ||
}; | ||
} | ||
} | ||
|
||
public static IEnumerable<KeyValuePair<Item,float>> GenerateDictionary (int count) { | ||
for (int i = 0;count > i;i++) { | ||
yield return new KeyValuePair<Item,float>( | ||
new Item { id = i }, | ||
Random.Range(0,10) | ||
); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
Assets/MackySoft/MackySoft.Choice/Tests/Editor/ItemEnumerableGenerator.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
Assets/MackySoft/MackySoft.Choice/Tests/Editor/WeightedSelectMethodTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using NUnit.Framework; | ||
|
||
namespace MackySoft.Choice.Tests { | ||
public class WeightedSelectMethodTests { | ||
|
||
[Test] | ||
public void Linear_ReturnValidValue ([Random(0f,1f,10)] float value) { | ||
var source = ItemEnumerableGenerator.GenerateEnumerable(100).ToArray(); | ||
var weightedSelector = source.ToWeightedSelector(x => x.item,x => x.weight,WeightedSelectMethod.Linear); | ||
Assert.IsNotNull(weightedSelector.SelectItem(value)); | ||
} | ||
|
||
[Test, Repeat(100)] | ||
public void Linear_ReturnValidValue_0 () { | ||
var source = ItemEnumerableGenerator.GenerateEnumerable(100).ToArray(); | ||
var weightedSelector = source.ToWeightedSelector(x => x.item,x => x.weight,WeightedSelectMethod.Linear); | ||
Assert.AreSame(source.FirstOrDefault(x => x.weight > 0f).item,weightedSelector.SelectItem(0f)); | ||
} | ||
|
||
[Test, Repeat(100)] | ||
public void Linear_ReturnValidValue_1 () { | ||
var source = ItemEnumerableGenerator.GenerateEnumerable(100).ToArray(); | ||
var weightedSelector = source.ToWeightedSelector(x => x.item,x => x.weight,WeightedSelectMethod.Linear); | ||
Assert.AreSame(source.LastOrDefault(x => x.weight > 0f).item,weightedSelector.SelectItem(1f)); | ||
} | ||
|
||
[Test] | ||
public void Binary_ReturnValidValue ([Random(0f,1f,10)] float value) { | ||
var source = ItemEnumerableGenerator.GenerateEnumerable(100).ToArray(); | ||
var weightedSelector = source.ToWeightedSelector(x => x.item,x => x.weight,WeightedSelectMethod.Binary); | ||
Assert.IsNotNull(weightedSelector.SelectItem(value)); | ||
} | ||
|
||
[Test, Repeat(100)] | ||
public void Binary_ReturnValidValue_0 () { | ||
var source = ItemEnumerableGenerator.GenerateEnumerable(100).ToArray(); | ||
var weightedSelector = source.ToWeightedSelector(x => x.item,x => x.weight,WeightedSelectMethod.Binary); | ||
Assert.AreSame(weightedSelector.FirstOrDefault(p => p.Value > 0f).Key,weightedSelector.SelectItem(0f)); | ||
} | ||
|
||
[Test, Repeat(100)] | ||
public void Binary_ReturnValidValue_1 () { | ||
var source = ItemEnumerableGenerator.GenerateEnumerable(100).ToArray(); | ||
var weightedSelector = source.ToWeightedSelector(x => x.item,x => x.weight,WeightedSelectMethod.Binary); | ||
Assert.AreSame(source.LastOrDefault(x => x.weight > 0f).item,weightedSelector.SelectItem(1f)); | ||
} | ||
|
||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/MackySoft/MackySoft.Choice/Tests/Editor/WeightedSelectMethodTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.