test_integration_busy.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2019 - 2021 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_integration_busy.cpp
  10. * @brief integration test implementation for the cmd busy function
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. extern "C" {
  15. #include "shellmatta.h"
  16. }
  17. #include <string.h>
  18. static uint32_t write_callCnt = 0u;
  19. static char write_data[1024];
  20. static uint32_t write_length;
  21. static uint32_t busyCallCnt;
  22. static uint32_t notBusyCallCnt;
  23. static bool suspendBusy;
  24. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  25. {
  26. write_callCnt ++;
  27. while((length > 0) && (write_length < sizeof(write_data)))
  28. {
  29. write_data[write_length] = *data;
  30. data ++;
  31. length --;
  32. write_length ++;
  33. }
  34. return SHELLMATTA_OK;
  35. }
  36. static shellmatta_retCode_t busyCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  37. {
  38. (void) handle;
  39. (void) arguments;
  40. (void) length;
  41. shellmatta_retCode_t ret = SHELLMATTA_BUSY;
  42. static const char *callArgs = NULL;
  43. static uint32_t callLength = 0u;;
  44. if(busyCallCnt < 10u)
  45. {
  46. if(NULL == callArgs)
  47. {
  48. callArgs = arguments;
  49. callLength = length;
  50. }
  51. else
  52. {
  53. CHECK(callArgs == arguments);
  54. CHECK(callLength == length);
  55. }
  56. busyCallCnt ++;
  57. }
  58. else
  59. {
  60. ret = SHELLMATTA_OK;
  61. }
  62. if(true == suspendBusy)
  63. {
  64. ret = SHELLMATTA_CONTINUE;
  65. }
  66. return ret;
  67. }
  68. shellmatta_cmd_t busyCmd = {(char*)"busy", (char*)"b", NULL, NULL, busyCmdFct, NULL};
  69. static shellmatta_retCode_t notBusyCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  70. {
  71. (void) handle;
  72. (void) arguments;
  73. (void) length;
  74. notBusyCallCnt ++;
  75. return SHELLMATTA_OK;
  76. }
  77. shellmatta_cmd_t notBusyCmd = {(char*)"notBusy", (char*)"n", NULL, NULL, notBusyCmdFct, NULL};
  78. TEST_CASE( "shellmatta busy 1" ) {
  79. shellmatta_retCode_t ret;
  80. shellmatta_instance_t inst;
  81. shellmatta_handle_t handle;
  82. char buffer[1024];
  83. char historyBuffer[1024];
  84. char *dummyData = (char*) "busy and some arguments\r\n"
  85. "\r\nshellmatta->notBusy and some arguments\r\n"
  86. "\r\nshellmatta->";
  87. shellmatta_doInit( &inst,
  88. &handle,
  89. buffer,
  90. sizeof(buffer),
  91. historyBuffer,
  92. sizeof(historyBuffer),
  93. "shellmatta->",
  94. NULL,
  95. writeFct);
  96. busyCallCnt = 0u;
  97. notBusyCallCnt = 0u;
  98. write_callCnt = 0u;
  99. memset(write_data, 0, sizeof(write_data));
  100. write_length = 0u;
  101. suspendBusy = false;
  102. shellmatta_addCmd(handle, &busyCmd);
  103. shellmatta_addCmd(handle, &notBusyCmd);
  104. do
  105. {
  106. ret = shellmatta_processData(handle, (char*)"busy and some arguments\r"
  107. "notBusy and some arguments\r", 51);
  108. } while (SHELLMATTA_BUSY == ret);
  109. CHECK( 10u == busyCallCnt);
  110. CHECK( 1u == notBusyCallCnt );
  111. CHECK( write_length == strlen(dummyData));
  112. REQUIRE( strcmp(dummyData, write_data) == 0);
  113. }
  114. TEST_CASE( "shellmatta busy suspend with continuous mode" ) {
  115. shellmatta_retCode_t ret;
  116. shellmatta_instance_t inst;
  117. shellmatta_handle_t handle;
  118. char buffer[1024];
  119. char historyBuffer[1024];
  120. char *dummyData = (char*) "busy and some arguments\r\n";
  121. shellmatta_doInit( &inst,
  122. &handle,
  123. buffer,
  124. sizeof(buffer),
  125. historyBuffer,
  126. sizeof(historyBuffer),
  127. "shellmatta->",
  128. NULL,
  129. writeFct);
  130. busyCallCnt = 0u;
  131. notBusyCallCnt = 0u;
  132. write_callCnt = 0u;
  133. memset(write_data, 0, sizeof(write_data));
  134. write_length = 0u;
  135. suspendBusy = false;
  136. shellmatta_addCmd(handle, &busyCmd);
  137. shellmatta_addCmd(handle, &notBusyCmd);
  138. do
  139. {
  140. ret = shellmatta_processData(handle, (char*)"busy and some arguments\r123", 27u);
  141. suspendBusy = true;
  142. } while (SHELLMATTA_BUSY == ret);
  143. ret = shellmatta_processData(handle, (char*)"", 0u);
  144. CHECK( SHELLMATTA_CONTINUE == ret);
  145. CHECK( 6u == busyCallCnt);
  146. CHECK( 0u == notBusyCallCnt );
  147. CHECK( write_length == strlen(dummyData));
  148. REQUIRE( strcmp(dummyData, write_data) == 0);
  149. }