Skip to content

Commit

Permalink
add some app test to the C library
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Nov 4, 2024
1 parent 0bcd855 commit 8bb21a2
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 15 deletions.
3 changes: 2 additions & 1 deletion scripts/generateHelicsH.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Created on Mon Dec 7 14:25:17 2020
@author: phlpt
@author: Philip Top
"""

import re
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/helics/shared_api_library/FederateExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
#include <map>
Expand Down Expand Up @@ -460,7 +464,7 @@ void helicsFederateInfoSetIntegerProperty(HelicsFederateInfo fedInfo, int intege
HelicsFederate generateNewHelicsFederateObject(std::shared_ptr<helics::Federate> fed, helics::FederateType type)
{
auto fedI = std::make_unique<helics::FedObject>();
fedI->fedptr = fed;
fedI->fedptr = std::move(fed);
fedI->type = type;
fedI->valid = fedValidationIdentifier;
auto* hfed = reinterpret_cast<HelicsFederate>(fedI.get());
Expand Down
2 changes: 1 addition & 1 deletion src/helics/shared_api_library/helicsApps.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 deletions src/helics/shared_api_library/helicsAppsExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>


namespace helics {

Expand Down Expand Up @@ -54,9 +50,9 @@ helics::apps::App* getApp(HelicsApp app, HelicsError* err)
return appObj->app.get();
}

std::shared_ptr<helics::apps::App> getAppSharedPtr(HelicsApp core, HelicsError* err)
std::shared_ptr<helics::apps::App> getAppSharedPtr(HelicsApp app, HelicsError* err)
{
auto* appObj = helics::getAppObject(core, err);
auto* appObj = helics::getAppObject(app, err);
if (appObj == nullptr) {
return nullptr;
}
Expand Down Expand Up @@ -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<helics::AppObject>();
app->valid = helics::appValidationIdentifier;
auto nstring = AS_STRING_VIEW(appName);
Expand All @@ -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);
Expand All @@ -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<HelicsApp>(app.get());
getMasterHolder()->addApp(std::move(app));
Expand Down
1 change: 1 addition & 0 deletions src/helics/shared_api_library/helicsExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <atomic>
Expand Down
10 changes: 10 additions & 0 deletions tests/helics/shared_library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
87 changes: 87 additions & 0 deletions tests/helics/shared_library/appTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
/** these test cases test out the value converters
*/
#include "ctestFixtures.hpp"
#include "helics/helics.h"
#include <atomic>
#include <csignal>
#include <future>
#include <string>
#include <thread>

// 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);
}

0 comments on commit 8bb21a2

Please sign in to comment.