Argentum_02

textEditor.cpp

Aug 8th, 2019
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #include <string>
  2. #include <string_view>
  3. #include "test_runner.h"
  4. #include "math.h"
  5. using namespace std;
  6.  
  7. class Editor {
  8.  public:
  9.   // Реализуйте конструктор по умолчанию и объявленные методы
  10.   Editor(){
  11.       str = "";
  12.       point = str.begin();
  13.       buffer = "";
  14.   }
  15.   void Left(){
  16.       if(point!=str.begin()){
  17.           point-=1;
  18.       }
  19.   }
  20.   void Right(){
  21.       if(point!=str.end()){
  22.           point++;
  23.       }
  24.   }
  25.   void Insert(char token){
  26.       str.insert(point,token);
  27.       Right();
  28.   }
  29.   void Cut(size_t tokens = 1){
  30.       Copy(tokens);
  31.       str.erase(point,point+min(tokens,size_t(str.end()-point)));
  32.   }
  33.   void Copy(size_t tokens = 1){
  34.       buffer = str.substr(point - str.begin(),min(tokens,size_t(str.end()-point)));
  35.   }
  36.   void Paste(){
  37.       str.insert(point,buffer.begin(),buffer.end());
  38.       point += buffer.length();
  39.   }
  40.   string GetText() const{
  41.       return str;
  42.   }
  43.  private:
  44.   string str;
  45.   string::iterator point;
  46.   string buffer;
  47. };
  48.  
  49. void TypeText(Editor& editor, const string& text) {
  50.   for(char c : text) {
  51.     editor.Insert(c);
  52.   }
  53. }
  54.  
  55. void TestEditing() {
  56.   {
  57.     Editor editor;
  58.  
  59.     const size_t text_len = 12;
  60.     const size_t first_part_len = 7;
  61.     TypeText(editor, "hello, world");
  62.     for(size_t i = 0; i < text_len; ++i) {
  63.       editor.Left();
  64.     }
  65.     editor.Cut(first_part_len);
  66.     for(size_t i = 0; i < text_len - first_part_len; ++i) {
  67.       editor.Right();
  68.     }
  69.     TypeText(editor, ", ");
  70.     editor.Paste();
  71.     editor.Left();
  72.     editor.Left();
  73.     editor.Cut(3);
  74.    
  75.     ASSERT_EQUAL(editor.GetText(), "world, hello");
  76.   }
  77.   {
  78.     Editor editor;
  79.    
  80.     TypeText(editor, "misprnit");
  81.     editor.Left();
  82.     editor.Left();
  83.     editor.Left();
  84.     editor.Cut(1);
  85.     editor.Right();
  86.     editor.Paste();
  87.    
  88.     ASSERT_EQUAL(editor.GetText(), "misprint");
  89.   }
  90. }
  91.  
  92. void TestReverse() {
  93.   Editor editor;
  94.  
  95.   const string text = "esreveR";
  96.   for(char c : text) {
  97.     editor.Insert(c);
  98.     editor.Left();
  99.   }
  100.  
  101.   ASSERT_EQUAL(editor.GetText(), "Reverse");
  102. }
  103.  
  104. void TestNoText() {
  105.   Editor editor;
  106.   ASSERT_EQUAL(editor.GetText(), "");
  107.  
  108.   editor.Left();
  109.   editor.Left();
  110.   editor.Right();
  111.   editor.Right();
  112.   editor.Copy(0);
  113.   editor.Cut(0);
  114.   editor.Paste();
  115.  
  116.   ASSERT_EQUAL(editor.GetText(), "");
  117. }
  118.  
  119. void TestEmptyBuffer() {
  120.   Editor editor;
  121.  
  122.   editor.Paste();
  123.   TypeText(editor, "example");
  124.   editor.Left();
  125.   editor.Left();
  126.   editor.Paste();
  127.   editor.Right();
  128.   editor.Paste();
  129.   editor.Copy(0);
  130.   editor.Paste();
  131.   editor.Left();
  132.   editor.Cut(0);
  133.   editor.Paste();
  134.  
  135.   ASSERT_EQUAL(editor.GetText(), "example");
  136. }
  137.  
  138. int main() {
  139.   TestRunner tr;
  140.   RUN_TEST(tr, TestEditing);
  141.   RUN_TEST(tr, TestReverse);
  142.   RUN_TEST(tr, TestNoText);
  143.   RUN_TEST(tr, TestEmptyBuffer);
  144.   return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment