|
| 1 | +""" |
| 2 | +mbed SDK |
| 3 | +Copyright (c) 2016 ARM Limited |
| 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 | +import sys |
| 18 | +import os |
| 19 | + |
| 20 | +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")) |
| 21 | +sys.path.insert(0, ROOT) |
| 22 | + |
| 23 | +import unittest |
| 24 | +from tools.memap import MemapParser |
| 25 | +from copy import deepcopy |
| 26 | + |
| 27 | +""" |
| 28 | +Tests for test_api.py |
| 29 | +""" |
| 30 | + |
| 31 | +class MemapParserTests(unittest.TestCase): |
| 32 | + """ |
| 33 | + Test cases for Test Api |
| 34 | + """ |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + """ |
| 38 | + Called before each test case |
| 39 | +
|
| 40 | + :return: |
| 41 | + """ |
| 42 | + self.memap_parser = MemapParser() |
| 43 | + |
| 44 | + self.memap_parser.modules = { |
| 45 | + "Misc": { |
| 46 | + "unknown": 0, |
| 47 | + ".ARM": 8, |
| 48 | + ".ARM.extab": 0, |
| 49 | + ".init": 12, |
| 50 | + "OUTPUT": 0, |
| 51 | + ".stack": 0, |
| 52 | + ".eh_frame": 0, |
| 53 | + ".fini_array": 4, |
| 54 | + ".heap": 0, |
| 55 | + ".stabstr": 0, |
| 56 | + ".interrupts_ram": 0, |
| 57 | + ".init_array": 0, |
| 58 | + ".stab": 0, |
| 59 | + ".ARM.attributes": 7347, |
| 60 | + ".bss": 8517, |
| 61 | + ".flash_config": 16, |
| 62 | + ".interrupts": 1024, |
| 63 | + ".data": 2325, |
| 64 | + ".ARM.exidx": 0, |
| 65 | + ".text": 59906, |
| 66 | + ".jcr": 0 |
| 67 | + }, |
| 68 | + "Fill": { |
| 69 | + "unknown": 12, |
| 70 | + ".ARM": 0, |
| 71 | + ".ARM.extab": 0, |
| 72 | + ".init": 0, |
| 73 | + "OUTPUT": 0, |
| 74 | + ".stack": 0, |
| 75 | + ".eh_frame": 0, |
| 76 | + ".fini_array": 0, |
| 77 | + ".heap": 65536, |
| 78 | + ".stabstr": 0, |
| 79 | + ".interrupts_ram": 1024, |
| 80 | + ".init_array": 0, |
| 81 | + ".stab": 0, |
| 82 | + ".ARM.attributes": 0, |
| 83 | + ".bss": 2235, |
| 84 | + ".flash_config": 0, |
| 85 | + ".interrupts": 0, |
| 86 | + ".data": 3, |
| 87 | + ".ARM.exidx": 0, |
| 88 | + ".text": 136, |
| 89 | + ".jcr": 0 |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + self.memap_parser.compute_report() |
| 94 | + |
| 95 | + def tearDown(self): |
| 96 | + """ |
| 97 | + Called after each test case |
| 98 | +
|
| 99 | + :return: |
| 100 | + """ |
| 101 | + pass |
| 102 | + |
| 103 | + def generate_test_helper(self, output_type, file_output=None): |
| 104 | + """ |
| 105 | + Helper that ensures that the member variables "modules", "mem_report", |
| 106 | + and "mem_summary" are unchanged after calling "generate_output" |
| 107 | + |
| 108 | + :param output_type: type string that is passed to "generate_output" |
| 109 | + :param file_output: path to output file that is passed to "generate_output" |
| 110 | + :return: |
| 111 | + """ |
| 112 | + |
| 113 | + old_modules = deepcopy(self.memap_parser.modules) |
| 114 | + old_report = deepcopy(self.memap_parser.mem_report) |
| 115 | + old_summary = deepcopy(self.memap_parser.mem_summary) |
| 116 | + self.memap_parser.generate_output(output_type, file_output) |
| 117 | + self.assertEqual(self.memap_parser.modules, old_modules, |
| 118 | + "generate_output modified the 'modules' property") |
| 119 | + self.assertEqual(self.memap_parser.mem_report, old_report, |
| 120 | + "generate_output modified the 'mem_report' property") |
| 121 | + self.assertEqual(self.memap_parser.mem_summary, old_summary, |
| 122 | + "generate_output modified the 'mem_summary' property") |
| 123 | + |
| 124 | + def test_report_computed(self): |
| 125 | + """ |
| 126 | + Test ensures the report and summary are computed |
| 127 | + |
| 128 | + :return: |
| 129 | + """ |
| 130 | + self.assertTrue(self.memap_parser.mem_report) |
| 131 | + self.assertTrue(self.memap_parser.mem_summary) |
| 132 | + self.assertEqual(self.memap_parser.mem_report[-1]['summary'], |
| 133 | + self.memap_parser.mem_summary, |
| 134 | + "mem_report did not contain a correct copy of mem_summary") |
| 135 | + |
| 136 | + def test_generate_output_table(self): |
| 137 | + """ |
| 138 | + Test ensures that an output of type "table" can be generated correctly |
| 139 | + |
| 140 | + :return: |
| 141 | + """ |
| 142 | + self.generate_test_helper('table') |
| 143 | + |
| 144 | + def test_generate_output_json(self): |
| 145 | + """ |
| 146 | + Test ensures that an output of type "json" can be generated correctly |
| 147 | + |
| 148 | + :return: |
| 149 | + """ |
| 150 | + file_name = '.json_test_output.json' |
| 151 | + self.generate_test_helper('json', file_output=file_name) |
| 152 | + self.assertTrue(os.path.exists(file_name), "Failed to create json file") |
| 153 | + os.remove(file_name) |
| 154 | + |
| 155 | + def test_generate_output_csv_ci(self): |
| 156 | + """ |
| 157 | + Test ensures that an output of type "csv-ci" can be generated correctly |
| 158 | + |
| 159 | + :return: |
| 160 | + """ |
| 161 | + file_name = '.csv_ci_test_output.csv' |
| 162 | + self.generate_test_helper('csv-ci', file_output=file_name) |
| 163 | + self.assertTrue(os.path.exists(file_name), "Failed to create csv-ci file") |
| 164 | + os.remove(file_name) |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == '__main__': |
| 168 | + unittest.main() |
0 commit comments