• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#
17import os
18import unittest
19
20from vts.utils.python.coverage import arc_summary
21from vts.utils.python.coverage import block_summary
22from vts.utils.python.coverage import file_summary
23from vts.utils.python.coverage import function_summary
24from vts.utils.python.coverage import gcda_parser
25from vts.utils.python.coverage import gcno_parser
26from vts.utils.python.coverage.parser_test import MockStream
27
28
29class GCDAParserTest(unittest.TestCase):
30    """Tests for GCDA parser of vts.utils.python.coverage.
31    """
32
33    def setUp(self):
34        """Creates a stream for each test.
35      """
36        self.stream = MockStream(gcda_parser.GCDAParser.MAGIC)
37
38    def testReadFunction(self):
39        """Verifies that the correct function is read and returned.
40        """
41        ident = 100
42        checksum = 0
43        fs = file_summary.FileSummary()
44        func = function_summary.FunctionSummary(ident, 'test', 'test.c', 0)
45        fs.functions[ident] = func
46        self.stream = MockStream.concat_int(self.stream, ident)
47        self.stream = MockStream.concat_int(self.stream, 0)
48        self.stream = MockStream.concat_int(self.stream, 0)
49        self.stream = MockStream.concat_string(self.stream, 'test')
50        length = 5
51        parser = gcda_parser.GCDAParser(self.stream)
52        parser.file_summary = fs
53        func = parser.ReadFunction(5)
54        assert (func.ident == ident)
55
56    def testReadCountsNormal(self):
57        """Verifies that counts are read correctly.
58
59        Verifies that arcs are marked as resolved and count is correct.
60        """
61        n = 5
62        fs = file_summary.FileSummary()
63        func = function_summary.FunctionSummary(0, 'test', 'test.c', 0)
64        blocks = [block_summary.BlockSummary(i, 0) for i in range(n)]
65        func.blocks = blocks
66        fs.functions[func.ident] = func
67        for i in range(1, n):
68            arc = arc_summary.ArcSummary(blocks[0], blocks[i], 0)
69            blocks[0].exit_arcs.append(arc)
70            blocks[i].entry_arcs.append(arc)
71            self.stream = MockStream.concat_int64(self.stream, i)
72        parser = gcda_parser.GCDAParser(self.stream)
73        parser.file_summary = fs
74        parser.ReadCounts(func)
75        for i, arc in zip(range(1, n), blocks[0].exit_arcs):
76            self.assertEqual(i, arc.count)
77            self.assertTrue(arc.resolved)
78
79    def testReadCountsFakeOrOnTree(self):
80        """Verifies that counts are read correctly when there are skipped arcs.
81
82        Verifies that the fake arc and the arc on the tree are skipped while other
83        arcs are read and resolved correctly.
84        """
85        n = 10
86        fs = file_summary.FileSummary()
87        func = function_summary.FunctionSummary(0, 'test', 'test.c', 0)
88        blocks = [block_summary.BlockSummary(i, 0) for i in range(n)]
89        func.blocks = blocks
90        fs.functions[func.ident] = func
91
92        arc = arc_summary.ArcSummary(blocks[0], blocks[1],
93                                     arc_summary.ArcSummary.GCOV_ARC_FAKE)
94        blocks[0].exit_arcs.append(arc)
95        blocks[1].entry_arcs.append(arc)
96
97        arc = arc_summary.ArcSummary(blocks[0], blocks[2],
98                                     arc_summary.ArcSummary.GCOV_ARC_ON_TREE)
99        blocks[0].exit_arcs.append(arc)
100        blocks[2].entry_arcs.append(arc)
101
102        for i in range(3, n):
103            arc = arc_summary.ArcSummary(blocks[0], blocks[i], 0)
104            blocks[0].exit_arcs.append(arc)
105            blocks[i].entry_arcs.append(arc)
106            self.stream = MockStream.concat_int64(self.stream, i)
107
108        parser = gcda_parser.GCDAParser(self.stream)
109        parser.file_summary = fs
110        parser.ReadCounts(func)
111        self.assertFalse(blocks[0].exit_arcs[0].resolved)
112        self.assertFalse(blocks[0].exit_arcs[1].resolved)
113        for i, arc in zip(range(3, n), blocks[0].exit_arcs[2:]):
114            self.assertEqual(i, arc.count)
115            self.assertTrue(arc.resolved)
116
117    def testSampleFile(self):
118        """Asserts correct parsing of sample GCDA file.
119
120        Verifies the block coverage counts for each function.
121        """
122        gcno_path = os.path.join(
123            os.getenv('ANDROID_BUILD_TOP'),
124            'test/vts/utils/python/coverage/testdata/sample.gcno')
125        gcda_path = os.path.join(
126            os.getenv('ANDROID_BUILD_TOP'),
127            'test/vts/utils/python/coverage/testdata/sample.gcda')
128        summary = gcno_parser.ParseGcnoFile(gcno_path)
129        gcda_parser.ParseGcdaFile(gcda_path, summary)
130        # Function: main
131        expected_list = [2, 0, 2, 2, 2, 0, 2, 2, 500, 502, 2, 2]
132        for index, expected in zip(range(len(expected_list)), expected_list):
133            self.assertEqual(summary.functions[3].blocks[index].count,
134                             expected)
135
136        # Function: testFunctionName
137        expected_list = [2, 2, 2, 2, 2]
138        for index, expected in zip(range(len(expected_list)), expected_list):
139            self.assertEqual(summary.functions[4].blocks[index].count,
140                             expected)
141
142
143if __name__ == "__main__":
144    unittest.main()
145