From 8bb21a2c7f1a051c3285a9c9c95d635dab7ceaf9 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 4 Nov 2024 04:58:49 -0800 Subject: [PATCH] add some app test to the C library --- scripts/generateHelicsH.py | 3 +- .../shared_api_library/FederateExport.cpp | 6 +- src/helics/shared_api_library/helicsApps.h | 2 +- .../shared_api_library/helicsAppsExport.cpp | 21 ++--- .../shared_api_library/helicsExport.cpp | 1 + tests/helics/shared_library/CMakeLists.txt | 10 +++ tests/helics/shared_library/appTests.cpp | 87 +++++++++++++++++++ 7 files changed, 115 insertions(+), 15 deletions(-) create mode 100644 tests/helics/shared_library/appTests.cpp diff --git a/scripts/generateHelicsH.py b/scripts/generateHelicsH.py index 0b24f6ced1..68160da0dd 100755 --- a/scripts/generateHelicsH.py +++ b/scripts/generateHelicsH.py @@ -3,7 +3,7 @@ """ Created on Mon Dec 7 14:25:17 2020 -@author: phlpt +@author: Philip Top """ import re @@ -73,6 +73,7 @@ "src/helics/shared_api_library/api-data.h", "src/helics/shared_api_library/helicsData.h", "src/helics/shared_api_library/helicsCore.h", + "src/helics/shared_api_library/helicsApps.h", "src/helics/shared_api_library/ValueFederate.h", "src/helics/shared_api_library/MessageFederate.h", "src/helics/shared_api_library/MessageFilters.h", diff --git a/src/helics/shared_api_library/FederateExport.cpp b/src/helics/shared_api_library/FederateExport.cpp index 1287b42580..428d9ceaa5 100644 --- a/src/helics/shared_api_library/FederateExport.cpp +++ b/src/helics/shared_api_library/FederateExport.cpp @@ -7,11 +7,15 @@ SPDX-License-Identifier: BSD-3-Clause #include "../core/core-exceptions.hpp" #include "../core/coreTypeOperations.hpp" +#include "../core/CoreTypes.hpp" +#include "../application_api/FederateInfo.hpp" #include "../helics.hpp" #include "gmlc/concurrency/TripWire.hpp" #include "helicsCallbacks.h" #include "helicsCore.h" #include "internal/api_objects.h" +#include "api-data.h" +#include "helics/helics_enums.h" #include #include @@ -460,7 +464,7 @@ void helicsFederateInfoSetIntegerProperty(HelicsFederateInfo fedInfo, int intege HelicsFederate generateNewHelicsFederateObject(std::shared_ptr fed, helics::FederateType type) { auto fedI = std::make_unique(); - fedI->fedptr = fed; + fedI->fedptr = std::move(fed); fedI->type = type; fedI->valid = fedValidationIdentifier; auto* hfed = reinterpret_cast(fedI.get()); diff --git a/src/helics/shared_api_library/helicsApps.h b/src/helics/shared_api_library/helicsApps.h index ad290949a9..1be3590a3b 100644 --- a/src/helics/shared_api_library/helicsApps.h +++ b/src/helics/shared_api_library/helicsApps.h @@ -90,7 +90,7 @@ HELICS_EXPORT void helicsAppRunTo(HelicsApp app, HelicsTime stopTime, HelicsErro */ HELICS_EXPORT void helicsAppFinalize(HelicsApp app, HelicsError* err); -/** check if the App is ready to run*/ +/** check if the App is active and ready to run*/ HELICS_EXPORT HelicsBool helicsAppIsActive(HelicsApp app); #ifdef __cplusplus diff --git a/src/helics/shared_api_library/helicsAppsExport.cpp b/src/helics/shared_api_library/helicsAppsExport.cpp index 048882eaf1..5b79e852fd 100644 --- a/src/helics/shared_api_library/helicsAppsExport.cpp +++ b/src/helics/shared_api_library/helicsAppsExport.cpp @@ -12,14 +12,10 @@ SPDX-License-Identifier: BSD-3-Clause #include "helics/helics_apps.hpp" #include "helicsApps.h" #include "internal/api_objects.h" +#include "api-data.h" -#include -#include -#include -#include -#include #include -#include + namespace helics { @@ -54,9 +50,9 @@ helics::apps::App* getApp(HelicsApp app, HelicsError* err) return appObj->app.get(); } -std::shared_ptr getAppSharedPtr(HelicsApp core, HelicsError* err) +std::shared_ptr getAppSharedPtr(HelicsApp app, HelicsError* err) { - auto* appObj = helics::getAppObject(core, err); + auto* appObj = helics::getAppObject(app, err); if (appObj == nullptr) { return nullptr; } @@ -103,6 +99,7 @@ HelicsApp helicsCreateApp(const char* appName, const char* appType, const char* } try { + auto cstring=AS_STRING(configFile); auto app = std::make_unique(); app->valid = helics::appValidationIdentifier; auto nstring = AS_STRING_VIEW(appName); @@ -111,8 +108,8 @@ HelicsApp helicsCreateApp(const char* appName, const char* appType, const char* return nullptr; } - std::string_view appTypeName(appType); - bool loadFile = true; + const std::string_view appTypeName(appType); + bool loadFile = !cstring.empty(); if (fedInfo == nullptr) { helics::FederateInfo newFedInfo = helics::loadFederateInfo(AS_STRING(configFile)); app->app = buildApp(appTypeName, nstring, newFedInfo); @@ -129,8 +126,8 @@ HelicsApp helicsCreateApp(const char* appName, const char* appType, const char* assignError(err, HELICS_ERROR_INVALID_ARGUMENT, invalidAppTypeString); return nullptr; } - if (loadFile) { - app->app->loadFile(AS_STRING(configFile)); + if (loadFile && !cstring.empty()) { + app->app->loadFile(cstring); } auto* retapp = reinterpret_cast(app.get()); getMasterHolder()->addApp(std::move(app)); diff --git a/src/helics/shared_api_library/helicsExport.cpp b/src/helics/shared_api_library/helicsExport.cpp index 891600bbb3..98cb7a3a8b 100644 --- a/src/helics/shared_api_library/helicsExport.cpp +++ b/src/helics/shared_api_library/helicsExport.cpp @@ -14,6 +14,7 @@ SPDX-License-Identifier: BSD-3-Clause #include "helicsApps.h" #include "helicsCore.h" #include "internal/api_objects.h" +#include "api-data.h" #include #include diff --git a/tests/helics/shared_library/CMakeLists.txt b/tests/helics/shared_library/CMakeLists.txt index 4ee53763cf..552bf9acd2 100644 --- a/tests/helics/shared_library/CMakeLists.txt +++ b/tests/helics/shared_library/CMakeLists.txt @@ -30,6 +30,10 @@ set(c_shared_library_test_sources CallbackFederateTests.cpp ) +if(TARGET HELICS::apps) + list(APPEND c_shared_library_test_sources appTests.cpp) +endif() + set(cpp_shared_library_test_sources cpptestFixtures.cpp cppshared-library-tests.cpp @@ -68,6 +72,12 @@ target_compile_definitions( shared-library-tests PRIVATE "-DTEST_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../test_files/\"" ) +if(TARGET HELICS::apps) +target_compile_definitions( + shared-library-tests PRIVATE "-DAPP_TEST_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../apps/test_files/\"" +) +endif() + add_test(NAME shared-library-tests COMMAND shared-library-tests) target_compile_definitions( diff --git a/tests/helics/shared_library/appTests.cpp b/tests/helics/shared_library/appTests.cpp new file mode 100644 index 0000000000..e07d4dd8fa --- /dev/null +++ b/tests/helics/shared_library/appTests.cpp @@ -0,0 +1,87 @@ +/* +Copyright (c) 2017-2024, +Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable +Energy, LLC. See the top-level NOTICE for additional details. All rights reserved. +SPDX-License-Identifier: BSD-3-Clause +*/ + +#include +/** these test cases test out the value converters + */ +#include "ctestFixtures.hpp" +#include "helics/helics.h" +#include +#include +#include +#include +#include + +// select an incorrect app +TEST(app_tests, load_error) +{ + auto err = helicsErrorInitialize(); + + helicsCreateApp("testApp","whatever",NULL,NULL,&err); + EXPECT_NE(err.error_code,0); +} + +/** this the same test as in the player tests +just meant to test the methods in C not the player itself +*/ +TEST(app_tests, simple_player) +{ + auto err = helicsErrorInitialize(); + HelicsFederateInfo fedInfo=helicsCreateFederateInfo(); + helicsFederateInfoSetCoreType(fedInfo,HELICS_CORE_TYPE_TEST,&err); + helicsFederateInfoSetCoreName(fedInfo,"pcore1",&err); + helicsFederateInfoSetCoreInitString(fedInfo,"-f2 --autobroker",&err); + + + auto play1=helicsCreateApp("player1","player",NULL,fedInfo,&err); + EXPECT_TRUE(helicsAppIsActive(play1)==HELICS_TRUE); + + auto play1Fed=helicsAppGetFederate(play1,&err); + EXPECT_TRUE(helicsFederateIsValid(play1Fed)); + + helicsAppLoadFile(play1,(std::string(APP_TEST_DIR) + "example1.player").c_str(),&err); + EXPECT_EQ(err.error_code,0); + auto vFed=helicsCreateValueFederate("block1",fedInfo,&err); + + auto sub1 = helicsFederateRegisterSubscription(vFed,"pub1",nullptr,&err); + auto sub2 = helicsFederateRegisterSubscription(vFed,"pub2",nullptr,&err); + auto err2 = helicsErrorInitialize(); + + auto fut = std::async(std::launch::async, [&play1,&err2]() { helicsAppRun(play1, &err2);}); + helicsFederateEnterExecutingMode(vFed,&err); + auto val = helicsInputGetDouble(sub1,&err); + EXPECT_EQ(val, 0.3); + + auto retTime = helicsFederateRequestTime(vFed,5,&err); + EXPECT_EQ(retTime, 1.0); + val = helicsInputGetDouble(sub1,&err); + EXPECT_EQ(val, 0.5); + val = helicsInputGetDouble(sub2,&err); + EXPECT_DOUBLE_EQ(val, 0.4); + + retTime = helicsFederateRequestTime(vFed,5,&err); + EXPECT_EQ(retTime, 2.0); + val = helicsInputGetDouble(sub1,&err); + EXPECT_EQ(val, 0.7); + val = helicsInputGetDouble(sub2,&err); + EXPECT_EQ(val, 0.6); + + retTime = helicsFederateRequestTime(vFed,5,&err); + EXPECT_EQ(retTime, 3.0); + val = helicsInputGetDouble(sub1,&err); + EXPECT_EQ(val, 0.8); + val = helicsInputGetDouble(sub2,&err); + EXPECT_EQ(val, 0.9); + + retTime = helicsFederateRequestTime(vFed,5,&err); + EXPECT_EQ(retTime, 5.0); + helicsFederateFinalize(vFed,&err); + fut.get(); + EXPECT_EQ(err2.error_code,0); + EXPECT_EQ(err.error_code,0); +} +