Esempio n. 1
0
        public void Max_Throws_If_Argument_Is_Null()
        {
            var source   = Enumerable.Empty <Foo>();
            var comparer = XComparer.By(_getFooValue);

            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            TestHelper.AssertThrowsWhenArgumentNull(() => source.Max(comparer));
        }
Esempio n. 2
0
        public void Find_1()
        {
            string book   = File.ReadAllText(@"Resources\Text1.txt");
            var    sample = "Шаблон проектирования программы в виде трех слабо связанных между собой частей: модели, представления и контроллера известен в программировании давно";
            int    res    = XComparer.Find(book, sample, 0);

            Assert.AreEqual(259140, res);
        }
Esempio n. 3
0
        public void Find_2()
        {
            int    L = 10000, D = 1000;
            string book   = File.ReadAllText(@"Resources\Text1.txt");
            var    sample = book.Substring(L, D);
            int    res    = XComparer.Find(book, sample, 0);

            Assert.AreEqual(L, res);
        }
Esempio n. 4
0
        public void ExtFinish_3()
        {
            var book = "0001234567890123456789";
            var manu = "00034567890123400";
            var xc   = new XComparer(manu, 5);
            int res  = xc.ExtFinish(book, 12, 10);

            Assert.AreEqual(15, res);
        }
Esempio n. 5
0
        public void FromComparison_Returns_A_Comparer_Based_On_A_Comparison()
        {
            var a = new Foo("foo");
            var b = new Foo("bar");

            var comparer = XComparer <Foo> .FromComparison((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));

            comparer.Compare(a, b).Should().BeGreaterThan(0);
        }
Esempio n. 6
0
        public void ExtStart_3()
        {
            var book = "0001234567890123456789";
            var manu = "01234567890123400";
            var xc   = new XComparer(manu, 5);
            int res  = xc.ExtStart(book, 7, 5);

            Assert.AreEqual(0, res);
        }
Esempio n. 7
0
        public void Min_Throws_If_Source_Is_Null()
        {
            IEnumerable <Foo> source = null;
            var comparer             = XComparer.By(_getFooValue);
            // ReSharper disable once AssignNullToNotNullAttribute
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            var ex = Assert.Throws <ArgumentNullException>(() => source.Min(comparer));

            ex.ParamName.Should().Be("source");
        }
Esempio n. 8
0
        public void Compare_1()
        {
            var manu = "00034567890123400";
            var book = "01234567890123456789";
            var xc   = new XComparer(manu, 5);
            var res  = xc.Compare(book);

            Assert.AreEqual(3, res[0].ManStart);
            Assert.AreEqual(12, res[0].Length);
        }
Esempio n. 9
0
        public void Compare_4()
        {
            int    L = 10000, D = 1000;
            string book = File.ReadAllText(@"Resources\Text1.txt");
            var    manu = new string('0', L) + book.Substring(L, D) + new string('0', L);
            var    xc   = new XComparer(manu, 100);
            var    res  = xc.Compare(book);

            Assert.AreEqual(L, res[0].ManStart);
            Assert.AreEqual(D, res[0].Length);
        }
Esempio n. 10
0
        public int CompareTo(object obj)
        {
            var o = obj as P;

            if (o == null)
            {
                return(-1);
            }

            return(XComparer.XCompare(this, o)); // default
        }
Esempio n. 11
0
        public void By_Returns_A_Comparer_Based_On_Key()
        {
            var a = new Foo {
                X = 0, Y = 0
            };
            var b = new Foo {
                X = 1, Y = 0
            };

            Func <Foo, int> keySelector = f => f.X;
            var             comparer    = XComparer.By(keySelector);

            int expected = -1;
            int actual   = comparer.Compare(a, b);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 12
0
        public void ThenByDescending_Returns_A_Comparer_Based_On_First_Then_Descending_Second_Key()
        {
            var a = new Foo {
                X = 0, Y = 0
            };
            var b = new Foo {
                X = 0, Y = 1
            };

            Func <Foo, int> keySelector1 = f => f.X;
            Func <Foo, int> keySelector2 = f => f.Y;
            var             comparer     = XComparer.By(keySelector1).ThenByDescending(keySelector2);

            int expected = 1;
            int actual   = comparer.Compare(a, b);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 13
0
        public void RankBy_Uses_Specified_Key_Comparer()
        {
            var source = new[]
            {
                new Player("Alice", -42),
                new Player("Bob", -25),
                new Player("Charlie", 75),
                new Player("David", -17),
                new Player("Emily", 31)
            }.ForbidMultipleEnumeration();
            var keyComparer = XComparer <int> .By(Math.Abs);

            var result = source.RankBy(p => p.Score, (player, rank) => $"{rank}. {player.Name}", keyComparer);

            result.Should().Equal(
                "1. David",
                "2. Bob",
                "3. Emily",
                "4. Alice",
                "5. Charlie");
        }
Esempio n. 14
0
        public void ChainWith_Uses_Next_Comparer_If_First_Returns_Zero()
        {
            var a = new Foo {
                X = 0, Y = 0
            };
            var b = new Foo {
                X = 0, Y = 1
            };

            var first = XComparer <Foo> .By(f => f.X);

            var next     = Intercept(XComparer <Foo> .By(f => f.Y));
            var comparer = first.ChainWith(next);

            int expected = -1;
            int actual   = comparer.Compare(a, b);

            Assert.AreEqual(expected, actual);

            Assert.AreEqual(1, next.CallCount);
        }
Esempio n. 15
0
        private int Analize()
        {
            Book manual = new Book(name: "", source: File.ReadAllText(manualPath));

            XComparer comparer = new XComparer(manual.Digest, GetFrigmentSize());

            string rootPath = Path.GetDirectoryName(manualPath);

            int max = 0;

            reportBox.Items.Clear();
            foreach (string bookPath in Directory.GetFiles(rootPath, "*.txt"))
            {
                if (bookPath == manualPath)
                {
                    continue;
                }

                Book book = new Book(
                    name: Path.GetFileName(bookPath),
                    source: File.ReadAllText(bookPath));

                var report = comparer.Compare(book.Digest);

                foreach (var reportItem in report)
                {
                    reportItem.Book   = book;
                    reportItem.Manual = manual;

                    reportBox.Items.Add(reportItem);
                }

                if (report.Count > 0)
                {
                    max = Math.Max(max, report.Max(r => r.Length));
                }
            }
            return(max);
        }
Esempio n. 16
0
        public void Chaining_Multiple_Comparers_Works_As_Well()
        {
            var a = new Foo {
                X = 0, Y = 0, Z = 1
            };
            var b = new Foo {
                X = 0, Y = 0, Z = 0
            };

            var first = XComparer <Foo> .By(f => f.X);

            var second = XComparer <Foo> .By(f => f.Y);

            var third = XComparer <Foo> .By(f => f.Z);

            // Test various ways of chaining

            // (first.second).third
            var comparer = first.ChainWith(second).ChainWith(third);
            int expected = 1;
            int actual   = comparer.Compare(a, b);

            Assert.AreEqual(expected, actual);


            // first.(second.third)
            comparer = first.ChainWith(second.ChainWith(third));
            expected = 1;
            actual   = comparer.Compare(a, b);
            Assert.AreEqual(expected, actual);

            // (first.second).(first.second)
            comparer = first.ChainWith(second).ChainWith(first.ChainWith(second));
            expected = 0;
            actual   = comparer.Compare(a, b);
            Assert.AreEqual(expected, actual);
        }
Esempio n. 17
0
 public static IComparer <T> ByDescending <TKey>(
     [NotNull] Func <T, TKey> keySelector,
     IComparer <TKey>?keyComparer = null)
 {
     return(XComparer.ByDescending(keySelector, keyComparer));
 }
Esempio n. 18
0
 /// <summary>
 /// Creates a comparer that uses the specified <see cref="Comparison{T}"/> delegate to compare objects.
 /// </summary>
 /// <param name="comparison">The <see cref="Comparison{T}"/> delegate used to compare objects.</param>
 /// <returns>A comparer that uses the specified <see cref="Comparison{T}"/> delegate to compare objects.</returns>
 public static IComparer <T> FromComparison([NotNull] Comparison <T> comparison)
 {
     return(XComparer.FromComparison(comparison));
 }