Skip to content

Commit 1901f5c

Browse files
committed
test: Add zephyr test
Add Zephyr system to libmetal test. Signed-off-by: Wendy Liang <jliang@xilinx.com>
1 parent 7d805ae commit 1901f5c

File tree

8 files changed

+674
-46
lines changed

8 files changed

+674
-46
lines changed

test/CMakeLists.txt

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,70 @@ collect (PROJECT_LIB_HEADERS metal-test.h)
77
collect (PROJECT_LIB_TESTS version.c)
88
collect (PROJECT_LIB_TESTS metal-test.c)
99

10-
collector_list (_list PROJECT_INC_DIRS)
11-
include_directories (${_list} ${CMAKE_CURRENT_SOURCE_DIR})
10+
collector_list (_hdirs PROJECT_INC_DIRS)
11+
include_directories (${_hdirs} ${CMAKE_CURRENT_SOURCE_DIR})
1212

13-
collector_list (_list PROJECT_LIB_DIRS)
14-
link_directories (${_list})
13+
collector_list (_ldirs PROJECT_LIB_DIRS)
14+
link_directories (${_ldirs})
1515

1616
collector_list (_deps PROJECT_LIB_DEPS)
1717

18-
collector_list (_list PROJECT_LIB_TESTS)
19-
20-
if (WITH_SHARED_LIB)
21-
set (_lib ${PROJECT_NAME}-shared)
22-
add_executable (test-${_lib} ${_list})
23-
target_link_libraries (test-${_lib} ${_deps} ${_lib})
24-
install (TARGETS test-${_lib} RUNTIME DESTINATION bin)
25-
if (PROJECT_EC_FLAGS)
26-
string(REPLACE " " ";" _ec_flgs ${PROJECT_EC_FLAGS})
27-
target_compile_options (test-${_lib} PUBLIC ${_ec_flgs})
28-
endif (PROJECT_EC_FLAGS)
29-
add_dependencies (test-${_lib} ${PROJECT_NAME}-shared)
30-
if (WITH_TESTS_EXEC)
31-
add_test (test-${_lib} test-${_lib})
32-
endif (WITH_TESTS_EXEC)
33-
endif (WITH_SHARED_LIB)
34-
35-
if (WITH_STATIC_LIB)
36-
get_property (_linker_options GLOBAL PROPERTY TEST_LINKER_OPTIONS)
37-
set (_lib ${PROJECT_NAME}-static)
38-
add_executable (test-${_lib} ${_list})
39-
target_link_libraries (test-${_lib} -Wl,-Map=test-${_lib}.map ${_linker_options} -Wl,--start-group ${_lib} ${_deps} -Wl,--end-group)
40-
install (TARGETS test-${_lib} RUNTIME DESTINATION bin)
41-
if (PROJECT_EC_FLAGS)
42-
string(REPLACE " " ";" _ec_flgs ${PROJECT_EC_FLAGS})
43-
target_compile_options (test-${_lib} PUBLIC ${_ec_flgs})
44-
endif (PROJECT_EC_FLAGS)
45-
add_dependencies (test-${_lib} ${PROJECT_NAME}-static)
46-
if (WITH_TESTS_EXEC)
47-
add_test (test-${_lib} test-${_lib})
48-
endif (WITH_TESTS_EXEC)
49-
endif (WITH_STATIC_LIB)
50-
51-
collector_list (_headers PROJECT_HDR_TESTS)
52-
foreach (INCLUDE ${_headers})
53-
string (REGEX REPLACE "[^a-zA-Z0-9]+" "-" _f ${INCLUDE})
54-
configure_file (metal-header-template.c ${_f}.c)
55-
list (APPEND _flist "${CMAKE_CURRENT_BINARY_DIR}/${_f}")
56-
endforeach (INCLUDE)
57-
add_library (metal-headers STATIC ${_flist})
58-
59-
collector_list (_list PROJECT_HDR_TESTS)
18+
collector_list (_srcs PROJECT_LIB_TESTS)
6019

20+
if (WITH_ZEPHYR)
21+
set (_tfiles "")
22+
foreach (f ${_srcs})
23+
list(APPEND _tfiles "${CMAKE_CURRENT_SOURCE_DIR}/${f}")
24+
endforeach(f)
25+
target_sources(app PRIVATE ${_tfiles})
26+
target_include_directories (app PRIVATE ${_hdirs} ${CMAKE_CURRENT_SOURCE_DIR})
27+
set (_eldflags "")
28+
foreach (d ${_ldirs})
29+
list (APPEND _eldflags "-L${d}")
30+
endforeach(d)
31+
add_dependencies(app metal)
32+
list (APPEND _deps -lmetal)
33+
target_link_libraries(app LINK_INTERFACE_LIBRARIES "${_eldflags}" ${_deps})
34+
target_compile_options (app PRIVATE -DNOT_HAVE_STRERROR)
35+
else (WITH_ZEPHYR)
36+
if(WITH_SHARED_LIB)
37+
set (_lib ${PROJECT_NAME}-shared)
38+
add_executable (test-${_lib} ${_srcs})
39+
target_link_libraries (test-${_lib} ${_deps} ${_lib})
40+
install (TARGETS test-${_lib} RUNTIME DESTINATION bin)
41+
if (PROJECT_EC_FLAGS)
42+
string(REPLACE " " ";" _ec_flgs ${PROJECT_EC_FLAGS})
43+
target_compile_options (test-${_lib} PUBLIC ${_ec_flgs})
44+
endif (PROJECT_EC_FLAGS)
45+
add_dependencies (test-${_lib} ${PROJECT_NAME}-shared)
46+
if (WITH_TESTS_EXEC)
47+
add_test (test-${_lib} test-${_lib})
48+
endif (WITH_TESTS_EXEC)
49+
endif (WITH_SHARED_LIB)
50+
51+
if (WITH_STATIC_LIB)
52+
get_property (_linker_options GLOBAL PROPERTY TEST_LINKER_OPTIONS)
53+
set (_lib ${PROJECT_NAME}-static)
54+
add_executable (test-${_lib} ${_srcs})
55+
target_link_libraries (test-${_lib} -Wl,-Map=test-${_lib}.map ${_linker_options} -Wl,--start-group ${_lib} ${_deps} -Wl,--end-group)
56+
install (TARGETS test-${_lib} RUNTIME DESTINATION bin)
57+
if (PROJECT_EC_FLAGS)
58+
string(REPLACE " " ";" _ec_flgs ${PROJECT_EC_FLAGS})
59+
target_compile_options (test-${_lib} PUBLIC ${_ec_flgs})
60+
endif (PROJECT_EC_FLAGS)
61+
add_dependencies (test-${_lib} ${PROJECT_NAME}-static)
62+
if (WITH_TESTS_EXEC)
63+
add_test (test-${_lib} test-${_lib})
64+
endif (WITH_TESTS_EXEC)
65+
endif (WITH_STATIC_LIB)
66+
67+
collector_list (_headers PROJECT_HDR_TESTS)
68+
foreach (INCLUDE ${_headers})
69+
string (REGEX REPLACE "[^a-zA-Z0-9]+" "-" _f ${INCLUDE})
70+
configure_file (metal-header-template.c ${_f}.c)
71+
list (APPEND _flist "${CMAKE_CURRENT_BINARY_DIR}/${_f}")
72+
endforeach (INCLUDE)
73+
add_library (metal-headers STATIC ${_flist})
74+
endif (WITH_ZEPHYR)
6175

6276
# vim: expandtab:ts=2:sw=2:smartindent

test/system/zephyr/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
collect (PROJECT_LIB_TESTS main.c)
2+
collect (PROJECT_LIB_TESTS alloc.c)
3+
collect (PROJECT_LIB_TESTS irq.c)
4+
collect (PROJECT_LIB_TESTS mutex.c)
5+
collect (PROJECT_LIB_TESTS atomic.c)
6+
7+
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_MACHINE})
8+
add_subdirectory(${PROJECT_MACHINE})
9+
endif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_MACHINE})
10+
11+
# vim: expandtab:ts=2:sw=2:smartindent

test/system/zephyr/alloc.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2016, Xilinx Inc. and Contributors. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* 3. Neither the name of Xilinx nor the names of its contributors may be used
15+
* to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <stdlib.h>
32+
33+
#include <metal/alloc.h>
34+
#include <metal/log.h>
35+
#include <metal/sys.h>
36+
#include <misc/printk.h>
37+
#include "metal-test-internal.h"
38+
39+
static int alloc(void)
40+
{
41+
void *ptr;
42+
43+
ptr = metal_allocate_memory(1000);
44+
if (!ptr) {
45+
metal_log(METAL_LOG_DEBUG, "failed to allocate memmory\n");
46+
return errno;
47+
}
48+
49+
metal_free_memory(ptr);
50+
51+
return 0;
52+
}
53+
METAL_ADD_TEST(alloc);

test/system/zephyr/atomic.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2016, Xilinx Inc. and Contributors. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* 3. Neither the name of Xilinx nor the names of its contributors may be used
15+
* to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <stdlib.h>
32+
33+
#include <metal/atomic.h>
34+
#include <metal/log.h>
35+
#include <metal/sys.h>
36+
#include "metal-test-internal.h"
37+
38+
static const int atomic_test_count = 1000;
39+
40+
static int atomic(void)
41+
{
42+
atomic_int counter = ATOMIC_VAR_INIT(0);
43+
int value, error=0, i;
44+
45+
for (i = 0; i < atomic_test_count; i++) {
46+
atomic_fetch_add(&counter, 1);
47+
}
48+
49+
value = atomic_load(&counter);
50+
value -= atomic_test_count;
51+
if (value) {
52+
metal_log(METAL_LOG_DEBUG, "counter mismatch, delta = %d\n", value);
53+
error = -1;
54+
}
55+
56+
return error;
57+
}
58+
METAL_ADD_TEST(atomic);

0 commit comments

Comments
 (0)