Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
memap_test.py
00001 """ 00002 mbed SDK 00003 Copyright (c) 2017 ARM Limited 00004 00005 Licensed under the Apache License, Version 2.0 (the "License"); 00006 you may not use this file except in compliance with the License. 00007 You may obtain a copy of the License at 00008 00009 http://www.apache.org/licenses/LICENSE-2.0 00010 00011 Unless required by applicable law or agreed to in writing, software 00012 distributed under the License is distributed on an "AS IS" BASIS, 00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 See the License for the specific language governing permissions and 00015 limitations under the License. 00016 """ 00017 import sys 00018 from os.path import isfile, join 00019 import json 00020 00021 import pytest 00022 00023 import tools.memap 00024 from tools.memap import MemapParser 00025 from copy import deepcopy 00026 00027 """ 00028 Tests for test_api.py 00029 """ 00030 00031 @pytest.fixture 00032 def memap_parser (): 00033 """ 00034 Called before each test case 00035 00036 :return: 00037 """ 00038 memap_parser = MemapParser() 00039 00040 memap_parser.modules = { 00041 "mbed-os/targets/TARGET/TARGET_MCUS/api/pinmap.o": { 00042 ".text": 1, 00043 ".data": 2, 00044 ".bss": 3, 00045 ".heap": 0, 00046 ".stack": 0, 00047 ".interrupts_ram":0, 00048 ".init":0, 00049 ".ARM.extab":0, 00050 ".ARM.exidx":0, 00051 ".ARM.attributes":0, 00052 ".eh_frame":0, 00053 ".init_array":0, 00054 ".fini_array":0, 00055 ".jcr":0, 00056 ".stab":0, 00057 ".stabstr":0, 00058 ".ARM.exidx":0, 00059 ".ARM":0, 00060 ".interrupts":0, 00061 ".flash_config":0, 00062 "unknown":0, 00063 "OUTPUT":0, 00064 }, 00065 "[lib]/libc.a/lib_a-printf.o": { 00066 ".text": 4, 00067 ".data": 5, 00068 ".bss": 6, 00069 ".heap": 0, 00070 ".stack": 0, 00071 ".interrupts_ram":0, 00072 ".init":0, 00073 ".ARM.extab":0, 00074 ".ARM.exidx":0, 00075 ".ARM.attributes":0, 00076 ".eh_frame":0, 00077 ".init_array":0, 00078 ".fini_array":0, 00079 ".jcr":0, 00080 ".stab":0, 00081 ".stabstr":0, 00082 ".ARM.exidx":0, 00083 ".ARM":0, 00084 ".interrupts":0, 00085 ".flash_config":0, 00086 "unknown":0, 00087 "OUTPUT":0, 00088 }, 00089 "main.o": { 00090 ".text": 7, 00091 ".data": 8, 00092 ".bss": 0, 00093 ".heap": 0, 00094 ".stack": 0, 00095 ".interrupts_ram":0, 00096 ".init":0, 00097 ".ARM.extab":0, 00098 ".ARM.exidx":0, 00099 ".ARM.attributes":0, 00100 ".eh_frame":0, 00101 ".init_array":0, 00102 ".fini_array":0, 00103 ".jcr":0, 00104 ".stab":0, 00105 ".stabstr":0, 00106 ".ARM.exidx":0, 00107 ".ARM":0, 00108 ".interrupts":0, 00109 ".flash_config":0, 00110 "unknown":0, 00111 "OUTPUT":0, 00112 }, 00113 "test.o": { 00114 ".text": 0, 00115 ".data": 0, 00116 ".bss": 0, 00117 ".heap": 0, 00118 ".stack": 0, 00119 ".interrupts_ram":0, 00120 ".init":0, 00121 ".ARM.extab":0, 00122 ".ARM.exidx":0, 00123 ".ARM.attributes":0, 00124 ".eh_frame":0, 00125 ".init_array":0, 00126 ".fini_array":0, 00127 ".jcr":0, 00128 ".stab":0, 00129 ".stabstr":0, 00130 ".ARM.exidx":0, 00131 ".ARM":0, 00132 ".interrupts":0, 00133 ".flash_config":0, 00134 "unknown":0, 00135 "OUTPUT":0, 00136 }, 00137 } 00138 return memap_parser 00139 00140 00141 def generate_test_helper (memap_parser, format, depth, sep, file_output=None): 00142 """ 00143 Helper that tests that the member variables "modules" is 00144 unchanged after calling "generate_output" 00145 00146 :param memap_parser: the parser object 00147 :param depth: how much detail to put in the report 00148 :param format: the file type to output 00149 :param file_output: the file to output to 00150 """ 00151 old_modules = deepcopy(memap_parser.modules) 00152 00153 tools.memap.sep = sep 00154 memap_parser.generate_output(format, depth, file_output=file_output) 00155 00156 assert memap_parser.modules == old_modules,\ 00157 "generate_output modified the 'modules' property" 00158 00159 for file_name in memap_parser.short_modules: 00160 assert(len(file_name.split(tools.memap.sep)) <= depth) 00161 00162 00163 @pytest.mark.parametrize("depth", [1, 2, 20]) 00164 @pytest.mark.parametrize("sep", ["\\", "/"]) 00165 def test_report_computed (memap_parser, depth, sep): 00166 """ 00167 Test that a report and summary are computed 00168 00169 :param memap_parser: Mocked parser 00170 :param depth: the detail of the output 00171 """ 00172 00173 memap_parser.generate_output('table', depth, sep) 00174 00175 # Report is created after generating output 00176 assert memap_parser.mem_summary 00177 assert memap_parser.mem_report 00178 00179 00180 @pytest.mark.parametrize("depth", [1, 2, 20]) 00181 @pytest.mark.parametrize("sep", ["\\", "/"]) 00182 def test_generate_output_table (memap_parser, depth, sep): 00183 """ 00184 Test that an output of type "table" can be generated correctly 00185 :param memap_parser: Mocked parser 00186 :param depth: the detail of the output 00187 """ 00188 generate_test_helper(memap_parser, 'table', depth, sep) 00189 00190 00191 @pytest.mark.parametrize("depth", [1, 2, 20]) 00192 @pytest.mark.parametrize("sep", ["\\", "/"]) 00193 def test_generate_output_json (memap_parser, tmpdir, depth, sep): 00194 """ 00195 Test that an output of type "json" can be generated correctly 00196 :param memap_parser: Mocked parser 00197 :param tmpdir: a unique location to place an output file 00198 :param depth: the detail of the output 00199 """ 00200 file_name = str(tmpdir.join('output.json').realpath()) 00201 generate_test_helper(memap_parser, 'json', depth, sep, file_name) 00202 assert isfile(file_name), "Failed to create json file" 00203 json.load(open(file_name)) 00204 00205 00206 @pytest.mark.parametrize("depth", [1, 2, 20]) 00207 @pytest.mark.parametrize("sep", ["\\", "/"]) 00208 def test_generate_output_csv_ci (memap_parser, tmpdir, depth, sep): 00209 """ 00210 Test ensures that an output of type "csv-ci" can be generated correctly 00211 00212 :param memap_parser: Mocked parser 00213 :param tmpdir: a unique location to place an output file 00214 :param depth: the detail of the output 00215 """ 00216 file_name = str(tmpdir.join('output.csv').realpath()) 00217 generate_test_helper(memap_parser, 'csv-ci', depth, sep, file_name) 00218 assert isfile(file_name), "Failed to create csv-ci file"
Generated on Tue Jul 12 2022 14:24:27 by
