summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_format.cpp173
1 files changed, 130 insertions, 43 deletions
diff --git a/test/test_format.cpp b/test/test_format.cpp
index 68c94cd..f0ea93f 100644
--- a/test/test_format.cpp
+++ b/test/test_format.cpp
@@ -15,8 +15,8 @@
* limitations under the License.
*/
-#include "fud_string_view.hpp"
#include "fud_format.hpp"
+#include "fud_string_view.hpp"
#include "gtest/gtest.h"
@@ -26,17 +26,20 @@ TEST(FormatTest, BasePositionalTest)
{
size_t length = 0;
- auto formatSpecResult = FormatSpec::make(StringView{" {1:}"}, length);
+ auto formatSpecResult = FormatSpec::parse(StringView{" {1:}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.takeError(), FudStatus::ArgumentInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{1}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
auto formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(formatSpec.position, 1);
EXPECT_EQ(length, 3);
- formatSpecResult = FormatSpec::make(StringView{"{1:}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{""}, length);
+ ASSERT_EQ(formatSpecResult.takeErrorOr(FudStatus::NotImplemented), FudStatus::ArgumentInvalid);
+
+ formatSpecResult = FormatSpec::parse(StringView{"{1:}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 4);
@@ -49,14 +52,14 @@ TEST(FormatTest, BasePositionalTest)
EXPECT_FALSE(formatSpec.alternateForm);
EXPECT_FALSE(formatSpec.leadingZero);
EXPECT_FALSE(formatSpec.hasLocale);
- EXPECT_TRUE(std::holds_alternative<std::monostate>(formatSpec.formatType));
+ EXPECT_EQ(formatSpec.formatType, FormatType::Unspecified);
}
TEST(FormatTest, AlignTest)
{
size_t length = 0;
- auto formatSpecResult = FormatSpec::make(StringView{"{1:<}"}, length);
+ auto formatSpecResult = FormatSpec::parse(StringView{"{1:<}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
auto formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(formatSpec.position, 1);
@@ -68,16 +71,16 @@ TEST(FormatTest, AlignTest)
EXPECT_FALSE(formatSpec.alternateForm);
EXPECT_FALSE(formatSpec.leadingZero);
EXPECT_FALSE(formatSpec.hasLocale);
- EXPECT_TRUE(std::holds_alternative<std::monostate>(formatSpec.formatType));
+ EXPECT_EQ(formatSpec.formatType, FormatType::Unspecified);
- formatSpecResult = FormatSpec::make(StringView{"{1:<<}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:<<}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(formatSpec.position, 1);
EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
EXPECT_EQ(formatSpec.fill.fill, '<');
- formatSpecResult = FormatSpec::make(StringView{"{:<<}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:<<}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 5);
@@ -85,7 +88,7 @@ TEST(FormatTest, AlignTest)
EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
EXPECT_EQ(formatSpec.fill.fill, '<');
- formatSpecResult = FormatSpec::make(StringView{"{1:_<}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:_<}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 6);
@@ -98,7 +101,7 @@ TEST(FormatTest, SpecialTest)
{
size_t length = 0;
- auto formatSpecResult = FormatSpec::make(StringView{"{1:_< }"}, length);
+ auto formatSpecResult = FormatSpec::parse(StringView{"{1:_< }"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
auto formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 7);
@@ -109,7 +112,7 @@ TEST(FormatTest, SpecialTest)
EXPECT_FALSE(formatSpec.alternateForm);
EXPECT_FALSE(formatSpec.leadingZero);
- formatSpecResult = FormatSpec::make(StringView{"{1:+}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:+}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
EXPECT_EQ(length, 5);
formatSpec = formatSpecResult.takeOkay();
@@ -118,7 +121,7 @@ TEST(FormatTest, SpecialTest)
EXPECT_FALSE(formatSpec.alternateForm);
EXPECT_FALSE(formatSpec.leadingZero);
- formatSpecResult = FormatSpec::make(StringView{"{1:-}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:-}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 5);
@@ -127,7 +130,7 @@ TEST(FormatTest, SpecialTest)
EXPECT_FALSE(formatSpec.alternateForm);
EXPECT_FALSE(formatSpec.leadingZero);
- formatSpecResult = FormatSpec::make(StringView{"{1:_<#}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:_<#}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 7);
@@ -138,7 +141,7 @@ TEST(FormatTest, SpecialTest)
EXPECT_TRUE(formatSpec.alternateForm);
EXPECT_FALSE(formatSpec.leadingZero);
- formatSpecResult = FormatSpec::make(StringView{"{1:_<0}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:_<0}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 7);
@@ -149,7 +152,7 @@ TEST(FormatTest, SpecialTest)
EXPECT_FALSE(formatSpec.alternateForm);
EXPECT_TRUE(formatSpec.leadingZero);
- formatSpecResult = FormatSpec::make(StringView{"{1: #}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1: #}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 6);
@@ -160,15 +163,15 @@ TEST(FormatTest, SpecialTest)
EXPECT_TRUE(formatSpec.alternateForm);
EXPECT_FALSE(formatSpec.leadingZero);
- formatSpecResult = FormatSpec::make(StringView{"{1:# }"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:# }"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{1:##}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:##}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{1: 0}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1: 0}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 6);
@@ -179,19 +182,19 @@ TEST(FormatTest, SpecialTest)
EXPECT_FALSE(formatSpec.alternateForm);
EXPECT_TRUE(formatSpec.leadingZero);
- formatSpecResult = FormatSpec::make(StringView{"{1:0 }"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:0 }"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{1:0#}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:0#}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{1:#00}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:#00}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{1: #0}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1: #0}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 7);
@@ -207,7 +210,7 @@ TEST(FormatTest, WidthTest)
{
size_t length = 0;
- auto formatSpecResult = FormatSpec::make(StringView{"{1:1}"}, length);
+ auto formatSpecResult = FormatSpec::parse(StringView{"{1:1}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
auto formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 5);
@@ -215,7 +218,7 @@ TEST(FormatTest, WidthTest)
EXPECT_FALSE(formatSpec.takesWidth);
EXPECT_EQ(formatSpec.width, 1);
- formatSpecResult = FormatSpec::make(StringView{"{1:543}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:543}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 7);
@@ -223,7 +226,7 @@ TEST(FormatTest, WidthTest)
EXPECT_FALSE(formatSpec.takesWidth);
EXPECT_EQ(formatSpec.width, 543);
- formatSpecResult = FormatSpec::make(StringView{"{:543}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:543}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 6);
@@ -232,16 +235,16 @@ TEST(FormatTest, WidthTest)
EXPECT_EQ(formatSpec.width, 543);
// leading zero
- formatSpecResult = FormatSpec::make(StringView{"{1:00}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:00}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
// #x100000000 4294967296
- formatSpecResult = FormatSpec::make(StringView{"{1:4294967296}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:4294967296}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
- formatSpecResult = FormatSpec::make(StringView{"{1:{1}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:{1}}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 7);
@@ -250,15 +253,15 @@ TEST(FormatTest, WidthTest)
EXPECT_EQ(formatSpec.width, 1);
// #x10000 65536
- formatSpecResult = FormatSpec::make(StringView{"{1:{65536}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:{65536}}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
- formatSpecResult = FormatSpec::make(StringView{"{:{1}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:{1}}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{:{}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:{}}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 5);
@@ -271,7 +274,7 @@ TEST(FormatTest, PrecisionTest)
{
size_t length = 0;
- auto formatSpecResult = FormatSpec::make(StringView{"{1:.1}"}, length);
+ auto formatSpecResult = FormatSpec::parse(StringView{"{1:.1}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
auto formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 6);
@@ -279,7 +282,7 @@ TEST(FormatTest, PrecisionTest)
EXPECT_FALSE(formatSpec.takesPrecision);
EXPECT_EQ(formatSpec.precision, 1);
- formatSpecResult = FormatSpec::make(StringView{"{1:.543}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:.543}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 8);
@@ -287,7 +290,7 @@ TEST(FormatTest, PrecisionTest)
EXPECT_FALSE(formatSpec.takesPrecision);
EXPECT_EQ(formatSpec.precision, 543);
- formatSpecResult = FormatSpec::make(StringView{"{:.543}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:.543}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 7);
@@ -296,16 +299,16 @@ TEST(FormatTest, PrecisionTest)
EXPECT_EQ(formatSpec.precision, 543);
// leading zero
- formatSpecResult = FormatSpec::make(StringView{"{1:00}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:00}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
// #x100000000 4294967296
- formatSpecResult = FormatSpec::make(StringView{"{1:.4294967296}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:.4294967296}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
- formatSpecResult = FormatSpec::make(StringView{"{1:.{1}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:.{1}}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 8);
@@ -314,15 +317,15 @@ TEST(FormatTest, PrecisionTest)
EXPECT_EQ(formatSpec.precision, 1);
// #x10000 65536
- formatSpecResult = FormatSpec::make(StringView{"{1:.{65536}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{1:.{65536}}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
- formatSpecResult = FormatSpec::make(StringView{"{:.{1}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:.{1}}"}, length);
ASSERT_TRUE(formatSpecResult.isError());
EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
- formatSpecResult = FormatSpec::make(StringView{"{:.{}}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:.{}}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 6);
@@ -335,14 +338,14 @@ TEST(FormatTest, LocaleTest)
{
size_t length = 0;
- auto formatSpecResult = FormatSpec::make(StringView{"{1:L}"}, length);
+ auto formatSpecResult = FormatSpec::parse(StringView{"{1:L}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
auto formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 5);
EXPECT_EQ(formatSpec.position, 1);
EXPECT_TRUE(formatSpec.hasLocale);
- formatSpecResult = FormatSpec::make(StringView{"{:L}"}, length);
+ formatSpecResult = FormatSpec::parse(StringView{"{:L}"}, length);
ASSERT_TRUE(formatSpecResult.isOkay());
formatSpec = formatSpecResult.takeOkay();
EXPECT_EQ(length, 4);
@@ -350,4 +353,88 @@ TEST(FormatTest, LocaleTest)
EXPECT_TRUE(formatSpec.hasLocale);
}
+TEST(FormatTest, FormatTest)
+{
+ auto lengthArray{Array<size_t, 17>::constFill(0U)};
+ const Array<FormatSpecResult, 17> formatTypeResults{
+ FormatSpec::parse(StringView{"{:s}"}, lengthArray[0x00]),
+ FormatSpec::parse(StringView{"{:?}"}, lengthArray[0x01]),
+ FormatSpec::parse(StringView{"{:b}"}, lengthArray[0x02]),
+ FormatSpec::parse(StringView{"{:B}"}, lengthArray[0x03]),
+ FormatSpec::parse(StringView{"{:c}"}, lengthArray[0x04]),
+ FormatSpec::parse(StringView{"{:d}"}, lengthArray[0x05]),
+ FormatSpec::parse(StringView{"{:o}"}, lengthArray[0x06]),
+ FormatSpec::parse(StringView{"{:x}"}, lengthArray[0x07]),
+ FormatSpec::parse(StringView{"{:X}"}, lengthArray[0x08]),
+ FormatSpec::parse(StringView{"{:a}"}, lengthArray[0x09]),
+ FormatSpec::parse(StringView{"{:A}"}, lengthArray[0x0A]),
+ FormatSpec::parse(StringView{"{:e}"}, lengthArray[0x0B]),
+ FormatSpec::parse(StringView{"{:E}"}, lengthArray[0x0C]),
+ FormatSpec::parse(StringView{"{:f}"}, lengthArray[0x0D]),
+ FormatSpec::parse(StringView{"{:F}"}, lengthArray[0x0E]),
+ FormatSpec::parse(StringView{"{:g}"}, lengthArray[0x0F]),
+ FormatSpec::parse(StringView{"{:G}"}, lengthArray[0x10])};
+ const Array<FormatType, 17> formatTypes{
+ FormatType::String,
+ FormatType::Escaped,
+ FormatType::BinaryLower,
+ FormatType::BinaryUpper,
+ FormatType::Character,
+ FormatType::Decimal,
+ FormatType::Octal,
+ FormatType::HexLower,
+ FormatType::HexUpper,
+ FormatType::FloatHexLower,
+ FormatType::FloatHexUpper,
+ FormatType::ScientificLower,
+ FormatType::ScientificUpper,
+ FormatType::FixedLower,
+ FormatType::FixedUpper,
+ FormatType::GeneralLower,
+ FormatType::GeneralUpper,
+ };
+ for (size_t index = 0; index < lengthArray.size(); ++index) {
+ const auto& formatSpecResult = formatTypeResults[index];
+ const auto& length = lengthArray[index];
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ auto formatSpec = formatSpecResult.getOkay();
+ EXPECT_EQ(length, 4);
+ EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified);
+ EXPECT_EQ(formatSpec.formatType, formatTypes[index]);
+ }
+
+ size_t length = 0;
+
+ auto formatSpecResult = FormatSpec::parse(StringView{"{:N}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
+}
+
+TEST(FormatTest, ValidateSpecHelperTest)
+{
+ constexpr size_t invalid = 0xFFFF;
+ EXPECT_EQ(impl::validateSpecHelper("").getOkayOr(invalid), 0);
+ EXPECT_EQ(impl::validateSpecHelper("{}").getOkayOr(invalid), 1);
+ EXPECT_EQ(impl::validateSpecHelper("{{{}").getOkayOr(invalid), 1);
+ EXPECT_EQ(impl::validateSpecHelper("{}}}").getOkayOr(invalid), 1);
+ EXPECT_EQ(impl::validateSpecHelper("{{{}}}").getOkayOr(invalid), 1);
+ EXPECT_EQ(impl::validateSpecHelper("}").getErrorOr(FudStatus::Failure), FudStatus::FormatInvalid);
+ EXPECT_EQ(impl::validateSpecHelper("{}}").getErrorOr(FudStatus::Failure), FudStatus::FormatInvalid);
+ EXPECT_EQ(impl::validateSpecHelper("{").getErrorOr(FudStatus::Failure), FudStatus::FormatInvalid);
+ EXPECT_EQ(impl::validateSpecHelper("{{}").getErrorOr(FudStatus::Failure), FudStatus::FormatInvalid);
+ EXPECT_EQ(impl::validateSpecHelper("{{}").getOkayOr(invalid), invalid);
+ EXPECT_EQ(impl::validateSpecHelper("{{{}}} {} {{ }} {{{}}}").getOkayOr(invalid), 3);
+ EXPECT_EQ(impl::validateSpecHelper("{{{}}} {} {{ }} {{ }}}").getOkayOr(invalid), invalid);
+ EXPECT_EQ(
+ impl::validateSpecHelper("{{{}}} {} {{ }} {{{ }}").getErrorOr(FudStatus::Failure),
+ FudStatus::FormatInvalid);
+}
+
+TEST(FormatTest, OneArgFormatTest)
+{
+ String sink{};
+ auto formatResult = format(sink, FormatCharMode::Unchecked, "Test {:X}", 42U);
+ EXPECT_TRUE(formatResult.isOkay());
+}
+
} // namespace fud