123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*
- * Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- */
- /**
- * @file test_utils_shellAsciiToUInt32.c
- * @brief unittest for shellmatta utils_shellAsciiToUInt32
- * @author Stefan Strobel <stefan.strobel@shimatta.net>
- */
- #include "test/framework/catch.hpp"
- #include "src/shellmatta_utils.c"
- #include <string.h>
- SCENARIO("Check utils_shellAsciiToUInt32 with valid strings")
- {
- GIVEN("Initialized shellmatta utils module")
- {
- shellmatta_retCode_t ret;
- uint32_t result = 0xFFFFFFFFu;
- WHEN("Passing 123456789 with base 10")
- {
- ret = utils_shellAsciiToUInt32((char*)"123456789", &result, 10u);
- THEN("utils_shellAsciiToUInt32 returns successfully")
- {
- CHECK(ret == SHELLMATTA_OK);
- REQUIRE(result == 123456789);
- }
- }
- WHEN("Passing 4294967295 with base 10")
- {
- ret = utils_shellAsciiToUInt32((char*)"4294967295", &result, 10u);
- THEN("utils_shellAsciiToUInt32 returns successfully")
- {
- CHECK(ret == SHELLMATTA_OK);
- REQUIRE(result == 4294967295);
- }
- }
- WHEN("Passing 110100100001110011100011 with base 2")
- {
- ret = utils_shellAsciiToUInt32((char*)"110100100001110011100011", &result, 2u);
- THEN("utils_shellAsciiToUInt32 returns successfully")
- {
- CHECK(ret == SHELLMATTA_OK);
- REQUIRE(result == 0b110100100001110011100011);
- }
- }
- WHEN("Passing 435476600 with base 8")
- {
- ret = utils_shellAsciiToUInt32((char*)"435476600", &result, 8u);
- THEN("utils_shellAsciiToUInt32 returns successfully")
- {
- CHECK(ret == SHELLMATTA_OK);
- REQUIRE(result == 0435476600);
- }
- }
- WHEN("Passing AABBCCDD with base 16")
- {
- ret = utils_shellAsciiToUInt32((char*)"AABBCCDD", &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns successfully")
- {
- CHECK(ret == SHELLMATTA_OK);
- REQUIRE(result == 0xAABBCCDDu);
- }
- }
- WHEN("Passing deadbeef with base 16")
- {
- ret = utils_shellAsciiToUInt32((char*)"deadbeef", &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns successfully")
- {
- CHECK(ret == SHELLMATTA_OK);
- REQUIRE(result == 0xdeadbeefu);
- }
- }
- WHEN("Passing an empty string with base 16")
- {
- ret = utils_shellAsciiToUInt32((char*)"", &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns successfully")
- {
- CHECK(ret == SHELLMATTA_OK);
- REQUIRE(result == 0u);
- }
- }
- }
- }
- SCENARIO("Check utils_shellAsciiToUInt32 with invalid strings")
- {
- GIVEN("Initialized shellmatta utils module")
- {
- shellmatta_retCode_t ret;
- uint32_t result = 0xFFFFFFFFu;
- WHEN("Passing 4294967296 with base 10 - overflow")
- {
- ret = utils_shellAsciiToUInt32((char*)"4294967296", &result, 10u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- REQUIRE(ret == SHELLMATTA_ERROR);
- }
- }
- WHEN("Passing 123456 with base 2 - wrong base")
- {
- ret = utils_shellAsciiToUInt32((char*)"123456", &result, 2u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- REQUIRE(ret == SHELLMATTA_ERROR);
- }
- }
- WHEN("Passing FOOBAR with base 16 - wrong characters")
- {
- ret = utils_shellAsciiToUInt32((char*)"FOOBAR", &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- REQUIRE(ret == SHELLMATTA_ERROR);
- }
- }
- WHEN("Passing = with base 16 - wrong characters")
- {
- ret = utils_shellAsciiToUInt32((char*)"=", &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- REQUIRE(ret == SHELLMATTA_ERROR);
- }
- }
- WHEN("Passing / with base 16 - wrong characters")
- {
- ret = utils_shellAsciiToUInt32((char*)"/", &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- REQUIRE(ret == SHELLMATTA_ERROR);
- }
- }
- WHEN("Passing } with base 16 - wrong characters")
- {
- ret = utils_shellAsciiToUInt32((char*)"}", &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- REQUIRE(ret == SHELLMATTA_ERROR);
- }
- }
- WHEN("Passing invalid string")
- {
- ret = utils_shellAsciiToUInt32(NULL, &result, 16u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- CHECK(ret == SHELLMATTA_USE_FAULT);
- }
- }
- WHEN("Passing invalid result")
- {
- ret = utils_shellAsciiToUInt32((char*)"", NULL, 16u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- CHECK(ret == SHELLMATTA_USE_FAULT);
- }
- }
- WHEN("Passing invalid base")
- {
- ret = utils_shellAsciiToUInt32((char*)"", &result, 17u);
- THEN("utils_shellAsciiToUInt32 returns an error")
- {
- CHECK(ret == SHELLMATTA_USE_FAULT);
- }
- }
- }
- }
|