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 @classmethod 31 def setUpClass(cls): 32 root_dir = os.path.join( 33 os.getenv('ANDROID_BUILD_TOP'), 34 'test/vts/utils/python/coverage/testdata/') 35 gcno_path = os.path.join(root_dir, 'sample.gcno') 36 with open(gcno_path, 'rb') as file: 37 gcno_summary = gcno_parser.GCNOParser(file).Parse() 38 gcda_path = os.path.join(root_dir, 'sample.gcda') 39 with open(gcda_path, 'rb') as file: 40 parser = gcda_parser.GCDAParser(file) 41 parser.Parse(gcno_summary) 42 cls.gcno_summary = gcno_summary 43 44 def testGenerateLineCoverageVector(self): 45 """Tests that coverage vector is correctly generated. 46 47 Runs GenerateLineCoverageVector on sample file and checks 48 result. 49 """ 50 src_lines_counts = coverage_report.GenerateLineCoverageVector( 51 'sample.c', self.gcno_summary) 52 expected = [-1, -1, -1, -1, 2, -1, -1, -1, -1, -1, 2, 53 2, 2, -1, 2, -1, 2, 0, -1, 2, -1, -1, 2, 2, 502, 54 500, -1, -1, 2, -1, 2, -1, -1, -1, 2, -1, 55 -1, -1, -1, 2, 2, 2] 56 self.assertEqual(src_lines_counts, expected) 57 58 59if __name__ == "__main__": 60 unittest.main() 61