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

Commit a69cfff

Browse files
author
Dimitry
committed
code coverage around test suite suggestions
1 parent eb50ff9 commit a69cfff

File tree

6 files changed

+127
-19
lines changed

6 files changed

+127
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added: [#5575](https://github.com/ethereum/aleth/pull/5575) Log active peer count and peer list every 30 seconds.
1010
- Added: [#5580](https://github.com/ethereum/aleth/pull/5580) Enable syncing from ETC nodes for blocks < dao hard fork block.
1111
- Added: [#5591](https://github.com/ethereum/aleth/pull/5591) Network logging bugfixes and improvements and add p2pcap log channel.
12+
- Added: [#5588](https://github.com/ethereum/aleth/pull/5588) Testeth print test suggestions
1213
- Changed: [#5559](https://github.com/ethereum/aleth/pull/5559) Update peer validation error messages.
1314
- Changed: [#5568](https://github.com/ethereum/aleth/pull/5568) Improve rlpx handshake log messages and create new rlpx log channel.
1415
- 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)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
2+
#include <string>
3+
#include <vector>
24

3-
char const* c_allTestNames[] =
5+
std::vector<std::string> const c_allTestNames
46
{
57
@allTests@
68
};

test/tools/libtesteth/TestHelper.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,27 @@ size_t levenshteinDistance(char const* _s, size_t _n, char const* _t, size_t _m)
666666
return r;
667667
}
668668

669+
std::vector<std::string> testSuggestions(
670+
vector<string> const& _testList, std::string const& _sMinusTArg)
671+
{
672+
vector<string> ret;
673+
size_t allTestsElementIndex = 0;
674+
// <index in availableTests, compared distance>
675+
typedef std::pair<size_t, size_t> NameDistance;
676+
// Use `vector` here because `set` does not work with sort
677+
std::vector<NameDistance> distanceMap;
678+
for (auto& it : _testList)
679+
{
680+
int const dist = test::levenshteinDistance(
681+
_sMinusTArg.c_str(), _sMinusTArg.size(), it.c_str(), it.size());
682+
distanceMap.emplace_back(allTestsElementIndex++, dist);
683+
}
684+
std::sort(distanceMap.begin(), distanceMap.end(),
685+
[](NameDistance const& _a, NameDistance const& _b) { return _a.second < _b.second; });
686+
for (size_t i = 0; i < 3 && i < distanceMap.size(); i++)
687+
ret.push_back(_testList[distanceMap[i].first]);
688+
return ret;
689+
}
669690

670691
void copyFile(fs::path const& _source, fs::path const& _destination)
671692
{

test/tools/libtesteth/TestHelper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ json_spirit::mObject fillJsonWithStateChange(eth::State const& _stateOrig, eth::
132132
json_spirit::mObject fillJsonWithState(eth::State const& _state);
133133
json_spirit::mObject fillJsonWithState(eth::State const& _state, eth::AccountMaskMap const& _map);
134134
json_spirit::mObject fillJsonWithTransaction(eth::Transaction const& _txn);
135+
std::vector<std::string> testSuggestions(
136+
std::vector<std::string> const& _testList, std::string const& _sMinusTArg);
135137

136138
//Fill Test Functions
137139
bool createRandomTest(); //returns true if succeed, false if there was an error;

test/tools/libtesteth/boostTest.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,31 +151,19 @@ int main(int argc, const char* argv[])
151151
std::cout << "Running tests using path: " << test::getTestPath() << std::endl;
152152
int result = 0;
153153
auto fakeInit = [](int, char* []) -> boost::unit_test::test_suite* { return nullptr; };
154-
result = unit_test_main(fakeInit, argc, const_cast<char**>(argv));
155154

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

163163
void printTestSuiteSuggestions(string const& _sMinusTArg)
164164
{
165-
size_t allTestsElementIndex = 0;
166-
// <index in availableTests, compared distance>
167-
typedef std::pair<size_t, size_t> NameDistance;
168-
// Use `vector` here because `set` does not work with sort
169-
std::vector<NameDistance> distanceMap;
170-
for (auto& it : c_allTestNames)
171-
{
172-
int const dist =
173-
test::levenshteinDistance(_sMinusTArg.c_str(), _sMinusTArg.size(), it, strlen(it));
174-
distanceMap.emplace_back(allTestsElementIndex++, dist);
175-
}
176-
std::sort(distanceMap.begin(), distanceMap.end(),
177-
[](NameDistance const& _a, NameDistance const& _b) { return _a.second < _b.second; });
165+
auto testList = test::testSuggestions(c_allTestNames, _sMinusTArg);
178166
std::cerr << "Did you mean: \n";
179-
for (size_t i = 0; i < 3 && i < distanceMap.size(); i++)
180-
std::cerr << "-t " << c_allTestNames[distanceMap[i].first] << "\n";
167+
for (auto const& element : testList)
168+
std::cerr << "-t " << element << "\n";
181169
}

test/unittests/libtesteth/testHelperTest.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ using namespace dev::test;
2828

2929
BOOST_FIXTURE_TEST_SUITE(TestHelperSuite, TestOutputHelperFixture)
3030

31+
BOOST_AUTO_TEST_SUITE(TranslateNetworks)
3132
BOOST_AUTO_TEST_CASE(translateNetworks_gtConstantinople)
3233
{
3334
set<string> networks = {">Constantinople"};
@@ -104,5 +105,98 @@ BOOST_AUTO_TEST_CASE(translateNetworks_leFrontier)
104105
BOOST_REQUIRE(networks.count(test::netIdToString(net)) == 0);
105106
}
106107
}
108+
BOOST_AUTO_TEST_SUITE_END()
109+
110+
BOOST_AUTO_TEST_SUITE(TestHelper)
111+
BOOST_AUTO_TEST_CASE(levenshteinDistance_similar)
112+
{
113+
char const* word1 = "someword";
114+
char const* word2 = "soemword";
115+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
116+
BOOST_CHECK_EQUAL(distance, 2);
117+
}
118+
119+
BOOST_AUTO_TEST_CASE(levenshteinDistance_similar2)
120+
{
121+
char const* word1 = "sOmeWord";
122+
char const* word2 = "someword";
123+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
124+
BOOST_CHECK_EQUAL(distance, 2);
125+
}
126+
127+
BOOST_AUTO_TEST_CASE(levenshteinDistance_similar3)
128+
{
129+
char const* word1 = "sOmeWoRd";
130+
char const* word2 = "someword";
131+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
132+
BOOST_CHECK_EQUAL(distance, 3);
133+
}
134+
135+
BOOST_AUTO_TEST_CASE(levenshteinDistance_similar4)
136+
{
137+
char const* word1 = "sOmeWoRd";
138+
char const* word2 = "soemword";
139+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
140+
BOOST_CHECK_EQUAL(distance, 5);
141+
}
142+
143+
BOOST_AUTO_TEST_CASE(levenshteinDistance_AgtB)
144+
{
145+
char const* word1 = "someword";
146+
char const* word2 = "other";
147+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
148+
BOOST_CHECK_EQUAL(distance, 4);
149+
}
150+
151+
BOOST_AUTO_TEST_CASE(levenshteinDistance_AgtB2)
152+
{
153+
char const* word1 = "some long sentence here";
154+
char const* word2 = "other shorter phrase";
155+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
156+
BOOST_CHECK_EQUAL(distance, 14);
157+
}
158+
159+
BOOST_AUTO_TEST_CASE(levenshteinDistance_BgtA)
160+
{
161+
char const* word1 = "other";
162+
char const* word2 = "someword";
163+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
164+
BOOST_CHECK_EQUAL(distance, 4);
165+
}
166+
167+
BOOST_AUTO_TEST_CASE(levenshteinDistance_BgtA2)
168+
{
169+
char const* word1 = "other shorter phrase";
170+
char const* word2 = "some long sentence here";
171+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
172+
BOOST_CHECK_EQUAL(distance, 14);
173+
}
174+
175+
BOOST_AUTO_TEST_CASE(levenshteinDistance_different)
176+
{
177+
char const* word1 = "abdefg";
178+
char const* word2 = "hijklmn";
179+
size_t distance = test::levenshteinDistance(word1, strlen(word1), word2, strlen(word2));
180+
BOOST_CHECK_EQUAL(distance, 6);
181+
}
182+
183+
BOOST_AUTO_TEST_CASE(getTestSuggestions)
184+
{
185+
vector<string> const testList = {
186+
"test1", "test2", "BlockSuite", "BlockSuite/TestCase", "GeneralBlockchainTests"};
187+
auto list = test::testSuggestions(testList, "blocksuit");
188+
BOOST_CHECK_EQUAL(test::inArray(list, string("BlockSuite")), true);
189+
}
190+
191+
BOOST_AUTO_TEST_CASE(getTestSuggestions2)
192+
{
193+
vector<string> const testList = {"test1", "test2", "BlockSuite", "BlockSuite/TestCase",
194+
"GeneralBlockchainTests", "GeneralStateTests/stExample", "BCGeneralStateTests/stExample"};
195+
196+
auto list = test::testSuggestions(testList, "GeneralStateTests/stExample2");
197+
BOOST_CHECK_EQUAL(test::inArray(list, string("GeneralStateTests/stExample")), true);
198+
BOOST_CHECK_EQUAL(test::inArray(list, string("BCGeneralStateTests/stExample")), true);
199+
}
200+
BOOST_AUTO_TEST_SUITE_END()
107201

108202
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)