move History.ToArray() into test code.

This commit is contained in:
Colin McMillen 2020-03-18 14:03:37 -04:00
parent 63a2645814
commit 67550a789b
2 changed files with 13 additions and 15 deletions

View File

@ -49,15 +49,5 @@ namespace SemiColinGames {
return items[lookup]; return items[lookup];
} }
} }
// This creates and populates a new array. It's O(n) and should probably only be used for tests.
// TODO: restrict visibility so that it can only be used by tests.
public T[] ToArray() {
T[] result = new T[items.Length];
for (int i = 0; i < items.Length; i++) {
result[i] = this[i];
}
return result;
}
} }
} }

View File

@ -4,6 +4,14 @@ using System;
namespace SemiColinGames.Tests { namespace SemiColinGames.Tests {
[TestClass] [TestClass]
public class HistoryTests { public class HistoryTests {
public static int[] ToArray(History<int> history) {
int[] result = new int[history.Length];
for (int i = 0; i < history.Length; i++) {
result[i] = history[i];
}
return result;
}
[TestMethod] [TestMethod]
public void TestLength() { public void TestLength() {
var h = new History<int>(3); var h = new History<int>(3);
@ -24,17 +32,17 @@ namespace SemiColinGames.Tests {
[TestMethod] [TestMethod]
public void TestAdds() { public void TestAdds() {
var h = new History<int>(3); var h = new History<int>(3);
Assert.AreEqual("0 0 0", String.Join(" ", h.ToArray())); Assert.AreEqual("0 0 0", String.Join(" ", ToArray(h)));
h.Add(2); h.Add(2);
Assert.AreEqual("2 0 0", String.Join(" ", h.ToArray())); Assert.AreEqual("2 0 0", String.Join(" ", ToArray(h)));
h.Add(3); h.Add(3);
h.Add(5); h.Add(5);
Assert.AreEqual("5 3 2", String.Join(" ", h.ToArray())); Assert.AreEqual("5 3 2", String.Join(" ", ToArray(h)));
h.Add(7); h.Add(7);
Assert.AreEqual("7 5 3", String.Join(" ", h.ToArray())); Assert.AreEqual("7 5 3", String.Join(" ", ToArray(h)));
h.Add(11); h.Add(11);
h.Add(13); h.Add(13);
Assert.AreEqual("13 11 7", String.Join(" ", h.ToArray())); Assert.AreEqual("13 11 7", String.Join(" ", ToArray(h)));
} }
[TestMethod] [TestMethod]