summaryrefslogtreecommitdiff
path: root/test/test_format.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_format.cpp')
-rw-r--r--test/test_format.cpp330
1 files changed, 326 insertions, 4 deletions
diff --git a/test/test_format.cpp b/test/test_format.cpp
index 319ed22..68c94cd 100644
--- a/test/test_format.cpp
+++ b/test/test_format.cpp
@@ -15,17 +15,339 @@
* limitations under the License.
*/
-// #include "fud_array.hpp"
-// #include "fud_format.hpp"
-// #include "fud_span.hpp"
+#include "fud_string_view.hpp"
+#include "fud_format.hpp"
#include "gtest/gtest.h"
namespace fud {
-TEST(FormatTest, FormatSpecTest)
+TEST(FormatTest, BasePositionalTest)
{
+ size_t length = 0;
+ auto formatSpecResult = FormatSpec::make(StringView{" {1:}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.takeError(), FudStatus::ArgumentInvalid);
+
+ formatSpecResult = FormatSpec::make(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);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 4);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default);
+ EXPECT_EQ(formatSpec.fill.fill, ' ');
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Default);
+ EXPECT_FALSE(formatSpec.takesWidth);
+ EXPECT_FALSE(formatSpec.takesPrecision);
+ EXPECT_FALSE(formatSpec.alternateForm);
+ EXPECT_FALSE(formatSpec.leadingZero);
+ EXPECT_FALSE(formatSpec.hasLocale);
+ EXPECT_TRUE(std::holds_alternative<std::monostate>(formatSpec.formatType));
+}
+
+TEST(FormatTest, AlignTest)
+{
+ size_t length = 0;
+
+ auto formatSpecResult = FormatSpec::make(StringView{"{1:<}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ auto formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(length, 5);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
+ EXPECT_EQ(formatSpec.fill.fill, ' ');
+ EXPECT_FALSE(formatSpec.takesWidth);
+ EXPECT_FALSE(formatSpec.takesPrecision);
+ EXPECT_FALSE(formatSpec.alternateForm);
+ EXPECT_FALSE(formatSpec.leadingZero);
+ EXPECT_FALSE(formatSpec.hasLocale);
+ EXPECT_TRUE(std::holds_alternative<std::monostate>(formatSpec.formatType));
+
+ formatSpecResult = FormatSpec::make(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);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 5);
+ EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
+ EXPECT_EQ(formatSpec.fill.fill, '<');
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:_<}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 6);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
+ EXPECT_EQ(formatSpec.fill.fill, '_');
+}
+
+TEST(FormatTest, SpecialTest)
+{
+ size_t length = 0;
+
+ auto formatSpecResult = FormatSpec::make(StringView{"{1:_< }"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ auto formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 7);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
+ EXPECT_EQ(formatSpec.fill.fill, '_');
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Space);
+ EXPECT_FALSE(formatSpec.alternateForm);
+ EXPECT_FALSE(formatSpec.leadingZero);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:+}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ EXPECT_EQ(length, 5);
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Plus);
+ EXPECT_FALSE(formatSpec.alternateForm);
+ EXPECT_FALSE(formatSpec.leadingZero);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:-}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 5);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Minus);
+ EXPECT_FALSE(formatSpec.alternateForm);
+ EXPECT_FALSE(formatSpec.leadingZero);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:_<#}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 7);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
+ EXPECT_EQ(formatSpec.fill.fill, '_');
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Default);
+ EXPECT_TRUE(formatSpec.alternateForm);
+ EXPECT_FALSE(formatSpec.leadingZero);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:_<0}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 7);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left);
+ EXPECT_EQ(formatSpec.fill.fill, '_');
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Default);
+ EXPECT_FALSE(formatSpec.alternateForm);
+ EXPECT_TRUE(formatSpec.leadingZero);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1: #}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 6);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default);
+ EXPECT_EQ(formatSpec.fill.fill, ' ');
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Space);
+ EXPECT_TRUE(formatSpec.alternateForm);
+ EXPECT_FALSE(formatSpec.leadingZero);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:# }"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:##}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1: 0}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 6);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default);
+ EXPECT_EQ(formatSpec.fill.fill, ' ');
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Space);
+ EXPECT_FALSE(formatSpec.alternateForm);
+ EXPECT_TRUE(formatSpec.leadingZero);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:0 }"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:0#}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:#00}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1: #0}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 7);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default);
+ EXPECT_EQ(formatSpec.fill.fill, ' ');
+ EXPECT_EQ(formatSpec.formatSign, FormatSign::Space);
+ EXPECT_TRUE(formatSpec.alternateForm);
+ EXPECT_TRUE(formatSpec.leadingZero);
+}
+
+TEST(FormatTest, WidthTest)
+{
+ size_t length = 0;
+
+ auto formatSpecResult = FormatSpec::make(StringView{"{1:1}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ auto formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 5);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_FALSE(formatSpec.takesWidth);
+ EXPECT_EQ(formatSpec.width, 1);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:543}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 7);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_FALSE(formatSpec.takesWidth);
+ EXPECT_EQ(formatSpec.width, 543);
+
+ formatSpecResult = FormatSpec::make(StringView{"{:543}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 6);
+ EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified);
+ EXPECT_FALSE(formatSpec.takesWidth);
+ EXPECT_EQ(formatSpec.width, 543);
+
+ // leading zero
+ formatSpecResult = FormatSpec::make(StringView{"{1:00}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
+
+ // #x100000000 4294967296
+ formatSpecResult = FormatSpec::make(StringView{"{1:4294967296}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:{1}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 7);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_TRUE(formatSpec.takesWidth);
+ EXPECT_EQ(formatSpec.width, 1);
+
+ // #x10000 65536
+ formatSpecResult = FormatSpec::make(StringView{"{1:{65536}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
+
+ formatSpecResult = FormatSpec::make(StringView{"{:{1}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
+
+ formatSpecResult = FormatSpec::make(StringView{"{:{}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 5);
+ EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified);
+ EXPECT_TRUE(formatSpec.takesWidth);
+ EXPECT_EQ(formatSpec.width, FormatSpec::widthUnspecified);
+}
+
+TEST(FormatTest, PrecisionTest)
+{
+ size_t length = 0;
+
+ auto formatSpecResult = FormatSpec::make(StringView{"{1:.1}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ auto formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 6);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_FALSE(formatSpec.takesPrecision);
+ EXPECT_EQ(formatSpec.precision, 1);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:.543}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 8);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_FALSE(formatSpec.takesPrecision);
+ EXPECT_EQ(formatSpec.precision, 543);
+
+ formatSpecResult = FormatSpec::make(StringView{"{:.543}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 7);
+ EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified);
+ EXPECT_FALSE(formatSpec.takesPrecision);
+ EXPECT_EQ(formatSpec.precision, 543);
+
+ // leading zero
+ formatSpecResult = FormatSpec::make(StringView{"{1:00}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
+
+ // #x100000000 4294967296
+ formatSpecResult = FormatSpec::make(StringView{"{1:.4294967296}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
+
+ formatSpecResult = FormatSpec::make(StringView{"{1:.{1}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 8);
+ EXPECT_EQ(formatSpec.position, 1);
+ EXPECT_TRUE(formatSpec.takesPrecision);
+ EXPECT_EQ(formatSpec.precision, 1);
+
+ // #x10000 65536
+ formatSpecResult = FormatSpec::make(StringView{"{1:.{65536}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError);
+
+ formatSpecResult = FormatSpec::make(StringView{"{:.{1}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isError());
+ EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid);
+
+ formatSpecResult = FormatSpec::make(StringView{"{:.{}}"}, length);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 6);
+ EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified);
+ EXPECT_TRUE(formatSpec.takesPrecision);
+ EXPECT_EQ(formatSpec.precision, FormatSpec::widthUnspecified);
+}
+
+TEST(FormatTest, LocaleTest)
+{
+ size_t length = 0;
+
+ auto formatSpecResult = FormatSpec::make(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);
+ ASSERT_TRUE(formatSpecResult.isOkay());
+ formatSpec = formatSpecResult.takeOkay();
+ EXPECT_EQ(length, 4);
+ EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified);
+ EXPECT_TRUE(formatSpec.hasLocale);
}
} // namespace fud