Skip to content

Commit

Permalink
UP-18, UP-19 unit tests for circular buffer, some for executor and in…
Browse files Browse the repository at this point in the history
…sert command
  • Loading branch information
extio1 committed May 22, 2024
1 parent 0e744aa commit 5d7956f
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 160 deletions.
16 changes: 15 additions & 1 deletion include/document/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@
const int pageWidth = 500;
const int pageHeight = 1000;

/**
* Interface for google mock.
* Executor's commands depend on this interface
*/
class IDocument {
public:
virtual void Insert(Glyph::GlyphPtr& glyph) = 0;
virtual void Remove(Glyph::GlyphPtr& glyph) = 0;
virtual void SelectGlyphs(GlyphContainer::GlyphList& glyphs) = 0;
virtual void PasteGlyphs(int x, int y) = 0;
virtual void CutGlyphs(GlyphContainer::GlyphList& glyphs) = 0;
};

class Compositor;

class Document {
class Document : public IDocument {
public:
using PageList = std::list<Page::PagePtr>;

Expand Down Expand Up @@ -73,4 +86,5 @@ class Document {
GlyphContainer::GlyphList selectedGlyphs;
};


#endif // TEXT_EDITOR_DOCUMENT_H_
31 changes: 0 additions & 31 deletions include/executor/command/copy.h

This file was deleted.

19 changes: 0 additions & 19 deletions include/executor/command/cut.h

This file was deleted.

4 changes: 2 additions & 2 deletions include/executor/command/insert_character.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class InsertCharacter : public ReversibleCommand {
public:
InsertCharacter(Document& doc, int x, int y, int wight, int height,
InsertCharacter(IDocument& doc, int x, int y, int wight, int height,
char symbol);

InsertCharacter(InsertCharacter&&) = default;
Expand All @@ -21,7 +21,7 @@ class InsertCharacter : public ReversibleCommand {
~InsertCharacter() override;

private:
Document& doc;
IDocument& doc;
Glyph::GlyphPtr character;
};

Expand Down
18 changes: 0 additions & 18 deletions include/executor/command/load_document.h

This file was deleted.

19 changes: 0 additions & 19 deletions include/executor/command/paste.h

This file was deleted.

4 changes: 2 additions & 2 deletions include/executor/command/remove_character.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class RemoveCharacter : public ReversibleCommand {
public:
RemoveCharacter(Document& doc, int x, int y);
RemoveCharacter(IDocument& doc, int x, int y);

RemoveCharacter(RemoveCharacter&&) = default;
RemoveCharacter& operator=(RemoveCharacter&&) = default;
Expand All @@ -20,7 +20,7 @@ class RemoveCharacter : public ReversibleCommand {
~RemoveCharacter() override;

private:
Document& doc;
IDocument& doc;
Glyph::GlyphPtr character;
};

Expand Down
18 changes: 0 additions & 18 deletions include/executor/command/save_document.h

This file was deleted.

14 changes: 8 additions & 6 deletions include/executor/utils/circular_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class CircularBuffer {
explicit CircularBuffer(size_t capacity)
: buffer(std::vector<T>(capacity)),
capacity(capacity),
next_indx(capacity) {}
next_index(capacity) {}

bool empty() { return capacity == 0; }

void push(T&& obj) {
buffer[next_indx++] = std::move(obj);
buffer[next_index++] = std::move(obj);
++capacity;
}

const T& get_next() { return buffer[next_indx++]; }
const T& get_next() { return buffer[next_index++]; }

const T& pop() {
auto& v = buffer[--next_indx];
auto& v = buffer[--next_index];
--capacity;
return v;
}
Expand Down Expand Up @@ -55,6 +55,8 @@ class CircularBuffer {
return v;
}

bool operator==(const int o) { return val == o; }

std::size_t get_value() { return val; }

private:
Expand Down Expand Up @@ -88,7 +90,7 @@ class CircularBuffer {
return v;
}

bool operator==(int o) { return val == o; }
bool operator==(const int o) { return val == o; }

std::size_t get_value() { return val; }

Expand All @@ -98,6 +100,6 @@ class CircularBuffer {
};

std::vector<T> buffer;
CircularValue next_indx; // Index there to push new element
CircularValue next_index; // Index there to push new element
LimitedValue capacity; // Current number of elements in circular buffer
};
3 changes: 2 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ int main() {
auto document = std::make_shared<Document>();
document->SetCompositor(std::make_shared<SimpleCompositor>());

auto controller = std::make_unique<Executor>(5);
auto controller = std::make_unique<Executor>(2);

controller->Do(std::make_shared<InsertCharacter>(*document, 0, 0, 10, 10, 'A'));
controller->Do(std::make_shared<InsertCharacter>(*document, 10, 0, 10, 10, 'B'));
controller->Do(std::make_shared<InsertCharacter>(*document, 20, 0, 10, 10, 'C'));
controller->Do(std::make_shared<InsertCharacter>(*document, 30, 0, 10, 10, 'D'));
controller->Undo();
controller->Redo();

Expand Down
8 changes: 0 additions & 8 deletions src/executor/command/copy.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/executor/command/insert_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "document/glyphs/character.h"

InsertCharacter::InsertCharacter(Document& doc, int x, int y, int width,
InsertCharacter::InsertCharacter(IDocument& doc, int x, int y, int width,
int height, char symbol)
: doc(doc),
character(std::make_shared<Character>(x, y, width, height, symbol))
Expand Down
16 changes: 0 additions & 16 deletions src/executor/command/load_document.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/executor/command/remove_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "document/glyphs/character.h"

RemoveCharacter::RemoveCharacter(Document& doc, int x, int y)
RemoveCharacter::RemoveCharacter(IDocument& doc, int x, int y)
: doc(doc),
character(std::make_shared<Character>(x, y, 0, 0, 0)) // Constuctor OK?
{}
Expand Down
16 changes: 0 additions & 16 deletions src/executor/command/save_document.cpp

This file was deleted.

3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(googletest)

add_subdirectory(model)
add_subdirectory(model)
add_subdirectory(controller)
10 changes: 10 additions & 0 deletions test/controller/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(circular_buffer_test circular_buffer_tests.cpp)
target_include_directories(circular_buffer_test PRIVATE ${include_dir})
target_link_libraries(circular_buffer_test PRIVATE GTest::gtest_main)
add_test(NAME circular_buffer_test COMMAND circular_buffer_test)

add_executable(executor_test executor_tests.cpp)
target_link_libraries(executor_test PRIVATE executor document compositor point GTest::gtest_main GTest::gmock_main)
add_test(NAME executor_test COMMAND executor_test)

set_tests_properties(executor_test PROPERTIES DEPENDS circular_buffer_test)
Loading

0 comments on commit 5d7956f

Please sign in to comment.