test_integration_busy.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2019 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 shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  24. {
  25. write_callCnt ++;
  26. while((length > 0) && (write_length < sizeof(write_data)))
  27. {
  28. write_data[write_length] = *data;
  29. data ++;
  30. length --;
  31. write_length ++;
  32. }
  33. return SHELLMATTA_OK;
  34. }
  35. static shellmatta_retCode_t busyCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  36. {
  37. (void) handle;
  38. (void) arguments;
  39. (void) length;
  40. shellmatta_retCode_t ret = SHELLMATTA_BUSY;
  41. static const char *callArgs = NULL;
  42. static uint32_t callLength = 0u;;
  43. if(busyCallCnt < 10u)
  44. {
  45. if(NULL == callArgs)
  46. {
  47. callArgs = arguments;
  48. callLength = length;
  49. }
  50. else
  51. {
  52. CHECK(callArgs == arguments);
  53. CHECK(callLength == length);
  54. }
  55. busyCallCnt ++;
  56. }
  57. else
  58. {
  59. ret = SHELLMATTA_OK;
  60. }
  61. return ret;
  62. }
  63. shellmatta_cmd_t busyCmd = {(char*)"busy", (char*)"b", NULL, NULL, busyCmdFct, NULL};
  64. static shellmatta_retCode_t notBusyCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  65. {
  66. (void) handle;
  67. (void) arguments;
  68. (void) length;
  69. notBusyCallCnt ++;
  70. return SHELLMATTA_OK;
  71. }
  72. shellmatta_cmd_t notBusyCmd = {(char*)"notBusy", (char*)"n", NULL, NULL, notBusyCmdFct, NULL};
  73. TEST_CASE( "shellmatta busy 1" ) {
  74. shellmatta_retCode_t ret;
  75. shellmatta_instance_t inst;
  76. shellmatta_handle_t handle;
  77. char buffer[1024];
  78. char historyBuffer[1024];
  79. char *dummyData = (char*) "busy and some arguments\r\n"
  80. "\r\nshellmatta->notBusy and some arguments\r\n"
  81. "\r\nshellmatta->";
  82. shellmatta_doInit( &inst,
  83. &handle,
  84. buffer,
  85. sizeof(buffer),
  86. historyBuffer,
  87. sizeof(historyBuffer),
  88. "shellmatta->",
  89. NULL,
  90. writeFct);
  91. busyCallCnt = 0u;
  92. notBusyCallCnt = 0u;
  93. write_callCnt = 0u;
  94. memset(write_data, 0, sizeof(write_data));
  95. write_length = 0u;
  96. shellmatta_addCmd(handle, &busyCmd);
  97. shellmatta_addCmd(handle, &notBusyCmd);
  98. do
  99. {
  100. ret = shellmatta_processData(handle, (char*)"busy and some arguments\r"
  101. "notBusy and some arguments\r", 51);
  102. } while (SHELLMATTA_BUSY == ret);
  103. CHECK( 10u == busyCallCnt);
  104. CHECK( 1u == notBusyCallCnt );
  105. CHECK( write_length == strlen(dummyData));
  106. REQUIRE( strcmp(dummyData, write_data) == 0);
  107. }