test_utils_removeChars.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file test_utils_removeChars.c
  10. * @brief unittest for shellmatta utils_removeChars
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. #include "src/shellmatta_utils.h"
  15. #include <string.h>
  16. static uint32_t write_callCnt = 0u;
  17. static char write_data[20];
  18. static uint32_t write_idx;
  19. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  20. {
  21. write_callCnt ++;
  22. strncpy(&write_data[write_idx], data, length);
  23. write_idx += length;
  24. return SHELLMATTA_OK;
  25. }
  26. TEST_CASE("shellmatta_utils_removeChars_nothing_removed"){
  27. shellmatta_instance_t inst;
  28. memset(&inst, 0, sizeof(inst));
  29. uint32_t length = 0u;
  30. bool backspace = true;
  31. inst.write = writeFct;
  32. write_callCnt = 0u;
  33. memset(write_data, 0, sizeof(write_data));
  34. write_idx = 0u;
  35. inst.cursor = 20u;
  36. inst.inputCount = 20u;
  37. char buffer[20] = "abcdefghijklmnopqr";
  38. inst.buffer = buffer;
  39. inst.bufferSize = 20u;
  40. utils_removeChars( &inst, length, backspace);
  41. CHECK( inst.cursor == 20u);
  42. CHECK( inst.inputCount == 20);
  43. REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0);
  44. }
  45. TEST_CASE("shellmatta_utils_removeChars_backspace_false"){
  46. shellmatta_instance_t inst;
  47. char buffer[20] = "abcdefghijklmnopqr";
  48. memset(&inst, 0, sizeof(inst));
  49. uint32_t length = 5u;
  50. bool backspace = false;
  51. inst.write = writeFct;
  52. write_callCnt = 0u;
  53. memset(write_data, 0, sizeof(write_data));
  54. write_idx = 0u;
  55. inst.cursor = 20;
  56. inst.inputCount = 20u;
  57. inst.buffer = buffer;
  58. inst.bufferSize = 20u;
  59. utils_removeChars(&inst, length, backspace);
  60. CHECK( inst.cursor == 20u);
  61. CHECK( inst.inputCount == 20);
  62. REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0);
  63. }
  64. TEST_CASE("shellmatta_utils_removeChars_remove_five"){
  65. shellmatta_instance_t inst;
  66. char buffer[20] = "abcdefghijklmnopqr";
  67. memset(&inst, 0, sizeof(inst));
  68. inst.write = writeFct;
  69. write_callCnt = 0u;
  70. memset(write_data, 0, sizeof(write_data));
  71. write_idx = 0u;
  72. uint32_t length = 5u;
  73. bool backspace = true;
  74. inst.cursor = 10u;
  75. inst.inputCount = 20u;
  76. inst.bufferSize = 20u;
  77. inst.buffer = buffer;
  78. utils_removeChars(&inst, length, backspace);
  79. CHECK( inst.cursor == 5u);
  80. CHECK( inst.inputCount == 15u);
  81. REQUIRE(strncmp("abcdeklmnopqr", buffer, sizeof(buffer)) == 0);
  82. }
  83. TEST_CASE("shellmatta_utils_removeChars_length_greater_than_CursorPos"){
  84. shellmatta_instance_t inst;
  85. char buffer[20] = "abcdefghijklmnopqr";
  86. memset(&inst, 0, sizeof(inst));
  87. inst.write = writeFct;
  88. write_callCnt = 0u;
  89. memset(write_data, 0, sizeof(write_data));
  90. write_idx = 0u;
  91. uint32_t length = 15u;
  92. bool backspace = true;
  93. inst.cursor = 10u;
  94. inst.inputCount = 20u;
  95. inst.bufferSize = 20u;
  96. inst.buffer = buffer;
  97. utils_removeChars(&inst, length, backspace);
  98. CHECK( inst.cursor == 0u);
  99. CHECK( inst.inputCount == 10u);
  100. REQUIRE(strncmp("klmnopqr", buffer, sizeof(buffer)) == 0);
  101. }
  102. TEST_CASE("shellmatta_utils_removeChars_remove_chars_in_the_middle_of_the_buffer_backspace_false"){
  103. shellmatta_instance_t inst;
  104. char buffer[20] = "abcdefghijklmnopqr";
  105. memset(&inst, 0, sizeof(inst));
  106. inst.write = writeFct;
  107. write_callCnt = 0u;
  108. memset(write_data, 0, sizeof(write_data));
  109. write_idx = 0u;
  110. uint32_t length = 5u;
  111. bool backspace = false;
  112. inst.cursor = 10u;
  113. inst.inputCount = 20u;
  114. inst.bufferSize = 20u;
  115. inst.buffer = buffer;
  116. utils_removeChars(&inst, length, backspace);
  117. CHECK( inst.cursor == 10u);
  118. CHECK( inst.inputCount == 15u);
  119. REQUIRE(strncmp("abcdefghijpqr", buffer, sizeof(buffer)) == 0);
  120. }
  121. TEST_CASE("shellmatta_utils_removeChars_remove_more_chars_in_middle_of_buffer_than_are_present_backspace_false"){
  122. shellmatta_instance_t inst;
  123. char buffer[20] = "abcdefghijklmnopqr";
  124. memset(&inst, 0, sizeof(inst));
  125. inst.write = writeFct;
  126. write_callCnt = 0u;
  127. memset(write_data, 0, sizeof(write_data));
  128. write_idx = 0u;
  129. uint32_t length = 15u;
  130. bool backspace = false;
  131. inst.cursor = 10u;
  132. inst.inputCount = 20u;
  133. inst.bufferSize = 20u;
  134. inst.buffer = buffer;
  135. utils_removeChars(&inst, length, backspace);
  136. CHECK( inst.cursor == 10u);
  137. CHECK( inst.inputCount == 10u);
  138. REQUIRE(strncmp("abcdefghij", buffer, 10u) == 0);
  139. }
  140. TEST_CASE("shellmatta_utils_removeChars_curser_outside_buffer"){
  141. shellmatta_instance_t inst;
  142. char buffer[20] = "abcdefghijklmnopqr";
  143. memset(&inst, 0, sizeof(inst));
  144. inst.write = writeFct;
  145. write_callCnt = 0u;
  146. memset(write_data, 0, sizeof(write_data));
  147. write_idx = 0u;
  148. uint32_t length = 15u;
  149. bool backspace = false;
  150. inst.cursor = 21u;
  151. inst.inputCount = 20u;
  152. inst.bufferSize = 20u;
  153. inst.buffer = buffer;
  154. utils_removeChars(&inst, length, backspace);
  155. CHECK( inst.cursor == 21u);
  156. CHECK( inst.inputCount == 20u);
  157. REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0);
  158. }