public void TryGetByLabelTest() { // Getting existing category { string name = "The name"; var target = new CategoricalVariable(name) { { 0.0, "Zero" }, { 1.0, "One" }, { 2.0, "Two" } }; Category expectedCategory; bool actualFlag, expectedFlag; string categoryLabel; categoryLabel = "One"; actualFlag = target.TryGet( categoryLabel, out Category actualCategory); expectedFlag = true; expectedCategory = new Category(1.0, categoryLabel); Assert.AreEqual(expectedFlag, actualFlag); CategoryAssert.AreEqual(expectedCategory, actualCategory); } // Getting not existing category { string name = "The name"; var target = new CategoricalVariable(name) { { 0.0, "Zero" }, { 1.0, "One" }, { 2.0, "Two" } }; Category expectedCategory; bool actualFlag, expectedFlag; string categoryLabel; categoryLabel = "Three"; actualFlag = target.TryGet( categoryLabel, out Category actualCategory); expectedFlag = false; expectedCategory = null; Assert.AreEqual(expectedFlag, actualFlag); CategoryAssert.AreEqual(expectedCategory, actualCategory); } }
public void TryGetByCodeTest() { // Getting existing category { string name = "The name"; var target = new CategoricalVariable(name) { { 0.0, "Zero" }, { 1.0, "One" }, { 2.0, "Two" } }; bool actualFlag, expectedFlag; double categoryCode; categoryCode = 1.0; actualFlag = target.TryGet( categoryCode, out Category actualCategory); expectedFlag = true; Category expectedCategory = new(categoryCode, "One"); Assert.AreEqual(expectedFlag, actualFlag); CategoryAssert.AreEqual(expectedCategory, actualCategory); } // Getting not existing category { string name = "The name"; var target = new CategoricalVariable(name) { { 0.0, "Zero" }, { 1.0, "One" }, { 2.0, "Two" } }; Category expectedCategory; bool actualFlag, expectedFlag; double categoryCode; categoryCode = 3.0; actualFlag = target.TryGet( categoryCode, out Category actualCategory); expectedFlag = false; expectedCategory = null; Assert.AreEqual(expectedFlag, actualFlag); CategoryAssert.AreEqual(expectedCategory, actualCategory); } }
public void GetEnumeratorTest() { // IEnumerable.GetEnumerator { var target = new CategoricalVariable("V") { 0, 1, 2 }; IEnumerable enumerable = (IEnumerable)target; IEnumerator enumerator = enumerable.GetEnumerator(); object current; int index = 0; while (enumerator.MoveNext()) { current = enumerator.Current; Assert.AreEqual(target.Categories[index], (Category)current); index++; } // reset enumerator.Reset(); // dispose enumerator = null; GC.Collect(10, GCCollectionMode.Forced); } // IEnumerable<Category>.GetEnumerator { var target = new CategoricalVariable("V") { 0, 1, 2 }; IEnumerable <Category> enumerable = (IEnumerable <Category>)target; IEnumerator <Category> enumerator = enumerable.GetEnumerator(); int index = 0; Category current; while (enumerator.MoveNext()) { current = enumerator.Current; Assert.AreEqual(target.Categories[index], current); index++; } // reset enumerator.Reset(); // dispose enumerator.Dispose(); } // IEnumerable<Category>.Current returns null { var target = new CategoricalVariable("V") { 0, 1, 2 }; var enumerable = (IEnumerable <Category>)target; var enumerator = enumerable.GetEnumerator(); Assert.IsNull(enumerator.Current); } // IEnumerable.Current fails { string STR_EXCEPT_ENU_OUT_OF_BOUNDS = "Enumeration has either not started or has already finished."; var target = new CategoricalVariable("V") { 0, 1, 2 }; var enumerable = (IEnumerable)target; var enumerator = enumerable.GetEnumerator(); ExceptionAssert.Throw( () => { object current = enumerator.Current; }, expectedType: typeof(InvalidOperationException), expectedMessage: STR_EXCEPT_ENU_OUT_OF_BOUNDS); } // valid input { var target = new CategoricalVariable("V") { { 0, "A" }, { 1, "B" }, { 2, "C" } }; Category[] expected = new Category[3] { new Category(0, "A"), new Category(1, "B"), new Category(2, "C") }; int index = 0; foreach (var category in target) { CategoryAssert.AreEqual( expected: expected[index], actual: category); index++; } } }