History: add ToArray() method to make tests cleaner

GitOrigin-RevId: 5bbf5be614ed6c2d9884ac924ab627ccaa57e355
This commit is contained in:
Colin McMillen 2020-01-14 17:36:30 -05:00
parent 3f6fc67cb6
commit 960521a6e0
2 changed files with 15 additions and 5 deletions

View File

@ -49,5 +49,14 @@ 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.
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

@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System; using System;
using System.Collections.Generic;
namespace SemiColinGames.Tests { namespace SemiColinGames.Tests {
[TestClass] [TestClass]
@ -24,17 +25,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.Format("{0} {1} {2}", h[0], h[1], h[2])); Assert.AreEqual("0 0 0", String.Join(" ", h.ToArray()));
h.Add(2); h.Add(2);
Assert.AreEqual("2 0 0", String.Format("{0} {1} {2}", h[0], h[1], h[2])); Assert.AreEqual("2 0 0", String.Join(" ", h.ToArray()));
h.Add(3); h.Add(3);
h.Add(5); h.Add(5);
Assert.AreEqual("5 3 2", String.Format("{0} {1} {2}", h[0], h[1], h[2])); Assert.AreEqual("5 3 2", String.Join(" ", h.ToArray()));
h.Add(7); h.Add(7);
Assert.AreEqual("7 5 3", String.Format("{0} {1} {2}", h[0], h[1], h[2])); Assert.AreEqual("7 5 3", String.Join(" ", h.ToArray()));
h.Add(11); h.Add(11);
h.Add(13); h.Add(13);
Assert.AreEqual("13 11 7", String.Format("{0} {1} {2}", h[0], h[1], h[2])); Assert.AreEqual("13 11 7", String.Join(" ", h.ToArray()));
} }
[TestMethod] [TestMethod]