Skip to content

Commit

Permalink
latest from coda-oss
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed May 13, 2022
1 parent 086e364 commit 0add5ad
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
63 changes: 46 additions & 17 deletions externals/coda-oss/modules/c++/include/TestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,35 @@ template<typename TX1, typename TX2>
inline void diePrintf_eq(const std::string& testName, const char* file, const char* func, int line,
const TX1& X1, const TX2& X2)
{
diePrintf("%s (%s,%s,%d): FAILED: Recv'd %s, Expected %s\n", testName, file, func, line, X1, X2);
diePrintf("%s (%s,%s,%d): FAILED: Recv'd %s, Expected %s\n", testName, file, func, line, X1, X2);
}
#define CODA_OSS_test_diePrintf_eq_(X1, X2) test::diePrintf_eq(testName, __FILE__, SYS_FUNC, __LINE__, X1, X2)

template<typename TX1, typename TX2, typename TMsg>
inline void diePrintf_eq_msg(const std::string& testName, const TMsg& msg, const char* file, int line,
const TX1& X1, const TX2& X2)
{
sys::diePrintf("%s (%s,%d): FAILED (%s): Recv'd %s, Expected %s\n", testName.c_str(), file, line, (msg).c_str(),
str::toString((X1)).c_str(), str::toString((X2)).c_str());
}
#define CODA_OSS_test_diePrintf_eq_msg_(msg, X1, X2) test::diePrintf_eq_msg(testName, msg, __FILE__, __LINE__, X1, X2)

template<typename TX1, typename TX2>
inline void diePrintf_ne(const std::string& testName, const char* file, const char* func, int line,
const TX1& X1, const TX2& X2)
{
diePrintf("%s (%s,%s,%d): FAILED: Recv'd %s should not equal %s\n", testName, file, func, line, X1, X2);
}
#define CODA_OSS_test_diePrintf_ne_(X1, X2) test::diePrintf_ne(testName, __FILE__, SYS_FUNC, __LINE__, X1, X2)
#define CODA_OSS_test_diePrintf_not_eq_(X1, X2) test::diePrintf_ne(testName, __FILE__, SYS_FUNC, __LINE__, X1, X2)

template<typename TX1, typename TX2, typename TMsg>
inline void diePrintf_ne_msg(const std::string& testName, const TMsg& msg, const char* file, int line,
const TX1& X1, const TX2& X2)
{
sys::diePrintf("%s (%s,%d): FAILED (%s): Recv'd %s should not equal %s\n", testName.c_str(), file, line, (msg).c_str(),
str::toString((X1)).c_str(), str::toString((X2)).c_str());
}
#define CODA_OSS_test_diePrintf_not_eq_msg_(msg, X1, X2) test::diePrintf_ne_msg(testName, msg, __FILE__, __LINE__, X1, X2)

template <typename TFunc>
inline void check(TFunc f, const std::string& testName)
Expand Down Expand Up @@ -118,6 +136,14 @@ inline void assert_null(const TX& X, const std::string& testName, const char* f
diePrintf("%s (%s,%s,%d): FAILED: Value should be NULL\n", testName, file, func, line);
}
}
template<typename TX>
inline void assert_not_null(const TX& X, const std::string& testName, const char* file, const char* func, int line)
{
if ((X == nullptr) || (!(X != nullptr)))
{
diePrintf("%s (%s,%s,%d): FAILED: Value should *not* be NULL\n", testName, file, func, line);
}
}

template<typename TX>
inline void test_assert_false(const TX& X, const std::string& testName, const char* file, const char* func, int line)
Expand Down Expand Up @@ -146,24 +172,21 @@ inline void test_assert_true(const TX& X, const std::string& testName, const ch
// is std::vector::size() (size_t) compared to a literal 1 which is an "int" not "size_t".

template<typename TX1, typename TX2, typename TEPS>
inline void assert_almost_eq_eps(TX1&& X1, TX2&& X2, TEPS&& EPS,
inline void assert_almost_eq_eps(const TX1& X1, const TX2& X2, const TEPS& EPS,
const std::string& testName, const char* file, const char* func, int line)
{
const auto abs_difference = std::abs(X1 - X2);
if (abs_difference > EPS || IS_NAN(abs_difference))
{
diePrintf("%s (%s,%d): FAILED: Recv'd %s, Expected %s\n", testName, file, func, line, X1, X2);
diePrintf("%s (%s,%s,%d): FAILED: Recv'd %s, Expected %s\n", testName, file, func, line, X1, X2);
}
}
template<typename TX1, typename TX2>
inline void assert_almost_eq(TX1&& X1, TX2&& X2,
inline void assert_almost_eq(const TX1& X1, const TX2& X2,
const std::string& testName, const char* file, const char* func, int line)
{
const auto abs_difference = std::abs(X1 - X2);
if (abs_difference > std::numeric_limits<float>::epsilon() || IS_NAN(abs_difference))
{
diePrintf("%s (%s,%s,%d): FAILED: Recv'd %s, Expected %s\n", testName, file, func, line, X1, X2);
}
const auto eps = std::numeric_limits<float>::epsilon();
assert_almost_eq_eps(X1, X2, eps, testName, file, func, line);
}

template<typename Tmsg>
Expand Down Expand Up @@ -244,6 +267,7 @@ inline int main(TFunc f)
#define TEST_CHECK(X) test::check([&](const std::string& testName) { X(testName); }, #X)
#define TEST_ASSERT(X) test::assert_(X, testName, __FILE__, SYS_FUNC, __LINE__)
#define TEST_ASSERT_NULL(X) test::assert_null(X, testName, __FILE__, SYS_FUNC, __LINE__)
#define TEST_ASSERT_NOT_NULL(X) test::assert_not_null(X, testName, __FILE__, SYS_FUNC, __LINE__)
#define TEST_ASSERT_FALSE(X) test::test_assert_false(X, testName, __FILE__, SYS_FUNC, __LINE__)
#define TEST_ASSERT_TRUE(X) test::test_assert_true(X, testName, __FILE__, SYS_FUNC, __LINE__)
#define TEST_ASSERT_ALMOST_EQ_EPS(X1, X2, EPS) test::assert_almost_eq_eps(X1, X2, EPS, testName, __FILE__, SYS_FUNC, __LINE__)
Expand All @@ -255,6 +279,7 @@ inline int main(TFunc f)
"%s (%s,%s,%d): FAILED: Should have thrown exception: " # Y , testName, __FILE__, SYS_FUNC, __LINE__)
# define TEST_CASE(X) void X(std::string testName)
#define TEST_MAIN(X) int main() { return test::main([&](){X;}); }
#define TEST_MAIN_ARGS(X) int main(int argc, char* argv[]) { return test::main([&](){X;}); }
/*
# define TEST_CHECK(X) try{ X(std::string(#X)); std::cerr << #X << ": PASSED" << std::endl; } \
catch(const except::Throwable& ex) { die_printf("%s: FAILED: Exception thrown: %s\n", std::string(#X).c_str(), ex.toString().c_str()); } \
Expand All @@ -264,14 +289,18 @@ inline int main(TFunc f)
# define TEST_ASSERT_FALSE(X) if ((X)) { die_printf("%s (%s,%s,%d): FAILED: Value should evaluate to false\n", testName.c_str(), __FILE__, SYS_FUNC, __LINE__); }
# define TEST_ASSERT_TRUE(X) if (!(X)) { die_printf("%s (%s,%s,%d): FAILED: Value should evaluate to true\n", testName.c_str(), __FILE__, SYS_FUNC, __LINE__); }
*/
#define CODA_OSS_test_eq_(X1, X2) (((X1) == (X2)) && ((X2) == (X1))) // X1 == X2 means X2 == X1
#define CODA_OSS_test_ne_(X1, X2) (((X1) != (X2)) && ((X2) != (X1))) // X1 != X2 means X2 != X1
#define CODA_OSS_test_ne(X1, X2) (CODA_OSS_test_ne_(X1, X2) && !CODA_OSS_test_eq_(X1, X2))
//#define CODA_OSS_test_eq_(X1, X2) (((X1) == (X2)) && ((X2) == (X1))) // X1 == X2 means X2 == X1
#define CODA_OSS_test_eq_(X1, X2) ((X1) == (X2)) // above breaks CODA :-(
//#define CODA_OSS_test_ne_(X1, X2) (((X1) != (X2)) && ((X2) != (X1))) // X1 != X2 means X2 != X1
#define CODA_OSS_test_ne_(X1, X2) ((X1) != (X2)) // above breaks CODA :-(
//#define CODA_OSS_test_ne(X1, X2) (CODA_OSS_test_ne_(X1, X2) && !CODA_OSS_test_eq_(X1, X2))
#define CODA_OSS_test_ne(X1, X2) CODA_OSS_test_ne_(X1, X2) // above breaks CODA :-(
#define TEST_ASSERT_EQ(X1, X2) if (CODA_OSS_test_ne((X1), (X2))) { CODA_OSS_test_diePrintf_eq_(X1, X2); }
#define TEST_ASSERT_EQ_MSG(msg, X1, X2) if (CODA_OSS_test_ne((X1), (X2))) { die_printf("%s (%s,%d): FAILED (%s): Recv'd %s, Expected %s\n", testName.c_str(), __FILE__, __LINE__, (msg).c_str(), str::toString((X1)).c_str(), str::toString((X2)).c_str()); }
#define CODA_OSS_test_eq(X1, X2) (CODA_OSS_test_eq_(X1, X2) && !CODA_OSS_test_ne_(X1, X2))
#define TEST_ASSERT_NOT_EQ(X1, X2) if (CODA_OSS_test_eq((X1), (X2))) { CODA_OSS_test_diePrintf_ne_(X1, X2); }
#define TEST_ASSERT_NOT_EQ_MSG(msg, X1, X2) if (CODA_OSS_test_eq((X1), (X2))) { die_printf("%s (%s,%d): FAILED (%s): Recv'd %s should not equal %s\n", testName.c_str(), __FILE__, __LINE__, (msg).c_str(), str::toString((X1)).c_str(), str::toString((X2)).c_str()); }
#define TEST_ASSERT_EQ_MSG(msg, X1, X2) if (CODA_OSS_test_ne((X1), (X2))) { CODA_OSS_test_diePrintf_eq_msg_(msg, X1, X2); }
//#define CODA_OSS_test_eq(X1, X2) (CODA_OSS_test_eq_(X1, X2) && !CODA_OSS_test_ne_(X1, X2))
#define CODA_OSS_test_eq(X1, X2) CODA_OSS_test_eq_(X1, X2) // above breaks CODA :-(
#define TEST_ASSERT_NOT_EQ(X1, X2) if (CODA_OSS_test_eq((X1), (X2))) { CODA_OSS_test_diePrintf_not_eq_(X1, X2); }
#define TEST_ASSERT_NOT_EQ_MSG(msg, X1, X2) if (CODA_OSS_test_eq((X1), (X2))) { CODA_OSS_test_diePrintf_not_eq_msg_(msg, X1, X2); }
# define TEST_ASSERT_GREATER_EQ(X1, X2) if ((X1) < X2) { test::diePrintf("%s (%s,%s,%d): FAILED: Value should be greater than or equal\n", testName, __FILE__, SYS_FUNC, __LINE__); }
# define TEST_ASSERT_GREATER(X1, X2) if ((X1) <= X2) { test::diePrintf("%s (%s,%s,%d): FAILED: Value should be greater than\n", testName, __FILE__, SYS_FUNC, __LINE__); }
# define TEST_ASSERT_LESSER_EQ(X1, X2) if ((X1) > X2) { test::diePrintf("%s (%s,%s,%d): FAILED: Value should be less than or equal\n", testName, __FILE__, SYS_FUNC, __LINE__); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "mt/BasicThreadPool.h"
#include "mt/CPUAffinityInitializer.h"
#include "mt/CPUAffinityThreadInitializer.h"
#include "mt/Runnable1D.h"


namespace mt
{
Expand Down Expand Up @@ -105,6 +107,35 @@ namespace mt
waitGroup();
}


/*!
* \brief Runs a given operation on a sequence of numbers in parallel
*
* \param numElements The number of elements to run - op will be called
* with 0 through numElements-1
* \param op A function-like object taking a parameter of type
* size_t which will be called for each number in the
* given range
*/
template <typename OpT>
void run1D(size_t numElements, const OpT& op)
{
std::vector<sys::Runnable*> runnables;
const ThreadPlanner planner(numElements, mNumThreads);

size_t threadNum(0);
size_t startElement(0);
size_t numElementsThisThread(0);
while(planner.getThreadInfo(threadNum++, startElement, numElementsThisThread))
{
runnables.push_back(new Runnable1D<OpT>(
startElement, numElementsThisThread, op));
}
addAndWaitGroup(runnables);

}


};
}
#endif
Expand Down

0 comments on commit 0add5ad

Please sign in to comment.