blob: 2f3608d851204d2a888aed8f30e4afec04968a72 [file] [log] [blame]
// Copyright 2019 The Pigweed Authors
//
// 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
//
// https://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 "pw_string/to_string.h"
#include <array>
#include <cinttypes>
#include <cmath>
#include <cstring>
#include <string>
#include "gtest/gtest.h"
#include "pw_status/status.h"
#include "pw_string/type_to_string.h"
namespace pw {
struct CustomType {
unsigned a;
unsigned b;
static constexpr const char* kToString = "This is a CustomType";
CustomType() = default;
// Non-copyable to verify that ToString doesn't copy it.
CustomType(const CustomType&) = delete;
CustomType& operator=(const CustomType&) = delete;
};
StatusWithSize ToString(const CustomType&, std::span<char> buffer) {
int result =
std::snprintf(buffer.data(), buffer.size(), CustomType::kToString);
if (result < 0) {
return StatusWithSize::Unknown();
}
if (static_cast<size_t>(result) < buffer.size()) {
return StatusWithSize(result);
}
return StatusWithSize::ResourceExhausted(buffer.empty() ? 0u
: buffer.size() - 1);
}
namespace {
char buffer[128] = {};
char expected[128] = {};
TEST(ToString, Bool) {
const volatile bool b = true;
EXPECT_EQ(4u, ToString(b, buffer).size());
EXPECT_STREQ("true", buffer);
EXPECT_EQ(5u, ToString(false, buffer).size());
EXPECT_STREQ("false", buffer);
}
TEST(ToString, Char) {
EXPECT_EQ(1u, ToString('%', buffer).size());
EXPECT_STREQ("%", buffer);
}
template <typename T>
constexpr T kInteger = 127;
TEST(ToString, Integer_AllTypesAreSupported) {
EXPECT_EQ(3u, ToString(kInteger<unsigned char>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<signed char>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<unsigned short>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<signed short>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<unsigned int>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<signed int>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<unsigned long>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<signed long>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<unsigned long long>, buffer).size());
EXPECT_STREQ("127", buffer);
EXPECT_EQ(3u, ToString(kInteger<signed long long>, buffer).size());
EXPECT_STREQ("127", buffer);
}
TEST(ToString, ScopedEnum) {
enum class MyEnum : short { kLuckyNumber = 8 };
auto result = ToString(MyEnum::kLuckyNumber, buffer);
EXPECT_EQ(1u, result.size());
EXPECT_EQ(Status::Ok(), result.status());
EXPECT_STREQ("8", buffer);
}
TEST(ToString, Integer_EmptyBuffer_WritesNothing) {
auto result = ToString(-1234, std::span(buffer, 0));
EXPECT_EQ(0u, result.size());
EXPECT_EQ(Status::ResourceExhausted(), result.status());
}
TEST(ToString, Integer_BufferTooSmall_WritesNullTerminator) {
auto result = ToString(-1234, std::span(buffer, 5));
EXPECT_EQ(0u, result.size());
EXPECT_FALSE(result.ok());
EXPECT_STREQ("", buffer);
}
TEST(ToString, Float) {
EXPECT_EQ(1u, ToString(0.0f, buffer).size());
EXPECT_STREQ("0", buffer);
EXPECT_EQ(3u, ToString(INFINITY, buffer).size());
EXPECT_STREQ("inf", buffer);
EXPECT_EQ(4u, ToString(-NAN, buffer).size());
EXPECT_STREQ("-NaN", buffer);
}
TEST(ToString, Pointer_NonNull_WritesValue) {
CustomType custom;
const size_t length = std::snprintf(expected,
sizeof(expected),
"%" PRIxPTR,
reinterpret_cast<intptr_t>(&custom));
EXPECT_EQ(length, ToString(&custom, buffer).size());
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(length, ToString(static_cast<void*>(&custom), buffer).size());
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(1u, ToString(reinterpret_cast<int*>(4), buffer).size());
EXPECT_STREQ("4", buffer);
}
TEST(ToString, Pointer_Nullptr_WritesNull) {
EXPECT_EQ(string::kNullPointerString.size(),
ToString(nullptr, buffer).size());
EXPECT_EQ(string::kNullPointerString, buffer);
}
TEST(ToString, Pointer_NullValuedPointer_WritesNull) {
EXPECT_EQ(string::kNullPointerString.size(),
ToString(static_cast<const CustomType*>(nullptr), buffer).size());
EXPECT_EQ(string::kNullPointerString, buffer);
}
TEST(ToString, Pointer_NullValuedCString_WritesNull) {
EXPECT_EQ(string::kNullPointerString.size(),
ToString(static_cast<char*>(nullptr), buffer).size());
EXPECT_EQ(string::kNullPointerString, buffer);
EXPECT_EQ(string::kNullPointerString.size(),
ToString(static_cast<const char*>(nullptr), buffer).size());
EXPECT_EQ(string::kNullPointerString, buffer);
}
TEST(ToString, String_Literal) {
EXPECT_EQ(0u, ToString("", buffer).size());
EXPECT_STREQ("", buffer);
EXPECT_EQ(5u, ToString("hello", buffer).size());
EXPECT_STREQ("hello", buffer);
}
TEST(ToString, String_Pointer) {
EXPECT_EQ(0u, ToString(static_cast<const char*>(""), buffer).size());
EXPECT_STREQ("", buffer);
EXPECT_EQ(5u, ToString(static_cast<const char*>("hello"), buffer).size());
EXPECT_STREQ("hello", buffer);
}
TEST(ToString, String_MutableBuffer) {
char chars[] = {'C', 'o', 'o', 'l', '\0'};
EXPECT_EQ(sizeof(chars) - 1, ToString(chars, buffer).size());
EXPECT_STREQ("Cool", buffer);
}
TEST(ToString, String_MutablePointer) {
char chars[] = {'b', 'o', 'o', 'l', '\0'};
EXPECT_EQ(sizeof(chars) - 1,
ToString(static_cast<char*>(chars), buffer).size());
EXPECT_STREQ("bool", buffer);
}
TEST(ToString, Object) {
CustomType custom;
EXPECT_EQ(std::strlen(CustomType::kToString),
ToString(custom, buffer).size());
EXPECT_STREQ(CustomType::kToString, buffer);
}
enum Foo : uint8_t {
BAR = 32,
BAZ = 100,
};
TEST(ToString, Enum) {
EXPECT_EQ(2u, ToString(Foo::BAR, buffer).size());
EXPECT_STREQ("32", buffer);
EXPECT_EQ(3u, ToString(Foo::BAZ, buffer).size());
EXPECT_STREQ("100", buffer);
}
TEST(ToString, Status) {
EXPECT_EQ(2u, ToString(Status(), buffer).size());
EXPECT_STREQ(Status().str(), buffer);
}
TEST(ToString, StatusCode) {
EXPECT_EQ(sizeof("UNAVAILABLE") - 1,
ToString(Status::Unavailable(), buffer).size());
EXPECT_STREQ("UNAVAILABLE", buffer);
}
TEST(ToString, StdArrayAsBuffer) {
std::array<char, 128> test_buffer;
EXPECT_EQ(5u, ToString(false, test_buffer).size());
EXPECT_STREQ("false", test_buffer.data());
EXPECT_EQ(2u, ToString("Hi", test_buffer).size());
EXPECT_STREQ("Hi", test_buffer.data());
EXPECT_EQ(string::kNullPointerString.size(),
ToString(static_cast<void*>(nullptr), test_buffer).size());
EXPECT_EQ(string::kNullPointerString, test_buffer.data());
}
TEST(ToString, StringView) {
std::string_view view = "cool";
EXPECT_EQ(4u, ToString(view, buffer).size());
EXPECT_STREQ("cool", buffer);
}
TEST(ToString, StringView_TooSmall_Truncates) {
std::string_view view = "kale!";
EXPECT_EQ(3u, ToString(view, std::span(buffer, 4)).size());
EXPECT_STREQ("kal", buffer);
}
TEST(ToString, StringView_EmptyBuffer_WritesNothing) {
constexpr char kOriginal[] = {'@', '#', '$', '%'};
char test_buffer[sizeof(kOriginal)];
std::memcpy(test_buffer, kOriginal, sizeof(kOriginal));
EXPECT_EQ(
0u,
ToString(std::string_view("Hello!"), std::span(test_buffer, 0)).size());
ASSERT_EQ(0, std::memcmp(kOriginal, test_buffer, sizeof(kOriginal)));
}
TEST(ToString, StdString) {
EXPECT_EQ(5u, ToString(std::string("Whoa!"), buffer).size());
EXPECT_STREQ("Whoa!", buffer);
EXPECT_EQ(0u, ToString(std::string(), buffer).size());
EXPECT_STREQ("", buffer);
}
} // namespace
} // namespace pw