/* * libfud * Copyright 2024 Dominick Allen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "fud_string_convert.hpp" #include "gtest/gtest.h" namespace fud { TEST(FudStringConvert, ConvertInt) { auto intResult = fromString(StringView{"0"}); ASSERT_TRUE(intResult.isOkay()); auto convertState = intResult.getOkay(); ASSERT_EQ(convertState.value, 0); ASSERT_EQ(convertState.nextIndex, 1); intResult = fromString(StringView{"1"}); ASSERT_TRUE(intResult.isOkay()); convertState = intResult.getOkay(); ASSERT_EQ(convertState.value, 1); ASSERT_EQ(convertState.nextIndex, 1); intResult = fromString(StringView{"-1"}); ASSERT_TRUE(intResult.isOkay()); convertState = intResult.getOkay(); ASSERT_EQ(convertState.value, -1); ASSERT_EQ(convertState.nextIndex, 2); intResult = fromString(StringView{"+1"}); ASSERT_TRUE(intResult.isOkay()); convertState = intResult.getOkay(); ASSERT_EQ(convertState.value, 1); ASSERT_EQ(convertState.nextIndex, 2); intResult = fromString(StringView{" +42"}); ASSERT_TRUE(intResult.isOkay()); convertState = intResult.getOkay(); ASSERT_EQ(convertState.value, 42); ASSERT_EQ(convertState.nextIndex, 4); intResult = fromString(StringView{" +42Q"}); ASSERT_TRUE(intResult.isOkay()); convertState = intResult.getOkay(); ASSERT_EQ(convertState.value, 42); ASSERT_EQ(convertState.nextIndex, 4); } TEST(FudStringConvert, ConvertUnsigned) { auto unsignedResult = fromString(StringView{"0"}); ASSERT_TRUE(unsignedResult.isOkay()); auto convertState = unsignedResult.getOkay(); ASSERT_EQ(convertState.value, 0); ASSERT_EQ(convertState.nextIndex, 1); unsignedResult = fromString(StringView{"1"}); ASSERT_TRUE(unsignedResult.isOkay()); convertState = unsignedResult.getOkay(); ASSERT_EQ(convertState.value, 1); ASSERT_EQ(convertState.nextIndex, 1); unsignedResult = fromString(StringView{"-1"}); ASSERT_TRUE(unsignedResult.isError()); ASSERT_EQ(unsignedResult.getError(), FudStatus::ArgumentInvalid); unsignedResult = fromString(StringView{"0xFFFF"}); ASSERT_TRUE(unsignedResult.isOkay()); convertState = unsignedResult.getOkay(); ASSERT_EQ(convertState.value, 0xFFFF); ASSERT_EQ(convertState.nextIndex, 6); unsignedResult = fromString(StringView{"0x10000"}); ASSERT_TRUE(unsignedResult.isError()); ASSERT_EQ(unsignedResult.getError(), FudStatus::RangeError); unsignedResult = fromString(StringView{"+1"}); ASSERT_TRUE(unsignedResult.isOkay()); convertState = unsignedResult.getOkay(); ASSERT_EQ(convertState.value, 1); ASSERT_EQ(convertState.nextIndex, 2); unsignedResult = fromString(StringView{" +42"}); ASSERT_TRUE(unsignedResult.isOkay()); convertState = unsignedResult.getOkay(); ASSERT_EQ(convertState.value, 42); ASSERT_EQ(convertState.nextIndex, 4); unsignedResult = fromString(StringView{" +42Q"}); ASSERT_TRUE(unsignedResult.isOkay()); convertState = unsignedResult.getOkay(); ASSERT_EQ(convertState.value, 42); ASSERT_EQ(convertState.nextIndex, 4); } TEST(FudStringConvert, ConvertFloat) { auto floatResult = fromString(StringView{"0"}); ASSERT_TRUE(floatResult.isOkay()); auto convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, 0.0); ASSERT_EQ(convertState.nextIndex, 1); floatResult = fromString(StringView{"1.0"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, 1.0); ASSERT_EQ(convertState.nextIndex, 3); floatResult = fromString(StringView{"1.5"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, 1.5); ASSERT_EQ(convertState.nextIndex, 3); floatResult = fromString(StringView{"1.5 next"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, 1.5); ASSERT_EQ(convertState.nextIndex, 3); floatResult = fromString(StringView{"-1.0"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, -1.0); ASSERT_EQ(convertState.nextIndex, 4); floatResult = fromString(StringView{"-1.5 next"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, -1.5); ASSERT_EQ(convertState.nextIndex, 4); floatResult = fromString(StringView{" -1.5 next"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, -1.5); ASSERT_EQ(convertState.nextIndex, 5); floatResult = fromString(StringView{" -1.5E+3 next"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, -1.5E+3); ASSERT_EQ(convertState.nextIndex, sizeof(" -1.5E+3") - 1); floatResult = fromString(StringView{" -15E+2 next"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, -1.5E+3); ASSERT_EQ(convertState.nextIndex, sizeof(" -15E+2") - 1); floatResult = fromString(StringView{" -1EA next"}); ASSERT_TRUE(floatResult.isError()); ASSERT_EQ(floatResult.getError(), FudStatus::ArgumentInvalid); floatResult = fromString(StringView{" -1.5EA next"}); ASSERT_TRUE(floatResult.isError()); ASSERT_EQ(floatResult.getError(), FudStatus::ArgumentInvalid); floatResult = fromString(StringView{" -1.5EQ next"}); ASSERT_TRUE(floatResult.isError()); ASSERT_EQ(floatResult.getError(), FudStatus::ArgumentInvalid); floatResult = fromString(StringView{" -1.5E3Q next"}); ASSERT_TRUE(floatResult.isOkay()); convertState = floatResult.getOkay(); ASSERT_EQ(convertState.value, -1.5E3); ASSERT_EQ(convertState.nextIndex, sizeof(" -1.5E3") - 1); } } // namespace fud