From 614e6008519bef03196f25e7ee723880d84d7538 Mon Sep 17 00:00:00 2001 From: tezc Date: Sun, 31 Jan 2021 05:21:59 +0300 Subject: [PATCH 1/3] more tests --- array/README.md | 11 +------ buffer/README.md | 71 +++++++++++++++++++++++++++++++++++++++++-- condition/cond_test.c | 4 +-- signal/signal_test.c | 54 ++++++++++++++++++++++++++++++-- time/CMakeLists.txt | 11 +++++++ time/sc_time.c | 12 ++++---- time/time_test.c | 67 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 207 insertions(+), 23 deletions(-) diff --git a/array/README.md b/array/README.md index afd8108..0d7ebc4 100644 --- a/array/README.md +++ b/array/README.md @@ -4,7 +4,7 @@ - Generic array which grows when you add elements. - Index access is possible (e.g float* arr; 'printf("%f", arr[i]')). -- Copy sc_array.h and sc_array.c to your project. +- Lazy allocation. No memory allocation until first 'add'. ##### Usage @@ -41,15 +41,6 @@ sc_array_destroy(p); ``` -#### Internals - -##### Memory -- Single array allocation. -- Lazy allocation. No memory allocation until first 'add'. - -##### Performance -- As all the items are in a single contiguous memory, it gives the best - performance you can expect. ##### Note diff --git a/buffer/README.md b/buffer/README.md index 715a6eb..637326a 100644 --- a/buffer/README.md +++ b/buffer/README.md @@ -10,8 +10,7 @@ - Integer operations are compiled into bounds check + a single MOV instruction on x86. Buffer keeps data in Little Endian format, so on big endian systems, integer put/get is bswap(byte swap) + MOV. -- Just copy sc_buf.h and sc_buf.c to your project. - +- Max capacity is 4GB. Max string size is ~2 GB ##### Usage @@ -61,4 +60,70 @@ int main() return 0; } -``` \ No newline at end of file +``` + +##### Lazy error check + +- Rather than checking error code for each + put(Out of memory/limit exceeded) / get(buffer underflow) operation, + error check can be postponed until end of an operation batch. + +```c + +#include "sc_buf.h" +#include + +int main() +{ + uint32_t val, val2; + struct sc_buf buf; + + sc_buf_init(&buf, 0); + sc_buf_put_32(&buf, 1); // Even if we get OOM, it is safe to continue. + sc_buf_put_32(&buf, 2); + sc_buf_put_32(&buf, 3); + sc_buf_put_32(&buf, 4); + + if (sc_buf_valid(&buf) == false) { + printf("OOM!."); + exit(EXIT_FAILURE); + } + + sc_buf_term(&buf); + + return 0; +} + +``` + +```c + +#include "sc_buf.h" +#include + +int main() +{ + uint32_t val; + struct sc_buf buf; + + sc_buf_init(&buf, 0); + sc_buf_put_32(&buf, 1); + sc_buf_put_32(&buf, 2); + + val = sc_buf_get_32(&buf); + val = sc_buf_get_32(&buf); + val = sc_buf_get_32(&buf); // Buffer underflow here + val = sc_buf_get_32(&buf); // Buffer underflow here + + if (sc_buf_valid(&buf) == false) { + printf("Buffer underflow!."); + exit(EXIT_FAILURE); + } + + sc_buf_term(&buf); + + return 0; +} + +``` + diff --git a/condition/cond_test.c b/condition/cond_test.c index dfa3623..5de2a63 100644 --- a/condition/cond_test.c +++ b/condition/cond_test.c @@ -17,8 +17,8 @@ struct sc_thread #else - #include - #include +#include +#include struct sc_thread { diff --git a/signal/signal_test.c b/signal/signal_test.c index 6d8d954..4741710 100644 --- a/signal/signal_test.c +++ b/signal/signal_test.c @@ -27,7 +27,7 @@ void test1() sc_signal_snprintf(tmp, sizeof(tmp), "%llu", 100000000000ll); assert(strcmp(tmp, "100000000000") == 0); - char* x = (char*)0xabcdef; + char *x = (char *) 0xabcdef; sc_signal_snprintf(tmp, sizeof(tmp), "%p", x); assert(strcmp(tmp, "0xabcdef") == 0); @@ -46,10 +46,60 @@ void test2() assert(sc_signal_init() == 0); } -int main() +#ifdef SC_HAVE_WRAP + #include + #include + #include + #include + +void sig_handler(int signum) +{ +} + +void test3(int argc, char **argv) +{ + + + pid_t pid = fork(); + if (pid == -1) { + assert(true); + } else if (pid) { + int status = 0; + wait(&status); + if (WIFSIGNALED(status)) { + return; + } else { + assert(true); + } + } else { + execvp(argv[1], argv + 1); + exit(1); + } +} + +void test4() +{ + assert(sc_signal_init() == 0); + sc_signal_shutdown_fd = STDOUT_FILENO; + raise(SIGINT); + sleep(3); +} + +#else +void test3() +{ +} +void test4() +{ +} +#endif + +int main(int argc, char **argv) { test1(); test2(); + test3(argc, argv); + test4(); return 0; } diff --git a/time/CMakeLists.txt b/time/CMakeLists.txt index deb360a..baf4972 100644 --- a/time/CMakeLists.txt +++ b/time/CMakeLists.txt @@ -35,6 +35,17 @@ if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR endif () endif () +if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR + "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + + target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP) + target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin) + target_link_options(${PROJECT_NAME}_test PRIVATE + -Wl,--wrap=nanosleep) + endif () +endif () + add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test) SET(MEMORYCHECK_COMMAND_OPTIONS diff --git a/time/sc_time.c b/time/sc_time.c index 363af7e..0ebf501 100644 --- a/time/sc_time.c +++ b/time/sc_time.c @@ -24,13 +24,13 @@ #include "sc_time.h" #if defined(_WIN32) || defined(_WIN64) -#include + #include #include #else -#include -#include -#include -#include + #include + #include + #include + #include #endif uint64_t sc_time_ms() @@ -142,7 +142,7 @@ int sc_time_sleep(uint64_t milliseconds) do { t = rem; rc = nanosleep(&t, &rem); - } while (rc != 0 && errno != EINTR); + } while (rc != 0 && errno == EINTR); return rc; #endif diff --git a/time/time_test.c b/time/time_test.c index e0297a7..b837480 100644 --- a/time/time_test.c +++ b/time/time_test.c @@ -39,9 +39,76 @@ void test2(void) assert(t2 > t1); } +#ifdef SC_HAVE_WRAP + #include + #include + #include + #include + +void sig_handler(int signum) +{ + +} + +void test3(void) +{ + uint64_t t1, t2; + + signal(SIGALRM, sig_handler); + alarm(2); + + t1 = sc_time_mono_ms(); + sc_time_sleep(4000); + t2 = sc_time_mono_ms(); + + assert(t2 > t1); + + alarm(2); + t1 = sc_time_mono_ns(); + sc_time_sleep(4000); + t2 = sc_time_mono_ns(); + + assert(t2 > t1); +} + +bool fail_nanosleep = false; +int __real_nanosleep(const struct timespec *__requested_time, + struct timespec *__remaining); +int __wrap_nanosleep(const struct timespec *__requested_time, + struct timespec *__remaining) +{ + if (fail_nanosleep) { + errno = ERANGE; + return -1; + } + + return __real_nanosleep(__requested_time, __remaining); +} + +void test4(void) +{ + fail_nanosleep = true; + assert(sc_time_sleep(100) != 0); + fail_nanosleep = false; +} + +#else +void test3(void) +{ +} + +void test4(void) +{ + +} +#endif + int main(int argc, char *argv[]) { test1(); test2(); + test3(); + test4(); + return 0; } From 8d1203807c47515ebc63ef5f71ae51d32a9720e6 Mon Sep 17 00:00:00 2001 From: tezc Date: Sun, 31 Jan 2021 05:24:01 +0300 Subject: [PATCH 2/3] more tests --- signal/signal_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/signal_test.c b/signal/signal_test.c index 4741710..9cb5d6a 100644 --- a/signal/signal_test.c +++ b/signal/signal_test.c @@ -86,7 +86,7 @@ void test4() } #else -void test3() +void test3((int argc, char **argv) { } void test4() From bf7140bfc16b16a4a04d02c0fbfda11661aae928 Mon Sep 17 00:00:00 2001 From: tezc Date: Sun, 31 Jan 2021 05:28:49 +0300 Subject: [PATCH 3/3] more tests --- signal/signal_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/signal_test.c b/signal/signal_test.c index 9cb5d6a..38fcfc1 100644 --- a/signal/signal_test.c +++ b/signal/signal_test.c @@ -86,7 +86,7 @@ void test4() } #else -void test3((int argc, char **argv) +void test3(int argc, char **argv) { } void test4()