Bläddra i källkod

Convert our tests to use Catch2

default_compile_flags
vector-of-bool 5 år sedan
förälder
incheckning
beb00fa96b
13 ändrade filer med 48 tillägg och 245 borttagningar
  1. +3
    -1
      package.dds
  2. +15
    -20
      src/browns/md5.test.cpp
  3. +11
    -38
      src/dds/toolchain/from_dds.test.cpp
  4. +3
    -13
      src/dds/toolchain/toolchain.test.cpp
  5. +0
    -44
      src/dds/util.test.hpp
  6. +6
    -20
      src/dds/util/string.test.cpp
  7. +8
    -12
      src/libman/parse.test.cpp
  8. +0
    -38
      src/neo/buffer.test.cpp
  9. +0
    -11
      src/neo/buffer.test.hpp
  10. +0
    -18
      src/neo/buffer_algorithm.test.cpp
  11. +0
    -17
      src/neo/const_buffer.test.cpp
  12. +0
    -3
      src/neo/mutable_buffer.test.cpp
  13. +2
    -10
      src/semver/version.test.cpp

+ 3
- 1
package.dds Visa fil

@@ -5,4 +5,6 @@ Depends: neo-buffer 0.1.0
Depends: spdlog 1.4.2
Depends: ms-wil 2019.11.10
Depends: range-v3 0.9.1
Depends: nlohmann-json 3.7.1
Depends: nlohmann-json 3.7.1

Test-Driver: Catch-Main

+ 15
- 20
src/browns/md5.test.cpp Visa fil

@@ -3,33 +3,28 @@
#include <browns/output.hpp>
#include <neo/buffer.hpp>

#include <catch2/catch.hpp>

#include <iostream>

int check_hash(std::string_view str, std::string_view expect_md5) {
auto md5_hash_str(std::string_view s) {
browns::md5 hash;
hash.feed(neo::buffer(str));
hash.feed(neo::buffer(s));
hash.pad();
auto dig = hash.digest();
auto dig_str = browns::format_digest(dig);
if (dig_str != expect_md5) {
std::cerr << "Hash of '" << str << "' did not match. Expected '" << expect_md5
<< "', but got '" << dig_str << "'\n";
return 1;
}
return 0;
return browns::format_digest(hash.digest());
}

int main() {
browns::md5 hash;
void check_hash(std::string_view str, std::string_view digest) {
INFO("Hashed string: " << str);
CHECK(md5_hash_str(str) == digest);
}

int n_fails = 0;
n_fails += check_hash("1234abcd1234abcd1234abcd1234abcd", "67aa636d72b967157c363f0acdf7011b");
n_fails += check_hash("The quick brown fox jumps over the lazy dog",
"9e107d9d372bb6826bd81d3542a419d6");
n_fails += check_hash("", "d41d8cd98f00b204e9800998ecf8427e");
n_fails += check_hash(
TEST_CASE("Known hashes") {
check_hash("1234abcd1234abcd1234abcd1234abcd", "67aa636d72b967157c363f0acdf7011b");
check_hash("The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6");
check_hash("", "d41d8cd98f00b204e9800998ecf8427e");
check_hash(
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"967ce152b23edc20ebd23b7eba57277c");
n_fails += check_hash("WWWWWWWWWWWWWWWWWWWWWWWWWWWWW", "9aff577d3248b8889b22f24ee9665c17");
return n_fails;
check_hash("WWWWWWWWWWWWWWWWWWWWWWWWWWWWW", "9aff577d3248b8889b22f24ee9665c17");
}

+ 11
- 38
src/dds/toolchain/from_dds.test.cpp Visa fil

@@ -2,39 +2,29 @@

#include <dds/proc.hpp>

#include <dds/util.test.hpp>
// #include <dds/util.test.hpp>
#include <catch2/catch.hpp>

namespace {

void check_tc_compile(std::string_view tc_content,
std::string_view compile,
std::string_view compile_warnings,
std::string_view ar,
std::string_view exe) {
std::string_view expected_compile,
std::string_view expected_compile_warnings,
std::string_view expected_ar,
std::string_view expected_exe) {
auto tc = dds::parse_toolchain_dds(tc_content);
bool any_error = false;

dds::compile_file_spec cf;
cf.source_path = "foo.cpp";
cf.out_path = "foo.o";
auto cf_cmd = tc.create_compile_command(cf);
auto cf_cmd_str = dds::quote_command(cf_cmd);
if (cf_cmd_str != compile) {
std::cerr << "Compile command came out incorrect!\n";
std::cerr << " Expected: " << compile << '\n';
std::cerr << " Actual: " << cf_cmd_str << "\n\n";
any_error = true;
}
CHECK(cf_cmd_str == expected_compile);

cf.enable_warnings = true;
cf_cmd = tc.create_compile_command(cf);
cf_cmd_str = dds::quote_command(cf_cmd);
if (cf_cmd_str != compile_warnings) {
std::cerr << "Compile command (with warnings) came out incorrect!\n";
std::cerr << " Expected: " << compile_warnings << '\n';
std::cerr << " Actual: " << cf_cmd_str << "\n\n";
any_error = true;
}
CHECK(cf_cmd_str == expected_compile_warnings);

dds::archive_spec ar_spec;
ar_spec.input_files.push_back("foo.o");
@@ -42,12 +32,7 @@ void check_tc_compile(std::string_view tc_content,
ar_spec.out_path = "stuff.a";
auto ar_cmd = tc.create_archive_command(ar_spec);
auto ar_cmd_str = dds::quote_command(ar_cmd);
if (ar_cmd_str != ar) {
std::cerr << "Archive command came out incorrect!\n";
std::cerr << " Expected: " << ar << '\n';
std::cerr << " Actual: " << ar_cmd_str << "\n\n";
any_error = true;
}
CHECK(ar_cmd_str == expected_ar);

dds::link_exe_spec exe_spec;
exe_spec.inputs.push_back("foo.o");
@@ -55,20 +40,10 @@ void check_tc_compile(std::string_view tc_content,
exe_spec.output = "meow.exe";
auto exe_cmd = tc.create_link_executable_command(exe_spec);
auto exe_cmd_str = dds::quote_command(exe_cmd);
if (exe_cmd_str != exe) {
std::cerr << "Executable linking command came out incorrect!\n";
std::cerr << " Expected: " << exe << '\n';
std::cerr << " Actual: " << exe_cmd_str << "\n\n";
any_error = true;
}

if (any_error) {
std::cerr << "The error-producing toolchain file content:\n" << tc_content << '\n';
dds::S_failed_checks++;
}
CHECK(exe_cmd_str == expected_exe);
}

void run_tests() {
TEST_CASE("Generating toolchain commands") {
check_tc_compile("Compiler-ID: GNU",
"g++ -fPIC -fdiagnostics-color -pthread -c foo.cpp -ofoo.o",
"g++ -fPIC -fdiagnostics-color -pthread -Wall -Wextra -Wpedantic -Wconversion "
@@ -151,5 +126,3 @@ void run_tests() {
}

} // namespace

DDS_TEST_MAIN;

+ 3
- 13
src/dds/toolchain/toolchain.test.cpp Visa fil

@@ -1,17 +1,13 @@
#include <dds/toolchain/toolchain.hpp>

#include <dds/util.test.hpp>

using namespace dds;

namespace {
#include <catch2/catch.hpp>

#define CHECK_SHLEX(str, ...) \
do { \
CHECK(dds::split_shell_string(str) == std::vector<std::string>(__VA_ARGS__)); \
INFO("Shell-lexing string: '" << str << "'"); CHECK(dds::split_shell_string(str) == std::vector<std::string>(__VA_ARGS__)); \
} while (0)

void test_shlex() {
TEST_CASE("Shell lexer") {
CHECK_SHLEX("foo", {"foo"});
CHECK_SHLEX("foo bar", {"foo", "bar"});
CHECK_SHLEX("\"foo\" bar", {"foo", "bar"});
@@ -27,9 +23,3 @@ void test_shlex() {
CHECK_SHLEX("Foo\nBar", {"Foo", "Bar"});
CHECK_SHLEX("foo \"\" bar", {"foo", "", "bar"});
}

void run_tests() { test_shlex(); }

} // namespace

DDS_TEST_MAIN;

+ 0
- 44
src/dds/util.test.hpp Visa fil

@@ -1,44 +0,0 @@
#pragma once

#include <iostream>

namespace dds {

int S_failed_checks = 0;

struct requirement_failed {};

#define CHECK(...) \
do { \
if (!(__VA_ARGS__)) { \
++::dds::S_failed_checks; \
std::cerr << "Check failed at " << __FILE__ << ':' << __LINE__ << ": " << #__VA_ARGS__ \
<< "\n"; \
} \
} while (0)

#define REQUIRE(...) \
do { \
if (!(__VA_ARGS__)) { \
++::dds::S_failed_checks; \
std::cerr << "Check failed at " << __FILE__ << ':' << __LINE__ << ": " << #__VA_ARGS__ \
<< "\n"; \
throw ::dds::requirement_failed(); \
} \
} while (0)

#define DDS_TEST_MAIN \
int main() { \
try { \
run_tests(); \
} catch (const ::dds::requirement_failed&) { \
return ::dds::S_failed_checks; \
} catch (const std::exception& e) { \
std::cerr << "An unhandled exception occured: " << e.what() << '\n'; \
return 2; \
} \
return ::dds::S_failed_checks; \
} \
static_assert(true)

} // namespace dds

+ 6
- 20
src/dds/util/string.test.cpp Visa fil

@@ -1,17 +1,15 @@
#include <dds/util/string.hpp>

#include <dds/util.test.hpp>
#include <catch2/catch.hpp>

using namespace dds;

namespace {

#define CHECK_SPLIT(str, key, ...) \
do { \
CHECK(dds::split(str, key) == std::vector<std::string>(__VA_ARGS__)); \
} while (0)

void test_starts_with() {
TEST_CASE("starts_with") {
CHECK(starts_with("foo", "foo"));
CHECK(starts_with("foo.bar", "foo"));
CHECK(starts_with("foo", "f"));
@@ -22,7 +20,7 @@ void test_starts_with() {
CHECK(!starts_with("foo.bar", "bar"));
}

void test_ends_with() {
TEST_CASE("ends_with") {
CHECK(ends_with("foo", "foo"));
CHECK(ends_with("foo.bar", "bar"));
CHECK(ends_with("foo", "o"));
@@ -33,34 +31,22 @@ void test_ends_with() {
CHECK(!ends_with("foo.bar", "foo"));
}

void test_trim() {
TEST_CASE( "trim" ) {
CHECK(trim_view("foo") == "foo");
CHECK(trim_view("foo ") == "foo");
CHECK(trim_view(" ").size() == 0);
}

void test_contains() {
TEST_CASE( "contains" ) {
CHECK(contains("foo", "foo"));
CHECK(contains("foo", ""));
CHECK(contains("foo", "o"));
CHECK(!contains("foo", "bar"));
}

void test_split() {
TEST_CASE( "split" ) {
CHECK_SPLIT("foo.bar", ".", {"foo", "bar"});
CHECK_SPLIT("foo.bar.baz", ".", {"foo", "bar", "baz"});
CHECK_SPLIT(".", ".", {"", ""});
CHECK_SPLIT("", ",", {""});
}

void run_tests() {
test_trim();
test_starts_with();
test_ends_with();
test_contains();
test_split();
}

} // namespace

DDS_TEST_MAIN;

+ 8
- 12
src/libman/parse.test.cpp Visa fil

@@ -1,11 +1,12 @@
#include <dds/util.test.hpp>
#include <libman/parse.hpp>

#include <catch2/catch.hpp>

#include <iostream>

using namespace lm;

void test_simple() {
TEST_CASE("Simple") {
auto lm_src = "";
auto kvs = parse_string(lm_src);
CHECK(kvs.size() == 0);
@@ -55,7 +56,7 @@ void test_simple() {
CHECK(kvs.find("foo")->value == "# Not a comment");
}

void test_multi() {
TEST_CASE("Multiple") {
auto kvs = parse_string("Foo: bar\nbaz: qux");
CHECK(kvs.size() == 2);
REQUIRE(kvs.find("Foo"));
@@ -80,8 +81,9 @@ void test_multi() {
CHECK(!iter);
}

void test_nested_kvlist() {
TEST_CASE("Nested kv-lists") {
auto check_1 = [](auto str) {
INFO("Testing string: '" << str << "'");
auto result = nested_kvlist::parse(str);
CHECK(result.primary == "Foo");
CHECK(result.pairs.size() == 1);
@@ -96,6 +98,7 @@ void test_nested_kvlist() {
check_1("Foo;bar=baz");

auto check_2 = [](auto str) {
INFO("Testing string: '" << str << "'");
auto result = nested_kvlist::parse(str);
CHECK(result.primary == "Foo");
CHECK(result.pairs.size() == 0);
@@ -107,6 +110,7 @@ void test_nested_kvlist() {
check_2("Foo; ");

auto check_3 = [](auto str) {
INFO("Testing string: '" << str << "'");
auto result = nested_kvlist::parse(str);
CHECK(result.primary == "Foo bar");
CHECK(result.pairs.size() == 2);
@@ -121,11 +125,3 @@ void test_nested_kvlist() {
check_3("Foo bar ; quux= baz=meow");
check_3("Foo bar ;quux= baz=meow");
}

void run_tests() {
test_simple();
test_multi();
test_nested_kvlist();
}

DDS_TEST_MAIN;

+ 0
- 38
src/neo/buffer.test.cpp Visa fil

@@ -1,38 +0,0 @@
#include <neo/buffer.hpp>

#include <neo/buffer.test.hpp>
#include <neo/buffer_algorithm.hpp>

#include <array>
#include <string>

struct my_simple_struct {
int a;
int b;
};

int main() {
// std::string s = "I am a string!";
// auto s_buf = neo::buffer(s);
// CHECK(s_buf.data() == s.data());

my_simple_struct foo;
foo.a = 12;
foo.b = 3;

neo::mutable_buffer pod_buf = neo::buffer(foo);
CHECK(pod_buf.size() == sizeof(foo));

my_simple_struct bar;
neo::buffer_copy(neo::buffer(bar), pod_buf);
CHECK(bar.a == foo.a);
CHECK(bar.b == foo.b);

bar.b = 55;

std::array<char, sizeof bar> buf;
neo::buffer_copy(neo::buffer(buf), neo::buffer(bar));

neo::buffer_copy(neo::buffer(foo), neo::buffer(buf));
CHECK(foo.b == 55);
}

+ 0
- 11
src/neo/buffer.test.hpp Visa fil

@@ -1,11 +0,0 @@
#pragma once

#include <iostream>

#define CHECK(...) \
do { \
if (!(__VA_ARGS__)) { \
std::cerr << "Check failed: " << (#__VA_ARGS__) << '\n'; \
return 1; \
} \
} while (0)

+ 0
- 18
src/neo/buffer_algorithm.test.cpp Visa fil

@@ -1,18 +0,0 @@
#include <neo/buffer_algorithm.hpp>

#include <neo/buffer.test.hpp>

#include <neo/const_buffer.hpp>

#include <string_view>

int main() {
auto buf = neo::const_buffer("A string");
auto buf_iter = neo::buffer_sequence_begin(buf);
CHECK(buf_iter->data() == buf.data());
CHECK(buf_iter != neo::buffer_sequence_end(buf));

CHECK(neo::buffer_size(buf) == buf.size());

// neo::buffer_size(12);
}

+ 0
- 17
src/neo/const_buffer.test.cpp Visa fil

@@ -1,17 +0,0 @@
#include <neo/buffer.test.hpp>
#include <neo/const_buffer.hpp>

#include <iostream>

int main() {
neo::const_buffer buf;
CHECK(buf.size() == 0);

buf = neo::const_buffer("meow");
CHECK(buf.size() == 4);
auto buf2 = buf + 3;
CHECK(buf2.size() == 1);
CHECK(buf2.data()[0] == std::byte('w'));
buf2 += 1;
CHECK(buf2.size() == 0);
}

+ 0
- 3
src/neo/mutable_buffer.test.cpp Visa fil

@@ -1,3 +0,0 @@
#include <neo/mutable_buffer.hpp>

int main() {}

+ 2
- 10
src/semver/version.test.cpp Visa fil

@@ -1,10 +1,8 @@
#include "./version.hpp"

#include <dds/util.test.hpp>
#include <catch2/catch.hpp>

namespace {

void test_simple_parse() {
TEST_CASE("Parsing") {
auto v1 = semver::version::parse("1.2.3");
CHECK(v1.major == 1);
CHECK(v1.minor == 2);
@@ -16,9 +14,3 @@ void test_simple_parse() {
v1.major = 999999;
CHECK(v1.to_string() == "999999.2.55");
}

void run_tests() { test_simple_parse(); }

} // namespace

DDS_TEST_MAIN;

Laddar…
Avbryt
Spara