public void TestToTooFewFields()
        {
            var          pi           = new ProjectItems((p, i) => new ProjectItems.SelfOptimizingPrefixTrieProjector(p, i, 2, "prefixTrie"));
            var          gc           = new GlobalContext();
            const string THREE_FIELDS = "THREE_FIELDS";

            pi.Configure(gc, $@"{{ -pl
    $ {THREE_FIELDS}(F1:F2:F3) ---% {THREE_FIELDS}

    ! a:b:c ---% ::
    ! a:b   ---% : // this threw an exception before a fix
    ! a    ---%
}}", forceReload: false);

            ItemType     threeFields = ItemType.Find(THREE_FIELDS);
            WorkingGraph graph       = gc.CurrentGraph;
            Item         abc         = graph.CreateItem(threeFields, "a:b:c");
            Item         ab          = graph.CreateItem(threeFields, "a:b:-");
            Item         a           = graph.CreateItem(threeFields, "a:-:-");

            var result = new List <Dependency>();

            pi.Transform(gc, new[] {
                graph.CreateDependency(abc, abc, null, "abc", 1),
                graph.CreateDependency(ab, ab, null, "ab", 1),
                graph.CreateDependency(a, a, null, "a", 1),
            }, "", result, s => null);

            Assert.AreEqual(0, result.Count);
        }
        public void TestMarkLaterCycleWithExplicitAsserts()
        {
            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            var a    = graph.CreateItem(ItemType.SIMPLE, "a");
            var b    = graph.CreateItem(ItemType.SIMPLE, "b");
            var c    = graph.CreateItem(ItemType.SIMPLE, "c");
            var d    = graph.CreateItem(ItemType.SIMPLE, "d");
            var e    = graph.CreateItem(ItemType.SIMPLE, "e");
            var deps = new[] {
                graph.CreateDependency(a, b, null, "", 1),
                graph.CreateDependency(b, c, null, "", 1),
                graph.CreateDependency(c, d, null, "", 1),
                graph.CreateDependency(d, e, null, "", 1),
                graph.CreateDependency(e, c, null, "", 1),
            };
            var result = new List <Dependency>();

            new MarkCycleDeps().Transform(gc, Ignore.Om,
                                          new MarkCycleDeps.TransformOptions {
                KeepOnlyCycleDependencies = true
            }, deps, result);

            result.Sort((x, y) => string.Compare(x.UsingItemAsString, y.UsingItemAsString, StringComparison.Ordinal));

            Assert.AreEqual(3, result.Count);
            Assert.AreEqual(c, result[0].UsingItem);
            Assert.AreEqual(1, result[0].BadCt);
            Assert.AreEqual(d, result[1].UsingItem);
            Assert.AreEqual(1, result[1].BadCt);
            Assert.AreEqual(e, result[2].UsingItem);
            Assert.AreEqual(1, result[2].BadCt);
        }
        private static List <Dependency> FindLaterCycle(string cycleMarkerPrefix)
        {
            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            var a    = graph.CreateItem(ItemType.SIMPLE, "a");
            var b    = graph.CreateItem(ItemType.SIMPLE, "b");
            var c    = graph.CreateItem(ItemType.SIMPLE, "c");
            var d    = graph.CreateItem(ItemType.SIMPLE, "d");
            var e    = graph.CreateItem(ItemType.SIMPLE, "e");
            var deps = new[] {
                graph.CreateDependency(a, b, null, "", 1),
                graph.CreateDependency(b, c, null, "", 1),
                graph.CreateDependency(c, d, null, "", 1),
                graph.CreateDependency(d, e, null, "", 1),
                graph.CreateDependency(e, c, null, "", 1),
            };
            var result = new List <Dependency>();

            new MarkCycleDeps().Transform(gc, Ignore.Om, new MarkCycleDeps.TransformOptions {
                IndexedMarkerPrefix = cycleMarkerPrefix
            }, deps, result);

            result.Sort((x, y) => string.Compare(x.UsingItemAsString, y.UsingItemAsString, StringComparison.Ordinal));
            return(result);
        }
Exemple #4
0
        public void TestMarkTrivialCut()
        {
            var          gc           = new GlobalContext();
            WorkingGraph graph        = gc.CurrentGraph;
            Item         a            = graph.CreateItem(ItemType.SIMPLE, "a");
            Item         b            = graph.CreateItem(ItemType.SIMPLE, "b");
            Item         c            = graph.CreateItem(ItemType.SIMPLE, "c");
            Item         d            = graph.CreateItem(ItemType.SIMPLE, "d");
            var          dependencies = new[] {
                graph.CreateDependency(a, b, null, "D10", 10, 0, 4, notOkReason: "test data"),
                graph.CreateDependency(b, c, null, "D20", 20, 0, 2, notOkReason: "test data"), // critical edge
                graph.CreateDependency(c, d, null, "D30", 30, 0, 1, notOkReason: "test data"),
                graph.CreateDependency(c, d, null, "D40", 40, 0, 3, notOkReason: "test data"),
            };

            const string             mark   = "CUT";
            IEnumerable <Dependency> result = Run(gc,
                                                  new MarkMinimalCutDeps.TransformOptions {
                SourceMatches = new List <ItemMatch> {
                    new ItemMatch("a", false, anyWhereMatcherOk: true)
                },
                TargetMatches = new List <ItemMatch> {
                    new ItemMatch("d", false, anyWhereMatcherOk: true)
                },
                MarkerToAddToCut = mark
            }, dependencies);

            Assert.IsTrue(result.All(z => z.MarkersContain(mark) == (z.Ct == 20)), string.Join("\r\n", result.Select(z => z.AsLimitableStringWithTypes(withExampleInfo: false, threeLines: false))));
        }
Exemple #5
0
        public void TestLoopIsNotEndless()
        {
            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;
            Item         a     = graph.CreateItem(ItemType.SIMPLE, "a");
            Item         b     = graph.CreateItem(ItemType.SIMPLE, "b");
            Item         c     = graph.CreateItem(ItemType.SIMPLE, "c");
            Item         d     = graph.CreateItem(ItemType.SIMPLE, "d");

            graph.AddDependencies(new[] {
                graph.CreateDependency(a, b, null, "", 1),
                // Loop on b & c; but we never reach d!
                graph.CreateDependency(b, c, null, "", 1),
                graph.CreateDependency(c, b, null, "", 1),

                // Loop on d so that it remains in graph
                graph.CreateDependency(d, d, null, "", 1)
            });

            // We want to go from a to d - this will never succeed; but we must make sure that
            // there is no endless loop around b and c.
            var traverser = new TestTraverser(graph.VisibleDependencies, new PathRegex("a.(b.c.)*d"));

            traverser.Traverse(a);

            Assert.AreEqual(0, traverser.RecordedPaths.Count());
        }
Exemple #6
0
        public void TestMarkCutFromMultipleSources()
        {
            var gc = new GlobalContext();

            Dependency[] exampleDependencies = CreateExampleGraph(gc);
            Item         s     = exampleDependencies[0].UsingItem;
            WorkingGraph graph = gc.CurrentGraph;
            Item         r0    = graph.CreateItem(ItemType.SIMPLE, "r0");
            Item         r1    = graph.CreateItem(ItemType.SIMPLE, "r1");
            Item         r2    = graph.CreateItem(ItemType.SIMPLE, "r2");

            Dependency[] dependencies = exampleDependencies.Concat(new[] {
                graph.CreateDependency(r0, s, null, "r0_s", 1000, 0, 1000, notOkReason: "test data"),
                graph.CreateDependency(r1, s, null, "r1_s", 1000, 0, 1000, notOkReason: "test data"),
                graph.CreateDependency(r2, s, null, "r2_s", 1000, 0, 1000, notOkReason: "test data"),
            }).ToArray();

            const string             mark   = "CUT";
            IEnumerable <Dependency> result = Run(gc, new MarkMinimalCutDeps.TransformOptions {
                SourceMatches = new List <ItemMatch> {
                    new ItemMatch("r*", false, anyWhereMatcherOk: true)
                },
                TargetMatches = new List <ItemMatch> {
                    new ItemMatch("t", false, anyWhereMatcherOk: true)
                },
                MarkerToAddToCut = mark
            }, dependencies);

            Assert.IsTrue(result.All(z => z.MarkersContain(mark) == new[] { 12, 36, 78 }.Contains(z.Ct)),
                          string.Join("\r\n", result.Select(z => z.AsLimitableStringWithTypes(withExampleInfo: false, threeLines: false))));
        }
            public override IEnumerable <Dependency> CreateSomeTestDependencies(WorkingGraph renderingGraph)
            {
                ItemType simple = ItemType.New("SIMPLE(Name)");
                Item     i1     = renderingGraph.CreateItem(simple, "I1");
                Item     i2     = renderingGraph.CreateItem(simple, "I2");

                return(new[] { renderingGraph.CreateDependency(i1, i1, new TextFileSourceLocation("Test", 1), "Test", ct: 1),
                               renderingGraph.CreateDependency(i1, i2, new TextFileSourceLocation("Test", 2), "Test", ct: 1) });
            }
        private static List <Dependency> CreateDependenciesAndFindCycles(GlobalContext gc, Item a, Item b, Item c, Item d, Item e, bool keepOnlyCyclesOption, string markerPrefix)
        {
            WorkingGraph graph = gc.CurrentGraph;

            var deps = new[] {
                // "Confusing" edges to sink b
                graph.CreateDependency(a, b, null, "", 1),
                graph.CreateDependency(b, b, null, "", 1),
                graph.CreateDependency(c, b, null, "", 1),
                graph.CreateDependency(d, b, null, "", 1),

                // a->c->d->e
                graph.CreateDependency(a, c, null, "", 1),
                graph.CreateDependency(c, d, null, "", 1),
                graph.CreateDependency(d, e, null, "", 1),

                // Cycle edges c<-d and a<-e
                graph.CreateDependency(d, c, null, "", 1),
                graph.CreateDependency(e, a, null, "", 1),
            };
            var result = new List <Dependency>();

            new MarkCycleDeps().Transform(gc, Ignore.Om,
                                          new MarkCycleDeps.TransformOptions {
                KeepOnlyCycleDependencies = keepOnlyCyclesOption,
                IndexedMarkerPrefix       = markerPrefix
            },
                                          deps, result);

            result.Sort((x, y) => string.Compare(x.UsingItemAsString, y.UsingItemAsString, StringComparison.Ordinal));
            return(result);
        }
Exemple #9
0
        public override IEnumerable <Dependency> CreateSomeTestDependencies(WorkingGraph transformingGraph)
        {
            Item a = transformingGraph.CreateItem(ItemType.SIMPLE, "A");
            Item b = transformingGraph.CreateItem(ItemType.SIMPLE, "B");

            return(new[] {
                transformingGraph.CreateDependency(a, a, source: null, markers: "", ct: 10, questionableCt: 5, badCt: 3, notOkReason: "test"),
                transformingGraph.CreateDependency(a, b, source: null, markers: "use+define", ct: 1, questionableCt: 0, badCt: 0),
                transformingGraph.CreateDependency(b, a, source: null, markers: "define", ct: 5, questionableCt: 0, badCt: 2, notOkReason: "test"),
            });
        }
Exemple #10
0
        public override IEnumerable <Dependency> CreateSomeTestDependencies(WorkingGraph transformingGraph)
        {
            Item am = transformingGraph.CreateItem(ItemType.SIMPLE, new[] { "A" }, new[] { "M" });
            Item bm = transformingGraph.CreateItem(ItemType.SIMPLE, new[] { "B" }, new[] { "M" });
            Item cn = transformingGraph.CreateItem(ItemType.SIMPLE, new[] { "C" }, new[] { "N" });

            return(new[] {
                transformingGraph.CreateDependency(am, am, source: null, markers: "", ct: 10, questionableCt: 5, badCt: 3, notOkReason: "test"),
                transformingGraph.CreateDependency(am, bm, source: null, markers: "use+define", ct: 1, questionableCt: 0, badCt: 0),
                transformingGraph.CreateDependency(am, cn, source: null, markers: "define", ct: 5, questionableCt: 0, badCt: 2, notOkReason: "test"),
                transformingGraph.CreateDependency(bm, am, source: null, markers: "define", ct: 5, questionableCt: 0, badCt: 2, notOkReason: "test"),
            });
        }
        private static void SmallTestForPrefixOptimizedProjector(Func <Projection[], bool, ProjectItems.IProjector> createProjector)
        {
            var          pi    = new ProjectItems(createProjector);
            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            pi.Configure(gc, @"{ -pl
    $ (Ignore:Name) ---% SIMPLE

    ! :a* ---% A
    > :ab ---% AB
    ! :b* ---% B
    ! :c* ---% C
    ! :** ---% 
}", forceReload: false);

            ItemType generic2 = ItemType.Generic(2, ignoreCase: false);
            Item     a        = graph.CreateItem(generic2, "x:a");
            Item     ab       = graph.CreateItem(generic2, "x:ab");
            Item     ac       = graph.CreateItem(generic2, "x:ac");
            Item     ca       = graph.CreateItem(generic2, "x:ca");
            Item     cb       = graph.CreateItem(generic2, "x:cb");
            Item     s        = graph.CreateItem(generic2, "m:s");
            Item     t        = graph.CreateItem(generic2, "m:t");

            var result = new List <Dependency>();

            pi.Transform(gc, new ProjectItems.ConfigureOptions(), new ProjectItems.TransformOptions(), new[] {
                graph.CreateDependency(a, a, null, "a_a", 1),     // the first surviving dependency
                graph.CreateDependency(a, s, null, "a_s", 1),     // vanishes, because s is not mapped
                graph.CreateDependency(ab, s, null, "ab_s", 1),   // same
                graph.CreateDependency(ca, s, null, "ca_s", 1),   // etc.
                graph.CreateDependency(cb, cb, null, "cb_cb", 1), // the second surviving dependency
                graph.CreateDependency(cb, t, null, "cb_t", 1),   // vanishes, because t is not mapped
                graph.CreateDependency(a, t, null, "a_t", 1),
                graph.CreateDependency(ac, t, null, "ac_t", 1),
                graph.CreateDependency(a, s, null, "a_s", 1),

                // Counts:
                // !a  5
                // >ab 0
                // !ac 1
                // !b  0
                // !ca 1
                // !cb 3
                // !s  4
                // !t  3
            }, result);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("A", result[0].UsingItem.Values[0]);
            Assert.AreEqual("A", result[0].UsedItem.Values[0]);
            Assert.AreEqual("C", result[1].UsingItem.Values[0]);
            Assert.AreEqual("C", result[1].UsedItem.Values[0]);
        }
Exemple #12
0
        public override IEnumerable <Dependency> CreateSomeTestDependencies(WorkingGraph renderingGraph)
        {
            ItemType simple       = ItemType.New("SIMPLE(Name)");
            Item     root         = renderingGraph.CreateItem(simple, "root");
            Item     ok           = renderingGraph.CreateItem(simple, "ok");
            Item     questionable = renderingGraph.CreateItem(simple, "questionable");
            Item     bad          = renderingGraph.CreateItem(simple, "bad");

            return(new[] {
                renderingGraph.CreateDependency(root, ok, new TextFileSourceLocation("Test", 1), "Use", 4, 0, 0, "to root"),
                renderingGraph.CreateDependency(root, questionable, new TextFileSourceLocation("Test", 1), "Use", 4, 1, 0, "to questionable"),
                renderingGraph.CreateDependency(root, bad, new TextFileSourceLocation("Test", 1), "Use", 4, 2, 1, "to bad")
            });
        }
Exemple #13
0
 private Dependency NewDependency(WorkingGraph workingGraph, string usingA, string usingN, string usingC, string usedA, string usedN,
                                  string usedC)
 {
     return(workingGraph.CreateDependency(workingGraph.CreateItem(ITEMTYPE, usingA, usingN, usingC),
                                          workingGraph.CreateItem(ITEMTYPE, usedA, usedN, usedC),
                                          null, "Test", ct: 1));
 }
        public void TestMarkSmallCycle()
        {
            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            var a      = graph.CreateItem(ItemType.SIMPLE, "a");
            var b      = graph.CreateItem(ItemType.SIMPLE, "b");
            var deps   = new[] { graph.CreateDependency(a, b, null, "", 1), graph.CreateDependency(b, a, null, "", 1), };
            var result = new List <Dependency>();

            new MarkCycleDeps().Transform(gc, Ignore.Om, new MarkCycleDeps.TransformOptions(), deps, result);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(1, result[0].BadCt);
            Assert.AreEqual(1, result[1].BadCt);
        }
 private Dependency ToDependency(WorkingGraph readingGraph, Item usedItem, string containerUri)
 {
     return(readingGraph.CreateDependency(UsingItem.ToItem(), usedItem, _sequencePoint == null
                 ? (ISourceLocation) new LocalSourceLocation(containerUri, UsingItem.NamespaceName + "." + UsingItem.ClassName + (string.IsNullOrWhiteSpace(UsingItem.MemberName) ? "" : "." + UsingItem.MemberName))
                 : new ProgramFileSourceLocation(containerUri, _sequencePoint.Document.Url, _sequencePoint.StartLine, _sequencePoint.StartColumn, _sequencePoint.EndLine, _sequencePoint.EndColumn),
                                          Usage.ToString(), 1));
 }
Exemple #16
0
 private Dependency ResolveItemProxies(WorkingGraph readingGraph, Dependency d, Dictionary <Item, Item> itemsDictionary)
 {
     return(d.UsingItem is ItemProxy || d.UsedItem is ItemProxy
         ? readingGraph.CreateDependency(itemsDictionary[d.UsingItem], itemsDictionary[d.UsedItem],
                                         d.Source, d.MarkerSet, d.Ct, d.QuestionableCt, d.BadCt, d.NotOkReason, d.ExampleInfo)
         : d);
 }
            public static IEnumerable <Dependency> CreateSomeTestItems(int n, string prefix, WorkingGraph renderingGraph)
            {
                ItemType simple     = ItemType.New("SIMPLE(Name)");
                var      localItems = Enumerable.Range(0, n).Select(i => renderingGraph.CreateItem(simple, prefix + i)).ToArray();

                return(localItems.SelectMany(
                           (from, i) => localItems.Skip(i).Select(to => renderingGraph.CreateDependency(from, to, new TextFileSourceLocation(prefix, i), "Use", 10 * i))).ToArray());
            }
Exemple #18
0
        private static WorkingGraph CreateSmallTestgraph()
        {
            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;
            Item         a     = graph.CreateItem(ItemType.SIMPLE, "a");
            Item         b     = graph.CreateItem(ItemType.SIMPLE, "b");
            Item         c     = graph.CreateItem(ItemType.SIMPLE, "c");
            Item         d     = graph.CreateItem(ItemType.SIMPLE, "d");

            graph.AddDependencies(new[] {
                graph.CreateDependency(a, b, null, "", 1),
                graph.CreateDependency(b, c, null, "", 1),
                graph.CreateDependency(b, d, null, "", 1)
            });

            return(graph);
        }
Exemple #19
0
        public override IEnumerable <Dependency> CreateSomeTestDependencies(WorkingGraph renderingGraph)
        {
            ItemType simple = ItemType.New("SIMPLE(Name)");

            Item[] localItems = Enumerable.Range(0, 5).Select(i => renderingGraph.CreateItem(simple, "Item " + i)).ToArray();
            return(localItems.SelectMany(
                       (from, i) => localItems.Skip(i).Select(to => renderingGraph.CreateDependency(from, to, new TextFileSourceLocation("Test", i), "Test", ct: 10 * i))).ToArray());
        }
Exemple #20
0
        public void TestMarkAnotherCut()
        {
            // Graph from http://www.cs.princeton.edu/courses/archive/spring06/cos226/lectures/maxflow.pdf p.30 (and Wikipedia)
            var          gc           = new GlobalContext();
            WorkingGraph graph        = gc.CurrentGraph;
            Item         s            = graph.CreateItem(ItemType.SIMPLE, "s");
            Item         n2           = graph.CreateItem(ItemType.SIMPLE, "2");
            Item         n4           = graph.CreateItem(ItemType.SIMPLE, "4");
            Item         t            = graph.CreateItem(ItemType.SIMPLE, "t");
            var          dependencies = new[] {
                graph.CreateDependency(s, n2, null, "s_2", 102, 0, 100, notOkReason: "test data"),
                graph.CreateDependency(s, n4, null, "s_4", 104, 0, 100, notOkReason: "test data"),

                graph.CreateDependency(n2, t, null, "2_t", 203, 0, 100, notOkReason: "test data"),

                graph.CreateDependency(n4, n2, null, "4_7", 402, 0, 1, notOkReason: "test data"),
                graph.CreateDependency(n4, t, null, "4_7", 403, 0, 100, notOkReason: "test data"),
            };

            const string mark   = "CUT";
            const string source = "SOURCE";

            IEnumerable <Dependency> result = Run(gc, new MarkMinimalCutDeps.TransformOptions {
                SourceMatches = new List <ItemMatch> {
                    new ItemMatch("s", false, anyWhereMatcherOk: true)
                },
                TargetMatches = new List <ItemMatch> {
                    new ItemMatch("t", false, anyWhereMatcherOk: true)
                },
                MarkerToAddToCut        = mark,
                MarkerToAddToSourceSide = source
            }, dependencies);

            Assert.IsTrue(result.All(z => z.MarkersContain(mark) == new[] { 102, 104 }.Contains(z.Ct)),
                          string.Join("\r\n", result.Select(z => z.AsLimitableStringWithTypes(withExampleInfo: false, threeLines: false))));
            Assert.IsTrue(s.MarkersContain(source));
            Assert.IsFalse(n2.MarkersContain(source));
            Assert.IsFalse(n4.MarkersContain(source));
            Assert.IsFalse(t.MarkersContain(source));
        }
Exemple #21
0
        public void TestOneTopologicalSortStep()
        {
            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            Item a            = graph.CreateItem(ItemType.SIMPLE, "a");
            Item b            = graph.CreateItem(ItemType.SIMPLE, "b");
            Item c            = graph.CreateItem(ItemType.SIMPLE, "c");
            var  dependencies = new[] {
                graph.CreateDependency(a, b, source: null, markers: "", ct: 1),
                graph.CreateDependency(c, a, source: null, markers: "", ct: 100),
                graph.CreateDependency(c, b, source: null, markers: "", ct: 100)
            };

            var aggregated = new Dictionary <FromTo, Dependency>();

            foreach (var d in dependencies.Where(d => !Equals(d.UsingItem, d.UsedItem)))
            {
                new FromTo(d.UsingItem, d.UsedItem).AggregateDependency(graph, d, aggregated);
            }

            var aggregatedCounts = new MatrixDictionary <Item, int>((s, i) => s + i, (s, i) => s - i);

            foreach (var kvp in aggregated)
            {
                aggregatedCounts.Add(kvp.Key.From, kvp.Key.To, kvp.Value.Ct);
            }

            var itemsToRatios =
                aggregatedCounts.ColumnKeys.Select(
                    k => new {
                Item  = k,
                Ratio = aggregatedCounts.GetRowSum(k) / (aggregatedCounts.GetColumnSum(k) + aggregatedCounts.GetRowSum(k) + 0.001m)
            });
            decimal minToRatio = itemsToRatios.Min(ir => ir.Ratio);
            Item    minItem    = itemsToRatios.First(ir => ir.Ratio == minToRatio).Item;

            Assert.AreEqual(b, minItem);
        }
Exemple #22
0
        public void TestSimpleNamedCall()
        {
            ItemType     itemType = DotNetAssemblyDependencyReaderFactory.DOTNETITEM;
            var          gc       = new GlobalContext();
            WorkingGraph graph    = gc.CurrentGraph;
            Item         @using   = graph.CreateItem(itemType, "", "NamespacelessTestClassForNDepCheck", "NDepCheck.TestAssembly", "1.0.0.0", "", "");
            Item         used     = graph.CreateItem(itemType, "System", "Object", "mscorlib", "", "", "");
            var          d        = graph.CreateDependency(@using, used, null, "Test", ct: 1);

            DependencyRule r = CreateDependencyRule(itemType, "**", "Assembly.Name=mscorlib");

            Assert.IsTrue(r.IsMatch(d));
        }
        public void TestSmallSelfOptimizingPrefixTrieProjector2()
        {
            // This test found a problem in TrieNode.SetProjectors.
            var pi = new ProjectItems((p, i) => new ProjectItems.SelfOptimizingPrefixTrieProjector(p, i, 2, "prefixTrie"));
            var gc = new GlobalContext();

            pi.Configure(gc, @"{ -pl
    $ (:Name) ---% SIMPLE

    ! :abc ---% ADetail
    ! :a*  ---% A
    ! :t   ---% T
    ! :**  ---% 
}", forceReload: false);

            ItemType     generic2 = ItemType.Generic(2, ignoreCase: false);
            WorkingGraph graph    = gc.CurrentGraph;
            Item         a        = graph.CreateItem(generic2, "x:a");
            Item         ab       = graph.CreateItem(generic2, "x:ab");
            Item         abc      = graph.CreateItem(generic2, "x:abc");
            Item         abcd     = graph.CreateItem(generic2, "x:abcd");
            Item         t        = graph.CreateItem(generic2, "m:t");

            var result = new List <Dependency>();

            pi.Transform(gc, new[] {
                graph.CreateDependency(a, t, null, "a_t", 1),       // A_T
                graph.CreateDependency(ab, t, null, "ab_t", 1),     // A_ T
                graph.CreateDependency(abc, t, null, "abc_t", 1),   // ADetail _T
                graph.CreateDependency(abcd, t, null, "abcd_t", 1), // A _ T
            }, "", result, s => null);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("A", result[0].UsingItem.Values[0]);
            Assert.AreEqual(3, result[0].Ct);
            Assert.AreEqual("ADetail", result[1].UsingItem.Values[0]);
            Assert.AreEqual(1, result[1].Ct);
        }
        private void CreateAndRender(int n, string prefix, int boxHeight = 15)
        {
            var          gc     = new GlobalContext();
            WorkingGraph graph  = gc.CurrentGraph;
            ItemType     simple = ItemType.New("SIMPLE(Name)");

            Item[]       items        = Enumerable.Range(0, n).Select(i => graph.CreateItem(simple, prefix + i)).ToArray();
            Dependency[] dependencies =
                items.SelectMany(
                    (from, i) => items.Skip(i).Select(to => graph.CreateDependency(from, to, new TextFileSourceLocation(prefix, i), "Use", 10 * i))).ToArray();

            new SomewhatComplexTestRenderer(boxHeight).Render(gc, dependencies,
                                                              options: "", target: new WriteTarget(Path.GetTempFileName(), append: false, limitLinesForConsole: 100), ignoreCase: false);
        }
Exemple #25
0
        public void TestBackReferenceDependencyRuleMatchesWithOuterParentheses()
        {
            ItemType itemtype = ItemType.New("SO", new[] { "Schema", "Object" }, new[] { "", "" }, ignoreCase: false);
            var      rn3      = CreateDependencyRule(itemtype, "(s**):(t**)", @"\1*:\2*");
            var      rn5      = CreateDependencyRule(itemtype, "(**s**):(**t**)", @"\1:\2");

            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            Dependency dep = graph.CreateDependency(graph.CreateItem(itemtype, "sx", "tx"), graph.CreateItem(itemtype, "sx", "tx"), null, "Test", ct: 1);

            Assert.IsTrue(rn3.IsMatch(dep));
            Assert.IsTrue(rn5.IsMatch(dep));
        }
Exemple #26
0
        public override IEnumerable <Dependency> CreateSomeTestDependencies(WorkingGraph transformingGraph)
        {
            // Graph from http://web.stanford.edu/class/cs97si/08-network-flow-problems.pdf p.7
            Item s = transformingGraph.CreateItem(ItemType.SIMPLE, "s");
            Item a = transformingGraph.CreateItem(ItemType.SIMPLE, "a");
            Item b = transformingGraph.CreateItem(ItemType.SIMPLE, "b");
            Item c = transformingGraph.CreateItem(ItemType.SIMPLE, "c");
            Item d = transformingGraph.CreateItem(ItemType.SIMPLE, "d");
            Item t = transformingGraph.CreateItem(ItemType.SIMPLE, "t");

            return(new[] {
                transformingGraph.CreateDependency(s, a, null, "s_a", 101, 16, 0, "test data"),
                transformingGraph.CreateDependency(s, c, null, "s_c", 103, 13, 0, "test data"),
                transformingGraph.CreateDependency(a, b, null, "a_b", 112, 12, 0, "test data"),
                transformingGraph.CreateDependency(a, c, null, "a_c", 113, 10, 0, "test data"),
                transformingGraph.CreateDependency(b, c, null, "b_c", 123, 9, 0, "test data"),
                transformingGraph.CreateDependency(b, t, null, "b_t", 125, 20, 0, "test data"),
                transformingGraph.CreateDependency(c, a, null, "c_a", 131, 4, 0, "test data"),
                transformingGraph.CreateDependency(c, d, null, "c_d", 134, 14, 0, "test data"),
                transformingGraph.CreateDependency(d, b, null, "d_b", 142, 7, 0, "test data"),
                transformingGraph.CreateDependency(d, t, null, "d_t", 145, 4, 0, "test data"),
            });
        }
        public void TestProblemWithEmptyNamespace()
        {
            ItemType itemType = DotNetAssemblyDependencyReaderFactory.DOTNETITEM;

            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            var @using = graph.CreateItem(itemType, "", "NamespacelessTestClassForArchichect", "Archichect.TestAssembly", "1.0.0.0", "", "");
            var used   = graph.CreateItem(itemType, "System", "Object", "mscorlib", "", "", "");
            var d      = graph.CreateDependency(@using, used, null, "Test", ct: 1);

            var r = CreateDependencyRule(itemType, "**", "System.**");

            Assert.IsTrue(r.IsMatch(d));
        }
Exemple #28
0
        public void TestEmptyNamespaceInRule()
        {
            ItemType itemType = DotNetAssemblyDependencyReaderFactory.DOTNETITEM;

            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            Item @using = graph.CreateItem(itemType, "NDepCheck.TestAssembly.dir1.dir2", "SomeClass", "NDepCheck.TestAssembly", "1.0.0.0", "", "AnotherMethod");
            Item used   = graph.CreateItem(itemType, "", "NamespacelessTestClassForNDepCheck", "NDepCheck.TestAssembly", "1.0.0.0", "", "I");
            var  d      = graph.CreateDependency(@using, used, null, "Test", ct: 1);

            var r = CreateDependencyRule(itemType, "NDepCheck.TestAssembly.dir1.dir2:SomeClass:**", "-:NamespacelessTestClassForNDepCheck::I");

            Assert.IsTrue(r.IsMatch(d));
        }
        private void RecursivelyFlood(Item root, Item from, HashSet <Item> visited, Dictionary <FromTo, Dependency> checkPresence,
                                      DependencyPattern idempotentPattern, Dictionary <Item, Dependency[]> outgoing, IEnumerable <ItemMatch> toItemMatches,
                                      List <DependencyMatch> matches, List <DependencyMatch> excludes, Dictionary <string, int> markersToAddOrNull,
                                      List <Dependency> result, Dependency collectedEdge, [NotNull] Action checkAbort, WorkingGraph workingGraph, bool ignoreCase)
        {
            if (outgoing.ContainsKey(from))
            {
                checkAbort();
                foreach (var d in outgoing[from].Where(d => d.IsMarkerMatch(matches, excludes)))
                {
                    Item target = d.UsedItem;
                    if (visited.Add(target))
                    {
                        Dependency rootToTarget = collectedEdge == null
                            ? d
                            : workingGraph.CreateDependency(root, target, d.Source,
                                                            new MutableMarkerSet(ignoreCase,
                                                                                 markersToAddOrNull
                                                                                 ?? MutableMarkerSet.ConcatOrUnionWithMarkers(collectedEdge.AbstractMarkerSet,
                                                                                                                              d.AbstractMarkerSet, ignoreCase)),
                                                            collectedEdge.Ct + d.Ct, collectedEdge.QuestionableCt + d.QuestionableCt,
                                                            collectedEdge.BadCt + d.BadCt, collectedEdge.NotOkReason ?? d.NotOkReason, d.ExampleInfo);

                        if (IsMatch(toItemMatches, target))
                        {
                            Dependency alreadyThere;
                            var        rootTargetKey = new FromTo(root, target);
                            if (checkPresence.TryGetValue(rootTargetKey, out alreadyThere) && idempotentPattern.IsMatch(alreadyThere))
                            {
                                // we do not add a dependency
                            }
                            else
                            {
                                checkPresence[rootTargetKey] = rootToTarget;
                                result.Add(rootToTarget);
                            }
                        }

                        // Continue search
                        RecursivelyFlood(root, target, visited, checkPresence, idempotentPattern, outgoing,
                                         toItemMatches, matches, excludes, markersToAddOrNull, result, rootToTarget,
                                         checkAbort, workingGraph, ignoreCase);
                    }
                }
            }
        }
Exemple #30
0
        public void TestSimpleDependencyRuleMatches()
        {
            ItemType itemType = ItemType.New("NC", new[] { "Namespace", "Class" }, new[] { "", "" }, ignoreCase: false);

            var r1 = CreateDependencyRule(itemType, ":", ":");

            var rn1  = CreateDependencyRule(itemType, "n*", ":");
            var rn2  = CreateDependencyRule(itemType, "n*:", ":");
            var rn3  = CreateDependencyRule(itemType, "n**", ":");
            var rn4  = CreateDependencyRule(itemType, "n**:", ":");
            var rn5  = CreateDependencyRule(itemType, "n1", ":");
            var rn6  = CreateDependencyRule(itemType, "n1:", ":");
            var rn7  = CreateDependencyRule(itemType, "*n1:", ":");
            var rn8  = CreateDependencyRule(itemType, "**n1:", ":");
            var rc1  = CreateDependencyRule(itemType, ":c*", ":");
            var rc2  = CreateDependencyRule(itemType, ":c**", ":");
            var rc3  = CreateDependencyRule(itemType, ":*c1", ":");
            var rc4  = CreateDependencyRule(itemType, ":**c1", ":");
            var rnc1 = CreateDependencyRule(itemType, "n*:c*", ":");
            var rnc2 = CreateDependencyRule(itemType, "n**:c**", ":");

            var          gc    = new GlobalContext();
            WorkingGraph graph = gc.CurrentGraph;

            Dependency dep = graph.CreateDependency(graph.CreateItem(itemType, "n1", "c1"), graph.CreateItem(itemType, "n2", "c2"), null, "Test", ct: 1);

            Assert.IsTrue(r1.IsMatch(dep));

            Assert.IsTrue(rn1.IsMatch(dep));
            Assert.IsTrue(rn2.IsMatch(dep));
            Assert.IsTrue(rn3.IsMatch(dep));
            Assert.IsTrue(rn4.IsMatch(dep));
            Assert.IsTrue(rn5.IsMatch(dep));
            Assert.IsTrue(rn6.IsMatch(dep));
            Assert.IsTrue(rn7.IsMatch(dep));
            Assert.IsTrue(rn8.IsMatch(dep));

            Assert.IsTrue(rc1.IsMatch(dep));
            Assert.IsTrue(rc2.IsMatch(dep));
            Assert.IsTrue(rc3.IsMatch(dep));
            Assert.IsTrue(rc4.IsMatch(dep));

            Assert.IsTrue(rnc1.IsMatch(dep));
            Assert.IsTrue(rnc2.IsMatch(dep));
        }