Commit 9b3bc656 authored by Paul's avatar Paul
Browse files

Add initial test driver

parent c0bcc6fc
......@@ -3,6 +3,8 @@
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <vector>
#ifndef MIGRAPH_GUARD_TEST_TEST_HPP
#define MIGRAPH_GUARD_TEST_TEST_HPP
......@@ -154,11 +156,75 @@ bool throws(F f, const std::string& msg = "")
}
}
template <class T>
void run_test()
using string_map = std::unordered_map<std::string, std::vector<std::string>>;
template <class Keyword>
string_map parse(std::vector<std::string> as, Keyword keyword)
{
string_map result;
std::string flag;
for(auto&& x : as)
{
auto f = keyword(x);
if(f.empty())
{
result[flag].push_back(x);
}
else
{
flag = f.front();
result[flag]; // Ensure the flag exists
}
}
return result;
}
inline auto& get_test_cases()
{
static std::vector<std::pair<std::string, std::function<void()>>> cases;
return cases;
}
inline void add_test_case(std::string name, std::function<void()> f)
{
get_test_cases().emplace_back(name, f);
}
struct auto_register
{
auto_register(std::string name, std::function<void()> f)
{
add_test_case(name, f);
}
};
inline void run_test_case(std::string name, std::function<void()> f)
{
T t = {};
t.run();
std::cout << "[ RUN ] " << name << std::endl;
f();
std::cout << "[ COMPLETE ] " << name << std::endl;
}
inline void run(int argc, const char* argv[])
{
std::vector<std::string> as(argv + 1, argv + argc);
auto args = parse(as, [](auto&&) -> std::vector<std::string> {
return {};
});
auto cases = args[""];
if(cases.empty())
{
for(auto&& tc:get_test_cases())
run_test_case(tc.first, tc.second);
}
else
{
std::unordered_map<std::string, std::function<void()>> m(get_test_cases().begin(), get_test_cases().end());
for(auto&& name:cases)
run_test_case(name, m[name]);
}
}
} // namespace test
......@@ -179,4 +245,14 @@ void run_test()
// NOLINTNEXTLINE
#define STATUS(...) EXPECT((__VA_ARGS__) == 0)
#define TEST_CASE(...) \
void __VA_ARGS__ (); \
static test::auto_register __VA_ARGS__ ## _register = test::auto_register(#__VA_ARGS__, &__VA_ARGS__); \
void __VA_ARGS__ ()
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
#endif
......@@ -4,7 +4,7 @@
#include <string>
#include "test.hpp"
void literal_test()
TEST_CASE(literal_test)
{
EXPECT(migraph::literal{1} == migraph::literal{1});
EXPECT(migraph::literal{1} != migraph::literal{2});
......@@ -25,7 +25,7 @@ void literal_test()
EXPECT(l4.empty());
}
void literal_os1()
TEST_CASE(literal_os1)
{
migraph::literal l{1};
std::stringstream ss;
......@@ -33,7 +33,7 @@ void literal_os1()
EXPECT(ss.str() == "1");
}
void literal_os2()
TEST_CASE(literal_os2)
{
migraph::literal l{};
std::stringstream ss;
......@@ -41,7 +41,7 @@ void literal_os2()
EXPECT(ss.str().empty());
}
void literal_os3()
TEST_CASE(literal_os3)
{
migraph::shape s{migraph::shape::int64_type, {3}};
migraph::literal l{s, {1, 2, 3}};
......@@ -50,9 +50,11 @@ void literal_os3()
EXPECT(ss.str() == "1, 2, 3");
}
int main()
{
literal_test();
literal_os1();
literal_os2();
}
int main(int argc, const char* argv[]) { test::run(argc, argv); }
// int main()
// {
// literal_test();
// literal_os1();
// literal_os2();
// }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment