Пример #1
0
        public void AlphaNumericSorter_SortsNumbersWithChars()
        {
            var comp = new AlphaNumericComparator();

            Assert.IsTrue(comp.Compare("2", "10") < 0);
            Assert.IsTrue(comp.Compare("2a", "2") > 0);
            Assert.IsTrue(comp.Compare("3", "2a") > 0);
            Assert.IsTrue(comp.Compare("003", "3") == 0);
            Assert.IsTrue(comp.Compare("0000", "00000000000000000001") < 0);
        }
        public void AlphaNumericSorter_SortsNumbersWithChars()
        {
            var comp = new AlphaNumericComparator();

            Assert.IsTrue(comp.Compare("2", "10") < 0);
            Assert.IsTrue(comp.Compare("2a", "2") > 0);
            Assert.IsTrue(comp.Compare("3", "2a") > 0);
            Assert.IsTrue(comp.Compare("003", "3") == 0);
            Assert.IsTrue(comp.Compare("0000", "00000000000000000001") < 0);
        }
Пример #3
0
        public static bool WithCondition(this string @this,
            string keyword,
            SearchCondition condition)
        {
            var comparator = new AlphaNumericComparator();
            switch (condition)
            {
                case SearchCondition.Contains:
                    return @this.Contains(keyword);
                case SearchCondition.NotContains:
                    return [email protected](keyword);
                case SearchCondition.MoreThan:
                    return comparator.Compare(@this, keyword) == 1;
                case SearchCondition.LessThan:
                    return comparator.Compare(@this, keyword) == -1;
                case SearchCondition.EqualTo:
                    return comparator.Compare(@this, keyword) == 0;
                case SearchCondition.NotEqualTo:
                    return comparator.Compare(@this, keyword) != 0;
                case SearchCondition.LongerThan:
                    if (int.TryParse(keyword, out var longer))
                        return @this.Length > longer;

                    throw new ArgumentException($@"Unable to parse {nameof(keyword)} to number.", nameof(keyword));
                case SearchCondition.ShorterOrEqual:
                    if (int.TryParse(keyword, out var shorter))
                        return @this.Length <= shorter;

                    throw new ArgumentException($@"Unable to parse {nameof(keyword)} to number.", nameof(keyword));
                case SearchCondition.HasValue:
                    return @this.IsNotNullOrEmpty();
                case SearchCondition.IsEmpty:
                    return @this.IsNullOrEmpty();
                case SearchCondition.StartsWith:
                    return @this.StartsWith(keyword);
                case SearchCondition.DoesNotStartWith:
                    return [email protected](keyword);
                case SearchCondition.EndsWith:
                    return @this.EndsWith(keyword);
                case SearchCondition.DoesNotEndWith:
                    return [email protected](keyword);
                case SearchCondition.Undefined:
                default:
                    throw new ArgumentOutOfRangeException(nameof(condition), condition, null);
            }
        }
Пример #4
0
        public void AlphaNumericSorter_SortsNumbersOnly()
        {
            var comparator = new AlphaNumericComparator();

            var items =
                StringArrayFromObjects(1, 2, 6, 4, 7, 10, 5, 3, 9);

            var expected =
                StringArrayFromObjects(1, 2, 3, 4, 5, 6, 7, 9, 10);

            Trace.WriteLine(String.Format("Items: {0}", String.Join(", ", items)));
            Trace.WriteLine(String.Format("Expected: {0}", String.Join(", ", expected)));

            Array.Sort(items, comparator);

            Assert.IsTrue(expected.SequenceEqual(items));
        }
        public void AlphaNumericSorter_SortsNumbersOnly()
        {
            var comparator = new AlphaNumericComparator();

            var items = 
                StringArrayFromObjects(1, 2, 6, 4, 7, 10, 5, 3, 9);

            var expected = 
                StringArrayFromObjects(1, 2, 3, 4, 5, 6, 7, 9, 10);

            Trace.WriteLine(String.Format("Items: {0}", String.Join(", ", items)));
            Trace.WriteLine(String.Format("Expected: {0}", String.Join(", ", expected)));

            Array.Sort(items, comparator);

            Assert.IsTrue(expected.SequenceEqual(items));
        }