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
18import os
19import unittest
20
21from vts.utils.python.coverage import gcno_parser
22from vts.utils.python.coverage import gcda_parser
23from vts.utils.python.coverage import coverage_report
24
25
26class CoverageReportTest(unittest.TestCase):
27    """Unit tests for CoverageReport of vts.utils.python.coverage.
28    """
29
30    GOLDEN_GCNO_PATH = 'testdata/sample.gcno'
31    GOLDEN_GCDA_PATH = 'testdata/sample.gcda'
32
33    @classmethod
34    def setUpClass(cls):
35        dir_path = os.path.dirname(os.path.realpath(__file__))
36        gcno_path = os.path.join(dir_path, cls.GOLDEN_GCNO_PATH)
37        with open(gcno_path, 'rb') as file:
38            gcno_summary = gcno_parser.GCNOParser(file).Parse()
39        gcda_path = os.path.join(dir_path, cls.GOLDEN_GCDA_PATH)
40        with open(gcda_path, 'rb') as file:
41            parser = gcda_parser.GCDAParser(file)
42            parser.Parse(gcno_summary)
43        cls.gcno_summary = gcno_summary
44
45    def testGenerateLineCoverageVector(self):
46        """Tests that coverage vector is correctly generated.
47
48        Runs GenerateLineCoverageVector on sample file and checks
49        result.
50        """
51        coverage_dict = dict()
52        exclude_paths = []
53        src_lines_counts = coverage_report.GenerateLineCoverageVector(
54            self.gcno_summary, exclude_paths, coverage_dict)
55        expected = {'sample.c': [-1, -1, -1, -1, 2, -1, -1, -1, -1, -1, 2,
56                    2, 2, -1, 2, -1, 2, 0, -1, 2, -1, -1, 2, 2, 502,
57                    500, -1, -1, 2, -1, 2, -1, -1, -1, 2, -1,
58                    -1, -1, -1, 2, 2, 2]}
59        self.assertEqual(coverage_dict, expected)
60
61
62if __name__ == "__main__":
63    unittest.main()
64