-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
#include "doctest.h" | ||
|
||
#include "sqlist_tests.hpp" | ||
#include "slist_tests.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __SELEMENT_HPP | ||
#define __SELEMENT_HPP | ||
|
||
#include <iostream> | ||
#include "llist.hpp" | ||
|
||
class SElement { | ||
public: | ||
virtual SElement* clone() const = 0; | ||
|
||
// извеждане | ||
virtual void print(std::ostream& = std::cout) = 0; | ||
|
||
// събиране на елемeнти | ||
virtual void collect(LinkedList<int>&) = 0; | ||
|
||
~SElement() {} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef __SINT_HPP | ||
#define __SINT_HPP | ||
|
||
#include "selement.hpp" | ||
|
||
class SInt : public SElement { | ||
private: | ||
int data; | ||
public: | ||
SInt* clone() const { return new SInt(*this); } | ||
|
||
SInt(int _data = 0) : data(_data) {} | ||
|
||
// извеждане | ||
void print(std::ostream& os) { | ||
os << data; | ||
} | ||
|
||
// събиране на елемeнти | ||
void collect(LinkedList<int>& l) { | ||
l.insertLast(data); | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef __SLIST_HPP | ||
#define __SLIST_HPP | ||
|
||
#include "selement.hpp" | ||
|
||
class SList : public LinkedList<SElement*>, | ||
public SElement { | ||
public: | ||
SList* clone() const { return new SList(*this); } | ||
|
||
// TODO: голяма 4 | ||
|
||
SList& operator<<(SElement const& se) { | ||
LinkedList<SElement*>::insertLast(se.clone()); | ||
return *this; | ||
} | ||
|
||
// извеждане | ||
void print(std::ostream& os) { | ||
os << '('; | ||
/* | ||
for(SElement* se : *this) { | ||
se->print(os); | ||
os << ' '; | ||
} | ||
*/ | ||
SList::I it = begin(); | ||
if (it.valid()) { | ||
(*it++)->print(os); | ||
while(it.valid()) { | ||
os << ' '; | ||
(*it++)->print(os); | ||
} | ||
} | ||
os << ')'; | ||
} | ||
|
||
// събиране на елемeнти | ||
void collect(LinkedList<int>& l) { | ||
for(SElement* se : *this) | ||
se->collect(l); | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef __SLIST_TESTS_HPP | ||
#define __SLIST_TESTS_HPP | ||
|
||
#include "doctest.h" | ||
#include <sstream> | ||
#include "sint.hpp" | ||
#include "slist.hpp" | ||
|
||
SList SList1(SElement const& x) { | ||
return SList() << x; | ||
} | ||
|
||
SList SList2(SElement const& x, | ||
SElement const& y) { | ||
return SList1(x) << y; | ||
} | ||
|
||
TEST_CASE("Извеждане и събиране на елементи от дълбок списък") { | ||
SList sl = SList() | ||
<< | ||
SList2(SInt(1), SList1(SInt(2))) | ||
<< | ||
(SList() | ||
<< SList2(SList1(SInt(3)), SInt(4)) | ||
<< SList2(SInt(5), SList1(SInt(6))) | ||
<< SList() | ||
<< SList1(SInt(7))) | ||
<< SInt(8); | ||
|
||
std::ostringstream os; | ||
sl.print(os); | ||
CHECK_EQ(os.str(), "((1 (2)) (((3) 4) (5 (6)) () (7)) 8)"); | ||
|
||
LinkedList<int> result; | ||
sl.collect(result); | ||
|
||
int i = 1; | ||
for(int x : result) | ||
CHECK_EQ(x, i++); | ||
CHECK_EQ(i, 9); | ||
} | ||
|
||
#endif |