summaryrefslogtreecommitdiff
path: root/test/test_string_convert.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_string_convert.cpp')
-rw-r--r--test/test_string_convert.cpp186
1 files changed, 186 insertions, 0 deletions
diff --git a/test/test_string_convert.cpp b/test/test_string_convert.cpp
new file mode 100644
index 0000000..38f5123
--- /dev/null
+++ b/test/test_string_convert.cpp
@@ -0,0 +1,186 @@
+/*
+ * 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<int>(StringView{"0"});
+ ASSERT_TRUE(intResult.isOkay());
+ auto convertState = intResult.getOkay();
+ ASSERT_EQ(convertState.value, 0);
+ ASSERT_EQ(convertState.nextIndex, 1);
+
+ intResult = fromString<int>(StringView{"1"});
+ ASSERT_TRUE(intResult.isOkay());
+ convertState = intResult.getOkay();
+ ASSERT_EQ(convertState.value, 1);
+ ASSERT_EQ(convertState.nextIndex, 1);
+
+ intResult = fromString<int>(StringView{"-1"});
+ ASSERT_TRUE(intResult.isOkay());
+ convertState = intResult.getOkay();
+ ASSERT_EQ(convertState.value, -1);
+ ASSERT_EQ(convertState.nextIndex, 2);
+
+ intResult = fromString<int>(StringView{"+1"});
+ ASSERT_TRUE(intResult.isOkay());
+ convertState = intResult.getOkay();
+ ASSERT_EQ(convertState.value, 1);
+ ASSERT_EQ(convertState.nextIndex, 2);
+
+ intResult = fromString<int>(StringView{" +42"});
+ ASSERT_TRUE(intResult.isOkay());
+ convertState = intResult.getOkay();
+ ASSERT_EQ(convertState.value, 42);
+ ASSERT_EQ(convertState.nextIndex, 4);
+
+ intResult = fromString<int>(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<uint16_t>(StringView{"0"});
+ ASSERT_TRUE(unsignedResult.isOkay());
+ auto convertState = unsignedResult.getOkay();
+ ASSERT_EQ(convertState.value, 0);
+ ASSERT_EQ(convertState.nextIndex, 1);
+
+ unsignedResult = fromString<uint16_t>(StringView{"1"});
+ ASSERT_TRUE(unsignedResult.isOkay());
+ convertState = unsignedResult.getOkay();
+ ASSERT_EQ(convertState.value, 1);
+ ASSERT_EQ(convertState.nextIndex, 1);
+
+ unsignedResult = fromString<uint16_t>(StringView{"-1"});
+ ASSERT_TRUE(unsignedResult.isError());
+ ASSERT_EQ(unsignedResult.getError(), FudStatus::ArgumentInvalid);
+
+ unsignedResult = fromString<uint16_t>(StringView{"0xFFFF"});
+ ASSERT_TRUE(unsignedResult.isOkay());
+ convertState = unsignedResult.getOkay();
+ ASSERT_EQ(convertState.value, 0xFFFF);
+ ASSERT_EQ(convertState.nextIndex, 6);
+
+ unsignedResult = fromString<uint16_t>(StringView{"0x10000"});
+ ASSERT_TRUE(unsignedResult.isError());
+ ASSERT_EQ(unsignedResult.getError(), FudStatus::RangeError);
+
+ unsignedResult = fromString<uint16_t>(StringView{"+1"});
+ ASSERT_TRUE(unsignedResult.isOkay());
+ convertState = unsignedResult.getOkay();
+ ASSERT_EQ(convertState.value, 1);
+ ASSERT_EQ(convertState.nextIndex, 2);
+
+ unsignedResult = fromString<uint16_t>(StringView{" +42"});
+ ASSERT_TRUE(unsignedResult.isOkay());
+ convertState = unsignedResult.getOkay();
+ ASSERT_EQ(convertState.value, 42);
+ ASSERT_EQ(convertState.nextIndex, 4);
+
+ unsignedResult = fromString<uint16_t>(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<float>(StringView{"0"});
+ ASSERT_TRUE(floatResult.isOkay());
+ auto convertState = floatResult.getOkay();
+ ASSERT_EQ(convertState.value, 0.0);
+ ASSERT_EQ(convertState.nextIndex, 1);
+
+ floatResult = fromString<float>(StringView{"1.0"});
+ ASSERT_TRUE(floatResult.isOkay());
+ convertState = floatResult.getOkay();
+ ASSERT_EQ(convertState.value, 1.0);
+ ASSERT_EQ(convertState.nextIndex, 3);
+
+ floatResult = fromString<float>(StringView{"1.5"});
+ ASSERT_TRUE(floatResult.isOkay());
+ convertState = floatResult.getOkay();
+ ASSERT_EQ(convertState.value, 1.5);
+ ASSERT_EQ(convertState.nextIndex, 3);
+
+ floatResult = fromString<float>(StringView{"1.5 next"});
+ ASSERT_TRUE(floatResult.isOkay());
+ convertState = floatResult.getOkay();
+ ASSERT_EQ(convertState.value, 1.5);
+ ASSERT_EQ(convertState.nextIndex, 3);
+
+ floatResult = fromString<float>(StringView{"-1.0"});
+ ASSERT_TRUE(floatResult.isOkay());
+ convertState = floatResult.getOkay();
+ ASSERT_EQ(convertState.value, -1.0);
+ ASSERT_EQ(convertState.nextIndex, 4);
+
+ floatResult = fromString<float>(StringView{"-1.5 next"});
+ ASSERT_TRUE(floatResult.isOkay());
+ convertState = floatResult.getOkay();
+ ASSERT_EQ(convertState.value, -1.5);
+ ASSERT_EQ(convertState.nextIndex, 4);
+
+ floatResult = fromString<float>(StringView{" -1.5 next"});
+ ASSERT_TRUE(floatResult.isOkay());
+ convertState = floatResult.getOkay();
+ ASSERT_EQ(convertState.value, -1.5);
+ ASSERT_EQ(convertState.nextIndex, 5);
+
+ floatResult = fromString<float>(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<float>(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<float>(StringView{" -1EA next"});
+ ASSERT_TRUE(floatResult.isError());
+ ASSERT_EQ(floatResult.getError(), FudStatus::ArgumentInvalid);
+
+ floatResult = fromString<float>(StringView{" -1.5EA next"});
+ ASSERT_TRUE(floatResult.isError());
+ ASSERT_EQ(floatResult.getError(), FudStatus::ArgumentInvalid);
+
+ floatResult = fromString<float>(StringView{" -1.5EQ next"});
+ ASSERT_TRUE(floatResult.isError());
+ ASSERT_EQ(floatResult.getError(), FudStatus::ArgumentInvalid);
+
+ floatResult = fromString<float>(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