Beautiful test framework from Krzysztof Jusiak:
"match success"_test = [] {
int v[] = {1, 2, 3, 4, 3, 3, 7, 2, 3};
expect(ranges::equal({2, 5, 8}, match(v, 2, 3)));
};
but don't compile with MS C++
template <char...>
struct test {
template <class Test>
bool operator=(const Test& test) {
test();
return true;
}
};
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
#endif
template <class T, T... Chars>
constexpr auto operator""_test() {
return test<Chars...>{};
}
but clang does :-)
cmake supports different generators (-G)
cmake .. -G "Visual Studio 14 2015 Win64"
new in cmake 3.6: toolsets for Visual Studio (-T toolset)
cmake .. -G"Visual Studio 14 2015" -Tv140_clang_c2
works only for the clang (v140_clang_c2) and the default toolset
It's hard to compile simple C++ projects (main()
) with clang in Visual Studio
cmake_minimum_required(VERSION 2.8)
project(test_clang)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS "-std=c++1z")
set(CMAKE_CXX_FLAGS "-fms-extensions")
set(CMAKE_CXX_FLAGS "-g2 -gdwarf-2")
endif()
add_executable(test_clang main.cpp)
@echo on
mkdir build
cd build
cmake .. -G "Visual Studio 14 2015 Win64" -T v140_clang_c2
// idea from https://github.com/modern-cpp-examples/match3
#undef NDEBUG
#include <cstdio>
#include <cstdlib>
#define expect(...) \
(void)((__VA_ARGS__) || (expect_fail__(#__VA_ARGS__, __FILE__, __LINE__), 0))
#define static_expect(...) static_assert((__VA_ARGS__), "fail")
void expect_fail__(const char* msg, const char* file, int line)
{
std::printf("%s:%d: %s\n", file, line, msg);
std::exit(-1);
}
//*
template <char...>
struct test
{
template <class Test>
bool operator=(const Test& test)
{
test();
return true;
}
};
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
#endif
template <class T, T... Chars>
constexpr auto operator""_test()
{
return test<Chars...>{};
}
/*/
struct test
{
template <class Test>
auto operator=(const Test& test) { test(); return true; }
};
constexpr test operator "" _test(char const* t, std::size_t s)
{
return test{};
}
// */
int main()
{
"test_should_succes"_test = [] {
expect(2 == 2);
};
"test_should_fail"_test = [] {
expect(1 == 2);
};
}