public void AddByCodeOnlyTest()
        {
            // code already exists
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    2.0
                };

                ExceptionAssert.Throw(
                    () => { target.Add(2.0); },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CAT_ALREADY_EXISTS_IN_VARIABLE_LIST"));
            }

            // target is read only
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name);

                target.SetAsReadOnly();

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

            // Valid input
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name);

                double   code;
                Category category0, category1;

                code = 1.0;
                target.Add(code);
                category0 = new Category(code);

                code = 2.0;
                target.Add(code);
                category1 = new Category(code);

                CategoricalVariableAssert.IsStateAsExpected(
                    target,
                    name,
                    expectedCategories: new List <Category>(2)
                {
                    category0,
                    category1
                },
                    expectedReadOnlyFlag: false);
            }
        }
        public void SetAsReadOnlyTest()
        {
            string expectedName = "The name";

            var target = new CategoricalVariable(expectedName);

            target.SetAsReadOnly();

            CategoricalVariableAssert.IsStateAsExpected(
                target,
                expectedName,
                expectedCategories: new List <Category>(),
                expectedReadOnlyFlag: true);
        }
        public void ConstructorTest()
        {
            // name is null
            {
                ArgumentExceptionAssert.Throw(
                    () => { new CategoricalVariable(null); },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "name");
            }

            // name is white space
            {
                ArgumentExceptionAssert.Throw(
                    () => { new CategoricalVariable(" "); },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_STRING_IS_EMPTY_OR_WHITESPACE"),
                    expectedParameterName: "name");
            }

            // name is empty
            {
                ArgumentExceptionAssert.Throw(
                    () => { new CategoricalVariable(string.Empty); },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_STRING_IS_EMPTY_OR_WHITESPACE"),
                    expectedParameterName: "name");
            }

            // Valid name
            {
                string expectedName = "The name";

                var target = new CategoricalVariable(expectedName);

                CategoricalVariableAssert.IsStateAsExpected(
                    target,
                    expectedName,
                    expectedCategories: new List <Category>(),
                    expectedReadOnlyFlag: false);
            }
        }
        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");
            }
        }
        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"));
            }
        }
        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"));
            }
        }
        public void AddTest()
        {
            // code already exists
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    2.0
                };

                ExceptionAssert.Throw(
                    () => { target.Add(2.0, "second two"); },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CAT_ALREADY_EXISTS_IN_VARIABLE_LIST"));
            }

            // label already exists
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 2.0, "two" }
                };

                ExceptionAssert.Throw(
                    () => { target.Add(2.000001, "two"); },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CAT_ALREADY_EXISTS_IN_VARIABLE_LIST"));
            }

            // label is null
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name);

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

            // label is white space
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name);

                ArgumentExceptionAssert.Throw(
                    () => { target.Add(3.0, " "); },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_STRING_IS_EMPTY_OR_WHITESPACE"),
                    expectedParameterName: "label");
            }

            // label is empty
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name);

                ArgumentExceptionAssert.Throw(
                    () => { target.Add(3.0, string.Empty); },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_STRING_IS_EMPTY_OR_WHITESPACE"),
                    expectedParameterName: "label");
            }

            // target is read only
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name);

                target.SetAsReadOnly();

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

            // Valid input
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name);

                double   code;
                string   label;
                Category category0, category1;

                code  = 1.0;
                label = "one";
                target.Add(code, label);
                category0 = new Category(code, label);

                code  = 2.0;
                label = "two";
                target.Add(code, label);
                category1 = new Category(code, label);

                CategoricalVariableAssert.IsStateAsExpected(
                    target,
                    name,
                    expectedCategories: new List <Category>(2)
                {
                    category0,
                    category1
                },
                    expectedReadOnlyFlag: false);
            }
        }