1# Copyright © 2020 Arm Ltd. All rights reserved. 2# Copyright 2020 NXP 3# SPDX-License-Identifier: MIT 4import os 5import sys 6import shutil 7 8import pytest 9 10sys.path.append(os.path.abspath('..')) 11from setup import find_armnn, find_includes, linux_gcc_lib_search, check_armnn_version 12 13 14@pytest.fixture(autouse=True) 15def _setup_armnn(tmpdir): 16 includes = str(os.path.join(tmpdir, 'include')) 17 libs = str(os.path.join(tmpdir, 'lib')) 18 os.environ["TEST_ARMNN_INCLUDE"] = includes 19 os.environ["TEST_ARMNN_LIB"] = libs 20 os.environ["EMPTY_ARMNN_INCLUDE"] = '' 21 22 os.mkdir(includes) 23 os.mkdir(libs) 24 25 with open(os.path.join(libs, "libarmnn.so"), "w"): 26 pass 27 28 with open(os.path.join(libs, "libarmnnSomeThing1.so"), "w"): 29 pass 30 with open(os.path.join(libs, "libarmnnSomeThing1.so.1"), "w"): 31 pass 32 with open(os.path.join(libs, "libarmnnSomeThing1.so.1.2"), "w"): 33 pass 34 35 with open(os.path.join(libs, "libarmnnSomeThing2.so"), "w"): 36 pass 37 38 with open(os.path.join(libs, "libSomeThing3.so"), "w"): 39 pass 40 41 yield 42 43 del os.environ["TEST_ARMNN_INCLUDE"] 44 del os.environ["TEST_ARMNN_LIB"] 45 del os.environ["EMPTY_ARMNN_INCLUDE"] 46 shutil.rmtree(includes) 47 shutil.rmtree(libs) 48 49 50def test_find_armnn(tmpdir): 51 lib_names, lib_paths = find_armnn(lib_name='libarmnn*.so', 52 armnn_libs_env="TEST_ARMNN_LIB", 53 default_lib_search=("/lib",)) 54 armnn_includes = find_includes(armnn_include_env="TEST_ARMNN_INCLUDE") 55 56 assert [':libarmnn.so', ':libarmnnSomeThing1.so', ':libarmnnSomeThing2.so'] == sorted(lib_names) 57 assert [os.path.join(tmpdir, 'lib')] == lib_paths 58 assert [os.path.join(tmpdir, 'include')] == armnn_includes 59 60 61def test_find_armnn_default_path(tmpdir): 62 lib_names, lib_paths = find_armnn(lib_name='libarmnn*.so', 63 armnn_libs_env="RUBBISH_LIB", 64 default_lib_search=(os.environ["TEST_ARMNN_LIB"],)) 65 armnn_includes = find_includes('TEST_ARMNN_INCLUDE') 66 assert [':libarmnn.so', ':libarmnnSomeThing1.so', ':libarmnnSomeThing2.so'] == sorted(lib_names) 67 assert [os.path.join(tmpdir, 'lib')] == lib_paths 68 assert [os.path.join(tmpdir, 'include')] == armnn_includes 69 70 71def test_not_find_armnn(tmpdir): 72 with pytest.raises(RuntimeError) as err: 73 find_armnn(lib_name='libarmnn*.so', armnn_libs_env="RUBBISH_LIB", 74 default_lib_search=("/lib",)) 75 76 assert 'ArmNN library libarmnn*.so was not found in (\'/lib\',)' in str(err.value) 77 78 79@pytest.mark.parametrize("env", ["RUBBISH_INCLUDE", "EMPTY_ARMNN_INCLUDE"]) 80def test_rubbish_armnn_include(tmpdir, env): 81 includes = find_includes(armnn_include_env=env) 82 assert includes == ['/usr/local/include', '/usr/include'] 83 84 85def test_gcc_serch_path(): 86 assert linux_gcc_lib_search() 87 88 89def test_armnn_version(): 90 check_armnn_version('32.0.0', '32.0.0') 91 92 93def test_incorrect_armnn_version(): 94 with pytest.raises(AssertionError) as err: 95 check_armnn_version('32.0.0', '32.1.0') 96 97 assert 'Expected ArmNN version is 32.1.0 but installed ArmNN version is 32.0.0' in str(err.value) 98 99 100def test_armnn_version_patch_does_not_matter(): 101 check_armnn_version('32.0.0', '32.0.1') 102