Home | History | Annotate | Download | only in coverage
      1 #!/usr/bin/env python
      2 #
      3 # Copyright (C) 2016 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 
     18 import os
     19 import unittest
     20 
     21 from vts.utils.python.coverage import arc_summary
     22 from vts.utils.python.coverage import block_summary
     23 
     24 
     25 class ArcSummaryTest(unittest.TestCase):
     26     """Tests for ArcSummary of vts.utils.python.coverage.
     27     """
     28 
     29     def testResolveRemove(self):
     30         """Verifies that fake, non-fallthrough arc are resolved correctly.
     31 
     32         The arc should be removed as an exit arc from the source.
     33         """
     34         src = block_summary.BlockSummary(0, 0)
     35         dst = block_summary.BlockSummary(1, 0)
     36         flag = arc_summary.ArcSummary.GCOV_ARC_FAKE
     37         arc = arc_summary.ArcSummary(src, dst, flag)
     38         src.exit_arcs.append(arc)
     39         dst.entry_arcs.append(arc)
     40         self.assertTrue(arc.Resolve())
     41         self.assertEqual(len(src.exit_arcs), 0)
     42 
     43     def testResolveFromSource(self):
     44         """Verifies that arcs can be resolved from the source.
     45 
     46         In the case when the source has fully-resolved entry arcs, the arc
     47         count should be resolved from the source. I.e. there is only one
     48         missing arc and it can be solved for from the source.
     49         """
     50         middle = block_summary.BlockSummary(-1, 0)
     51         n = 10
     52 
     53         # Create resolved arcs entering the middle block
     54         for ident in range(n):
     55             block = block_summary.BlockSummary(ident, 0)
     56             arc = arc_summary.ArcSummary(block, middle, 0)
     57             arc.resolved = True
     58             arc.count = 1
     59             block.exit_arcs.append(arc)
     60             middle.entry_arcs.append(arc)
     61 
     62         # Create resolved arcs exiting the middle block
     63         for ident in range(n, 2 * n - 1):
     64             block = block_summary.BlockSummary(ident, 0)
     65             arc = arc_summary.ArcSummary(middle, block, 0)
     66             arc.resolved = True
     67             arc.count = 1
     68             block.entry_arcs.append(arc)
     69             middle.exit_arcs.append(arc)
     70 
     71         # Create one unresolved arc exiting the middle
     72         last = block_summary.BlockSummary(2 * n - 1, 0)
     73         arc = arc_summary.ArcSummary(middle, last, 0)
     74         middle.exit_arcs.append(arc)
     75         last.entry_arcs.append(arc)
     76         self.assertTrue(arc.Resolve())
     77         self.assertTrue(arc.resolved)
     78         self.assertEqual(arc.count, 1)
     79 
     80     def testResolveFromDest(self):
     81         """Verifies that arcs can be resolved from the destination block.
     82 
     83         In the case when the source has fully-resolved exit arcs, the arc
     84         count should be resolved from the source. I.e. there is only one
     85         missing arc and it can be solved for from the destination.
     86         """
     87         middle = block_summary.BlockSummary(-1, 0)
     88         n = 10
     89 
     90         # Create resolved arcs exiting the middle block
     91         for ident in range(n):
     92             block = block_summary.BlockSummary(ident, 0)
     93             arc = arc_summary.ArcSummary(middle, block, 0)
     94             arc.resolved = True
     95             arc.count = 1
     96             block.entry_arcs.append(arc)
     97             middle.exit_arcs.append(arc)
     98 
     99         # Create resolved arcs entering the middle block
    100         for ident in range(n, 2 * n - 1):
    101             block = block_summary.BlockSummary(ident, 0)
    102             arc = arc_summary.ArcSummary(block, middle, 0)
    103             arc.resolved = True
    104             arc.count = 1
    105             block.exit_arcs.append(arc)
    106             middle.entry_arcs.append(arc)
    107 
    108         # Create one unresolved arc entering the middle
    109         block = block_summary.BlockSummary(2 * n - 1, 0)
    110         arc = arc_summary.ArcSummary(block, middle, 0)
    111         middle.entry_arcs.append(arc)
    112         block.exit_arcs.append(arc)
    113         self.assertTrue(arc.Resolve())
    114         self.assertTrue(arc.resolved)
    115         self.assertEqual(arc.count, 1)
    116 
    117 
    118 if __name__ == "__main__":
    119     unittest.main()
    120