Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
code coverage around test suite suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed May 25, 2019
1 parent eb50ff9 commit a69cfff
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added: [#5575](https://github.com/ethereum/aleth/pull/5575) Log active peer count and peer list every 30 seconds.
- Added: [#5580](https://github.com/ethereum/aleth/pull/5580) Enable syncing from ETC nodes for blocks < dao hard fork block.
- Added: [#5591](https://github.com/ethereum/aleth/pull/5591) Network logging bugfixes and improvements and add p2pcap log channel.
- Added: [#5588](https://github.com/ethereum/aleth/pull/5588) Testeth print test suggestions
- Changed: [#5559](https://github.com/ethereum/aleth/pull/5559) Update peer validation error messages.
- Changed: [#5568](https://github.com/ethereum/aleth/pull/5568) Improve rlpx handshake log messages and create new rlpx log channel.
- Changed: [#5576](https://github.com/ethereum/aleth/pull/5576) Moved sstore_combinations and static_Call50000_sha256 tests to stTimeConsuming test suite. (testeth runs them only with `--all` flag)
Expand Down
4 changes: 3 additions & 1 deletion test/tools/libtesteth/AllTestNames.h.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include <string>
#include <vector>

char const* c_allTestNames[] =
std::vector<std::string> const c_allTestNames
{
@allTests@
};
21 changes: 21 additions & 0 deletions test/tools/libtesteth/TestHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,27 @@ size_t levenshteinDistance(char const* _s, size_t _n, char const* _t, size_t _m)
return r;
}

std::vector<std::string> testSuggestions(
vector<string> const& _testList, std::string const& _sMinusTArg)
{
vector<string> ret;
size_t allTestsElementIndex = 0;
// <index in availableTests, compared distance>
typedef std::pair<size_t, size_t> NameDistance;
// Use `vector` here because `set` does not work with sort
std::vector<NameDistance> distanceMap;
for (auto& it : _testList)
{
int const dist = test::levenshteinDistance(
_sMinusTArg.c_str(), _sMinusTArg.size(), it.c_str(), it.size());
distanceMap.emplace_back(allTestsElementIndex++, dist);
}
std::sort(distanceMap.begin(), distanceMap.end(),
[](NameDistance const& _a, NameDistance const& _b) { return _a.second < _b.second; });
for (size_t i = 0; i < 3 && i < distanceMap.size(); i++)
ret.push_back(_testList[distanceMap[i].first]);
return ret;
}

void copyFile(fs::path const& _source, fs::path const& _destination)
{
Expand Down
2 changes: 2 additions & 0 deletions test/tools/libtesteth/TestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ json_spirit::mObject fillJsonWithStateChange(eth::State const& _stateOrig, eth::
json_spirit::mObject fillJsonWithState(eth::State const& _state);
json_spirit::mObject fillJsonWithState(eth::State const& _state, eth::AccountMaskMap const& _map);
json_spirit::mObject fillJsonWithTransaction(eth::Transaction const& _txn);
std::vector<std::string> testSuggestions(
std::vector<std::string> const& _testList, std::string const& _sMinusTArg);

//Fill Test Functions
bool createRandomTest(); //returns true if succeed, false if there was an error;
Expand Down
24 changes: 6 additions & 18 deletions test/tools/libtesteth/boostTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,19 @@ int main(int argc, const char* argv[])
std::cout << "Running tests using path: " << test::getTestPath() << std::endl;
int result = 0;
auto fakeInit = [](int, char* []) -> boost::unit_test::test_suite* { return nullptr; };
result = unit_test_main(fakeInit, argc, const_cast<char**>(argv));

// Print suggestions of a test case
if (result == boost::exit_exception_failure) // test suite not found
result = unit_test_main(fakeInit, argc, const_cast<char**>(argv));
// Print suggestions of a test case if test suite not found
if (result == boost::exit_exception_failure && !dev::test::inArray(c_allTestNames, sMinusTArg))
printTestSuiteSuggestions(sMinusTArg);
dev::test::TestOutputHelper::get().printTestExecStats();
return result;
}

void printTestSuiteSuggestions(string const& _sMinusTArg)
{
size_t allTestsElementIndex = 0;
// <index in availableTests, compared distance>
typedef std::pair<size_t, size_t> NameDistance;
// Use `vector` here because `set` does not work with sort
std::vector<NameDistance> distanceMap;
for (auto& it : c_allTestNames)
{
int const dist =
test::levenshteinDistance(_sMinusTArg.c_str(), _sMinusTArg.size(), it, strlen(it));
distanceMap.emplace_back(allTestsElementIndex++, dist);
}
std::sort(distanceMap.begin(), distanceMap.end(),
[](NameDistance const& _a, NameDistance const& _b) { return _a.second < _b.second; });
auto testList = test::testSuggestions(c_allTestNames, _sMinusTArg);
std::cerr << "Did you mean: \n";
for (size_t i = 0; i < 3 && i < distanceMap.size(); i++)
std::cerr << "-t " << c_allTestNames[distanceMap[i].first] << "\n";
for (auto const& element : testList)
std::cerr << "-t " << element << "\n";
}
94 changes: 94 additions & 0 deletions test/unittests/libtesteth/testHelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using namespace dev::test;

BOOST_FIXTURE_TEST_SUITE(TestHelperSuite, TestOutputHelperFixture)

BOOST_AUTO_TEST_SUITE(TranslateNetworks)
BOOST_AUTO_TEST_CASE(translateNetworks_gtConstantinople)
{
set<string> networks = {">Constantinople"};
Expand Down Expand Up @@ -104,5 +105,98 @@ BOOST_AUTO_TEST_CASE(translateNetworks_leFrontier)
BOOST_REQUIRE(networks.count(test::netIdToString(net)) == 0);
}
}
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(TestHelper)
BOOST_AUTO_TEST_CASE(levenshteinDistance_similar)
{
char const* word1 = "someword";
char const* word2 = "soemword";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 2);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_similar2)
{
char const* word1 = "sOmeWord";
char const* word2 = "someword";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 2);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_similar3)
{
char const* word1 = "sOmeWoRd";
char const* word2 = "someword";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 3);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_similar4)
{
char const* word1 = "sOmeWoRd";
char const* word2 = "soemword";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 5);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_AgtB)
{
char const* word1 = "someword";
char const* word2 = "other";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 4);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_AgtB2)
{
char const* word1 = "some long sentence here";
char const* word2 = "other shorter phrase";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 14);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_BgtA)
{
char const* word1 = "other";
char const* word2 = "someword";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 4);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_BgtA2)
{
char const* word1 = "other shorter phrase";
char const* word2 = "some long sentence here";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 14);
}

BOOST_AUTO_TEST_CASE(levenshteinDistance_different)
{
char const* word1 = "abdefg";
char const* word2 = "hijklmn";
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
BOOST_CHECK_EQUAL(distance, 6);
}

BOOST_AUTO_TEST_CASE(getTestSuggestions)
{
vector<string> const testList = {
"test1", "test2", "BlockSuite", "BlockSuite/TestCase", "GeneralBlockchainTests"};
auto list = test::testSuggestions(testList, "blocksuit");
BOOST_CHECK_EQUAL(test::inArray(list, string("BlockSuite")), true);
}

BOOST_AUTO_TEST_CASE(getTestSuggestions2)
{
vector<string> const testList = {"test1", "test2", "BlockSuite", "BlockSuite/TestCase",
"GeneralBlockchainTests", "GeneralStateTests/stExample", "BCGeneralStateTests/stExample"};

auto list = test::testSuggestions(testList, "GeneralStateTests/stExample2");
BOOST_CHECK_EQUAL(test::inArray(list, string("GeneralStateTests/stExample")), true);
BOOST_CHECK_EQUAL(test::inArray(list, string("BCGeneralStateTests/stExample")), true);
}
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit a69cfff

Please sign in to comment.