test_utils_shellAsciiToUInt32.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "test/framework/catch.hpp"
  2. #include "src/shellmatta_utils.c"
  3. #include <string.h>
  4. SCENARIO("Check utils_shellAsciiToUInt32 with valid strings")
  5. {
  6. GIVEN("Initialized shellmatta utils module")
  7. {
  8. shellmatta_retCode_t ret;
  9. uint32_t result = 0xFFFFFFFFu;
  10. WHEN("Passing 123456789 with base 10")
  11. {
  12. ret = utils_shellAsciiToUInt32((char*)"123456789", &result, 10u);
  13. THEN("utils_shellAsciiToUInt32 returns successfully")
  14. {
  15. CHECK(ret == SHELLMATTA_OK);
  16. REQUIRE(result == 123456789);
  17. }
  18. }
  19. WHEN("Passing 4294967295 with base 10")
  20. {
  21. ret = utils_shellAsciiToUInt32((char*)"4294967295", &result, 10u);
  22. THEN("utils_shellAsciiToUInt32 returns successfully")
  23. {
  24. CHECK(ret == SHELLMATTA_OK);
  25. REQUIRE(result == 4294967295);
  26. }
  27. }
  28. WHEN("Passing 110100100001110011100011 with base 2")
  29. {
  30. ret = utils_shellAsciiToUInt32((char*)"110100100001110011100011", &result, 2u);
  31. THEN("utils_shellAsciiToUInt32 returns successfully")
  32. {
  33. CHECK(ret == SHELLMATTA_OK);
  34. REQUIRE(result == 0b110100100001110011100011);
  35. }
  36. }
  37. WHEN("Passing 435476600 with base 8")
  38. {
  39. ret = utils_shellAsciiToUInt32((char*)"435476600", &result, 8u);
  40. THEN("utils_shellAsciiToUInt32 returns successfully")
  41. {
  42. CHECK(ret == SHELLMATTA_OK);
  43. REQUIRE(result == 0435476600);
  44. }
  45. }
  46. WHEN("Passing AABBCCDD with base 16")
  47. {
  48. ret = utils_shellAsciiToUInt32((char*)"AABBCCDD", &result, 16u);
  49. THEN("utils_shellAsciiToUInt32 returns successfully")
  50. {
  51. CHECK(ret == SHELLMATTA_OK);
  52. REQUIRE(result == 0xAABBCCDDu);
  53. }
  54. }
  55. WHEN("Passing deadbeef with base 16")
  56. {
  57. ret = utils_shellAsciiToUInt32((char*)"deadbeef", &result, 16u);
  58. THEN("utils_shellAsciiToUInt32 returns successfully")
  59. {
  60. CHECK(ret == SHELLMATTA_OK);
  61. REQUIRE(result == 0xdeadbeefu);
  62. }
  63. }
  64. WHEN("Passing an empty string with base 16")
  65. {
  66. ret = utils_shellAsciiToUInt32((char*)"", &result, 16u);
  67. THEN("utils_shellAsciiToUInt32 returns successfully")
  68. {
  69. CHECK(ret == SHELLMATTA_OK);
  70. REQUIRE(result == 0u);
  71. }
  72. }
  73. }
  74. }
  75. SCENARIO("Check utils_shellAsciiToUInt32 with invalid strings")
  76. {
  77. GIVEN("Initialized shellmatta utils module")
  78. {
  79. shellmatta_retCode_t ret;
  80. uint32_t result = 0xFFFFFFFFu;
  81. WHEN("Passing 4294967296 with base 10 - overflow")
  82. {
  83. ret = utils_shellAsciiToUInt32((char*)"4294967296", &result, 10u);
  84. THEN("utils_shellAsciiToUInt32 returns an error")
  85. {
  86. REQUIRE(ret == SHELLMATTA_ERROR);
  87. }
  88. }
  89. WHEN("Passing 123456 with base 2 - wrong base")
  90. {
  91. ret = utils_shellAsciiToUInt32((char*)"123456", &result, 2u);
  92. THEN("utils_shellAsciiToUInt32 returns an error")
  93. {
  94. REQUIRE(ret == SHELLMATTA_ERROR);
  95. }
  96. }
  97. WHEN("Passing FOOBAR with base 16 - wrong characters")
  98. {
  99. ret = utils_shellAsciiToUInt32((char*)"FOOBAR", &result, 16u);
  100. THEN("utils_shellAsciiToUInt32 returns an error")
  101. {
  102. REQUIRE(ret == SHELLMATTA_ERROR);
  103. }
  104. }
  105. WHEN("Passing = with base 16 - wrong characters")
  106. {
  107. ret = utils_shellAsciiToUInt32((char*)"=", &result, 16u);
  108. THEN("utils_shellAsciiToUInt32 returns an error")
  109. {
  110. REQUIRE(ret == SHELLMATTA_ERROR);
  111. }
  112. }
  113. WHEN("Passing / with base 16 - wrong characters")
  114. {
  115. ret = utils_shellAsciiToUInt32((char*)"/", &result, 16u);
  116. THEN("utils_shellAsciiToUInt32 returns an error")
  117. {
  118. REQUIRE(ret == SHELLMATTA_ERROR);
  119. }
  120. }
  121. WHEN("Passing } with base 16 - wrong characters")
  122. {
  123. ret = utils_shellAsciiToUInt32((char*)"}", &result, 16u);
  124. THEN("utils_shellAsciiToUInt32 returns an error")
  125. {
  126. REQUIRE(ret == SHELLMATTA_ERROR);
  127. }
  128. }
  129. WHEN("Passing invalid string")
  130. {
  131. ret = utils_shellAsciiToUInt32(NULL, &result, 16u);
  132. THEN("utils_shellAsciiToUInt32 returns an error")
  133. {
  134. CHECK(ret == SHELLMATTA_USE_FAULT);
  135. }
  136. }
  137. WHEN("Passing invalid result")
  138. {
  139. ret = utils_shellAsciiToUInt32((char*)"", NULL, 16u);
  140. THEN("utils_shellAsciiToUInt32 returns an error")
  141. {
  142. CHECK(ret == SHELLMATTA_USE_FAULT);
  143. }
  144. }
  145. WHEN("Passing invalid base")
  146. {
  147. ret = utils_shellAsciiToUInt32((char*)"", &result, 17u);
  148. THEN("utils_shellAsciiToUInt32 returns an error")
  149. {
  150. CHECK(ret == SHELLMATTA_USE_FAULT);
  151. }
  152. }
  153. }
  154. }