blob: 1621b52d82a7c67ca75cbe903b8372a1bcd00fde [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "http2/platform/api/http2_string_utils.h"
#include <cstdint>
#include "absl/strings/string_view.h"
#include "common/platform/api/quiche_test.h"
namespace http2 {
namespace test {
namespace {
TEST(Http2StringUtilsTest, Http2StrAppend) {
// No arguments on empty string.
std::string output;
Http2StrAppend(&output);
EXPECT_TRUE(output.empty());
// Single string-like argument.
const char kFoo[] = "foo";
const std::string string_foo(kFoo);
const absl::string_view stringpiece_foo(string_foo);
Http2StrAppend(&output, kFoo);
EXPECT_EQ("foo", output);
Http2StrAppend(&output, string_foo);
EXPECT_EQ("foofoo", output);
Http2StrAppend(&output, stringpiece_foo);
EXPECT_EQ("foofoofoo", output);
// No arguments on non-empty string.
Http2StrAppend(&output);
EXPECT_EQ("foofoofoo", output);
output.clear();
// Two string-like arguments.
const char kBar[] = "bar";
const absl::string_view stringpiece_bar(kBar);
const std::string string_bar(kBar);
Http2StrAppend(&output, kFoo, kBar);
EXPECT_EQ("foobar", output);
Http2StrAppend(&output, kFoo, string_bar);
EXPECT_EQ("foobarfoobar", output);
Http2StrAppend(&output, kFoo, stringpiece_bar);
EXPECT_EQ("foobarfoobarfoobar", output);
Http2StrAppend(&output, string_foo, kBar);
EXPECT_EQ("foobarfoobarfoobarfoobar", output);
output.clear();
Http2StrAppend(&output, string_foo, string_bar);
EXPECT_EQ("foobar", output);
Http2StrAppend(&output, string_foo, stringpiece_bar);
EXPECT_EQ("foobarfoobar", output);
Http2StrAppend(&output, stringpiece_foo, kBar);
EXPECT_EQ("foobarfoobarfoobar", output);
Http2StrAppend(&output, stringpiece_foo, string_bar);
EXPECT_EQ("foobarfoobarfoobarfoobar", output);
output.clear();
Http2StrAppend(&output, stringpiece_foo, stringpiece_bar);
EXPECT_EQ("foobar", output);
// Many-many arguments.
Http2StrAppend(&output, "foo", "bar", "baz", "qux", "quux", "quuz", "corge",
"grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud");
EXPECT_EQ(
"foobarfoobarbazquxquuxquuzcorgegraultgarplywaldofredplughxyzzythud",
output);
output.clear();
// Numerical arguments.
const int16_t i = 1;
const uint64_t u = 8;
const double d = 3.1415;
Http2StrAppend(&output, i, " ", u);
EXPECT_EQ("1 8", output);
Http2StrAppend(&output, d, i, i, u, i);
EXPECT_EQ("1 83.14151181", output);
Http2StrAppend(&output, "i: ", i, ", u: ", u, ", d: ", d);
EXPECT_EQ("1 83.14151181i: 1, u: 8, d: 3.1415", output);
output.clear();
// Boolean arguments.
const bool t = true;
const bool f = false;
Http2StrAppend(&output, t);
EXPECT_EQ("1", output);
Http2StrAppend(&output, f);
EXPECT_EQ("10", output);
Http2StrAppend(&output, f, t, t, f);
EXPECT_EQ("100110", output);
output.clear();
// Mixed string-like, numerical, and Boolean arguments.
Http2StrAppend(&output, kFoo, i, string_foo, f, u, t, stringpiece_bar, d, t);
EXPECT_EQ("foo1foo081bar3.14151", output);
Http2StrAppend(&output, d, t, t, string_bar, i, u, kBar, t, d, f);
EXPECT_EQ("foo1foo081bar3.141513.141511bar18bar13.14150", output);
}
TEST(Http2StringUtilsTest, Http2StringPrintf) {
EXPECT_EQ("", Http2StringPrintf("%s", ""));
EXPECT_EQ("foobar", Http2StringPrintf("%sbar", "foo"));
EXPECT_EQ("foobar", Http2StringPrintf("%s%s", "foo", "bar"));
EXPECT_EQ("foo: 1, bar: 2.0",
Http2StringPrintf("foo: %d, bar: %.1f", 1, 2.0));
}
} // namespace
} // namespace test
} // namespace http2