Just using the VS built in stuff, and I've migrated all my Range tests (with a couple added to cover cases I'd missed ).
I kinda like it:
using System;
using NonContiguousRange;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
namespace RangeTests
{
[TestClass]
public class RangeTester
{
private Range ra = new Range(0, 4);
private Range rb = new Range(5, 9);
private Range rc = new Range(10, 14);
private Range rd = new Range(1, 3);
private Range re = new Range(6, 8);
private Range rf = new Range(16, 19);
private Range rg = new Range(0, 0);
private Range rh = new Range(1, 1);
private Range ri = new Range(2, 2);
private Range rj = new Range(0, 19);
private Range rk = new Range(0, 4);
private Range rl = new Range(-1, 1);
private Range rm = new Range(3, 5);
private string testType = "";
#region Tests
[TestMethod]
public void ConstructorsTest()
{
testType = "\nConstructor";
Assert.AreEqual(5, ra.Count, 0, $"{testType}: Range items count incorrect");
Assert.AreEqual(0, ra.Min, 0, $"{testType}: Range minimum incorrect");
Assert.AreEqual(4, ra.Max, 0, $"{testType}: Range maximum incorrect");
Check(ra, new int[] { 0, 1, 2, 3, 4 }, "Wrong elements");
Check(new Range(new int[] { 0, 1, 2, 5, 6, 7 }.AsEnumerable()), new int[] { 0, 1, 2, 5, 6, 7 }, "Enumerable parameter wrong elements");
}
[TestMethod]
public void HumanReadabilityTest()
{
testType = "\nHuman Readability";
Assert.AreEqual("(0:4)", ra.ToString(), false, $"{testType}: Contiguous range incorrect");
Assert.AreEqual("(0:4) | (6:8)", ra.UnionWith(re).ToString(), false, $"{testType}: Noncontiguous range incorrect");
}
[TestMethod]
public void Emptyness()
{
testType = "\nEmptiness";
Assert.IsTrue(Enumerable.SequenceEqual(Range.GetEmpty(), new int[0]), $"{testType}: Empty isn't");
}
[TestMethod]
public void Unions()
{
testType = "\nUnions";
Check(ra.UnionWith(ra), new int[] { 0, 1, 2, 3, 4 }, "Self union");
Check(ra.UnionWith(rb), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, "Contiguous 1");
Check(rb.UnionWith(rc), new int[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, "Contiguous 2");
Check(ra.UnionWith(rc), new int[] { 0, 1, 2, 3, 4, 10, 11, 12, 13, 14 }, "Noncontiguous 1");
Check(ra.UnionWith(re), new int[] { 0, 1, 2, 3, 4, 6, 7, 8 }, "Noncontiguous 2");
Check(ra.UnionWith(rg), new int[] { 0, 1, 2, 3, 4 }, "Single element in original range");
Check(rd.UnionWith(rg), new int[] { 0, 1, 2, 3 }, "Single element contiguous");
Check(rb.UnionWith(rg), new int[] { 0, 5, 6, 7, 8, 9 }, "Single element noncontiguous");
Check(rg.UnionWith(rh).UnionWith(ri), new int[] { 0, 1, 2 }, "Three single element contiguous");
Assert.AreEqual(10, ra.UnionWith(rc).Count, 0, $"{testType}: Count");
Assert.AreEqual(0, ra.UnionWith(rc).Min, 0, $"{testType}: Minimum value");
Assert.AreEqual(14, ra.UnionWith(rc).Max, 0, $"{testType}: Maximum value");
}
[TestMethod]
public void Intersections()
{
testType = "\nIntersections";
Check(ra.Intersecting(ra), new int[] { 0, 1, 2, 3, 4 }, "Self");
Check(ra.Intersecting(rl), new int[] { 0, 1 }, "Left intersection");
Check(ra.Intersecting(rd), new int[] { 1, 2, 3 }, "Center intersection");
Check(ra.Intersecting(rm), new int[] { 3, 4 }, "Right intersection");
Check(ra.Intersecting(rb), new int[0], "Contiguous 1");
Check(rb.Intersecting(rc), new int[0], "Contiguous 2");
Check(ra.Intersecting(rc), new int[0], "Noncontiguous, non-intersecting");
Check(ra.Intersecting(rg), new int[] { 0 }, "Single element, intersecting");
Check(rb.Intersecting(rg), new int[0], "Single element, non-intersecting");
Check(rg.Intersecting(rh).Intersecting(ri), new int[0], "Single element, single element");
}
[TestMethod]
public void Exceptions()
{
testType = "\nExceptions";
Check(ra.Excepting(ra), new int[0], "Self");
Check(ra.Excepting(rl), new int[] { 2, 3, 4 }, "Left intersection");
Check(ra.Excepting(rd), new int[] { 0, 4 }, "Center intersection");
Check(ra.Excepting(rm), new int[] { 0, 1, 2 }, "Right intersection");
Check(ra.Excepting(rb), new int[] { 0, 1, 2, 3, 4 }, "Contiguous 1");
Check(rb.Excepting(rc), new int[] { 5, 6, 7, 8, 9 }, "Contiguous 2");
Check(ra.Excepting(rc), new int[] { 0, 1, 2, 3, 4 }, "Noncontiguous");
Check(ra.Excepting(rg), new int[] { 1, 2, 3, 4 }, "Single Element, intersecting 1");
Check(rj.Excepting(rh).Excepting(rb).Excepting(rf), new int[] { 0, 2, 3, 4, 10, 11, 12, 13, 14, 15 }, "Multiple");
Check(rg.Excepting(rh).Excepting(ri), new int[] { 0 }, "Multiple single element");
}
[TestMethod]
public void Miscellaneous()
{
testType = "\nMiscellaneous";
Range nc = ra.UnionWith(rf);
Check(ra.EndAt(19), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }, "Expand right, contiguous");
Check(rf.StartAt(0), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }, "Expand left, contiguous");
Check(nc.EndAt(9), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, "Contract right, non-contiguous");
Check(nc.StartAt(9), new int[] { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }, "Contract left, non-contiguous");
Check(ra.UnionWith(re).MakeContiguous(), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, "Make contiguous");
}
[TestMethod]
public void AddToRange()
{
testType = "\nAdding to";
Check(ra.Add(5), new int[] { 0, 1, 2, 3, 4, 5 }, "Single, right");
Check(ra.Add(-1), new int[] { -1, 0, 1, 2, 3, 4 }, "Single, left");
Check(ra.AddRange(new int[] { 5, -1 }), new int[] { -1, 0, 1, 2, 3, 4, 5 }, "Array");
Check(ra + 5, new int[] { 0, 1, 2, 3, 4, 5 }, "Operator +, single, right");
Check(ra + -1, new int[] { -1, 0, 1, 2, 3, 4 }, "Operator +, single, left");
Check(ra + new int[] { 5, -1 }, new int[] { -1, 0, 1, 2, 3, 4, 5 }, "Operator +, array");
Check(ra + rb, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, "Operator +, range");
}
[TestMethod]
public void RemoveFromRange()
{
testType = "\nRemove from range";
Check(ra.Remove(0), new int[] { 1, 2, 3, 4 }, "Single, left");
Check(ra.Remove(2), new int[] { 0, 1, 3, 4 }, "Single, center");
Check(ra.Remove(4), new int[] { 0, 1, 2, 3 }, "Single, right");
Check(ra.RemoveRange(new int[] { 0, 1 }), new int[] { 2, 3, 4 }, "Array, left");
Check(ra.RemoveRange(new int[] { 2, 3 }), new int[] { 0, 1, 4 }, "Array, center");
Check(ra.RemoveRange(new int[] { 3, 4 }), new int[] { 0, 1, 2 }, "Array, right");
Check(ra - 1, new int[] { 0, 2, 3, 4 }, "Operator -, single");
Check(ra - -1, new int[] { 0, 1, 2, 3, 4 }, "Operator -, single, not included");
Check(ra - new int[] { 1, 2 }, new int[] { 0, 3, 4 }, "Operator -, array");
Check(ra - rd, new int[] { 0, 4 }, "Operator -, range");
}
[TestMethod]
public void Comparisons()
{
testType = "\nComparisons";
Assert.IsTrue(ra.Equals(rk), "Equals");
Assert.IsTrue(ra.Equals((object)rk), "Equals (object)");
Assert.IsTrue(ra == rk, "==");
Assert.IsFalse(ra != rk, "!=");
}
[TestMethod]
public void Contiguousness()
{
testType = "\nContiguousness";
Assert.IsTrue(ra.IsContiguousWith(rb), "Contiguous, right");
Assert.IsTrue(rb.IsContiguousWith(ra), "Contiguous, left");
Assert.IsFalse(ra.IsContiguousWith(rc), "NonContiguous, right");
Assert.IsFalse(re.IsContiguousWith(rd), "Noncontiguous, left");
}
[TestMethod]
public void Intersectivity()
{
testType = "\nIntersectivity";
Assert.IsFalse(ra.IsIntersecting(rb), "Non-intersecting, right");
Assert.IsFalse(rb.IsIntersecting(ra), "Non-intersecting, left");
Assert.IsTrue(ra.IsIntersecting(rm), "Intersecting, right");
Assert.IsTrue(ra.IsIntersecting(rd), "Intersecting, center");
Assert.IsTrue(rm.IsIntersecting(ra), "Intersecting, left");
}
#endregion
#region Support functions
private void Check(Range range, int[] expected, string desc)
{
string result = "Range length incorrect";
if (range.Count == expected.Length)
{
result = "Range values did not match";
if (Enumerable.SequenceEqual(range, expected))
{
return;
}
}
ReportFailure(desc, result, range, expected);
}
private void ReportFailure(string desc, string result, string range, string expected)
{
Assert.IsTrue(false, $"{testType}: {desc} - {result}\n Got : {range}\n Expected: {expected}");
}
private void ReportFailure(string desc, string result, Range range, int[] expected)
{
Assert.IsTrue(false, $"{testType}: {desc} - {result}\n Got : {string.Join(", ", range.Select(r => r.ToString()))}\n Expected: {string.Join(", ", expected.Select(r => r.ToString()))}");
}
#endregion
}
} So I thought I'd move to the next step: autogenerating test stubs.
Right click in my project code, and select "Create Unit Tests" expecting ... well, not a lot, but something usable.
All I get is a message box:
Value cannot be null.
Parameter name: codeClass. Off to Google ... and lo: Create unit test command fails in STRUCTS[^]
It's a bug that was reported in 2018, and closed a couple of months later with no action.
Truely, MS do not like to fix bugs - just change the icons and ship it ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|