test_utils_shellItoa.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_shellItoa.c
  10. * @brief unittest for shellmatta utils_shellItoa
  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. TEST_CASE( "shellmatta_utils.c - itoa - 123456 base 10") {
  17. char buffer[64];
  18. memset(buffer, 0, sizeof(buffer));
  19. CHECK(utils_shellItoa(123456, buffer, 10) == 6);
  20. REQUIRE_THAT(buffer, Catch::Matchers::Equals("123456"));
  21. }
  22. TEST_CASE( "shellmatta_utils.c - itoa - 0x0ABBCCDD base 16") {
  23. char buffer[64];
  24. memset(buffer, 0, sizeof(buffer));
  25. CHECK(utils_shellItoa(0x0ABBCCDD, buffer, 16) == 7);
  26. REQUIRE_THAT(buffer, Catch::Matchers::Equals("ABBCCDD"));
  27. }
  28. TEST_CASE( "shellmatta_utils.c - itoa - -574236 base 10") {
  29. char buffer[64];
  30. memset(buffer, 0, sizeof(buffer));
  31. CHECK(utils_shellItoa(-574236, buffer, 10) == 7);
  32. REQUIRE_THAT(buffer, Catch::Matchers::Equals("-574236"));
  33. }
  34. TEST_CASE( "shellmatta_utils.c - itoa - 0x80000000 base 2") {
  35. char buffer[64];
  36. memset(buffer, 0, sizeof(buffer));
  37. CHECK(utils_shellItoa(0x80000000, buffer, 2) == 33);
  38. REQUIRE_THAT(buffer, Catch::Matchers::Equals("-10000000000000000000000000000000"));
  39. }
  40. TEST_CASE( "shellmatta_utils.c - itoa - 0x7FFFFFFF base 2") {
  41. char buffer[64];
  42. memset(buffer, 0, sizeof(buffer));
  43. CHECK(utils_shellItoa(0x7FFFFFFF, buffer, 2) == 31);
  44. REQUIRE_THAT(buffer, Catch::Matchers::Equals("1111111111111111111111111111111"));
  45. }
  46. TEST_CASE( "shellmatta_utils.c - itoa - 0x7FFFFFFF base 1 - wrong base") {
  47. char buffer[64];
  48. memset(buffer, 0, sizeof(buffer));
  49. CHECK(utils_shellItoa(0x7FFFFFFF, buffer, 1) == 0);
  50. REQUIRE_THAT(buffer, Catch::Matchers::Equals("\0"));
  51. }
  52. TEST_CASE( "shellmatta_utils.c - itoa - 0x7FFFFFFF base 17 - wrong base") {
  53. char buffer[64];
  54. memset(buffer, 0, sizeof(buffer));
  55. CHECK(utils_shellItoa(0x7FFFFFFF, buffer, 17) == 0);
  56. REQUIRE_THAT(buffer, Catch::Matchers::Equals("\0"));
  57. }