Skip to content

Commit 84991fb

Browse files
committed
Expand microtimer contract tests
1 parent 81a2ea2 commit 84991fb

14 files changed

Lines changed: 492 additions & 371 deletions

File tree

tests/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_executable(microtimer_tests test_all.c)
2+
target_link_libraries(microtimer_tests PRIVATE microtimer)
3+
target_compile_features(microtimer_tests PRIVATE c_std_99)
4+
add_test(NAME microtimer_tests COMMAND microtimer_tests)
5+
6+
add_executable(microtimer_c_consumer consumers/c_consumer.c)
7+
target_link_libraries(microtimer_c_consumer PRIVATE microtimer)
8+
target_compile_features(microtimer_c_consumer PRIVATE c_std_99)
9+
10+
add_executable(microtimer_cpp_consumer consumers/cpp_consumer.cpp)
11+
target_link_libraries(microtimer_cpp_consumer PRIVATE microtimer)
12+
set_target_properties(microtimer_cpp_consumer PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES)
13+
14+
add_subdirectory(compile_fail)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
project(microtimer_add_subdirectory_consumer LANGUAGES C)
3+
4+
add_subdirectory(../.. microtimer-build)
5+
6+
add_executable(add_subdirectory_consumer main.c)
7+
target_link_libraries(add_subdirectory_consumer PRIVATE microtimer::microtimer)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "mtimer.h"
2+
3+
static uint32_t subdir_clock(void)
4+
{
5+
return 0u;
6+
}
7+
8+
static void subdir_cb(uint8_t id, void *ctx)
9+
{
10+
(void)id;
11+
(void)ctx;
12+
}
13+
14+
int main(void)
15+
{
16+
mtimer_t tm;
17+
18+
if (mtimer_init(&tm, subdir_clock) != MTIMER_OK) {
19+
return 1;
20+
}
21+
return mtimer_create(&tm, "subdir", 1u, MTIMER_PERIODIC, subdir_cb, 0) >= 0 ? 0 : 1;
22+
}

tests/compile_fail/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function(microtimer_add_compile_fail_test name source regex)
2+
set(binary_dir "${CMAKE_CURRENT_BINARY_DIR}/${name}")
3+
try_compile(
4+
${name}_RESULT
5+
"${binary_dir}"
6+
"${CMAKE_CURRENT_SOURCE_DIR}/${source}"
7+
CMAKE_FLAGS
8+
"-DCMAKE_C_STANDARD=99"
9+
"-DCMAKE_C_STANDARD_REQUIRED=ON"
10+
"-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
11+
OUTPUT_VARIABLE ${name}_OUTPUT)
12+
13+
if(${name}_RESULT)
14+
message(FATAL_ERROR "compile-fail test '${name}' unexpectedly compiled")
15+
endif()
16+
17+
string(REGEX MATCH "${regex}" ${name}_MATCH "${${name}_OUTPUT}")
18+
if(NOT "${${name}_MATCH}")
19+
message(FATAL_ERROR "compile-fail test '${name}' did not emit expected diagnostic")
20+
endif()
21+
endfunction()
22+
23+
microtimer_add_compile_fail_test(max_timers_zero max_timers_zero.c "MTIMER_MAX_TIMERS must be >= 1")
24+
microtimer_add_compile_fail_test(max_timers_negative max_timers_negative.c "MTIMER_MAX_TIMERS must be >= 1")
25+
microtimer_add_compile_fail_test(max_timers_256 max_timers_256.c "MTIMER_MAX_TIMERS must be <= 255")
26+
microtimer_add_compile_fail_test(max_timers_too_large max_timers_too_large.c "MTIMER_MAX_TIMERS must be <= 255")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define MTIMER_MAX_TIMERS 256
2+
#include "../../include/mtimer_config.h"
3+
4+
int main(void)
5+
{
6+
return 0;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define MTIMER_MAX_TIMERS (-1)
2+
#include "../../include/mtimer_config.h"
3+
4+
int main(void)
5+
{
6+
return 0;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define MTIMER_MAX_TIMERS 1024
2+
#include "../../include/mtimer_config.h"
3+
4+
int main(void)
5+
{
6+
return 0;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define MTIMER_MAX_TIMERS 0
2+
#include "../../include/mtimer_config.h"
3+
4+
int main(void)
5+
{
6+
return 0;
7+
}

tests/consumers/c_consumer.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "mtimer.h"
2+
3+
static uint32_t consumer_clock(void)
4+
{
5+
return 0u;
6+
}
7+
8+
static void consumer_cb(uint8_t id, void *ctx)
9+
{
10+
(void)id;
11+
(void)ctx;
12+
}
13+
14+
int main(void)
15+
{
16+
mtimer_t tm;
17+
18+
if (mtimer_init(&tm, consumer_clock) != MTIMER_OK) {
19+
return 1;
20+
}
21+
if (mtimer_create(&tm, "consumer", 1u, MTIMER_ONESHOT, consumer_cb, 0) < 0) {
22+
return 1;
23+
}
24+
return 0;
25+
}

tests/consumers/cpp_consumer.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extern "C" {
2+
#include "mtimer.h"
3+
}
4+
5+
static uint32_t consumer_clock()
6+
{
7+
return 0u;
8+
}
9+
10+
static void consumer_cb(uint8_t id, void *ctx)
11+
{
12+
(void)id;
13+
(void)ctx;
14+
}
15+
16+
int main()
17+
{
18+
mtimer_t tm;
19+
return mtimer_init(&tm, consumer_clock) == MTIMER_OK &&
20+
mtimer_create(&tm, "cpp", 1u, MTIMER_ONESHOT, consumer_cb, nullptr) >= 0
21+
? 0
22+
: 1;
23+
}

0 commit comments

Comments
 (0)