2016-01-17 08:32:55 +01:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "template_test.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2016-01-31 11:41:56 +01:00
|
|
|
#include <sstream>
|
2016-01-17 08:32:55 +01:00
|
|
|
|
|
|
|
#include <CUnit/CUnit.h>
|
|
|
|
|
|
|
|
#include "template.h"
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
|
|
|
void test_template_immutable_string(void) {
|
|
|
|
ImmutableString null;
|
|
|
|
|
2016-01-17 10:00:36 +01:00
|
|
|
CU_ASSERT("" == null);
|
2016-01-17 08:32:55 +01:00
|
|
|
CU_ASSERT(0 == null.size());
|
2016-01-17 10:09:12 +01:00
|
|
|
CU_ASSERT(null.empty());
|
2016-01-17 08:32:55 +01:00
|
|
|
|
|
|
|
ImmutableString from_cstr("alpha");
|
|
|
|
|
|
|
|
CU_ASSERT(0 == strcmp("alpha", from_cstr.c_str()));
|
|
|
|
CU_ASSERT(5 == from_cstr.size());
|
2016-01-17 10:09:12 +01:00
|
|
|
CU_ASSERT(!from_cstr.empty());
|
2016-01-17 08:32:55 +01:00
|
|
|
CU_ASSERT("alpha" == from_cstr);
|
|
|
|
CU_ASSERT(from_cstr == "alpha");
|
|
|
|
CU_ASSERT(std::string("alpha") == from_cstr);
|
|
|
|
CU_ASSERT(from_cstr == std::string("alpha"));
|
|
|
|
|
|
|
|
// copy constructor
|
2016-01-17 14:24:10 +01:00
|
|
|
ImmutableString src("charlie");
|
|
|
|
ImmutableString copy = src;
|
2016-01-17 08:32:55 +01:00
|
|
|
|
|
|
|
CU_ASSERT("charlie" == copy);
|
|
|
|
CU_ASSERT(7 == copy.size());
|
|
|
|
|
|
|
|
// copy assignment
|
|
|
|
ImmutableString copy2;
|
2016-01-17 14:24:10 +01:00
|
|
|
copy2 = src;
|
2016-01-17 08:32:55 +01:00
|
|
|
|
|
|
|
CU_ASSERT("charlie" == copy2);
|
|
|
|
CU_ASSERT(7 == copy2.size());
|
|
|
|
|
|
|
|
// move constructor
|
|
|
|
ImmutableString move = std::move(copy);
|
|
|
|
|
|
|
|
CU_ASSERT("charlie" == move);
|
|
|
|
CU_ASSERT(7 == move.size());
|
2016-01-17 10:00:36 +01:00
|
|
|
CU_ASSERT("" == copy);
|
2016-01-17 08:32:55 +01:00
|
|
|
CU_ASSERT(0 == copy.size());
|
|
|
|
|
|
|
|
// move assignment
|
|
|
|
move = std::move(from_cstr);
|
|
|
|
|
|
|
|
CU_ASSERT("alpha" == move);
|
|
|
|
CU_ASSERT(5 == move.size());
|
2016-01-17 10:00:36 +01:00
|
|
|
CU_ASSERT("" == from_cstr);
|
2016-01-17 08:32:55 +01:00
|
|
|
CU_ASSERT(0 == from_cstr.size());
|
|
|
|
|
|
|
|
// from string literal
|
|
|
|
auto from_lit = StringRef::from_lit("bravo");
|
|
|
|
|
|
|
|
CU_ASSERT("bravo" == from_lit);
|
|
|
|
CU_ASSERT(5 == from_lit.size());
|
2016-01-17 08:42:19 +01:00
|
|
|
|
|
|
|
// equality
|
|
|
|
ImmutableString eq("delta");
|
|
|
|
|
|
|
|
CU_ASSERT("delta1" != eq);
|
|
|
|
CU_ASSERT("delt" != eq);
|
|
|
|
CU_ASSERT(eq != "delta1");
|
|
|
|
CU_ASSERT(eq != "delt");
|
2016-01-17 10:09:12 +01:00
|
|
|
|
|
|
|
// operator[]
|
|
|
|
ImmutableString br_op("foxtrot");
|
|
|
|
|
|
|
|
CU_ASSERT('f' == br_op[0]);
|
|
|
|
CU_ASSERT('o' == br_op[1]);
|
|
|
|
CU_ASSERT('t' == br_op[6]);
|
|
|
|
CU_ASSERT('\0' == br_op[7]);
|
2016-01-31 11:41:56 +01:00
|
|
|
|
|
|
|
// operator==(const ImmutableString &, const ImmutableString &)
|
|
|
|
{
|
|
|
|
ImmutableString a("foo");
|
|
|
|
ImmutableString b("foo");
|
|
|
|
ImmutableString c("fo");
|
|
|
|
|
|
|
|
CU_ASSERT(a == b);
|
|
|
|
CU_ASSERT(a != c);
|
|
|
|
CU_ASSERT(c != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator<<
|
|
|
|
{
|
|
|
|
ImmutableString a("foo");
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << a;
|
|
|
|
|
|
|
|
CU_ASSERT("foo" == ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator +=(std::string &, const ImmutableString &)
|
|
|
|
{
|
|
|
|
std::string a = "alpha";
|
|
|
|
a += ImmutableString("bravo");
|
|
|
|
|
|
|
|
CU_ASSERT("alphabravo" == a);
|
|
|
|
}
|
2016-01-17 08:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_template_string_ref(void) {
|
|
|
|
StringRef empty;
|
|
|
|
|
|
|
|
CU_ASSERT("" == empty);
|
|
|
|
CU_ASSERT(0 == empty.size());
|
|
|
|
|
|
|
|
// from std::string
|
|
|
|
std::string alpha = "alpha";
|
|
|
|
|
|
|
|
StringRef ref(alpha);
|
|
|
|
|
|
|
|
CU_ASSERT("alpha" == ref);
|
|
|
|
CU_ASSERT(ref == "alpha");
|
|
|
|
CU_ASSERT(alpha == ref);
|
|
|
|
CU_ASSERT(ref == alpha);
|
|
|
|
CU_ASSERT(5 == ref.size());
|
|
|
|
|
|
|
|
// from string literal
|
|
|
|
auto from_lit = StringRef::from_lit("alpha");
|
|
|
|
|
|
|
|
CU_ASSERT("alpha" == from_lit);
|
|
|
|
CU_ASSERT(5 == from_lit.size());
|
|
|
|
|
|
|
|
// from ImmutableString
|
2016-08-26 15:40:59 +02:00
|
|
|
auto im = ImmutableString::from_lit("bravo");
|
2016-01-17 08:32:55 +01:00
|
|
|
|
|
|
|
StringRef imref(im);
|
|
|
|
|
|
|
|
CU_ASSERT("bravo" == imref);
|
|
|
|
CU_ASSERT(5 == imref.size());
|
|
|
|
|
|
|
|
// from C-string
|
|
|
|
StringRef cstrref("charlie");
|
|
|
|
|
|
|
|
CU_ASSERT("charlie" == cstrref);
|
|
|
|
CU_ASSERT(7 == cstrref.size());
|
|
|
|
|
|
|
|
// from C-string and its length
|
|
|
|
StringRef cstrnref("delta", 5);
|
|
|
|
|
|
|
|
CU_ASSERT("delta" == cstrnref);
|
|
|
|
CU_ASSERT(5 == cstrnref.size());
|
2016-01-31 11:41:56 +01:00
|
|
|
|
|
|
|
// operator[]
|
|
|
|
StringRef br_op("foxtrot");
|
|
|
|
|
|
|
|
CU_ASSERT('f' == br_op[0]);
|
|
|
|
CU_ASSERT('o' == br_op[1]);
|
|
|
|
CU_ASSERT('t' == br_op[6]);
|
|
|
|
CU_ASSERT('\0' == br_op[7]);
|
|
|
|
|
|
|
|
// operator<<
|
|
|
|
{
|
|
|
|
StringRef a("foo");
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << a;
|
|
|
|
|
|
|
|
CU_ASSERT("foo" == ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator +=(std::string &, const StringRef &)
|
|
|
|
{
|
|
|
|
std::string a = "alpha";
|
|
|
|
a += StringRef("bravo");
|
|
|
|
|
|
|
|
CU_ASSERT("alphabravo" == a);
|
|
|
|
}
|
2016-01-17 08:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nghttp2
|