Skip to content

Commit

Permalink
test: Add tests for unused function/data under linux
Browse files Browse the repository at this point in the history
  • Loading branch information
zchrissirhcz committed Nov 24, 2024
1 parent 2e2d919 commit 124d39b
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,15 @@ def test_link_as_needed(self):

def test_unused_data_and_function(self):
if os_name == 'mac':
self.check_generate('unused_data_and_function', args='-DCMAKE_BUILD_TYPE=Release REMOVE_UNUSED_DATA_AND_FUNCTION=0')
self.check_generate('unused_data_and_function', args='-DCMAKE_BUILD_TYPE=Release -DREMOVE_UNUSED_DATA_AND_FUNCTION=0')
self.check_build('unused_data_and_function')
cmd = 'objdump -t build/unused_data_and_function/test'
ret, out = check_output(cmd)
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertEqual(len(lines), 11, lines)
self.assertTrue(lines[5].endswith('g F __TEXT,__text _unused_function'))
self.assertTrue(lines[6].endswith('g O __DATA,__data _unused_global_variable'))
self.assertTrue(any(line.strip().endswith("g F __TEXT,__text _unused_function") for line in lines))
self.assertTrue(any(line.strip().endswith("g O __DATA,__data _unused_global_variable") for line in lines))
shutil.rmtree('build/unused_data_and_function')

self.check_generate('unused_data_and_function', args='-DCMAKE_BUILD_TYPE=Release -DREMOVE_UNUSED_DATA_AND_FUNCTION=1')
Expand All @@ -277,9 +276,30 @@ def test_unused_data_and_function(self):
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertEqual(len(lines), 9, lines)
for line in lines:
self.assertTrue(not line.endswith('unused_function'))
self.assertTrue(all(not line.strip().endswith("g F __TEXT,__text _unused_function") for line in lines))
self.assertTrue(all(not line.strip().endswith("g O __DATA,__data _unused_global_variable") for line in lines))
shutil.rmtree('build/unused_data_and_function')
elif os_name == 'linux':
self.check_generate('unused_data_and_function', args='-DCMAKE_BUILD_TYPE=Release -DREMOVE_UNUSED_DATA_AND_FUNCTION=0')
self.check_build('unused_data_and_function')
cmd = 'objdump -t build/unused_data_and_function/test'
ret, out = check_output(cmd)
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertTrue(any(line.strip().endswith(" unused_function") for line in lines))
self.assertTrue(any(line.strip().endswith(" unused_global_variable") for line in lines))
shutil.rmtree('build/unused_data_and_function')

self.check_generate('unused_data_and_function', args='-DCMAKE_BUILD_TYPE=Release -DREMOVE_UNUSED_DATA_AND_FUNCTION=1')
self.check_build('unused_data_and_function')
cmd = 'objdump -t build/unused_data_and_function/test'
ret, out = check_output(cmd)
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertTrue(all(not line.strip().endswith(" unused_function") for line in lines))
self.assertTrue(all(not line.strip().endswith(" unused_global_variable") for line in lines))
shutil.rmtree('build/unused_data_and_function')

if __name__ == "__main__":
Expand Down

0 comments on commit 124d39b

Please sign in to comment.