You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.5KB

  1. #include <assert.h>
  2. #include <assert.h>
  3. #include "SqCommand.h"
  4. #include "UndoRedoStack.h"
  5. class Cmd : public SqCommand
  6. {
  7. public:
  8. virtual void execute() override
  9. {
  10. ++executeCount;
  11. }
  12. virtual void undo() override
  13. {
  14. ++undoCount;
  15. }
  16. int id;
  17. int executeCount = 0;
  18. int undoCount = 0;
  19. };
  20. using Cp = std::shared_ptr<Cmd>;
  21. static void test0()
  22. {
  23. UndoRedoStack ur;
  24. assert(!ur.canRedo());
  25. assert(!ur.canUndo());
  26. std::shared_ptr<Cmd> cmd(std::make_shared<Cmd>());
  27. assert(cmd->executeCount == 0);
  28. assert(cmd->undoCount == 0);
  29. ur.execute(cmd);
  30. assert(cmd->executeCount == 1);
  31. assert(cmd->undoCount == 0);
  32. assert(ur.canUndo());
  33. assert(!ur.canRedo());
  34. ur.undo();
  35. assert(cmd->undoCount == 1);
  36. assert(!ur.canUndo());
  37. assert(ur.canRedo());
  38. ur.redo();
  39. }
  40. static void test1()
  41. {
  42. UndoRedoStack ur;
  43. assert(!ur.canRedo());
  44. assert(!ur.canUndo());
  45. std::shared_ptr<Cmd> cmd(std::make_shared<Cmd>());
  46. assert(cmd->executeCount == 0);
  47. assert(cmd->undoCount == 0);
  48. cmd->id = 55;
  49. ur.execute(cmd);
  50. assert(cmd->executeCount == 1);
  51. assert(cmd->undoCount == 0);
  52. std::shared_ptr<Cmd> cmd2(std::make_shared<Cmd>());
  53. cmd2->id = 77;
  54. ur.execute(cmd2);
  55. assert(cmd2->executeCount == 1);
  56. assert(cmd->executeCount == 1);
  57. assert(cmd2->undoCount == 0);
  58. ur.undo();
  59. assert(cmd2->undoCount == 1);
  60. ur.undo();
  61. assert(cmd->undoCount == 1);
  62. }
  63. void testUndoRedo()
  64. {
  65. test0();
  66. test1();
  67. }