HW4: fixed bug
This commit is contained in:
parent
ecfdcbf96b
commit
2855763d6c
2 changed files with 122 additions and 3 deletions
|
@ -19,7 +19,7 @@
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
#define DEBUG 1
|
|
||||||
class ConcreteRoadMap : public AbstractRoadMap {
|
class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
private:
|
private:
|
||||||
map< string, set<string> > adj;
|
map< string, set<string> > adj;
|
||||||
|
@ -123,7 +123,7 @@ bool ConcreteRoadMap::reachable(const string& c1, const string& c2) const {
|
||||||
set<string> discovered;
|
set<string> discovered;
|
||||||
s.push_back(c1);
|
s.push_back(c1);
|
||||||
while (!s.empty()) {
|
while (!s.empty()) {
|
||||||
const string& v = s.front();
|
const string v = s.front();
|
||||||
if (v == c2) return true;
|
if (v == c2) return true;
|
||||||
s.pop_front();
|
s.pop_front();
|
||||||
|
|
||||||
|
@ -150,7 +150,12 @@ int ConcreteRoadMap::countIslands() const {
|
||||||
s.push(std::pair<string, string>(itr->first, itr->first));
|
s.push(std::pair<string, string>(itr->first, itr->first));
|
||||||
first = true;
|
first = true;
|
||||||
while (!s.empty()) {
|
while (!s.empty()) {
|
||||||
const std::pair<string, string>& v = s.top();
|
const std::pair<string, string> v = s.top();
|
||||||
|
#if DEBUG
|
||||||
|
std::cout << "Front is: " << v.first << " with parent " <<
|
||||||
|
v.second << "\n";
|
||||||
|
#endif
|
||||||
|
|
||||||
s.pop();
|
s.pop();
|
||||||
if (discovered.find(v.first) == discovered.end()) {
|
if (discovered.find(v.first) == discovered.end()) {
|
||||||
|
|
||||||
|
@ -164,9 +169,17 @@ int ConcreteRoadMap::countIslands() const {
|
||||||
|
|
||||||
for (auto j = set.begin(); j != set.end(); ++j) {
|
for (auto j = set.begin(); j != set.end(); ++j) {
|
||||||
if (*j != v.second) {
|
if (*j != v.second) {
|
||||||
|
#if DEBUG
|
||||||
|
cout << "Neighbor: " << *j << " parent: " <<
|
||||||
|
v.first << "\n";
|
||||||
|
#endif
|
||||||
s.push(std::pair<string, string>(*j, v.first));
|
s.push(std::pair<string, string>(*j, v.first));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
#if DEBUG
|
||||||
|
cout << v.first << " is discovered\n";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
first = false;
|
first = false;
|
||||||
|
|
106
roadmap/tests/optional_test1.cpp
Normal file
106
roadmap/tests/optional_test1.cpp
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "basic_testing.h"
|
||||||
|
|
||||||
|
#include "../roadmap.h"
|
||||||
|
|
||||||
|
TEST(basic_test) {
|
||||||
|
std::unique_ptr<AbstractRoadMap> m(createRoadMap());
|
||||||
|
|
||||||
|
// Add roads on one island:
|
||||||
|
m->addRoad({"Locarno", "Lugano", "Agno"});
|
||||||
|
assert_false(m->hasLoop());
|
||||||
|
m->addRoad("Agno", "Berna");
|
||||||
|
assert_false(m->hasLoop());
|
||||||
|
m->addRoad("Locarno", "Berna");
|
||||||
|
m->addRoad("Locarno", "Basel");
|
||||||
|
m->addRoad({"Ginevra", "Basel", "Sciaffusa", "Zurigo"});
|
||||||
|
|
||||||
|
assert_int_eq(m->neighborCountForCity("Locarno"), 3);
|
||||||
|
assert_true(m->hasLoop());
|
||||||
|
assert_int_eq(m->countIslands(), 1);
|
||||||
|
assert_int_eq(m->neighborCountForCity("Basel"), 3);
|
||||||
|
|
||||||
|
m->addRoad("Basel", "Paris");
|
||||||
|
assert_int_eq(m->neighborCountForCity("Basel"), 4);
|
||||||
|
|
||||||
|
m->addRoad("London", "Edinburgh");
|
||||||
|
m->addRoad("Dublin", "Belfast");
|
||||||
|
m->addRoad("Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch", "Anglesey");
|
||||||
|
|
||||||
|
assert_int_eq(m->countIslands(), 4);
|
||||||
|
|
||||||
|
m->addRoad("Belfast", "Edinburgh");
|
||||||
|
assert_int_eq(m->countIslands(), 3);
|
||||||
|
assert_true(m->reachable("Agno", "Locarno"));
|
||||||
|
assert_false(m->reachable("Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch", "Edinburgh"));
|
||||||
|
m->addRoad("Anglesey", "London");
|
||||||
|
assert_true(m->reachable("Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch", "Edinburgh"));
|
||||||
|
m->clear();
|
||||||
|
assert_int_eq(m->countIslands(), 0);
|
||||||
|
|
||||||
|
TEST_PASSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(loops_test) {
|
||||||
|
std::unique_ptr<AbstractRoadMap> m(createRoadMap());
|
||||||
|
|
||||||
|
// Add roads on one island:
|
||||||
|
m->addRoad({"Naples", "Lugano", "Praia Grande"});
|
||||||
|
assert_false(m->hasLoop());
|
||||||
|
// Add roads on another island:
|
||||||
|
m->addRoad("Colorado", "Boulder");
|
||||||
|
assert_false(m->hasLoop());
|
||||||
|
// Add roads on another island:
|
||||||
|
m->addRoad("Milano", "Siena");
|
||||||
|
assert_false(m->hasLoop());
|
||||||
|
assert_int_eq(m->countIslands(), 3);
|
||||||
|
|
||||||
|
// Merge two islands
|
||||||
|
m->addRoad("Milano", "Boulder");
|
||||||
|
assert_false(m->hasLoop());
|
||||||
|
assert_int_eq(m->countIslands(), 2);
|
||||||
|
|
||||||
|
// Create Loop
|
||||||
|
m->addRoad("Colorado", "Siena");
|
||||||
|
assert_true(m->hasLoop());
|
||||||
|
assert_int_eq(m->countIslands(), 2);
|
||||||
|
|
||||||
|
// Create another Loop
|
||||||
|
m->addRoad("Lugano", "Zurich");
|
||||||
|
m->addRoad("Praia Grande", "Zurich");
|
||||||
|
assert_int_eq(m->countIslands(), 2);
|
||||||
|
|
||||||
|
// Merge two islands
|
||||||
|
m->addRoad("Praia Grande", "Naples");
|
||||||
|
assert_false(m->reachable("Colorado", "Praia Grande"));
|
||||||
|
assert_int_eq(m->countIslands(), 2);
|
||||||
|
m->addRoad("Praia Grande", "Siena");
|
||||||
|
m->addRoad("Naples", "Colorado");
|
||||||
|
assert_int_eq(m->countIslands(), 1);
|
||||||
|
assert_int_eq(m->neighborCountForCity("Zurich"), 2);
|
||||||
|
m->addRoad({"Colorado", "Zurich", "Siena"});
|
||||||
|
assert_true(m->reachable("Praia Grande", "Colorado"));
|
||||||
|
|
||||||
|
m->clear();
|
||||||
|
assert_int_eq(m->countIslands(), 0);
|
||||||
|
|
||||||
|
TEST_PASSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(santaclaus_test) {
|
||||||
|
std::unique_ptr<AbstractRoadMap> m(createRoadMap());
|
||||||
|
|
||||||
|
|
||||||
|
TEST_PASSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
RUN_TEST(basic_test);
|
||||||
|
RUN_TEST(loops_test);
|
||||||
|
RUN_TEST(santaclaus_test);
|
||||||
|
PRINT_TEST_RESULTS;
|
||||||
|
assert(ALL_TESTS_PASSED);
|
||||||
|
}
|
Reference in a new issue