From 77d837634cc967821a94ca928e797a4bf3640395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 14:07:42 +0200 Subject: [PATCH 1/8] Rename test source files --- src/CMakeLists.txt | 14 +++++++------- src/{test_errors.cpp => errors_test.cpp} | 4 ++-- src/{test_table.cpp => table_test.cpp} | 0 3 files changed, 9 insertions(+), 9 deletions(-) rename src/{test_errors.cpp => errors_test.cpp} (91%) rename src/{test_table.cpp => table_test.cpp} (100%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 76e6d0c..7a93dd7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,14 +10,14 @@ target_link_libraries(table PRIVATE errors) add_dependencies(table data) if (UPPAALLIBS_WITH_TESTS) - add_executable(test_table test_table.cpp) - target_link_libraries(test_table PRIVATE doctest::doctest_with_main) - add_dependencies(test_table table) - add_test(NAME test_table COMMAND test_table) + add_executable(table_test table_test.cpp) + target_link_libraries(table_test PRIVATE doctest::doctest_with_main) + add_dependencies(table_test table) + add_test(NAME table_test COMMAND table_test) if (CMAKE_BUILD_TYPE STREQUAL "Debug") - add_executable(test_errors test_errors.cpp) - target_link_libraries(test_errors PRIVATE errors doctest::doctest_with_main) - add_test(NAME test_errors COMMAND test_errors) + add_executable(errors_test errors_test.cpp) + target_link_libraries(errors_test PRIVATE errors doctest::doctest_with_main) + add_test(NAME errors_test COMMAND errors_test) endif() endif (UPPAALLIBS_WITH_TESTS) \ No newline at end of file diff --git a/src/test_errors.cpp b/src/errors_test.cpp similarity index 91% rename from src/test_errors.cpp rename to src/errors_test.cpp index fa204ee..70046a6 100644 --- a/src/test_errors.cpp +++ b/src/errors_test.cpp @@ -26,8 +26,8 @@ TEST_CASE("Error message") CHECK(message == "Testing: errors 42 3.141000"); const auto at_pos = content.find(" at ", in_pos + 1); REQUIRE(at_pos != std::string_view::npos); - const auto test_errors_pos = content.find("test_errors.cpp", at_pos + 4); + const auto test_errors_pos = content.find("errors_test.cpp", at_pos + 4); REQUIRE(test_errors_pos != std::string_view::npos); const auto location = content.substr(test_errors_pos); - CHECK(location == "test_errors.cpp:12\n"); + CHECK(location == "errors_test.cpp:12\n"); } \ No newline at end of file diff --git a/src/test_table.cpp b/src/table_test.cpp similarity index 100% rename from src/test_table.cpp rename to src/table_test.cpp From ac8d2e48a2d55fd60d2bdfc15de87656a06afc37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 14:24:12 +0200 Subject: [PATCH 2/8] Added leak sanitizer --- CMakeLists.txt | 3 +-- cmake/sanitizers.cmake | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b53d656..5f4d2f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,5 @@ cmake_minimum_required(VERSION 3.15) - -project(uppaal_libs) +project(uppaal_libs LANGUAGES C CXX) include(cmake/stdcpp.cmake) include(cmake/sanitizers.cmake) diff --git a/cmake/sanitizers.cmake b/cmake/sanitizers.cmake index 48e138d..9c2c293 100644 --- a/cmake/sanitizers.cmake +++ b/cmake/sanitizers.cmake @@ -1,6 +1,7 @@ # Various sanitizers (runtime checks) for debugging option(SSP "Stack Smashing Protector (GCC/Clang/ApplClang on Unix)" OFF) # Available on Windows too option(UBSAN "Undefined Behavior Sanitizer (GCC/Clang/AppleClang on Unix)" OFF) +option(LSAN "Leak Sanitizer (GCC/Clang/AppleClang on Unix)" OFF) option(ASAN "Address Sanitizer (GCC/Clang/AppleClang on Unix, MSVC on Windows)" OFF) option(TSAN "Thread Sanitizer (GCC/Clang/AppleClang on Unix)" OFF) option(RTC_C "Runtime Checks for Conversions (MSVC on Windows)" OFF) @@ -15,7 +16,7 @@ else (SSP) message(STATUS "Disabled Stack Smashing Protector") endif(SSP) -if (ASAN OR UBSAN OR TSAN) +if (LSAN OR ASAN OR UBSAN OR TSAN) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") else() add_compile_options(-fno-omit-frame-pointer) @@ -36,6 +37,18 @@ else (UBSAN) message(STATUS "Disabled Undefined Behavior Sanitizer") endif(UBSAN) +if (LSAN) + if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + add_compile_options(/fsanitize=leak) + else() + add_compile_options(-fsanitize=leak) + add_link_options(-fsanitize=leak) + endif() + message(STATUS "Enabled Leak Sanitizer") +else(LSAN) + message(STATUS "Disabled Leak Sanitizer") +endif(LSAN) + if (ASAN) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") add_compile_options(/fsanitize=address) From 9836640a0300235978ac5a3667f322f95ea850b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 15:48:11 +0200 Subject: [PATCH 3/8] Added libarray example demonstrating storing and loading of double data --- array.xml | 80 ++++++++++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 8 +++++ src/array.cpp | 33 +++++++++++++++++++ src/array.hpp | 17 ++++++++++ src/array_test.cpp | 64 +++++++++++++++++++++++++++++++++++++ src/library.hpp | 2 +- src/table_test.cpp | 5 ++- table.xml | 75 +++---------------------------------------- 8 files changed, 209 insertions(+), 75 deletions(-) create mode 100644 array.xml create mode 100644 src/array.cpp create mode 100644 src/array.hpp create mode 100644 src/array_test.cpp diff --git a/array.xml b/array.xml new file mode 100644 index 0000000..03e588c --- /dev/null +++ b/array.xml @@ -0,0 +1,80 @@ + + + + const int N = 3; + +import "/tmp/uppaal-libs/libarray.so" { + int get_size(); /// size of stored data array + void set_size(int size); /// resize the storage + void load_data(double& values[N], int size); /// Reads the stored data into a given array + void store_data(const double& values[N], int size); /// Stores the data +}; + +int init_size = get_size(); + +int size; +const double in[N] = { 2.1718, 3.1415, 42.0 }; +double out[N]; + + + system P; + + + simulate [<=1; 100] { size } : 100 : P.Done + + + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a93dd7..425410c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,7 +9,15 @@ add_library(table SHARED table.cpp) target_link_libraries(table PRIVATE errors) add_dependencies(table data) +add_library(array SHARED array.cpp) +target_link_libraries(array PRIVATE errors) + if (UPPAALLIBS_WITH_TESTS) + add_executable(array_test array_test.cpp) + target_link_libraries(array_test PRIVATE doctest::doctest_with_main) + add_dependencies(array_test array) + add_test(NAME array_test COMMAND array_test) + add_executable(table_test table_test.cpp) target_link_libraries(table_test PRIVATE doctest::doctest_with_main) add_dependencies(table_test table) diff --git a/src/array.cpp b/src/array.cpp new file mode 100644 index 0000000..24c34de --- /dev/null +++ b/src/array.cpp @@ -0,0 +1,33 @@ +#include "array.hpp" +#include "errors.hpp" +#include + +static auto data = std::vector{}; + +C_PUBLIC int get_size() +{ + return data.size(); +} + +C_PUBLIC void set_size(int size) +{ + if (size < 0) + log_err("size cannot be negative"); + data.resize(size); +} + +C_PUBLIC void load_data(double values[], int size) +{ + if (size < 0) + log_err("size cannot be negative"); + auto sz = std::min(static_cast(size), data.size()); + std::copy(data.data(), data.data()+sz, values); +} + +C_PUBLIC void store_data(const double values[], int size) +{ + if (size < 0) + log_err("size cannot be negative"); + data.resize(size); + data.assign(values, values+size); +} diff --git a/src/array.hpp b/src/array.hpp new file mode 100644 index 0000000..da83e20 --- /dev/null +++ b/src/array.hpp @@ -0,0 +1,17 @@ +#ifndef UPPAAL_LIBS_ARRAY_HPP +#define UPPAAL_LIBS_ARRAY_HPP + +#include "dynlib.h" + +/// Demonstrates data exchange via arrays + +/// Returns the last saved data array size +C_PUBLIC int get_size(); +/// Resized the stored data array +C_PUBLIC void set_size(int size); +/// Reads the stored data into a given array +C_PUBLIC void load_data(double values[], int size); +/// Writes data from a given array to storage +C_PUBLIC void store_data(const double values[], int size); + +#endif // UPPAAL_LIBS_ARRAY_HPP diff --git a/src/array_test.cpp b/src/array_test.cpp new file mode 100644 index 0000000..6402037 --- /dev/null +++ b/src/array_test.cpp @@ -0,0 +1,64 @@ +#include "library.hpp" + +#include + +#include +#include +#include + +#if defined(__linux__) +const auto array_path = std::filesystem::current_path() / "libarray.so"; +#elif defined(__APPLE__) +const auto array_path = std::filesystem::current_path() / "libarray.dylib"; +#elif defined(__MINGW32__) +const auto array_path = std::filesystem::current_path() / "libarray.dll"; +#elif defined(_WIN32) +const auto array_path = [] { + // CMake on Windows puts Release binaries into CMAKE_CURRENT_BINARY_DIR/Release + // otherwise binaries are in CMAKE_CURRENT_BINARY_DIR + auto buffer = std::string(1024, '\0'); + auto size = GetModuleFileNameA( + NULL, buffer.data(), static_cast(buffer.size())); // path to current executable + while (size >= buffer.size()) { + buffer.resize(buffer.size() * 2, '\0'); + size = GetModuleFileNameA(NULL, buffer.data(), static_cast(buffer.size())); + } + buffer.resize(size); // truncate the path + return std::filesystem::path{buffer}.parent_path() / "array.dll"; +}(); +#else +#error("Unknown platform") +#endif + +TEST_CASE("Array storage") +{ + using get_size_fn = int(*)(); + using set_size_fn = void(*)(int); + using load_data_fn = void(*)(double[], int); + using store_data_fn = void(*)(const double[], int); + try { + auto lib = Library(array_path); + auto get_size = lib.lookup("get_size"); + auto set_size = lib.lookup("set_size"); + auto load_data = lib.lookup("load_data"); + auto store_data = lib.lookup("store_data"); + CHECK(get_size() == 0); + set_size(2); + CHECK(get_size() == 2); + const auto input = std::vector{2.1718, 3.1415, 42.0}; + auto output = std::vector(3, 0.0); + load_data(output.data(), output.size()); + CHECK(get_size() == 2); + CHECK(output[0] == 0); + CHECK(output[1] == 0); + store_data(input.data(), input.size()); + CHECK(get_size() == 3); + load_data(output.data(), output.size()); + CHECK(get_size() == 3); + CHECK(output[0] == 2.1718); + CHECK(output[1] == 3.1415); + CHECK(output[2] == 42.0); + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } +} \ No newline at end of file diff --git a/src/library.hpp b/src/library.hpp index b5c9c1e..3757071 100644 --- a/src/library.hpp +++ b/src/library.hpp @@ -18,7 +18,7 @@ class Library void* handle; // library handle public: - Library(const char* filepath): handle{dlopen(filepath, RTLD_LAZY | RTLD_LOCAL)} + Library(const std::string& filepath): handle{dlopen(filepath.c_str(), RTLD_LAZY | RTLD_LOCAL)} { if (!handle) throw std::runtime_error{dlerror()}; diff --git a/src/table_test.cpp b/src/table_test.cpp index cdb04fb..c5f328d 100644 --- a/src/table_test.cpp +++ b/src/table_test.cpp @@ -50,9 +50,8 @@ TEST_CASE("load libtable") auto approx = doctest::Approx{0}.epsilon(0.00001); try { - auto lib_path_str = table_path.string(); - std::cout << "Loading " << lib_path_str << std::endl; - auto lib = Library{lib_path_str.c_str()}; // may throw upon errors + std::cout << "Loading " << table_path << std::endl; + auto lib = Library{table_path.string()}; // may throw upon errors auto table_new_int [[maybe_unused]] = lib.lookup("table_new_int"); auto table_new_double = lib.lookup("table_new_double"); auto table_resize_int [[maybe_unused]] = lib.lookup("table_resize_int"); diff --git a/table.xml b/table.xml index 937bb7c..75ee2e9 100644 --- a/table.xml +++ b/table.xml @@ -1,5 +1,5 @@ - + import "/tmp/uppaal-libs/libtable-dbg.so" { int table_new_int(int rows, int cols, int value); // creates a new table and returns its id @@ -109,82 +109,15 @@ system Process; - simulate 1 [<=1] { id, rows, cols, len } + simulate [<=1] { id, rows, cols, len } - simulate 1 [<=10] { interpolate(id, t, 0, 1), interpolate(id, t, 0, 3) } + simulate [<=10] { interpolate(id, t, 0, 1), interpolate(id, t, 0, 3) } - - - //simulate 1 [<=10] {table[0][0], table[0][1], table[0,2]} + //simulate [<=10] {table[0][0], table[0][1], table[0,2]} From 337e8eaa431a18405035dc8c98c5b6e6820d5b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 15:55:47 +0200 Subject: [PATCH 4/8] Fixed formatting of new files --- src/array.cpp | 9 +++------ src/array_test.cpp | 8 ++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/array.cpp b/src/array.cpp index 24c34de..ebddc7c 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -4,10 +4,7 @@ static auto data = std::vector{}; -C_PUBLIC int get_size() -{ - return data.size(); -} +C_PUBLIC int get_size() { return data.size(); } C_PUBLIC void set_size(int size) { @@ -21,7 +18,7 @@ C_PUBLIC void load_data(double values[], int size) if (size < 0) log_err("size cannot be negative"); auto sz = std::min(static_cast(size), data.size()); - std::copy(data.data(), data.data()+sz, values); + std::copy(data.data(), data.data() + sz, values); } C_PUBLIC void store_data(const double values[], int size) @@ -29,5 +26,5 @@ C_PUBLIC void store_data(const double values[], int size) if (size < 0) log_err("size cannot be negative"); data.resize(size); - data.assign(values, values+size); + data.assign(values, values + size); } diff --git a/src/array_test.cpp b/src/array_test.cpp index 6402037..7dbec32 100644 --- a/src/array_test.cpp +++ b/src/array_test.cpp @@ -32,10 +32,10 @@ const auto array_path = [] { TEST_CASE("Array storage") { - using get_size_fn = int(*)(); - using set_size_fn = void(*)(int); - using load_data_fn = void(*)(double[], int); - using store_data_fn = void(*)(const double[], int); + using get_size_fn = int (*)(); + using set_size_fn = void (*)(int); + using load_data_fn = void (*)(double[], int); + using store_data_fn = void (*)(const double[], int); try { auto lib = Library(array_path); auto get_size = lib.lookup("get_size"); From 3d99339b41c013167573c658178a0bd4dff38d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 16:06:19 +0200 Subject: [PATCH 5/8] Upgraded Github actions script versions --- .github/workflows/build.yml | 4 ++-- .github/workflows/release.yml | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a07ca54..f020a75 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,7 +29,7 @@ jobs: - name: Install clang-format run: sudo apt-get update && sudo apt-get -qqy install clang-format - name: Checkout source - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Check Formatting @@ -48,7 +48,7 @@ jobs: steps: - name: Checkout source - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 92f77c9..1284483 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -26,7 +26,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 @@ -59,7 +59,7 @@ jobs: cp "$BUILD_DIR/src/libtable.so" "$TARGET/libtable${SUFFIX}.so" - name: Upload Artifacts - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: ${{ env.TARGET }} path: ${{ env.TARGET }} @@ -70,7 +70,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Download Windows Artifacts - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: windows-msvc path: windows-msvc @@ -79,7 +79,7 @@ jobs: run: ls -R .. && zip -r9 ../uppaal-libs-windows-msvc.zip * - name: Download macOS Artifacts - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: macos path: macos @@ -88,7 +88,7 @@ jobs: run: ls -R .. && zip -r9 ../uppaal-libs-macos.zip * - name: Download Linux Artifacts - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: linux path: linux From 0c7242a7c4b69aa3baba0f2d63fb52e50df985b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 17:13:30 +0200 Subject: [PATCH 6/8] Added a second array to stress test more complicated function signatures --- array.xml | 134 +++++++++++++++++++++++++++++++-------------- src/array.cpp | 51 ++++++++++++----- src/array.hpp | 24 ++++++-- src/array_test.cpp | 56 +++++++++++++++++-- 4 files changed, 201 insertions(+), 64 deletions(-) diff --git a/array.xml b/array.xml index 03e588c..06ab35e 100644 --- a/array.xml +++ b/array.xml @@ -1,73 +1,127 @@ - const int N = 3; + typedef int[0,INT32_MAX] uint32_t; +const uint32_t N = 3; -import "/tmp/uppaal-libs/libarray.so" { - int get_size(); /// size of stored data array - void set_size(int size); /// resize the storage - void load_data(double& values[N], int size); /// Reads the stored data into a given array - void store_data(const double& values[N], int size); /// Stores the data +import "/tmp/uppaal-libs/libarray.so" +{ + uint32_t get_size(); /// size of stored data array + void set_size(uint32_t size); /// resize the storage + void load_data(double& values[N], uint32_t size); /// Reads the stored data into a given array + void store_data(const double& values[N], uint32_t size); /// Stores the data + + uint32_t get_size2(); /// size of stored data2 array + void set_size2(uint32_t size); /// resize the data2 array + void load_data2(double& values[N], uint32_t size); /// Reads the stored data2 into a given array + void store_data2(const double& values[N], uint32_t size); /// Stores into the data2 + + void load_both(double& values[N], uint32_t size, double& values2[N], uint32_t size2); /// Reads both data and data2 + void store_both(const double& values[N], uint32_t size, const double& values2[N], uint32_t size2); /// Store into both data and data2 }; -int init_size = get_size(); +uint32_t init_size = get_size(); +uint32_t init_size2 = get_size2(); -int size; +uint32_t size; +uint32_t size2; const double in[N] = { 2.1718, 3.1415, 42.0 }; +const double in2[N] = { 42.0, 2.1718, 3.1415 }; double out[N]; +double out2[N]; system P; diff --git a/src/array.cpp b/src/array.cpp index ebddc7c..7a5f4b2 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -1,30 +1,51 @@ #include "array.hpp" -#include "errors.hpp" #include static auto data = std::vector{}; -C_PUBLIC int get_size() { return data.size(); } +C_PUBLIC uint32_t get_size() { return data.size(); } -C_PUBLIC void set_size(int size) -{ - if (size < 0) - log_err("size cannot be negative"); - data.resize(size); -} +C_PUBLIC void set_size(uint32_t size) { data.resize(size); } -C_PUBLIC void load_data(double values[], int size) +C_PUBLIC void load_data(double values[], uint32_t size) { - if (size < 0) - log_err("size cannot be negative"); auto sz = std::min(static_cast(size), data.size()); std::copy(data.data(), data.data() + sz, values); } -C_PUBLIC void store_data(const double values[], int size) +C_PUBLIC void store_data(const double values[], uint32_t size) +{ + data.assign(values, values + size); +} + +static auto data2 = std::vector{}; + +C_PUBLIC uint32_t get_size2() { return data2.size(); } + +C_PUBLIC void set_size2(uint32_t size) { data2.resize(size); } + +C_PUBLIC void load_data2(double values[], uint32_t size) +{ + auto sz = std::min(static_cast(size), data2.size()); + std::copy(data2.data(), data2.data() + sz, values); +} + +C_PUBLIC void store_data2(const double values[], uint32_t size) +{ + data2.assign(values, values + size); +} + +C_PUBLIC void load_both(double values[], uint32_t size, double values2[], uint32_t size2) +{ + auto sz1 = std::min(static_cast(size), data.size()); + auto sz2 = std::min(static_cast(size2), data2.size()); + std::copy(data.data(), data.data() + sz1, values); + std::copy(data2.data(), data2.data() + sz2, values2); +} + +C_PUBLIC void store_both(const double values[], uint32_t size, const double values2[], + uint32_t size2) { - if (size < 0) - log_err("size cannot be negative"); - data.resize(size); data.assign(values, values + size); + data2.assign(values2, values2 + size2); } diff --git a/src/array.hpp b/src/array.hpp index da83e20..94b0048 100644 --- a/src/array.hpp +++ b/src/array.hpp @@ -2,16 +2,32 @@ #define UPPAAL_LIBS_ARRAY_HPP #include "dynlib.h" +#include /// Demonstrates data exchange via arrays /// Returns the last saved data array size -C_PUBLIC int get_size(); +C_PUBLIC uint32_t get_size(); /// Resized the stored data array -C_PUBLIC void set_size(int size); +C_PUBLIC void set_size(uint32_t size); /// Reads the stored data into a given array -C_PUBLIC void load_data(double values[], int size); +C_PUBLIC void load_data(double values[], uint32_t size); /// Writes data from a given array to storage -C_PUBLIC void store_data(const double values[], int size); +C_PUBLIC void store_data(const double values[], uint32_t size); + +/// Returns the last saved data2 array size +C_PUBLIC uint32_t get_size2(); +/// Resized the stored data2 array +C_PUBLIC void set_size2(uint32_t size); +/// Reads the stored data2 into a given array +C_PUBLIC void load_data2(double values[], uint32_t size); +/// Writes a given array into data2 +C_PUBLIC void store_data2(const double values[], uint32_t size); + +/// Reads the stored data2 into a given array +C_PUBLIC void load_both(double values1[], uint32_t size1, double values2[], uint32_t size2); +/// Writes a given array into data2 +C_PUBLIC void store_both(const double values1[], uint32_t size1, const double values2[], + uint32_t size2); #endif // UPPAAL_LIBS_ARRAY_HPP diff --git a/src/array_test.cpp b/src/array_test.cpp index 7dbec32..e1d1498 100644 --- a/src/array_test.cpp +++ b/src/array_test.cpp @@ -32,16 +32,28 @@ const auto array_path = [] { TEST_CASE("Array storage") { - using get_size_fn = int (*)(); - using set_size_fn = void (*)(int); - using load_data_fn = void (*)(double[], int); - using store_data_fn = void (*)(const double[], int); + using get_size_fn = uint32_t (*)(); + using set_size_fn = void (*)(uint32_t); + using load_data_fn = void (*)(double[], uint32_t); + using store_data_fn = void (*)(const double[], uint32_t); + using load_both_fn = void (*)(double[], uint32_t, double[], uint32_t); + using store_both_fn = void (*)(const double[], uint32_t, const double[], uint32_t); try { auto lib = Library(array_path); auto get_size = lib.lookup("get_size"); auto set_size = lib.lookup("set_size"); auto load_data = lib.lookup("load_data"); auto store_data = lib.lookup("store_data"); + + auto get_size2 = lib.lookup("get_size2"); + auto set_size2 = lib.lookup("set_size2"); + auto load_data2 = lib.lookup("load_data2"); + auto store_data2 = lib.lookup("store_data2"); + + auto load_both = lib.lookup("load_both"); + auto store_both = lib.lookup("store_both"); + + /// Test access to the first array: CHECK(get_size() == 0); set_size(2); CHECK(get_size() == 2); @@ -54,11 +66,45 @@ TEST_CASE("Array storage") store_data(input.data(), input.size()); CHECK(get_size() == 3); load_data(output.data(), output.size()); - CHECK(get_size() == 3); + REQUIRE(get_size() == 3); CHECK(output[0] == 2.1718); CHECK(output[1] == 3.1415); CHECK(output[2] == 42.0); + + /// Test access to the second array: + CHECK(get_size2() == 0); + set_size2(2); + CHECK(get_size2() == 2); + load_data2(output.data(), output.size()); + REQUIRE(get_size2() == 2); + CHECK(output[0] == 0); + CHECK(output[1] == 0); + store_data2(input.data(), input.size()); + REQUIRE(get_size2() == 3); + load_data2(output.data(), output.size()); + CHECK(get_size2() == 3); + CHECK(output[0] == 2.1718); + CHECK(output[1] == 3.1415); + CHECK(output[2] == 42.0); + + /// Test access to both arrays at once: + const auto input2 = std::vector{42.0, 2.1718, 3.1415}; + auto output2 = std::vector(3, 0.0); + load_both(output.data(), output.size(), output2.data(), output2.size()); + CHECK(output == input); + CHECK(output2 == input); + set_size(2); + set_size2(2); + CHECK(get_size() == 2); + CHECK(get_size2() == 2); + store_both(input2.data(), input2.size(), input2.data(), input2.size()); + REQUIRE(get_size() == 3); + REQUIRE(get_size2() == 3); + load_both(output.data(), output.size(), output2.data(), output2.size()); + CHECK(output == input2); + CHECK(output2 == input2); } catch (std::exception& e) { std::cerr << e.what() << std::endl; + REQUIRE(false); } } \ No newline at end of file From 5057f4e10243561359a51dea3230dddd10f6f187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 17:17:03 +0200 Subject: [PATCH 7/8] Attempt to fix filesystem paths for MSVC --- src/array_test.cpp | 2 +- src/library.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/array_test.cpp b/src/array_test.cpp index e1d1498..aada1de 100644 --- a/src/array_test.cpp +++ b/src/array_test.cpp @@ -39,7 +39,7 @@ TEST_CASE("Array storage") using load_both_fn = void (*)(double[], uint32_t, double[], uint32_t); using store_both_fn = void (*)(const double[], uint32_t, const double[], uint32_t); try { - auto lib = Library(array_path); + auto lib = Library{array_path.string()}; auto get_size = lib.lookup("get_size"); auto set_size = lib.lookup("set_size"); auto load_data = lib.lookup("load_data"); diff --git a/src/library.hpp b/src/library.hpp index 3757071..4ec3cec 100644 --- a/src/library.hpp +++ b/src/library.hpp @@ -55,7 +55,7 @@ class Library HMODULE handle; // library handle public: - Library(const char* filepath): handle{LoadLibrary(TEXT(filepath))} + Library(const std::string& filepath): handle{LoadLibrary(TEXT(filepath.c_str()))} { if (!handle) { auto err_no = static_cast(::GetLastError()); From c4dfa326176863b4cfcf50fa25b7ae39e90e822c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 4 Apr 2024 18:34:52 +0200 Subject: [PATCH 8/8] Cleaned up the build and table.xml --- compile.sh | 7 +++++++ table.xml | 33 +++++++++++++++------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/compile.sh b/compile.sh index a52f34e..73f596a 100755 --- a/compile.sh +++ b/compile.sh @@ -151,6 +151,13 @@ for target in $targets ; do x86_64*mingw32) extension=dll SANITIZE="-DSSP=ON" + libgcc_path=$($target-g++ --print-file-name=libgcc_s_seh-1.dll) + libgcc_path=$(realpath "$libgcc_path") + libgcc_path=$(dirname "$libgcc_path") + libwinpthread_path=$($target-g++ --print-file-name=libwinpthread-1.dll) + libwinpthread_path=$(realpath "$libwinpthread_path") + libwinpthread_path=$(dirname "$libwinpthread_path") + export WINEPATH="$libwinpthread_path;$libgcc_path" ;; *) echo "Unknown target platform: $target" diff --git a/table.xml b/table.xml index 75ee2e9..71ff154 100644 --- a/table.xml +++ b/table.xml @@ -5,20 +5,20 @@ int table_new_int(int rows, int cols, int value); // creates a new table and returns its id int table_new_double(int rows, int cols, double value); // creates a new table and returns its id int table_read_csv(const string& filename, int skip_lines); // reads table from csv file and returns id - int table_write_csv(int id, const string& filename); // writes table to csv file and returns number of rows + int table_write_csv(int id, const string& filename); // writes table to csv file and returns number of rows int table_copy(int id); // creates a new table copy and returns its id int table_clear(int id); // releases resources associated with the table and returns id int table_resize_int(int id, int rows, int cols, int value); // resizes the table with given dimensions int table_rows(int id); // number of rows in the table int table_cols(int id); // number of columns in the table - int read_int(int id, int row, int col); // read integer at row:col - double read_double(int id, int row, int col); // read double at row:col + int read_int(int id, int row, int col); // read integer at row:col + double read_double(int id, int row, int col); // read double at row:col // interpolated look up for key in key_column (sorted in ascending order) and returns value_column double interpolate(int id, double key, int key_column, int value_column); - void write_int(int id, int row, int col, int value); // write integer at row:col - void write_double(int id, int row, int col, double value); // write double at row:col - void read_int_col(int id, int row, int col, int& items[4], int offset, int count); // read column - void read_int_row(int id, int row, int col, int& items[3], int offset, int count); // read row + void write_int(int id, int row, int col, int value); // write integer at row:col + void write_double(int id, int row, int col, double value); // write double at row:col + void read_int_col(int id, int row, int col, int& items[4], int offset, int count); // read column + void read_int_row(int id, int row, int col, int& items[4], int offset, int count); // read row }; const int id = table_read_csv("/tmp/uppaal-libs/table_input.csv", 0); @@ -42,23 +42,20 @@ int table[rows][cols]; void read_all() { if (rows>0 && cols>0) { - for (i : int[0,rows-1]) - for (j : int[0,cols-1]) - table[i][j] = read_int(_id, i, j); + for (r : int[0,rows-1]) + for (c : int[0,cols-1]) + table[r][c] = read_int(_id, r, c); } } void read_by_row() { -// crash -// for (r : int[0,rows-1]) -/* - const int r = 0; - //row_t row; - int row[3]; + for (r : int[0,rows-1]) { + int row[4]; read_int_row(id, r, 0, row, 0, cols); - table[r] = row; -*/ + for (c : int[0,cols-1]) + table[r][c] = row[c]; + } }