Skip to content

Commit

Permalink
Minor tidy up, and skip the tests if no file is found. (#295)
Browse files Browse the repository at this point in the history
* Update catch2 (#275)

* Update catch2.

v3.3 has some nice features. Including a SKIP() macro. 💙

* Skip the segfault test.

* SKIP unit tests if sample data not generated.

Since we're introducing a bit of complexity into the unit-testing
framework, at least we should skip the tests (developer quality of
life) with a nice warning explaining why.

* Skip function.

2 lines → 1. And gets rid of another global.

Co-authored-by: willGraham01 <[email protected]>

* Revert to explicitly skipping the broken test.

---------

Co-authored-by: willGraham01 <[email protected]>
  • Loading branch information
samcunliffe and willGraham01 committed May 19, 2023
1 parent fcd12db commit a8d6b56
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 10 deletions.
16 changes: 16 additions & 0 deletions tdms/tests/include/unit_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@
*/
#pragma once

// std
#include <complex>
#include <filesystem>
#include <random>
#include <string>

// external
#include <catch2/catch_test_macros.hpp>

// tdms
#include "globals.h"

using tdms_math_constants::DCPI;

namespace tdms_unit_test_data {

inline std::string cant_find_test_data_message("foo");

inline void skip_if_missing(const std::string &expected_data_file) {
if (!std::filesystem::exists(expected_data_file)) {
// TODO: add explaination of how to run the test data generation?
SKIP("Can't find the test data .hdf5/.mat, probably it wasn't generated "
"before running the unit-test suite. So skipping this test.");
}
}


#ifdef CMAKE_SOURCE_DIR
inline std::string tdms_object_data(std::string(CMAKE_SOURCE_DIR) +
"/tests/unit/benchmark_scripts/"
Expand Down
2 changes: 2 additions & 0 deletions tdms/tests/unit/hdf5_and_tdms_objects/test_hdf5_Cuboid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ TEST_CASE("HDF5: Read Cuboid") {
Cuboid cube;

SECTION("Read into existing object") {
tdms_unit_test_data::skip_if_missing(tdms_object_data);
HDF5Reader MATFile(tdms_object_data);
MATFile.read(&cube);
// Check expected values, noting the -1 offset that is applied because of
Expand All @@ -33,6 +34,7 @@ TEST_CASE("HDF5: Read Cuboid") {
}

SECTION("Throw error if too many elements provided") {
tdms_unit_test_data::skip_if_missing(tdms_bad_object_data);
HDF5Reader MATFile(tdms_bad_object_data);
// Error should be thrown due to incorrect dimensions
REQUIRE_THROWS_AS(MATFile.read(&cube), std::runtime_error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using namespace std;
using tdms_unit_test_data::tdms_object_data;

TEST_CASE("HDF5: Read DispersiveMultiLayer") {
tdms_unit_test_data::skip_if_missing(tdms_object_data);
HDF5Reader MATFile(tdms_object_data);
// read from dispersive_aux group
DispersiveMultiLayer dml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TEST_CASE("HDF5: Read FrequencyVector") {
FrequencyVectors f_vec;

SECTION("Correct data") {
tdms_unit_test_data::skip_if_missing(tdms_object_data);
HDF5Reader MATFile(tdms_object_data);

SECTION("Read into existing FrequencyVectors object") {
Expand All @@ -59,6 +60,7 @@ TEST_CASE("HDF5: Read FrequencyVector") {

// Bad object data provides a 2D array for fx_vec component
SECTION("Incorrect sizes") {
tdms_unit_test_data::skip_if_missing(tdms_bad_object_data);
HDF5Reader MATFile(tdms_bad_object_data);
REQUIRE_THROWS_AS(MATFile.read(&f_vec), std::runtime_error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using tdms_unit_test_data::tdms_object_data;
* _match_ those we expect from the data file.
*/
TEST_CASE("HDF5: Read InterfaceComponent") {
tdms_unit_test_data::skip_if_missing(tdms_object_data);
HDF5Reader MATFile(tdms_object_data);

SECTION("Read into existing InterfaceComponent") {
Expand Down
1 change: 1 addition & 0 deletions tdms/tests/unit/hdf5_io_tests/test_hdf5_dimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using namespace std;
using tdms_unit_test_data::struct_testdata;

TEST_CASE("Fetch dimensions correctly") {
tdms_unit_test_data::skip_if_missing(struct_testdata);
HDF5Reader MATFile(struct_testdata);

SECTION("2D array") {
Expand Down
9 changes: 6 additions & 3 deletions tdms/tests/unit/hdf5_io_tests/test_hdf5_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

using namespace std;
using tdms_tests::create_tmp_dir;
using tdms_unit_test_data::struct_testdata,
using tdms_unit_test_data::cant_find_test_data_message,
tdms_unit_test_data::struct_testdata,
tdms_unit_test_data::tdms_object_data;

TEST_CASE("Test file I/O construction/destruction.") {
Expand Down Expand Up @@ -151,15 +152,17 @@ TEST_CASE("Test read/write wrt standard datatypes") {
filesystem::remove_all(tmp);
}

TEST_CASE("Test group can be found") {
TEST_CASE("Test groups and datasets can be found") {
SECTION("Groups") {
tdms_unit_test_data::skip_if_missing(struct_testdata);
HDF5Reader reader(struct_testdata);
REQUIRE(!reader.contains("group_doesnt_exist"));
REQUIRE(reader.contains("example_struct"));
}
SECTION("DataSets") {
tdms_unit_test_data::skip_if_missing(tdms_object_data);
HDF5Reader reader(tdms_object_data);
REQUIRE(!reader.contains("dataset_doesnt_exist"));
REQUIRE(reader.contains("phasorsurface"));
REQUIRE(!reader.contains("flibble"));
}
}
3 changes: 3 additions & 0 deletions tdms/tests/unit/hdf5_io_tests/test_hdf5_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using tdms_tests::uint16s_to_string;
using tdms_unit_test_data::struct_testdata, tdms_unit_test_data::hdf5_test_file;

TEST_CASE("HDF5: Read from a MATLAB struct") {
tdms_unit_test_data::skip_if_missing(struct_testdata);
HDF5Reader MATFile(struct_testdata);

SECTION("Read numeric scalars") {
Expand Down Expand Up @@ -84,6 +85,7 @@ TEST_CASE("HDF5Reader::read_dataset_in_group") {
bool entries_read_correctly = true;

SECTION(".mat files") {
tdms_unit_test_data::skip_if_missing(struct_testdata);
HDF5Reader Hfile(struct_testdata);

SECTION("Vector [int32]") {
Expand All @@ -106,6 +108,7 @@ TEST_CASE("HDF5Reader::read_dataset_in_group") {
}

SECTION(".hdf5 files") {
tdms_unit_test_data::skip_if_missing(hdf5_test_file);
HDF5Reader Hfile(hdf5_test_file);

// h5py saves int dtype at 64-bit integers
Expand Down
7 changes: 0 additions & 7 deletions tdms/tests/unit/hdf5_io_tests/test_hdf5_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Catch::Approx;
using namespace std;
using tdms_tests::create_tmp_dir;
using tdms_unit_test_data::struct_testdata, tdms_unit_test_data::hdf5_test_file;

TEST_CASE("HDF5Writer: Write doubles to a group.") {

Expand All @@ -35,27 +34,21 @@ TEST_CASE("HDF5Writer: Write doubles to a group.") {

{
HDF5Writer writer(file_name);

{
vector<double> write_me_out = {.0, .1, .2, .3, .4, .5, .6, .7, .8, .9};

writer.write_dataset_to_group(group_name, double_dataset, write_me_out);
}
{
vector<int> write_me_out = {0, 1, 2, 3, 4, 5};

writer.write_dataset_to_group(group_name, int_dataset, write_me_out);
}

REQUIRE(writer.contains(group_name));
}

// Read data back to confirm entries are as expected
{
HDF5Reader reader(file_name);

REQUIRE(reader.contains(group_name));

{
vector<double> read_back_in;
reader.read_dataset_in_group(group_name, double_dataset, read_back_in);
Expand Down

0 comments on commit a8d6b56

Please sign in to comment.