| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import unittest | 5 import unittest |
| 6 | 6 |
| 7 import resource_sack | 7 import resource_sack |
| 8 from test_utils import (MakeRequest, | 8 from test_utils import (MakeRequest, |
| 9 TestResourceGraph) | 9 TestDependencyGraph) |
| 10 | 10 |
| 11 | 11 |
| 12 class ResourceSackTestCase(unittest.TestCase): | 12 class ResourceSackTestCase(unittest.TestCase): |
| 13 def SimpleGraph(self, node_names): | 13 def SimpleGraph(self, node_names): |
| 14 """Create a simple graph from a list of nodes.""" | 14 """Create a simple graph from a list of nodes.""" |
| 15 requests = [MakeRequest(node_names[0], 'null')] | 15 requests = [MakeRequest(node_names[0], 'null')] |
| 16 for n in node_names[1:]: | 16 for n in node_names[1:]: |
| 17 requests.append(MakeRequest(n, node_names[0])) | 17 requests.append(MakeRequest(n, node_names[0])) |
| 18 return TestResourceGraph.FromRequestList(requests) | 18 return TestDependencyGraph(requests) |
| 19 | 19 |
| 20 def test_NodeMerge(self): | 20 def test_NodeMerge(self): |
| 21 g1 = TestResourceGraph.FromRequestList([ | 21 g1 = TestDependencyGraph([ |
| 22 MakeRequest(0, 'null'), | 22 MakeRequest(0, 'null'), |
| 23 MakeRequest(1, 0), | 23 MakeRequest(1, 0), |
| 24 MakeRequest(2, 0), | 24 MakeRequest(2, 0), |
| 25 MakeRequest(3, 1)]) | 25 MakeRequest(3, 1)]) |
| 26 g2 = TestResourceGraph.FromRequestList([ | 26 g2 = TestDependencyGraph([ |
| 27 MakeRequest(0, 'null'), | 27 MakeRequest(0, 'null'), |
| 28 MakeRequest(1, 0), | 28 MakeRequest(1, 0), |
| 29 MakeRequest(2, 0), | 29 MakeRequest(2, 0), |
| 30 MakeRequest(4, 2)]) | 30 MakeRequest(4, 2)]) |
| 31 sack = resource_sack.GraphSack() | 31 sack = resource_sack.GraphSack() |
| 32 sack.ConsumeGraph(g1) | 32 sack.ConsumeGraph(g1) |
| 33 sack.ConsumeGraph(g2) | 33 sack.ConsumeGraph(g2) |
| 34 self.assertEqual(5, len(sack.bags)) | 34 self.assertEqual(5, len(sack.bags)) |
| 35 for bag in sack.bags: | 35 for bag in sack.bags: |
| 36 if bag.label not in ('3/', '4/'): | 36 if bag.label not in ('3/', '4/'): |
| 37 self.assertEqual(2, bag.num_nodes) | 37 self.assertEqual(2, bag.num_nodes) |
| 38 else: | 38 else: |
| 39 self.assertEqual(1, bag.num_nodes) | 39 self.assertEqual(1, bag.num_nodes) |
| 40 | 40 |
| 41 def test_MultiParents(self): | 41 def test_MultiParents(self): |
| 42 g1 = TestResourceGraph.FromRequestList([ | 42 g1 = TestDependencyGraph([ |
| 43 MakeRequest(0, 'null'), | 43 MakeRequest(0, 'null'), |
| 44 MakeRequest(2, 0)]) | 44 MakeRequest(2, 0)]) |
| 45 g2 = TestResourceGraph.FromRequestList([ | 45 g2 = TestDependencyGraph([ |
| 46 MakeRequest(1, 'null'), | 46 MakeRequest(1, 'null'), |
| 47 MakeRequest(2, 1)]) | 47 MakeRequest(2, 1)]) |
| 48 sack = resource_sack.GraphSack() | 48 sack = resource_sack.GraphSack() |
| 49 sack.ConsumeGraph(g1) | 49 sack.ConsumeGraph(g1) |
| 50 sack.ConsumeGraph(g2) | 50 sack.ConsumeGraph(g2) |
| 51 self.assertEqual(3, len(sack.bags)) | 51 self.assertEqual(3, len(sack.bags)) |
| 52 labels = {bag.label: bag for bag in sack.bags} | 52 labels = {bag.label: bag for bag in sack.bags} |
| 53 def Predecessors(label): |
| 54 bag = labels['%s/' % label] |
| 55 return [e.from_node |
| 56 for e in bag._sack._graph.InEdges(bag)] |
| 53 self.assertEqual( | 57 self.assertEqual( |
| 54 set(['0/', '1/']), | 58 set(['0/', '1/']), |
| 55 set([bag.label for bag in labels['2/'].Predecessors()])) | 59 set([bag.label for bag in Predecessors(2)])) |
| 56 self.assertFalse(labels['0/'].Predecessors()) | 60 self.assertFalse(Predecessors(0)) |
| 57 self.assertFalse(labels['1/'].Predecessors()) | 61 self.assertFalse(Predecessors(1)) |
| 58 | 62 |
| 59 def test_Shortname(self): | 63 def test_Shortname(self): |
| 60 root = MakeRequest(0, 'null') | 64 root = MakeRequest(0, 'null') |
| 61 shortname = MakeRequest(1, 0) | 65 shortname = MakeRequest(1, 0) |
| 62 shortname.url = 'data:fake/content;' + 'lotsand' * 50 + 'lotsofdata' | 66 shortname.url = 'data:fake/content;' + 'lotsand' * 50 + 'lotsofdata' |
| 63 g1 = TestResourceGraph.FromRequestList([root, shortname]) | 67 g1 = TestDependencyGraph([root, shortname]) |
| 64 sack = resource_sack.GraphSack() | 68 sack = resource_sack.GraphSack() |
| 65 sack.ConsumeGraph(g1) | 69 sack.ConsumeGraph(g1) |
| 66 self.assertEqual(set(['0/', 'data:fake/content']), | 70 self.assertEqual(set(['0/', 'data:fake/content']), |
| 67 set([bag.label for bag in sack.bags])) | 71 set([bag.label for bag in sack.bags])) |
| 68 | 72 |
| 69 def test_Core(self): | 73 def test_Core(self): |
| 70 # We will use a core threshold of 0.5 to make it easier to define | 74 # We will use a core threshold of 0.5 to make it easier to define |
| 71 # graphs. Resources 0 and 1 are core and others are not. | 75 # graphs. Resources 0 and 1 are core and others are not. |
| 72 graphs = [self.SimpleGraph([0, 1, 2]), | 76 graphs = [self.SimpleGraph([0, 1, 2]), |
| 73 self.SimpleGraph([0, 1, 3]), | 77 self.SimpleGraph([0, 1, 3]), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 102 self.assertAlmostEqual( | 106 self.assertAlmostEqual( |
| 103 0.5, | 107 0.5, |
| 104 resource_sack.GraphSack.CoreSimilarity( | 108 resource_sack.GraphSack.CoreSimilarity( |
| 105 set([1, 2, 3]), set([1, 3, 4]))) | 109 set([1, 2, 3]), set([1, 3, 4]))) |
| 106 self.assertEqual( | 110 self.assertEqual( |
| 107 0, resource_sack.GraphSack.CoreSimilarity(set(), set())) | 111 0, resource_sack.GraphSack.CoreSimilarity(set(), set())) |
| 108 | 112 |
| 109 | 113 |
| 110 if __name__ == '__main__': | 114 if __name__ == '__main__': |
| 111 unittest.main() | 115 unittest.main() |
| OLD | NEW |