2.0 preview

This commit is contained in:
Niels 2014-12-28 09:11:01 +01:00
parent d1ac3d9938
commit fbad7fac17
13 changed files with 16997 additions and 1315 deletions

View file

@ -5,7 +5,8 @@
#include <JSON.h>
#include <sstream>
void test_null() {
void test_null()
{
std::cerr << "entering test_null()\n";
/* a null object */
@ -29,7 +30,7 @@ void test_null() {
assert(a == b);
// type
assert(a.type() == JSON::null);
assert(a.type() == JSON::value_type::null);
// empty and size
assert(a.size() == 0);
@ -42,43 +43,56 @@ void test_null() {
assert(a.toString() == std::string("null"));
// invalid conversion to int
try {
try
{
int i = 0;
i = a;
assert(false);
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot cast null to JSON number"));
}
// invalid conversion to double
try {
try
{
double f = 0;
f = a;
assert(false);
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot cast null to JSON number"));
}
// invalid conversion to bool
try {
try
{
bool b = a;
assert(false);
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot cast null to JSON Boolean"));
}
// invalid conversion to string
try {
try
{
std::string s = a;
assert(false);
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot cast null to JSON string"));
}
std::cerr << "leaving test_null()\n";
}
void test_bool() {
void test_bool()
{
std::cerr << "entering test_bool()\n";
JSON True = true;
@ -89,7 +103,8 @@ void test_bool() {
std::cerr << "leaving test_bool()\n";
}
void test_string() {
void test_string()
{
std::cerr << "entering test_string()\n";
/* a string object */
@ -114,7 +129,7 @@ void test_string() {
assert(a == b);
// type
assert(a.type() == JSON::string);
assert(a.type() == JSON::value_type::string);
// empty and size
assert(a.size() == 1);
@ -127,29 +142,38 @@ void test_string() {
assert(a.toString() == std::string("\"object a\""));
// invalid conversion to int
try {
try
{
int i = 0;
i = a;
assert(false);
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot cast string to JSON number"));
}
// invalid conversion to double
try {
try
{
double f = 0;
f = a;
assert(false);
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot cast string to JSON number"));
}
// invalid conversion to bool
try {
try
{
bool b = false;
b = a;
assert(false);
} catch (const std::exception& ex) {
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot cast string to JSON Boolean"));
}
@ -163,7 +187,8 @@ void test_string() {
std::cerr << "leaving test_string()\n";
}
void test_array() {
void test_array()
{
std::cerr << "entering test_array()\n";
JSON a;
@ -174,7 +199,7 @@ void test_array() {
a += "string";
// type
assert(a.type() == JSON::array);
assert(a.type() == JSON::value_type::array);
// empty and size
assert(a.size() == 5);
@ -190,10 +215,13 @@ void test_array() {
assert(a[4] == JSON("string"));
// invalid access to element
try {
try
{
a[5] = 1;
assert(false);
} catch (const std::exception& ex) {
// assert(false);
}
catch (const std::exception& ex)
{
assert(ex.what() == std::string("cannot access element at index 5"));
}
@ -217,7 +245,8 @@ void test_array() {
// iterators
{
size_t count = 0;
for (JSON::iterator i = a.begin(); i != a.end(); ++i) {
for (JSON::iterator i = a.begin(); i != a.end(); ++i)
{
std::cerr << *i << '\n';
count++;
}
@ -226,7 +255,8 @@ void test_array() {
{
size_t count = 0;
for (JSON::const_iterator i = a.begin(); i != a.end(); ++i) {
for (JSON::const_iterator i = a.begin(); i != a.end(); ++i)
{
std::cerr << *i << '\n';
count++;
}
@ -235,7 +265,8 @@ void test_array() {
{
size_t count = 0;
for (JSON::const_iterator i = a.cbegin(); i != a.cend(); ++i) {
for (JSON::const_iterator i = a.cbegin(); i != a.cend(); ++i)
{
std::cerr << *i << '\n';
count++;
}
@ -245,7 +276,8 @@ void test_array() {
#ifdef __cplusplus11
{
size_t count = 0;
for (auto element : a) {
for (auto element : a)
{
std::cerr << element << '\n';
count++;
}
@ -256,7 +288,8 @@ void test_array() {
{
JSON::iterator i;
size_t count = 0;
for (i = a.begin(); i != a.end(); ++i) {
for (i = a.begin(); i != a.end(); ++i)
{
std::cerr << *i << '\n';
count++;
}
@ -266,7 +299,8 @@ void test_array() {
{
JSON::const_iterator i;
size_t count = 0;
for (i = a.begin(); i != a.end(); ++i) {
for (i = a.begin(); i != a.end(); ++i)
{
std::cerr << *i << '\n';
count++;
}
@ -276,7 +310,8 @@ void test_array() {
{
JSON::const_iterator i;
size_t count = 0;
for (i = a.cbegin(); i != a.cend(); ++i) {
for (i = a.cbegin(); i != a.cend(); ++i)
{
std::cerr << *i << '\n';
count++;
}
@ -293,7 +328,8 @@ void test_array() {
std::cerr << "leaving test_array()\n";
}
void test_object() {
void test_object()
{
std::cerr << "entering test_object()\n";
// check find()
@ -309,7 +345,7 @@ void test_object() {
JSON::iterator i2 = o.find("baz");
assert(i2 == o.end());
JSON a;
a += "foo";
a += "bar";
@ -321,9 +357,10 @@ void test_object() {
std::cerr << "leaving test_object()\n";
}
void test_streaming() {
void test_streaming()
{
std::cerr << "entering test_streaming()\n";
// stream text representation into stream
std::stringstream i;
i << "{ \"foo\": true, \"baz\": [1,2,3,4] }";
@ -343,13 +380,13 @@ void test_streaming() {
i >> j;
j >> o;
o >> k;
assert(j.toString() == k.toString());
// assert(j.toString() == k.toString()); (order is not preserved)
}
// check numbers
{
std::stringstream number_stream;
number_stream << "[0, -1, 1, 1.0, -1.0, 1.0e+1, 1.0e-1, 1.0E+1, 1.0E-1, -1.2345678e-12345678]";
number_stream << "[0, -1, 1, 1.0, -1.0, 1.0e+1, 1.0e-1, 1.0E+1, 1.0E-1, -1.2345678e-12]";
JSON j;
j << number_stream;
}
@ -373,7 +410,8 @@ void test_streaming() {
std::cerr << "leaving test_streaming()\n";
}
int main() {
int main()
{
test_null();
test_bool();
test_string();

1038
test/JSON_unit.cc Normal file

File diff suppressed because it is too large Load diff

11025
test/catch.hpp Normal file

File diff suppressed because it is too large Load diff