Skip to content

Commit 9cb1d0f

Browse files
committed
introduce some basic tests of ratequery
1 parent 0ff7b3a commit 9cb1d0f

File tree

4 files changed

+119
-20
lines changed

4 files changed

+119
-20
lines changed

tests/unit/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ add_executable(runVisitorTests test_visitor.cpp)
6767
target_link_libraries(runVisitorTests testdeps)
6868
gtest_discover_tests(runVisitorTests)
6969

70-
# one might argue that the following is more of an integration or end-to-end
71-
# test than a unit-test
72-
add_executable(runGhostZoneTests test_ghost_zone.cpp)
73-
target_link_libraries(runGhostZoneTests grtest_utils testdeps)
74-
gtest_discover_tests(runGhostZoneTests)
70+
# tests of the API functions
71+
# -> one might argue that these are better classified as integration or
72+
# end-to-end tests than as unit-test
73+
add_executable(runApiTests test_ghost_zone.cpp test_api_ratequery.cpp)
74+
target_link_libraries(runApiTests grtest_utils testdeps)
75+
gtest_discover_tests(runApiTests)
7576

7677
# this target tests that the members of the chemistry_data struct can be
7778
# accessed through the "dynamic api." The test cases in this target are

tests/unit/grtest_api_fixture.hpp

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <ostream>
2424
#include <string>
2525

26+
#include "grtest_utils.hpp"
2627
#include "grackle.h"
2728

2829
namespace grtest {
@@ -42,6 +43,7 @@ enum class ChemPreset {
4243
primchem1,
4344
primchem2,
4445
primchem3,
46+
primchem4_dustspecies3
4547
};
4648

4749
inline std::string to_string(const ChemPreset& preset) {
@@ -56,6 +58,8 @@ inline std::string to_string(const ChemPreset& preset) {
5658
return "pc=2";
5759
case ChemPreset::primchem3:
5860
return "pc=3";
61+
case ChemPreset::primchem4_dustspecies3:
62+
return "pc=3-dust_species=4";
5963
}
6064
}
6165

@@ -98,6 +102,14 @@ inline InitStatus setup_chemistry_data_from_preset(chemistry_data* my_chem,
98102
my_chem->dust_chemistry = 1;
99103
return InitStatus::success;
100104
}
105+
case ChemPreset::primchem4_dustspecies3: {
106+
my_chem->primordial_chemistry = 4;
107+
my_chem->dust_chemistry = 1;
108+
my_chem->metal_chemistry = 1;
109+
my_chem->dust_species = 1;
110+
my_chem->use_dust_density_field = 1;
111+
return InitStatus::success;
112+
}
101113
}
102114
}
103115

@@ -195,9 +207,11 @@ class GrackleCtxPack {
195207
bool is_initialized() const { return this->my_chemistry_ != nullptr; }
196208

197209
// getter functions
198-
const code_units& initial_units() const { return this->initial_units_; }
199-
chemistry_data* my_chemistry() { return this->my_chemistry_.get(); }
200-
chemistry_data_storage* my_rates() { return this->my_rates_.get(); }
210+
const code_units& initial_units() const { return initial_units_; }
211+
chemistry_data* my_chemistry() { return my_chemistry_.get(); }
212+
const chemistry_data* my_chemistry() const { return my_chemistry_.get(); }
213+
chemistry_data_storage* my_rates() { return my_rates_.get(); }
214+
const chemistry_data_storage* my_rates() const { return my_rates_.get(); }
201215

202216
/// create an initialized instance from a preset
203217
static GrackleCtxPack create(const FullConfPreset& preset,
@@ -231,25 +245,62 @@ class GrackleCtxPack {
231245
}
232246
};
233247

234-
/// defines a parameterized test-fixture that can used to run a parametrized
248+
/// test-fixture that can used to run one or more tests with a chemsitry data
249+
/// configuration intialized from a given chemistry presets.
250+
///
251+
/// This sets up a GrackleCtxPack (where the contents are configured with
252+
/// appropriate presets) and deallocates that memory at the end of the test.
253+
///
254+
/// How To Use
255+
/// ==========
256+
/// To make use of this fixture in a test-suite called `MyFeatureTest`, you
257+
/// need to either:
258+
/// 1. make a type alias (via `using` or `typedef`), named `MyFeatureTest`, of
259+
/// the relevant instantiation of this class template, OR
260+
/// 2. make a subclass, named `MyFeatureTest`, of the relevant instantiation of
261+
/// this class template
262+
template <ChemPreset chem_preset, InitialUnitPreset unit_preset>
263+
class ConfigPresetFixture : public testing::Test {
264+
protected:
265+
void SetUp() override {
266+
// called immediately after the constructor (but before the test-case)
267+
268+
grtest::InitStatus status;
269+
pack = GrackleCtxPack::create(FullConfPreset{chem_preset, unit_preset},
270+
&status);
271+
if (!pack.is_initialized()) {
272+
if (status == InitStatus::datafile_notfound) {
273+
GTEST_SKIP() << "something went wrong with finding the data file";
274+
} else {
275+
FAIL() << "Error in initialize_chemistry_data.";
276+
}
277+
}
278+
}
279+
280+
GrackleCtxPack pack;
281+
};
282+
283+
/// defines a parameterized test-fixture that can be used to run a parametrized
235284
/// set of tests that are initialized with different chemistry data presets.
236285
///
237286
/// This sets up a GrackleCtxPack (where the contents are configured with
238287
/// appropriate presets) and deallocates that memory at the end of the test.
239288
///
240289
/// How To Use
241290
/// ==========
242-
/// To make use of this, you might create a subclass of this type that is named
243-
/// for the test suite. I don't love this strategy, but it seems to be the
244-
/// standard way to do things. We can revisit this in the future.
291+
/// To make use of this fixture in a test-suite called `MyFeatureTest`, you
292+
/// need to either:
293+
/// 1. make a type alias (via `using` or `typedef`), named `MyFeatureTest`, of
294+
/// this class, OR
295+
/// 2. make a subclass, named `MyFeatureTest`, of this class
245296
class ParametrizedConfigPresetFixture
246297
: public testing::TestWithParam<FullConfPreset> {
247298
protected:
248299
void SetUp() override {
249300
// called immediately after the constructor (but before the test-case)
250301
grtest::InitStatus status;
251-
pack_ = GrackleCtxPack::create(GetParam(), &status);
252-
if (!pack_.is_initialized()) {
302+
pack = GrackleCtxPack::create(GetParam(), &status);
303+
if (!pack.is_initialized()) {
253304
if (status == InitStatus::datafile_notfound) {
254305
GTEST_SKIP() << "something went wrong with finding the data file";
255306
} else {
@@ -258,7 +309,7 @@ class ParametrizedConfigPresetFixture
258309
}
259310
}
260311

261-
GrackleCtxPack pack_;
312+
GrackleCtxPack pack;
262313
};
263314

264315
} // namespace grtest

tests/unit/test_api_ratequery.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// See the LICENSE file for license and copyright information
4+
// SPDX-License-Identifier: NCSA AND BSD-3-Clause
5+
//
6+
//===----------------------------------------------------------------------===//
7+
///
8+
/// @file
9+
/// Define some basic tests of the experimental ratequery API
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <limits>
14+
#include <gtest/gtest.h>
15+
#include "grtest_api_fixture.hpp"
16+
17+
#include "grackle.h"
18+
19+
/// returns the rateid used to denote invalid rate names
20+
grunstable_rateid_type get_invalid_rateid(const grtest::GrackleCtxPack& pack) {
21+
// although we don't use pack, yet a forthcoming refactor requires it
22+
return grunstable_ratequery_id(nullptr);
23+
}
24+
25+
using SimpleRateQueryTest =
26+
grtest::ConfigPresetFixture<grtest::ChemPreset::primchem4_dustspecies3,
27+
grtest::InitialUnitPreset::simple_z0>;
28+
29+
TEST_F(SimpleRateQueryTest, InvalidIthRate) {
30+
const char* name = grunstable_ith_rate(
31+
std::numeric_limits<unsigned long long>::max(), nullptr);
32+
EXPECT_EQ(name, nullptr);
33+
}
34+
35+
TEST_F(SimpleRateQueryTest, EmptyNameQuery) {
36+
grunstable_rateid_type rateid = grunstable_ratequery_id("");
37+
EXPECT_EQ(rateid, get_invalid_rateid(pack));
38+
}
39+
40+
TEST_F(SimpleRateQueryTest, InvalidNameQuery) {
41+
grunstable_rateid_type rateid = grunstable_ratequery_id("NotAValidName");
42+
EXPECT_EQ(rateid, get_invalid_rateid(pack));
43+
}
44+
45+
TEST_F(SimpleRateQueryTest, PtrInvalidRateId) {
46+
double* ptr =
47+
grunstable_ratequery_get_ptr(pack.my_rates(), get_invalid_rateid(pack));
48+
EXPECT_EQ(ptr, nullptr);
49+
}

tests/unit/test_ghost_zone.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,14 @@ bool equal_ghost_values(val_vec_map_t& ref, val_vec_map_t& actual,
249249
return true;
250250
}
251251

252-
class APIGhostZoneTest: public grtest::ParametrizedConfigPresetFixture
253-
{};
252+
using APIGhostZoneTest = grtest::ParametrizedConfigPresetFixture;
254253

255254
TEST_P(APIGhostZoneTest, GridZoneStartEnd) {
256255

257256
grid_props my_grid_props = {{5,6,7}, {1,0,2}};
258257

259-
// alias the pack_ attribute tracked by the fixture
260-
grtest::GrackleCtxPack& pack = pack_;
261-
code_units my_units = pack_.initial_units();
258+
// the pack attribute holds grtest::GrackleCtxPack
259+
code_units my_units = pack.initial_units();
262260

263261
// initialize pseudo random number generator
264262
std::uint32_t seed = 1379069008;

0 commit comments

Comments
 (0)