Ejemplo n.º 1
0
        public void RemoveByLabelTest()
        {
            // Removing existing category
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                CategoricalVariable actualVariable, expectedVariable;
                bool   actualFlag, expectedFlag;
                string categoryLabel;

                categoryLabel = "One";

                actualFlag     = target.Remove(categoryLabel);
                actualVariable = target;

                expectedFlag = true;

                expectedVariable = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 2.0, "Two" }
                };

                Assert.AreEqual(expectedFlag, actualFlag);
                CategoricalVariableAssert.AreEqual(expectedVariable, actualVariable);
            }

            // Removing not existing category
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                CategoricalVariable actualVariable, expectedVariable;
                bool   actualFlag, expectedFlag;
                string categoryLabel;

                categoryLabel = "Three";

                actualFlag     = target.Remove(categoryLabel);
                actualVariable = target;

                expectedFlag = false;

                expectedVariable = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                Assert.AreEqual(expectedFlag, actualFlag);
                CategoricalVariableAssert.AreEqual(expectedVariable, actualVariable);
            }

            // label is null
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                ArgumentExceptionAssert.Throw(
                    () => { target.Remove(null); },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "label");
            }

            // Removing categories in read-only variables
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                target.SetAsReadOnly();

                ExceptionAssert.Throw(
                    () => { target.Remove("One"); },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CAT_VARIABLE_IS_READONLY"));

                ExceptionAssert.Throw(
                    () => { target.Remove("Three"); },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CAT_VARIABLE_IS_READONLY"));
            }
        }
Ejemplo n.º 2
0
        public void FromDoubleMatrixByMethodTest()
        {
            // With named rows
            {
                string name = "The name";

                var target = DoubleMatrix.Dense(3, 1,
                                                new double[3] {
                    0.0, 1.0, 2.0
                });
                target.Name = name;
                target.SetRowName(0, "Zero");
                target.SetRowName(1, "One");
                target.SetRowName(2, "Two");

                var actual = CategoricalVariable.FromDoubleMatrix(target);

                var expected = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                CategoricalVariableAssert.AreEqual(expected, actual);
            }

            // With unnamed rows
            {
                string name = "The name";

                var target = DoubleMatrix.Dense(3, 1,
                                                new double[3] {
                    0.0, 1.0, 2.0
                });
                target.Name = name;

                var actual = CategoricalVariable.FromDoubleMatrix(target);

                var expected = new CategoricalVariable(name)
                {
                    0.0,
                    1.0,
                    2.0
                };

                CategoricalVariableAssert.AreEqual(expected, actual);
            }

            // value is null
            {
                DoubleMatrix target = null;

                ArgumentExceptionAssert.Throw(
                    () => { var result = CategoricalVariable.FromDoubleMatrix(target); },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "value");
            }

            // value is not a column vector
            {
                var target = DoubleMatrix.Dense(2, 3);

                ArgumentExceptionAssert.Throw(
                    () => { var result = CategoricalVariable.FromDoubleMatrix(target); },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR"),
                    expectedParameterName: "value");
            }
        }
Ejemplo n.º 3
0
        public void RemoveByCodeTest()
        {
            // Removing existing category
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                CategoricalVariable actualVariable, expectedVariable;
                bool   actualFlag, expectedFlag;
                double categoryCode;

                categoryCode = 1.0;

                actualFlag     = target.Remove(categoryCode);
                actualVariable = target;

                expectedFlag = true;

                expectedVariable = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 2.0, "Two" }
                };

                Assert.AreEqual(expectedFlag, actualFlag);
                CategoricalVariableAssert.AreEqual(expectedVariable, actualVariable);
            }

            // Removing not existing category
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                CategoricalVariable actualVariable, expectedVariable;
                bool   actualFlag, expectedFlag;
                double categoryCode;

                categoryCode = 3.0;

                actualFlag     = target.Remove(categoryCode);
                actualVariable = target;

                expectedFlag = false;

                expectedVariable = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                Assert.AreEqual(expectedFlag, actualFlag);
                CategoricalVariableAssert.AreEqual(expectedVariable, actualVariable);
            }

            // Removing categories in read-only variables
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                target.SetAsReadOnly();

                ExceptionAssert.Throw(
                    () => { target.Remove(3.0); },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CAT_VARIABLE_IS_READONLY"));

                ExceptionAssert.Throw(
                    () => { target.Remove(1.0); },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CAT_VARIABLE_IS_READONLY"));
            }
        }