Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <string_view>
- #include "test_runner.h"
- #include "math.h"
- using namespace std;
- class Editor {
- public:
- // Реализуйте конструктор по умолчанию и объявленные методы
- Editor(){
- str = "";
- point = str.begin();
- buffer = "";
- }
- void Left(){
- if(point!=str.begin()){
- point-=1;
- }
- }
- void Right(){
- if(point!=str.end()){
- point++;
- }
- }
- void Insert(char token){
- str.insert(point,token);
- Right();
- }
- void Cut(size_t tokens = 1){
- Copy(tokens);
- str.erase(point,point+min(tokens,size_t(str.end()-point)));
- }
- void Copy(size_t tokens = 1){
- buffer = str.substr(point - str.begin(),min(tokens,size_t(str.end()-point)));
- }
- void Paste(){
- str.insert(point,buffer.begin(),buffer.end());
- point += buffer.length();
- }
- string GetText() const{
- return str;
- }
- private:
- string str;
- string::iterator point;
- string buffer;
- };
- void TypeText(Editor& editor, const string& text) {
- for(char c : text) {
- editor.Insert(c);
- }
- }
- void TestEditing() {
- {
- Editor editor;
- const size_t text_len = 12;
- const size_t first_part_len = 7;
- TypeText(editor, "hello, world");
- for(size_t i = 0; i < text_len; ++i) {
- editor.Left();
- }
- editor.Cut(first_part_len);
- for(size_t i = 0; i < text_len - first_part_len; ++i) {
- editor.Right();
- }
- TypeText(editor, ", ");
- editor.Paste();
- editor.Left();
- editor.Left();
- editor.Cut(3);
- ASSERT_EQUAL(editor.GetText(), "world, hello");
- }
- {
- Editor editor;
- TypeText(editor, "misprnit");
- editor.Left();
- editor.Left();
- editor.Left();
- editor.Cut(1);
- editor.Right();
- editor.Paste();
- ASSERT_EQUAL(editor.GetText(), "misprint");
- }
- }
- void TestReverse() {
- Editor editor;
- const string text = "esreveR";
- for(char c : text) {
- editor.Insert(c);
- editor.Left();
- }
- ASSERT_EQUAL(editor.GetText(), "Reverse");
- }
- void TestNoText() {
- Editor editor;
- ASSERT_EQUAL(editor.GetText(), "");
- editor.Left();
- editor.Left();
- editor.Right();
- editor.Right();
- editor.Copy(0);
- editor.Cut(0);
- editor.Paste();
- ASSERT_EQUAL(editor.GetText(), "");
- }
- void TestEmptyBuffer() {
- Editor editor;
- editor.Paste();
- TypeText(editor, "example");
- editor.Left();
- editor.Left();
- editor.Paste();
- editor.Right();
- editor.Paste();
- editor.Copy(0);
- editor.Paste();
- editor.Left();
- editor.Cut(0);
- editor.Paste();
- ASSERT_EQUAL(editor.GetText(), "example");
- }
- int main() {
- TestRunner tr;
- RUN_TEST(tr, TestEditing);
- RUN_TEST(tr, TestReverse);
- RUN_TEST(tr, TestNoText);
- RUN_TEST(tr, TestEmptyBuffer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment