test_utils_shellAsciiToUInt32.cpp 5.7 KB

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