Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fprops::from_name #60

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/fprops/fprops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2024 David Andrs <andrsd@gmail.com>
// SPDX-License-Identifier: MIT

#pragma once

#include "SinglePhaseFluidProperties.h"
#include <string>

namespace fprops {

/// Construct fluid properties from a fluid name
///
/// @param name Fluid name
/// @return Fluid properties
SinglePhaseFluidProperties * from_name(const std::string & name);

Check warning on line 15 in include/fprops/fprops.h

View workflow job for this annotation

GitHub Actions / c++ linter

include/fprops/fprops.h:15:30 [modernize-use-trailing-return-type]

use a trailing return type for this function

} // namespace fprops
37 changes: 37 additions & 0 deletions src/fprops.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2024 David Andrs <andrsd@gmail.com>
// SPDX-License-Identifier: MIT

#include "fprops/fprops.h"
#include "fprops/Air.h"
#include "fprops/CarbonDioxide.h"
#include "fprops/Helium.h"
#include "fprops/Nitrogen.h"
#include "fprops/Exception.h"

namespace fprops {

std::string
to_lower(const std::string & name)

Check warning on line 14 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:14:1 [modernize-use-trailing-return-type]

use a trailing return type for this function
{
std::string lower(name);
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
return lower;
}

SinglePhaseFluidProperties *
from_name(const std::string & name)

Check warning on line 22 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:22:1 [modernize-use-trailing-return-type]

use a trailing return type for this function
{
auto n = to_lower(name);

Check warning on line 24 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:24:10 [readability-identifier-length]

variable name 'n' is too short, expected at least 3 characters
if (n == "air")

Check warning on line 25 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:25:20 [readability-braces-around-statements]

statement should be inside braces
return new Air();
else if (n == "carbon_dioxide" || n == "co2")

Check warning on line 27 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:27:5 [readability-else-after-return]

do not use 'else' after 'return'

Check warning on line 27 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:27:50 [readability-braces-around-statements]

statement should be inside braces
return new CarbonDioxide();
else if (n == "helium" || n == "he")

Check warning on line 29 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:29:41 [readability-braces-around-statements]

statement should be inside braces
return new Helium();
else if (n == "nitrogen" || n == "n2")

Check warning on line 31 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:31:43 [readability-braces-around-statements]

statement should be inside braces
return new Nitrogen();
else

Check warning on line 33 in src/fprops.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/fprops.cpp:33:9 [readability-braces-around-statements]

statement should be inside braces
throw Exception("Unknown fluid name '{}'", name);
}

} // namespace fprops
24 changes: 24 additions & 0 deletions test/src/fprops_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "gmock/gmock.h"
#include "fprops/fprops.h"
#include "fprops/Air.h"
#include "fprops/CarbonDioxide.h"
#include "fprops/Helium.h"
#include "fprops/Nitrogen.h"

using namespace fprops;
using namespace testing;

TEST(FpropsTest, from_name)
{
auto air = fprops::from_name("air");
EXPECT_THAT(dynamic_cast<Air *>(air), NotNull());

auto co2 = fprops::from_name("co2");
EXPECT_THAT(dynamic_cast<CarbonDioxide *>(co2), NotNull());

auto he = fprops::from_name("helium");
EXPECT_THAT(dynamic_cast<Helium *>(he), NotNull());

auto n2 = fprops::from_name("nitrogen");
EXPECT_THAT(dynamic_cast<Nitrogen *>(n2), NotNull());
}
Loading