/* * 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_view.hpp" #include "fud_format.hpp" #include "gtest/gtest.h" namespace fud { 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(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(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