Skip to content

Commit

Permalink
Tests: Wrap up making filename
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedThree committed Jan 12, 2024
1 parent dfaca86 commit 2813ac8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
36 changes: 10 additions & 26 deletions tests/test_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "nc_complex/nc_complex.h"
#include "test_utilities.h"

namespace fs = std::filesystem;
using namespace plasmafair::nc_complex::testing;

TEST_CASE("Read test file") {
Expand Down Expand Up @@ -417,13 +416,10 @@ TEST_CASE("Read test file") {
}

TEST_CASE("Write complex-structure variable") {
const auto test_dir = fs::temp_directory_path() / pfnc_complex_dir;
fs::create_directory(test_dir);
const auto full_filename = test_dir / "test_write_struct.nc";
fs::remove(full_filename);
const auto full_filename = test_file("test_write_struct.nc");

int ncid = 0;
REQUIRE_NETCDF(nc_create(full_filename.string().c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));
REQUIRE_NETCDF(nc_create(full_filename.c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));

int x_dim_id = 0;
REQUIRE_NETCDF(nc_def_dim(ncid, "x", len_x, &x_dim_id));
Expand Down Expand Up @@ -524,13 +520,10 @@ TEST_CASE("Write complex-structure variable") {
}

TEST_CASE("Write complex-dimensioned variable") {
const auto test_dir = fs::temp_directory_path() / pfnc_complex_dir;
fs::create_directory(test_dir);
const auto full_filename = test_dir / "test_write_dim.nc";
fs::remove(full_filename);
const auto full_filename = test_file("test_write_dim.nc");

int ncid = 0;
REQUIRE_NETCDF(nc_create(full_filename.string().c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));
REQUIRE_NETCDF(nc_create(full_filename.c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));

int x_dim_id = 0;
REQUIRE_NETCDF(nc_def_dim(ncid, "x", len_x, &x_dim_id));
Expand Down Expand Up @@ -642,13 +635,10 @@ TEST_CASE("Write complex-dimensioned variable") {
}

TEST_CASE("Write custom-type variable") {
const auto test_dir = fs::temp_directory_path() / pfnc_complex_dir;
fs::create_directory(test_dir);
const auto full_filename = test_dir / "test_write_custom_type.nc";
fs::remove(full_filename);
const auto full_filename = test_file("test_write_custom_type.nc");

int ncid = 0;
REQUIRE_NETCDF(nc_create(full_filename.string().c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));
REQUIRE_NETCDF(nc_create(full_filename.c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));

int x_dim_id = 0;
REQUIRE_NETCDF(nc_def_dim(ncid, "x", len_x, &x_dim_id));
Expand Down Expand Up @@ -730,13 +720,10 @@ TEST_CASE("Write custom-type variable") {
}

TEST_CASE("Write custom-type variable (netCDF3)") {
const auto test_dir = fs::temp_directory_path() / pfnc_complex_dir;
fs::create_directory(test_dir);
const auto full_filename = test_dir / "test_write_custom_type_netcdf3.nc";
fs::remove(full_filename);
const auto full_filename = test_file("test_write_custom_type_netcdf3.nc");

int ncid = 0;
REQUIRE_NETCDF(nc_create(full_filename.string().c_str(), NC_CLOBBER, &ncid));
REQUIRE_NETCDF(nc_create(full_filename.c_str(), NC_CLOBBER, &ncid));

int x_dim_id = 0;
REQUIRE_NETCDF(nc_def_dim(ncid, "x", len_x, &x_dim_id));
Expand Down Expand Up @@ -820,13 +807,10 @@ TEST_CASE("Write custom-type variable (netCDF3)") {
}

TEST_CASE("Write custom-type variable with pre-existing type") {
const auto test_dir = fs::temp_directory_path() / pfnc_complex_dir;
fs::create_directory(test_dir);
const auto full_filename = test_dir / "test_write_preexisting_type.nc";
fs::remove(full_filename);
const auto full_filename = test_file("test_write_preexisting_type.nc");

int ncid = 0;
REQUIRE_NETCDF(nc_create(full_filename.string().c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));
REQUIRE_NETCDF(nc_create(full_filename.c_str(), NC_NETCDF4 | NC_CLOBBER, &ncid));

int x_dim_id = 0;
REQUIRE_NETCDF(nc_def_dim(ncid, "x", len_x, &x_dim_id));
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdio>
#include <filesystem>
#include <ostream>
#include <string_view>

#include "nc_complex/nc_complex.h"

Expand Down Expand Up @@ -54,6 +55,16 @@ inline auto test_directory() {
return test_dir;
}

/// Ensure the test directory exists and `filename` does *not* exist in it
inline auto test_file(std::string_view filename) {
namespace fs = std::filesystem;
const auto test_dir = fs::temp_directory_path() / pfnc_complex_dir;
fs::create_directory(test_dir);
const auto full_filename = test_dir / filename;
fs::remove(full_filename);
return full_filename.string();
}

struct NetCDFResult {
explicit NetCDFResult(int result) : ierr(result) {}
operator bool() const { return ierr == NC_NOERR; }
Expand Down

0 comments on commit 2813ac8

Please sign in to comment.