コード例 #1
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void BothAreNull_None_ReturenEmpty()
        {
            string str1 = null;
            string str2 = null;

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.IsTrue(!diff.Any());
        }
コード例 #2
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void DstIsNull_None_ReturenEmpty()
        {
            string str1 = null;
            var    str2 = string.Empty;

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.IsTrue(!diff.Any());
        }
コード例 #3
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void BothAreEmpty_ReturenEmpty()
        {
            string str1 = string.Empty;
            string str2 = string.Empty;

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.IsTrue(!diff.Any());
        }
コード例 #4
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void CompletelyDifferent_None_ResultIsCorrect()
        {
            var str1 = "aaa";
            var str2 = "bbb";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(2).Status);
        }
コード例 #5
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        private void EqualSeq_None_ResultIsCorrect(DiffOrderType orderType)
        {
            var str1 = "abcd";
            var str2 = "abcd";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(str1.Count(), diff.Count());
            Assert.IsTrue(diff.All(r => r.Status == DiffStatus.Equal));
            Assert.IsTrue(diff.Select(r => r.Obj1).SequenceEqual(str1.Select(c => c)));
            Assert.IsTrue(diff.Select(r => r.Obj2).SequenceEqual(str1.Select(c => c)));
        }
コード例 #6
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void Popped_None_ResultIsCorrect()
        {
            var str1 = "abc_";
            var str2 = "abc";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(2).Status);
            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(3).Status);
        }
コード例 #7
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void RondomString_None_EqualToRestoredString()
        {
            var str1 = "q4DU8sbeD4JdhFA4hWShCv3bbtD7djX5SaNnQUHJHdCEJs6X2LJipbEEr7bZZbzcUrpuKpRDKNz92x5P";
            var str2 = "3GKLWNDdCxip8kda2r2MUT45RrHUiESQhmhUZtMcpBGcSwJVS9uq4DWBAQk2zPUJCJabaeWuP5mxyPBz";

            var diff = DiffUtil.OptimizedDiff(str1, str2);
            var src  = new string(diff.CreateSrc().ToArray());
            var dst  = new string(diff.CreateDst().ToArray());

            Assert.AreEqual(src, str1);
            Assert.AreEqual(dst, str2);
        }
コード例 #8
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void ModifiedSeries_None_ResultIsCorrect()
        {
            string str1 = "abbbc";
            string str2 = "a___c";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(2).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(3).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(4).Status);
        }
コード例 #9
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void DeletedNextToModified_None_ResultIsCorrect()
        {
            string str1 = "abcdef";
            string str2 = "ab_ef";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(2).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(3).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(4).Status);
        }
コード例 #10
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void DstIsEmpty_None_NoException()
        {
            string str1 = "abced";
            string str2 = "";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(2).Status);
            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(3).Status);
            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(4).Status);
        }
コード例 #11
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void AppendAndShifted_None_ResultIsCorrect()
        {
            var str1 = "_aaa";
            var str2 = "aaa_";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(2).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(3).Status);
            Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(4).Status);
        }
コード例 #12
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void ModifiedBetweenInsertedAndInserted_None_ResultIsCorrect()
        {
            string str1 = "abcde";
            string str2 = "ab_g_de";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(2).Status);
            Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(3).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(4).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(5).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(6).Status);
        }
コード例 #13
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void ModifiedAlternately_None_ResultIsCorrect()
        {
            var str1 = "abcdefg";
            var str2 = "a_c_e_g";

            var diff = DiffUtil.OptimizedDiff(str1, str2);

            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(2).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(3).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(4).Status);
            Assert.AreEqual(DiffStatus.Modified, diff.ElementAt(5).Status);
            Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(6).Status);
        }
コード例 #14
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void DstContainsUppercase_IgnoreCaseCompare_ResultIsCorrect()
        {
            var str1 = "abc";
            var str2 = "dBf";

            var option = new DiffOption <char>
            {
                EqualityComparer = new IgnoreCaseCompare()
            };

            var opt = DiffUtil.OptimizedDiff(str1, str2, option);

            Assert.AreEqual(DiffStatus.Modified, opt.ElementAt(0).Status);
            Assert.AreEqual(DiffStatus.Equal, opt.ElementAt(1).Status);
            Assert.AreEqual(DiffStatus.Modified, opt.ElementAt(2).Status);
        }
コード例 #15
0
ファイル: ExcelSheet.cs プロジェクト: Dacheng-Wang/ExcelMerge
        private static Dictionary <int, ExcelColumnStatus> CreateColumnStatusMap(
            IEnumerable <ExcelColumn> srcColumns, IEnumerable <ExcelColumn> dstColumns, ExcelSheetDiffConfig config)
        {
            var option = new DiffOption <ExcelColumn>();

            if (config.SrcHeaderIndex >= 0)
            {
                option.EqualityComparer = new HeaderComparer();
                foreach (var sc in srcColumns)
                {
                    sc.HeaderIndex = config.SrcHeaderIndex;
                }
            }

            if (config.DstHeaderIndex >= 0)
            {
                foreach (var dc in dstColumns)
                {
                    dc.HeaderIndex = config.DstHeaderIndex;
                }
            }

            var results     = DiffUtil.OptimizedDiff(srcColumns, dstColumns, option);
            var ret         = new Dictionary <int, ExcelColumnStatus>();
            var columnIndex = 0;

            foreach (var result in results)
            {
                var status = ExcelColumnStatus.None;
                if (result.Status == DiffStatus.Deleted)
                {
                    status = ExcelColumnStatus.Deleted;
                }
                else if (result.Status == DiffStatus.Inserted)
                {
                    status = ExcelColumnStatus.Inserted;
                }

                ret.Add(columnIndex, status);
                columnIndex++;
            }

            return(ret);
        }
コード例 #16
0
ファイル: Test.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public void None_None_LimitOptionWorks()
        {
            var str1 = Enumerable.Repeat("Good dog", 1000).SelectMany(c => c);
            var str2 = Enumerable.Repeat("Bad dog", 1000).SelectMany(c => c);

            var option = new DiffOption <char>
            {
                Limit = 1000
            };

            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            DiffUtil.OptimizedDiff(str1, str2);
            sw.Stop();
            var time1 = sw.Elapsed;

            sw.Restart();
            DiffUtil.OptimizedDiff(str1, str2, option);
            sw.Stop();
            var time2 = sw.Elapsed;

            Assert.IsTrue(time2 < time1);
        }
コード例 #17
0
ファイル: ExcelSheet.cs プロジェクト: Dacheng-Wang/ExcelMerge
        public static ExcelSheetDiff Diff(ExcelSheet src, ExcelSheet dst, ExcelSheetDiffConfig config)
        {
            var srcColumns      = src.CreateColumns();
            var dstColumns      = dst.CreateColumns();
            var columnStatusMap = CreateColumnStatusMap(srcColumns, dstColumns, config);

            var option = new DiffOption <ExcelRow>();

            option.EqualityComparer =
                new RowComparer(new HashSet <int>(columnStatusMap.Where(i => i.Value != ExcelColumnStatus.None).Select(i => i.Key)));

            foreach (var row in src.Rows.Values)
            {
                var shifted = new List <ExcelCell>();
                var index   = 0;
                var queue   = new Queue <ExcelCell>(row.Cells);
                while (queue.Any())
                {
                    if (columnStatusMap[index] == ExcelColumnStatus.Inserted)
                    {
                        shifted.Add(new ExcelCell(string.Empty, 0, 0));
                    }
                    else
                    {
                        shifted.Add(queue.Dequeue());
                    }

                    index++;
                }

                row.UpdateCells(shifted);
            }

            foreach (var row in dst.Rows.Values)
            {
                var shifted = new List <ExcelCell>();
                var index   = 0;
                var queue   = new Queue <ExcelCell>(row.Cells);
                while (queue.Any())
                {
                    if (columnStatusMap[index] == ExcelColumnStatus.Deleted)
                    {
                        shifted.Add(new ExcelCell(string.Empty, 0, 0));
                    }
                    else
                    {
                        shifted.Add(queue.Dequeue());
                    }

                    index++;
                }

                row.UpdateCells(shifted);
            }

            var resultArray = DiffUtil.OptimizedDiff(src.Rows.Values, dst.Rows.Values, option).ToArray();

            if (resultArray.Length > 10000)
            {
                var count   = 0;
                var indices = Enumerable.Range(0, 100).ToList();
                foreach (var result in resultArray)
                {
                    if (result.Status != DiffStatus.Equal)
                    {
                        indices.AddRange(Enumerable.Range(Math.Max(0, count - 100), 200));
                    }

                    count++;
                }
                indices     = indices.Distinct().ToList();
                resultArray = indices.Where(i => i < resultArray.Length).Select(i => resultArray[i]).ToArray();
            }

            var sheetDiff = new ExcelSheetDiff(src, dst);

            DiffCells(resultArray, sheetDiff, columnStatusMap);

            return(sheetDiff);
        }