Example #1
0
        /// <summary>
        /// Creates a new instance of <see cref="ErrorSet"/>
        /// </summary>
        /// <param name="valueMapper">The value mapper to use to encode values</param>
        /// <param name="axisNames">THe set of axes that can be used to key errors on</param>
        public ErrorSet(ValueMapper valueMapper, string [] axisNames)
        {
            if (valueMapper == null)
            {
                throw new ArgumentNullException(nameof(valueMapper));
            }

            if (axisNames == null)
            {
                throw new ArgumentNullException(nameof(axisNames));
            }

            this._valueMapper = valueMapper;
            this._axisNames   = axisNames;

            this._errorsDictionary = new Dictionary.HybridErrorsDictionary(_axisNames);
        }
Example #2
0
        public void FilteredEnumerator()
        {
            var axisNames  = new string [] { "Axis 1", "Axis 2", "Axis 3" };
            var dictionary = new HybridErrorsDictionary(axisNames);

            var allErrors = new Dictionary <IDictionary <string, uint>, string>(new DictionaryEqualityComparer());

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 8;
                keys ["Axis 2"] = 3;
                keys ["Axis 3"] = 4;

                allErrors.Add(keys, "(8,3,4)");
            }

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 5;
                keys ["Axis 3"] = 4;
                allErrors.Add(keys, "(5,-,4)");
            }


            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 3"] = 4;
                allErrors.Add(keys, "(-,-,4)");
            }

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 2;
                allErrors.Add(keys, "(2,-,-)");
            }

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 2;
                keys ["Axis 2"] = 7;
                allErrors.Add(keys, "(2,7,-)");
            }

            foreach (var error in allErrors)
            {
                dictionary.AddItem(error.Key, error.Value);
            }


            var filter = new Dictionary <string, uint?>();

            filter ["Axis 2"] = 3;

            var filteredErrors = new Dictionary <IDictionary <string, uint>, string>(new DictionaryEqualityComparer());

            foreach (var error in allErrors)
            {
                bool include = true;
                foreach (var filterPair in filter)
                {
                    if (error.Key.ContainsKey(filterPair.Key) &&
                        error.Key [filterPair.Key] != filterPair.Value)
                    {
                        include = false;
                        break;
                    }
                }

                if (include)
                {
                    filteredErrors.Add(error.Key, error.Value);
                }
            }

            var enumerator = dictionary.GetEnumerator(filter);

            while (enumerator.MoveNext())
            {
                var entry = enumerator.Current;
                var keys  = new Dictionary <string, uint>();
                for (int i = 0; i < entry.Key.Length; i++)
                {
                    if (entry.Key [i].HasValue)
                    {
                        keys [axisNames [i]] = entry.Key [i].Value;
                    }
                }

                string expectedErrorMessage;
                Assert.IsTrue(filteredErrors.TryGetValue(keys, out expectedErrorMessage));
                Assert.AreEqual(expectedErrorMessage, entry.Value);

                Assert.IsTrue(filteredErrors.Remove(keys));
            }

            Assert.AreEqual(0,
                            filteredErrors.Count,
                            $"Missing error messages - '{string.Join( "', '", filteredErrors.Select( pair => pair.Value ) )}'");
        }
Example #3
0
        public void StandardEnumerator()
        {
            var axisNames  = new string [] { "Axis 1", "Axis 2", "Axis 3" };
            var dictionary = new HybridErrorsDictionary(axisNames);

            var allErrors = new Dictionary <IDictionary <string, uint>, string>(new DictionaryEqualityComparer());

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 8;
                keys ["Axis 2"] = 3;
                keys ["Axis 3"] = 4;

                allErrors.Add(keys, "(8,3,4)");
            }

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 5;
                keys ["Axis 3"] = 4;
                allErrors.Add(keys, "(5,-,4)");
            }


            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 3"] = 4;
                allErrors.Add(keys, "(-,-,4)");
            }

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 2;
                allErrors.Add(keys, "(2,-,-)");
            }

            {
                var keys = new Dictionary <string, uint>();
                keys ["Axis 1"] = 2;
                keys ["Axis 2"] = 7;
                allErrors.Add(keys, "(2,7,-)");
            }

            foreach (var error in allErrors)
            {
                dictionary.AddItem(error.Key, error.Value);
            }

            var enumerator = dictionary.GetEnumerator(null);

            while (enumerator.MoveNext())
            {
                var entry = enumerator.Current;

                var keys = new Dictionary <string, uint>();
                for (int i = 0; i < entry.Key.Length; i++)
                {
                    if (entry.Key [i].HasValue)
                    {
                        keys [axisNames [i]] = entry.Key [i].Value;
                    }
                }

                Assert.IsTrue(allErrors.TryGetValue(keys, out var expectedErrorMessage));
                Assert.AreEqual(expectedErrorMessage, entry.Value);

                allErrors.Remove(keys);
            }

            Assert.AreEqual(0,
                            allErrors.Count,
                            $"Missing error messages - '{string.Join( "', '", allErrors.Select( pair => pair.Value ) )}'");
        }