test_utils_removeChars.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "test/framework/catch.hpp"
  2. #include "src/shellmatta_utils.h"
  3. #include <string.h>
  4. static uint32_t write_callCnt = 0u;
  5. static char write_data[20];
  6. static uint32_t write_idx;
  7. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  8. {
  9. write_callCnt ++;
  10. strncpy(&write_data[write_idx], data, length);
  11. write_idx += length;
  12. return SHELLMATTA_OK;
  13. }
  14. TEST_CASE("shellmatta_utils_removeChars_nothing_removed"){
  15. shellmatta_instance_t inst;
  16. memset(&inst, 0, sizeof(inst));
  17. uint32_t length = 0u;
  18. bool backspace = true;
  19. inst.write = writeFct;
  20. write_callCnt = 0u;
  21. memset(write_data, 0, sizeof(write_data));
  22. write_idx = 0u;
  23. inst.cursor = 20u;
  24. inst.inputCount = 20u;
  25. char buffer[20] = "abcdefghijklmnopqr";
  26. inst.buffer = buffer;
  27. inst.bufferSize = 20u;
  28. utils_removeChars( &inst, length, backspace);
  29. CHECK( inst.cursor == 20u);
  30. CHECK( inst.inputCount == 20);
  31. REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0);
  32. }
  33. TEST_CASE("shellmatta_utils_removeChars_backspace_false"){
  34. shellmatta_instance_t inst;
  35. char buffer[20] = "abcdefghijklmnopqr";
  36. memset(&inst, 0, sizeof(inst));
  37. uint32_t length = 5u;
  38. bool backspace = false;
  39. inst.write = writeFct;
  40. write_callCnt = 0u;
  41. memset(write_data, 0, sizeof(write_data));
  42. write_idx = 0u;
  43. inst.cursor = 20;
  44. inst.inputCount = 20u;
  45. inst.buffer = buffer;
  46. inst.bufferSize = 20u;
  47. utils_removeChars(&inst, length, backspace);
  48. CHECK( inst.cursor == 20u);
  49. CHECK( inst.inputCount == 20);
  50. REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0);
  51. }
  52. TEST_CASE("shellmatta_utils_removeChars_remove_five"){
  53. shellmatta_instance_t inst;
  54. char buffer[20] = "abcdefghijklmnopqr";
  55. memset(&inst, 0, sizeof(inst));
  56. inst.write = writeFct;
  57. write_callCnt = 0u;
  58. memset(write_data, 0, sizeof(write_data));
  59. write_idx = 0u;
  60. uint32_t length = 5u;
  61. bool backspace = true;
  62. inst.cursor = 10u;
  63. inst.inputCount = 20u;
  64. inst.bufferSize = 20u;
  65. inst.buffer = buffer;
  66. utils_removeChars(&inst, length, backspace);
  67. CHECK( inst.cursor == 5u);
  68. CHECK( inst.inputCount == 15u);
  69. REQUIRE(strncmp("abcdeklmnopqr", buffer, sizeof(buffer)) == 0);
  70. }
  71. TEST_CASE("shellmatta_utils_removeChars_length_greater_than_CursorPos"){
  72. shellmatta_instance_t inst;
  73. char buffer[20] = "abcdefghijklmnopqr";
  74. memset(&inst, 0, sizeof(inst));
  75. inst.write = writeFct;
  76. write_callCnt = 0u;
  77. memset(write_data, 0, sizeof(write_data));
  78. write_idx = 0u;
  79. uint32_t length = 15u;
  80. bool backspace = true;
  81. inst.cursor = 10u;
  82. inst.inputCount = 20u;
  83. inst.bufferSize = 20u;
  84. inst.buffer = buffer;
  85. utils_removeChars(&inst, length, backspace);
  86. CHECK( inst.cursor == 0u);
  87. CHECK( inst.inputCount == 10u);
  88. REQUIRE(strncmp("klmnopqr", buffer, sizeof(buffer)) == 0);
  89. }
  90. TEST_CASE("shellmatta_utils_removeChars_remove_chars_in_the_middle_of_the_buffer_backspace_false"){
  91. shellmatta_instance_t inst;
  92. char buffer[20] = "abcdefghijklmnopqr";
  93. memset(&inst, 0, sizeof(inst));
  94. inst.write = writeFct;
  95. write_callCnt = 0u;
  96. memset(write_data, 0, sizeof(write_data));
  97. write_idx = 0u;
  98. uint32_t length = 5u;
  99. bool backspace = false;
  100. inst.cursor = 10u;
  101. inst.inputCount = 20u;
  102. inst.bufferSize = 20u;
  103. inst.buffer = buffer;
  104. utils_removeChars(&inst, length, backspace);
  105. CHECK( inst.cursor == 10u);
  106. CHECK( inst.inputCount == 15u);
  107. REQUIRE(strncmp("abcdefghijpqr", buffer, sizeof(buffer)) == 0);
  108. }
  109. TEST_CASE("shellmatta_utils_removeChars_remove_more_chars_in_middle_of_buffer_than_are_present_backspace_false"){
  110. shellmatta_instance_t inst;
  111. char buffer[20] = "abcdefghijklmnopqr";
  112. memset(&inst, 0, sizeof(inst));
  113. inst.write = writeFct;
  114. write_callCnt = 0u;
  115. memset(write_data, 0, sizeof(write_data));
  116. write_idx = 0u;
  117. uint32_t length = 15u;
  118. bool backspace = false;
  119. inst.cursor = 10u;
  120. inst.inputCount = 20u;
  121. inst.bufferSize = 20u;
  122. inst.buffer = buffer;
  123. utils_removeChars(&inst, length, backspace);
  124. CHECK( inst.cursor == 10u);
  125. CHECK( inst.inputCount == 10u);
  126. REQUIRE(strncmp("abcdefghij", buffer, 10u) == 0);
  127. }
  128. TEST_CASE("shellmatta_utils_removeChars_curser_outside_buffer"){
  129. shellmatta_instance_t inst;
  130. char buffer[20] = "abcdefghijklmnopqr";
  131. memset(&inst, 0, sizeof(inst));
  132. inst.write = writeFct;
  133. write_callCnt = 0u;
  134. memset(write_data, 0, sizeof(write_data));
  135. write_idx = 0u;
  136. uint32_t length = 15u;
  137. bool backspace = false;
  138. inst.cursor = 21u;
  139. inst.inputCount = 20u;
  140. inst.bufferSize = 20u;
  141. inst.buffer = buffer;
  142. utils_removeChars(&inst, length, backspace);
  143. CHECK( inst.cursor == 21u);
  144. CHECK( inst.inputCount == 20u);
  145. REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0);
  146. }