PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
anonymous_namespace{testDeckField.cpp} Namespace Reference

Data Structures

struct  SampleDeck
 A deck with one field of each kind the tables have to handle. More...
 

Functions

void check (bool ok, const std::string &what)
 
template<typename Call >
void checkThrows (Call call, const std::string &contains, const std::string &what)
 Fails unless the call throws with a message containing contains.
 
void testDefaultsAndSchema ()
 
void testReadAndWrite ()
 
void testAccepted ()
 
void testKeysAreNamed ()
 
void testValuesCheckedWhenBuilt ()
 
void testPrint ()
 
void testGroups ()
 
void testEditDistance ()
 
void run (void(*fn)(), const std::string &name)
 

Variables

int g_failures = 0
 
int g_checks = 0
 

Function Documentation

◆ check()

void anonymous_namespace{testDeckField.cpp}::check ( bool  ok,
const std::string &  what 
)

Definition at line 27 of file testDeckField.cpp.

27 {
28 ++g_checks;
29 if (ok)
30 return;
31 ++g_failures;
32 std::cerr << " FAIL: " << what << "\n";
33}

References g_checks, and g_failures.

Referenced by testDefaultsAndSchema(), testEditDistance(), testKeysAreNamed(), testPrint(), testReadAndWrite(), and testValuesCheckedWhenBuilt().

Here is the caller graph for this function:

◆ checkThrows()

template<typename Call >
void anonymous_namespace{testDeckField.cpp}::checkThrows ( Call  call,
const std::string &  contains,
const std::string &  what 
)

Fails unless the call throws with a message containing contains.

Definition at line 37 of file testDeckField.cpp.

38 {
39 ++g_checks;
40 try {
41 call();
42 } catch (const std::exception &e) {
43 if (std::string(e.what()).find(contains) != std::string::npos)
44 return;
45 ++g_failures;
46 std::cerr << " FAIL: " << what << ": message was " << e.what();
47 return;
48 }
49 ++g_failures;
50 std::cerr << " FAIL: " << what << ": nothing was thrown\n";
51}

References g_checks, and g_failures.

Referenced by testAccepted(), testGroups(), testKeysAreNamed(), and testValuesCheckedWhenBuilt().

Here is the caller graph for this function:

◆ run()

void anonymous_namespace{testDeckField.cpp}::run ( void(*)()  fn,
const std::string &  name 
)

Definition at line 224 of file testDeckField.cpp.

224 {
225 try {
226 fn();
227 } catch (const std::exception &e) {
228 ++g_checks;
229 ++g_failures;
230 std::cerr << " FAIL: " << name << " threw: " << e.what() << "\n";
231 }
232}

References g_checks, and g_failures.

◆ testAccepted()

void anonymous_namespace{testDeckField.cpp}::testAccepted ( )

Definition at line 119 of file testDeckField.cpp.

119 {
120 std::cout << "accepted values and bounds\n";
121 SampleDeck d;
123 [&] {
124 inp::readFields(d, json{{"Method", "backward_euler"}},
126 },
127 "must be one of", "a value outside the accepted set is rejected");
129 [&] {
130 inp::readFields(d, json{{"Count", 7}}, SampleDeck::fields());
131 },
132 "must be at most", "a value above the upper bound is rejected");
134 [&] {
135 inp::readFields(d, json{{"Count", 0}}, SampleDeck::fields());
136 },
137 "must be at least", "a value below the lower bound is rejected");
138}
nlohmann::ordered_json json
void checkThrows(Call call, const std::string &contains, const std::string &what)
Fails unless the call throws with a message containing contains.
void readFields(Deck &d, const json &j, const std::vector< Field< Deck > > &fs)
Reads every field of the table from the block.
Definition deckField.h:230
A deck with one field of each kind the tables have to handle.
static const std::vector< inp::Field< SampleDeck > > & fields()

References checkThrows(), anonymous_namespace{testDeckField.cpp}::SampleDeck::fields(), and inp::readFields().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testDefaultsAndSchema()

void anonymous_namespace{testDeckField.cpp}::testDefaultsAndSchema ( )

Definition at line 80 of file testDeckField.cpp.

80 {
81 std::cout << "defaults and schema\n";
83 check(d.at("Count") == 2, "Count default is written");
84 check(d.at("Method") == "central_difference", "Method default is written");
85 check(d.find("Optional") == d.end(),
86 "a field whose emit condition is false is absent");
87
89 check(s.size() == 5, "schema reports one entry per field");
90 check(s.at(0).at("key") == "Count", "schema reports the key");
91 check(s.at(0).at("type") == "size_t", "schema reports the type");
92 check(s.at(0).at("default") == 2, "schema reports the default");
93 check(s.at(0).at("doc") == "How many of them", "schema reports the doc");
94 check(s.at(4).find("default") == s.at(4).end(),
95 "schema omits the default of a field that is not written");
96}
void check(bool ok, const std::string &what)
json defaultsJson(const std::vector< Field< Deck > > &fs)
The block with every field at its default.
Definition deckField.h:260
json schemaJson(const std::vector< Field< Deck > > &fs)
Key, type, default and description of every field.
Definition deckField.h:276

References check(), inp::defaultsJson(), anonymous_namespace{testDeckField.cpp}::SampleDeck::fields(), and inp::schemaJson().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testEditDistance()

void anonymous_namespace{testDeckField.cpp}::testEditDistance ( )

Definition at line 217 of file testDeckField.cpp.

217 {
218 std::cout << "edit distance\n";
219 check(inp::editDistance("Count", "Count") == 0, "equal strings");
220 check(inp::editDistance("Coun", "Count") == 1, "one deletion");
221 check(inp::editDistance("", "abc") == 3, "an empty string");
222}
std::size_t editDistance(const std::string &a, const std::string &b)
Number of single character edits between two strings.
Definition deckField.h:354

References check(), and inp::editDistance().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testGroups()

void anonymous_namespace{testDeckField.cpp}::testGroups ( )

Definition at line 194 of file testDeckField.cpp.

194 {
195 std::cout << "quantities given in more than one way\n";
196 const std::vector<inp::OneOf> groups = {
197 {"the horizon", {"Horizon", "Horizon_Mesh_Ratio"}},
198 {"the contact stiffness", {"Kn", "V_Max"}, false},
199 };
200
201 inp::checkGroups(json{{"Horizon", 6.0e-4}, {"Kn", 1.0e12}}, groups);
202 // The second group is not required, so leaving it out is accepted.
203 inp::checkGroups(json{{"Horizon_Mesh_Ratio", 3.0}}, groups);
204
205 checkThrows([&] { inp::checkGroups(json{{"Kn", 1.0e12}}, groups); },
206 "None was given",
207 "a required group with neither key is rejected");
209 [&] {
211 json{{"Horizon", 6.0e-4}, {"Horizon_Mesh_Ratio", 3.0}}, groups);
212 },
213 "These were given: Horizon Horizon_Mesh_Ratio",
214 "a group with both keys is rejected and names them");
215}
void checkGroups(const json &block, const std::vector< OneOf > &groups)
Checks that each group has exactly one of its keys.
Definition deckField.h:317

References inp::checkGroups(), and checkThrows().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testKeysAreNamed()

void anonymous_namespace{testDeckField.cpp}::testKeysAreNamed ( )

Definition at line 140 of file testDeckField.cpp.

140 {
141 std::cout << "keys are named and checked\n";
142 checkThrows([] { inp::applyGiven(json{{"Coun", 3}}, SampleDeck::fields()); },
143 "Closest declared field is Count",
144 "a misspelled key names the closest field");
145 checkThrows([] { inp::applyGiven(json{{"Horizon", 1.}}, SampleDeck::fields()); },
146 "no field named Horizon", "an unrelated key is rejected");
148 "name and value pairs",
149 "a list of values without names is rejected");
150
151 // A key the deck reads outside the table is accepted when declared.
152 json j = inp::applyGiven(json{{"Nested", json::object()}},
153 SampleDeck::fields(), {"Nested"});
154 check(j.find("Nested") != j.end(), "a key named in extra is kept");
155}
json applyGiven(const json &given, const std::vector< Field< Deck > > &fs, const std::vector< std::string > &extra={})
The default block with the given keys replaced.
Definition deckField.h:443

References inp::applyGiven(), check(), checkThrows(), and anonymous_namespace{testDeckField.cpp}::SampleDeck::fields().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testPrint()

void anonymous_namespace{testDeckField.cpp}::testPrint ( )

Definition at line 182 of file testDeckField.cpp.

182 {
183 std::cout << "printed form\n";
184 SampleDeck d;
185 std::ostringstream oss;
186 inp::printFields(d, oss, SampleDeck::fields(), " ");
187 const std::string s = oss.str();
188 check(s.find(" Count = 2") != std::string::npos,
189 "the printed form has Count after the tab prefix");
190 check(s.find(" Method = central_difference") != std::string::npos,
191 "the printed form has Method after the tab prefix");
192}
void printFields(const Deck &d, std::ostringstream &oss, const std::vector< Field< Deck > > &fs, const std::string &tab="")
Appends every field of the table to a stream.
Definition deckField.h:251

References check(), anonymous_namespace{testDeckField.cpp}::SampleDeck::fields(), and inp::printFields().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testReadAndWrite()

void anonymous_namespace{testDeckField.cpp}::testReadAndWrite ( )

Definition at line 98 of file testDeckField.cpp.

98 {
99 std::cout << "read and write\n";
100 json j = inp::applyGiven(json{{"Count", 3}, {"Length", 2.25}},
102 SampleDeck d;
104 check(d.d_count == 3, "Count is read");
105 check(d.d_length == 2.25, "Length is read");
106 check(d.d_method == "central_difference", "Method keeps its default");
107 check(d.d_write, "Write keeps its default");
108
109 json out = json::object();
111 check(out == j, "writing what was read gives the block back");
112
113 // A block with no keys leaves every member at its default.
114 SampleDeck empty;
115 inp::readFields(empty, json::object(), SampleDeck::fields());
116 check(empty.d_count == 2, "an absent key leaves the default");
117}
void writeFields(const Deck &d, json &j, const std::vector< Field< Deck > > &fs)
Writes every field of the table into the block.
Definition deckField.h:237

References inp::applyGiven(), check(), anonymous_namespace{testDeckField.cpp}::SampleDeck::d_count, anonymous_namespace{testDeckField.cpp}::SampleDeck::fields(), inp::readFields(), and inp::writeFields().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testValuesCheckedWhenBuilt()

void anonymous_namespace{testDeckField.cpp}::testValuesCheckedWhenBuilt ( )

Definition at line 157 of file testDeckField.cpp.

157 {
158 std::cout << "values are checked when a deck is built\n";
159 // A deck is rejected where it is built, not only where it is read.
160 checkThrows([] { inp::applyGiven(json{{"Count", 9}}, SampleDeck::fields()); },
161 "must be at most",
162 "a value above the bound is rejected when the deck is built");
164 [] {
165 inp::applyGiven(json{{"Method", "backward_euler"}},
167 },
168 "must be one of",
169 "a value outside the accepted set is rejected when the deck is built");
170
171 json ok = inp::applyGiven(json{{"Count", 3}}, SampleDeck::fields());
172 check(ok.at("Count") == 3, "an accepted value is kept");
173
174 // Naming a field must not change whether it appears in the block.
175 json given = inp::applyGiven(json{{"Optional", -1.}}, SampleDeck::fields());
176 check(given.find("Optional") == given.end(),
177 "giving a value the field does not write leaves it out");
178 json set = inp::applyGiven(json{{"Optional", 2.5}}, SampleDeck::fields());
179 check(set.at("Optional") == 2.5, "giving a value the field writes keeps it");
180}

References inp::applyGiven(), check(), checkThrows(), and anonymous_namespace{testDeckField.cpp}::SampleDeck::fields().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_checks

int anonymous_namespace{testDeckField.cpp}::g_checks = 0

Definition at line 25 of file testDeckField.cpp.

Referenced by check(), checkThrows(), main(), and run().

◆ g_failures

int anonymous_namespace{testDeckField.cpp}::g_failures = 0

Definition at line 24 of file testDeckField.cpp.

Referenced by check(), checkThrows(), main(), and run().