Use std::unique_ptr for ValueBase object hierarchy

This commit is contained in:
Tatsuhiro Tsujikawa 2013-07-11 21:09:51 +09:00
parent 7a57ecc5f8
commit 7c06b903f3
77 changed files with 1242 additions and 1265 deletions

View file

@ -30,102 +30,91 @@ void ValueBaseJsonParserTest::testParseUpdate()
{
// empty object
std::string src = "{}";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const Dict* dict = downcast<Dict>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto dict = downcast<Dict>(r);
CPPUNIT_ASSERT(dict);
}
{
// empty object
std::string src = "{ }";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const Dict* dict = downcast<Dict>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto dict = downcast<Dict>(r);
CPPUNIT_ASSERT(dict);
}
{
// empty array
std::string src = "[]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
}
{
// empty array
std::string src = "[ ]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
}
{
// empty string
std::string src = "[\"\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string(), s->s());
}
{
// string
std::string src = "[\"foobar\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("foobar"), s->s());
}
{
// string with escape
std::string src = "[\"\\\\foo\\\"\\\"bar\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("\\foo\"\"bar"), s->s());
}
{
// string with escape
std::string src = "[\"foo\\\"\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("foo\""), s->s());
}
{
// string: utf-8 1 to 3 bytes.
std::string src = "[\"\\u0024\\u00A2\\u20AC\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("$¢€"), s->s());
}
{
// string: utf-8 4 bytes
std::string src = "[\"\\uD852\\uDF62\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
const unsigned char arr[] = { 0xF0u, 0xA4u, 0xADu, 0xA2u };
CPPUNIT_ASSERT_EQUAL(std::string(std::begin(arr), std::end(arr)), s->s());
}
{
// null
std::string src = "[null]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const Null* s = downcast<Null>(list->get(0));
CPPUNIT_ASSERT(s);
@ -133,9 +122,8 @@ void ValueBaseJsonParserTest::testParseUpdate()
{
// true, false
std::string src = "[true, false]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const Bool* trueValue = downcast<Bool>(list->get(0));
CPPUNIT_ASSERT(trueValue);
@ -147,38 +135,35 @@ void ValueBaseJsonParserTest::testParseUpdate()
{
// object: 1 member
std::string src = "{\"foo\":[\"bar\"]}";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
const Dict* dict = downcast<Dict>(r);
CPPUNIT_ASSERT(dict);
const List* list = downcast<List>(dict->get("foo"));
auto list = downcast<List>(dict->get("foo"));
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("bar"), s->s());
}
{
// object: 2 members
// TODO ValueBaseJsonParser does not allow empty dict key
std::string src = "{\"foo\":[\"bar\"], \"alpha\" : \"bravo\"}";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
const Dict* dict = downcast<Dict>(r);
CPPUNIT_ASSERT(dict);
const List* list = downcast<List>(dict->get("foo"));
auto list = downcast<List>(dict->get("foo"));
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("bar"), s->s());
const String* str = downcast<String>(dict->get("alpha"));
auto str = downcast<String>(dict->get("alpha"));
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), str->s());
}
{
// array: 2 values
std::string src = "[\"foo\", {}]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("foo"), s->s());
const Dict* dict = downcast<Dict>(list->get(1));
CPPUNIT_ASSERT(dict);
@ -186,9 +171,8 @@ void ValueBaseJsonParserTest::testParseUpdate()
{
// Number: currently we ignore frac and exp
std::string src = "[0,-1,1.2,-1.2e-10,-1e10]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const Integer* i = downcast<Integer>(list->get(0));
CPPUNIT_ASSERT_EQUAL((Integer::ValueType)0, i->i());
@ -204,28 +188,25 @@ void ValueBaseJsonParserTest::testParseUpdate()
{
// escape chars: ", \, /, \b, \f, \n, \r, \t
std::string src = "[\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
const String* s = downcast<String>(list->get(0));
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("\"\\/\b\f\n\r\t"), s->s());
}
{
// string: literal + escaped chars.
std::string src = "[\"foo\\u0024b\\u00A2\\u20ACbaz\"]";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
auto s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("foo$b¢€baz"), s->s());
}
{
// ignore garbage at the end of the input.
std::string src = "[]trail";
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
const List* list = downcast<List>(r);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
auto list = downcast<List>(r);
CPPUNIT_ASSERT(list);
CPPUNIT_ASSERT_EQUAL((ssize_t)2, error);
}
@ -236,8 +217,7 @@ void checkDecodeError(const std::string& src)
{
json::ValueBaseJsonParser parser;
ssize_t error;
std::shared_ptr<ValueBase> r = parser.parseFinal(src.c_str(), src.size(),
error);
auto r = parser.parseFinal(src.c_str(), src.size(), error);
CPPUNIT_ASSERT(!r);
CPPUNIT_ASSERT(error < 0);
}