TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
Test tools

Detailed Description

GoogleTest-based helpers for writing TRIQS C++ unit tests.

The Test tools module is a header-only collection of helpers, built on top of GoogleTest, that the TRIQS C++ test suite uses to compare its core data structures and to set up test executables.

It provides various macros and helper functions to simplify writing unit tests involving TRIQS components.

Macros

#define ASSERT_PRINT(X, Y)
 Check that streaming Y into an output stream produces the string X.
#define EXPECT_BLOCK2_GF_NEAR(X, ...)
 Non-fatal GoogleTest assertion that two Block2 Green's functions are close (see test_block2_gfs_are_close).
#define EXPECT_BLOCK_GF_NEAR(X, ...)
 Non-fatal GoogleTest assertion that two block Green's functions are close (see test_block_gfs_are_close).
#define EXPECT_GF_NEAR(X, ...)
 Non-fatal GoogleTest assertion that two Green's functions are close (see test_gfs_are_close).
#define EXPECT_OPERATOR_NEAR(X, ...)
 Non-fatal GoogleTest assertion that two many-body operators are close (see test_operators_are_close).
#define EXPECT_PRINT(X, Y)
 Check that streaming Y into an output stream produces the string X.
#define EXPECT_TAIL_NEAR(X, ...)
 Non-fatal GoogleTest assertion that two tails are close (see test_tails_are_close).
#define MAKE_MAIN
 Define a main function that runs all registered GoogleTest tests.

Functions

template<typename X, typename Y>
void triqs::gfs::assert_block2_gfs_are_close (X const &x, Y const &y, double precision)
 Assert that two doubly-indexed block Green's functions are numerically close.
template<typename X, typename Y>
void triqs::gfs::assert_block_gfs_are_close (X const &x, Y const &y, double precision)
 Assert that two block Green's functions are numerically close.
template<typename X, typename Y>
void triqs::gfs::assert_gfs_are_close (X const &x, Y const &y, double precision)
 Assert that two Green's functions are numerically close.
template<typename T>
rw_h5 (T const &x, std::string filename="ess", std::string name="x")
 Write an object to an HDF5 file and read it back.
template<typename X, typename Y>
::testing::AssertionResult test_block2_gfs_are_close (X const &x, Y const &y, double precision=1.e-6)
 Check that two block-of-block (Block2) Green's functions are element-wise close.
template<typename X, typename Y>
::testing::AssertionResult test_block_gfs_are_close (X const &x, Y const &y, double precision=1.e-6)
 Check that two block Green's functions are element-wise close.
template<typename X, typename Y>
::testing::AssertionResult test_gfs_are_close (X const &x, Y const &y, double precision=1.e-6)
 Check that two Green's functions are element-wise close.
template<typename X, typename Y>
::testing::AssertionResult test_operators_are_close (X const &x, Y const &y, double precision=1e-6)
 Check that two many-body operators are term-wise equal up to a given precision.
template<typename X, typename Y>
::testing::AssertionResult test_tails_are_close (X const &x, Y const &y, double precision=1.e-6)
 Check that the high-frequency tails of two Green's functions are element-wise close.

Macro Definition Documentation

◆ ASSERT_PRINT

#define ASSERT_PRINT ( X,
Y )

#include <triqs/test_tools/arrays.hpp>

Value:
{ \
std::stringstream ss; \
ss << (Y); \
ASSERT_EQ(X, ss.str()); \
}

Check that streaming Y into an output stream produces the string X.

Like EXPECT_PRINT but uses ASSERT_EQ, i.e. a fatal GoogleTest assertion that aborts the current test on failure.

Parameters
XExpected string.
YObject to be streamed via operator<<.

Definition at line 76 of file arrays.hpp.

◆ EXPECT_PRINT

#define EXPECT_PRINT ( X,
Y )

#include <triqs/test_tools/arrays.hpp>

Value:
{ \
std::stringstream ss; \
ss << (Y); \
EXPECT_EQ(X, ss.str()); \
}

Check that streaming Y into an output stream produces the string X.

Streams Y into a std::stringstream and compares the result to X with EXPECT_EQ, i.e. a non-fatal GoogleTest assertion that lets the test continue on failure.

Parameters
XExpected string.
YObject to be streamed via operator<<.

Definition at line 60 of file arrays.hpp.

◆ MAKE_MAIN

#define MAKE_MAIN

#include <triqs/test_tools/arrays.hpp>

Value:
int main(int argc, char **argv) { \
::testing::InitGoogleTest(&argc, argv); \
if (mpi::has_env) { \
mpi::environment env(argc, argv); \
std::cout << "MPI environment detected\n"; \
return RUN_ALL_TESTS(); \
} else \
return RUN_ALL_TESTS(); \
}

Define a main function that runs all registered GoogleTest tests.

Initializes GoogleTest and runs all tests. When TRIQS is built with MPI support, an mpi::environment is set up so that MPI-aware tests run under a valid communicator.

Definition at line 89 of file arrays.hpp.

Function Documentation

◆ assert_block2_gfs_are_close()

template<typename X, typename Y>
void triqs::gfs::assert_block2_gfs_are_close ( X const & x,
Y const & y,
double precision )

#include <triqs/gfs/gf_tests.hpp>

Assert that two doubly-indexed block Green's functions are numerically close.

Throws a runtime error if the number of blocks or the block names differ, then checks each \( (i, j) \) block pair with assert_gfs_are_close().

Template Parameters
XThe type of the first block2 Green's function.
YThe type of the second block2 Green's function.
Parameters
xThe first block2 Green's function.
yThe second block2 Green's function.
precisionThe maximum allowed element-wise absolute difference per block.

Definition at line 87 of file gf_tests.hpp.

◆ assert_block_gfs_are_close()

template<typename X, typename Y>
void triqs::gfs::assert_block_gfs_are_close ( X const & x,
Y const & y,
double precision )

#include <triqs/gfs/gf_tests.hpp>

Assert that two block Green's functions are numerically close.

Throws a runtime error if the number of blocks or the block names differ, then checks each block pair with assert_gfs_are_close().

Template Parameters
XThe type of the first block Green's function.
YThe type of the second block Green's function.
Parameters
xThe first block Green's function.
yThe second block Green's function.
precisionThe maximum allowed element-wise absolute difference per block.

Definition at line 68 of file gf_tests.hpp.

◆ assert_gfs_are_close()

template<typename X, typename Y>
void triqs::gfs::assert_gfs_are_close ( X const & x,
Y const & y,
double precision )

#include <triqs/gfs/gf_tests.hpp>

Assert that two Green's functions are numerically close.

Throws a runtime error if the meshes of the two Green's functions differ, or if the maximum element-wise absolute difference of their data exceeds precision.

Template Parameters
XThe type of the first Green's function.
YThe type of the second Green's function.
Parameters
xThe first Green's function.
yThe second Green's function.
precisionThe maximum allowed element-wise absolute difference.

Definition at line 48 of file gf_tests.hpp.

◆ rw_h5()

template<typename T>
T rw_h5 ( T const & x,
std::string filename = "ess",
std::string name = "x" )

#include <triqs/test_tools/arrays.hpp>

Write an object to an HDF5 file and read it back.

Serializes x to an HDF5 file, reads it back into a freshly default-constructed object and returns it. Useful for testing that a type's HDF5 read/write round-trip preserves its value.

Template Parameters
TType of the object. Must be default-constructible and h5-serializable.
Parameters
xObject to write to file.
filenameBase name of the HDF5 file (the .h5 extension is appended).
nameName of the dataset inside the file.
Returns
The object read back from the file.

Definition at line 112 of file arrays.hpp.

◆ test_block2_gfs_are_close()

template<typename X, typename Y>
::testing::AssertionResult test_block2_gfs_are_close ( X const & x,
Y const & y,
double precision = 1.e-6 )

#include <triqs/test_tools/gfs.hpp>

Check that two block-of-block (Block2) Green's functions are element-wise close.

Wraps triqs::gfs::assert_block2_gfs_are_close and turns a thrown triqs::exception into a GoogleTest AssertionFailure carrying the exception message.

Template Parameters
XType of the first Block2 Green's function.
YType of the second Block2 Green's function.
Parameters
xFirst Block2 Green's function.
ySecond Block2 Green's function.
precisionAbsolute tolerance used for the comparison.
Returns
AssertionSuccess if the Block2 Green's functions are close, AssertionFailure otherwise.

Definition at line 124 of file gfs.hpp.

◆ test_block_gfs_are_close()

template<typename X, typename Y>
::testing::AssertionResult test_block_gfs_are_close ( X const & x,
Y const & y,
double precision = 1.e-6 )

#include <triqs/test_tools/gfs.hpp>

Check that two block Green's functions are element-wise close.

Wraps triqs::gfs::assert_block_gfs_are_close and turns a thrown triqs::exception into a GoogleTest AssertionFailure carrying the exception message.

Template Parameters
XType of the first block Green's function.
YType of the second block Green's function.
Parameters
xFirst block Green's function.
ySecond block Green's function.
precisionAbsolute tolerance used for the comparison.
Returns
AssertionSuccess if the block Green's functions are close, AssertionFailure otherwise.

Definition at line 101 of file gfs.hpp.

◆ test_gfs_are_close()

template<typename X, typename Y>
::testing::AssertionResult test_gfs_are_close ( X const & x,
Y const & y,
double precision = 1.e-6 )

#include <triqs/test_tools/gfs.hpp>

Check that two Green's functions are element-wise close.

Wraps triqs::gfs::assert_gfs_are_close and turns a thrown triqs::exception into a GoogleTest AssertionFailure carrying the exception message.

Template Parameters
XType of the first Green's function.
YType of the second Green's function.
Parameters
xFirst Green's function.
ySecond Green's function.
precisionAbsolute tolerance used for the comparison.
Returns
AssertionSuccess if the Green's functions are close, AssertionFailure otherwise.

Definition at line 55 of file gfs.hpp.

◆ test_operators_are_close()

template<typename X, typename Y>
::testing::AssertionResult test_operators_are_close ( X const & x,
Y const & y,
double precision = 1e-6 )

#include <triqs/test_tools/many_body_operator.hpp>

Check that two many-body operators are term-wise equal up to a given precision.

Wraps triqs::operators::assert_operators_are_close and turns a thrown triqs::exception into a GoogleTest AssertionFailure carrying the exception message.

Template Parameters
XType of the first operator.
YType of the second operator.
Parameters
xFirst operator.
ySecond operator.
precisionAbsolute tolerance used for the term-wise comparison.
Returns
AssertionSuccess if the operators are close, AssertionFailure otherwise.

Definition at line 48 of file many_body_operator.hpp.

◆ test_tails_are_close()

template<typename X, typename Y>
::testing::AssertionResult test_tails_are_close ( X const & x,
Y const & y,
double precision = 1.e-6 )

#include <triqs/test_tools/gfs.hpp>

Check that the high-frequency tails of two Green's functions are element-wise close.

Wraps triqs::gfs::assert_tails_are_close and turns a thrown triqs::exception into a GoogleTest AssertionFailure carrying the exception message.

Template Parameters
XType of the first tail.
YType of the second tail.
Parameters
xFirst tail.
ySecond tail.
precisionAbsolute tolerance used for the comparison.
Returns
AssertionSuccess if the tails are close, AssertionFailure otherwise.

Definition at line 78 of file gfs.hpp.