| 1 |
// Copyright (C) 2011 Anders Logg |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// Unit tests for the parameter library |
| 19 |
|
| 20 |
#include <dolfin.h> |
| 21 |
#include <catch.hpp> |
| 22 |
|
| 23 |
using namespace dolfin; |
| 24 |
|
| 25 |
TEST_CASE("parameters io", "[test_parameter_io]" ) |
| 26 |
{ |
| 27 |
SECTION("flat parameters" ) |
| 28 |
{ |
| 29 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 30 |
return; |
| 31 |
|
| 32 |
// Create some parameters |
| 33 |
Parameters p0("test"); |
| 34 |
p0.add("filename", "foo.txt"); |
| 35 |
p0.add("maxiter", 100); |
| 36 |
p0.add("tolerance", 0.001); |
| 37 |
p0.add("monitor_convergence", true); |
| 38 |
|
| 39 |
// Save to file |
| 40 |
File f0("test_parameters.xml"); |
| 41 |
f0 << p0; |
| 42 |
|
| 43 |
// Read from file |
| 44 |
Parameters p1; |
| 45 |
File f1("test_parameters.xml"); |
| 46 |
f1 >> p1; |
| 47 |
|
| 48 |
// Get parameter values |
| 49 |
std::string filename(p1["filename"]); |
| 50 |
std::size_t maxiter(p1["maxiter"]); |
| 51 |
double tolerance(p1["tolerance"]); |
| 52 |
bool monitor_convergence(p1["monitor_convergence"]); |
| 53 |
|
| 54 |
// Check values |
| 55 |
CHECK(filename == "foo.txt"); |
| 56 |
CHECK(maxiter == (std::size_t) 100); |
| 57 |
CHECK(tolerance == Approx(0.001)); |
| 58 |
CHECK(monitor_convergence); |
| 59 |
} |
| 60 |
|
| 61 |
SECTION("nested parameters" ) |
| 62 |
{ |
| 63 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 64 |
return; |
| 65 |
|
| 66 |
// Create some nested parameters |
| 67 |
Parameters p0("test"); |
| 68 |
Parameters p00("sub0"); |
| 69 |
p00.add("filename", "foo.txt"); |
| 70 |
p00.add("maxiter", 100); |
| 71 |
p00.add("tolerance", 0.001); |
| 72 |
p00.add("monitor_convergence", true); |
| 73 |
p0.add("foo", "bar"); |
| 74 |
Parameters p01(p00); |
| 75 |
p01.rename("sub1"); |
| 76 |
p0.add(p00); |
| 77 |
p0.add(p01); |
| 78 |
|
| 79 |
// Save to file |
| 80 |
File f0("test_parameters.xml"); |
| 81 |
f0 << p0; |
| 82 |
|
| 83 |
// Read from file |
| 84 |
Parameters p1; |
| 85 |
File f1("test_parameters.xml"); |
| 86 |
f1 >> p1; |
| 87 |
|
| 88 |
// Get parameter values |
| 89 |
std::string foo(p1["foo"]); |
| 90 |
std::string filename(p1("sub0")["filename"]); |
| 91 |
std::size_t maxiter(p1("sub0")["maxiter"]); |
| 92 |
double tolerance(p1("sub0")["tolerance"]); |
| 93 |
bool monitor_convergence(p1("sub0")["monitor_convergence"]); |
| 94 |
|
| 95 |
// Check values |
| 96 |
CHECK(foo == "bar"); |
| 97 |
CHECK(filename == "foo.txt"); |
| 98 |
CHECK(maxiter == (std::size_t) 100); |
| 99 |
CHECK(tolerance == Approx(0.001)); |
| 100 |
CHECK(monitor_convergence); |
| 101 |
} |
| 102 |
} |