test_utils_insertChars.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "test/framework/catch.hpp"
  2. #include "src/shellmatta_utils.c"
  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_insertChars normal call" ) {
  15. shellmatta_instance_t inst;
  16. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  17. inst.buffer = buffer;
  18. inst.bufferSize = 20;
  19. inst.cursor = 8;
  20. inst.inputCount = 10;
  21. inst.echoEnabled = true;
  22. inst.write = writeFct;
  23. write_callCnt = 0u;
  24. memset(write_data, 0, sizeof(write_data));
  25. write_idx = 0u;
  26. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
  27. CHECK( inst.cursor == 12);
  28. CHECK( inst.inputCount == 14);
  29. CHECK( write_callCnt == 5u );
  30. CHECK( strncmp("blks\e[K\e[sij\e[u", write_data, 15u) == 0);
  31. REQUIRE( strncmp("abcdefghblksij\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  32. }
  33. TEST_CASE( "shellmatta_insertChars overwrite" ) {
  34. shellmatta_instance_t inst;
  35. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  36. inst.buffer = buffer;
  37. inst.bufferSize = 20;
  38. inst.cursor = 8;
  39. inst.inputCount = 10;
  40. inst.echoEnabled = true;
  41. inst.mode = SHELLMATTA_MODE_OVERWRITE;
  42. inst.write = writeFct;
  43. write_callCnt = 0u;
  44. memset(write_data, 0, sizeof(write_data));
  45. write_idx = 0u;
  46. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
  47. CHECK( inst.cursor == 12);
  48. CHECK( inst.inputCount == 14);
  49. CHECK( write_callCnt == 1u );
  50. CHECK( strncmp("blks", write_data, 5u) == 0);
  51. REQUIRE( strncmp("abcdefghblks\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  52. }
  53. TEST_CASE( "shellmatta_insertChars append" ) {
  54. shellmatta_instance_t inst;
  55. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  56. inst.buffer = buffer;
  57. inst.bufferSize = 20;
  58. inst.cursor = 10;
  59. inst.inputCount = 10;
  60. inst.echoEnabled = true;
  61. inst.mode = SHELLMATTA_MODE_INSERT;
  62. inst.write = writeFct;
  63. write_callCnt = 0u;
  64. memset(write_data, 0, sizeof(write_data));
  65. write_idx = 0u;
  66. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
  67. CHECK( inst.cursor == 14);
  68. CHECK( inst.inputCount == 14);
  69. CHECK( write_callCnt == 1u );
  70. CHECK( strncmp("blks", write_data, 5u) == 0);
  71. REQUIRE( strncmp("abcdefghijblks\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  72. }
  73. TEST_CASE( "shellmatta_insertChars 0 length" ) {
  74. shellmatta_instance_t inst;
  75. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  76. inst.buffer = buffer;
  77. inst.bufferSize = 20;
  78. inst.cursor = 8;
  79. inst.inputCount = 10;
  80. inst.echoEnabled = true;
  81. inst.write = writeFct;
  82. write_callCnt = 0u;
  83. memset(write_data, 0, sizeof(write_data));
  84. write_idx = 0u;
  85. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 0u);
  86. CHECK( inst.cursor == 8u );
  87. CHECK( inst.inputCount == 10u );
  88. CHECK( write_callCnt == 0u );
  89. CHECK( memcmp("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", write_data, sizeof(write_data) ) == 0u );
  90. REQUIRE( memcmp("abcdefghij\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  91. }