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 gcno_parser
     22 from vts.utils.python.coverage import gcda_parser
     23 from vts.utils.python.coverage import coverage_report
     24 
     25 
     26 class 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         src_lines_counts = coverage_report.GenerateLineCoverageVector(
     52             'sample.c', self.gcno_summary)
     53         expected = [-1, -1, -1, -1, 2, -1, -1, -1, -1, -1, 2,
     54                     2, 2, -1, 2, -1, 2, 0, -1, 2, -1, -1, 2, 2, 502,
     55                     500, -1, -1, 2, -1, 2, -1, -1, -1, 2, -1,
     56                     -1, -1, -1, 2, 2, 2]
     57         self.assertEqual(src_lines_counts, expected)
     58 
     59 
     60 if __name__ == "__main__":
     61     unittest.main()
     62