Skip to content

Commit 80e77f5

Browse files
committed
More tests.
1 parent 7f89fa3 commit 80e77f5

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/test/json/parse.cc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) 2015 Dr. Colin Hirsch
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#include "test.hh"
5+
6+
namespace tao
7+
{
8+
namespace json
9+
{
10+
void test_array()
11+
{
12+
const auto v = from_string( "[ null, true, false, 42, 43.0, \"foo\", [ 1, 2, 3 ], { \"a\" : \"b\", \"c\" : \"d\" } ]" );
13+
14+
TEST_ASSERT( v.type() == type::ARRAY );
15+
16+
const auto & a = v.get_array();
17+
18+
TEST_ASSERT( a.size() == 8 );
19+
TEST_ASSERT( a[ 0 ] == value( nullptr ) );
20+
TEST_ASSERT( a[ 1 ] == value( true ) );
21+
TEST_ASSERT( a[ 2 ] == value( false ) );
22+
TEST_ASSERT( a[ 3 ] == value( 42 ) );
23+
TEST_ASSERT( a[ 4 ] == value( 43.0 ) );
24+
TEST_ASSERT( a[ 5 ] == value( "foo" ) );
25+
TEST_ASSERT( a[ 6 ].type() == type::ARRAY );
26+
TEST_ASSERT( a[ 6 ].get_array().size() == 3 );
27+
TEST_ASSERT( a[ 6 ].get_array()[ 0 ] == value( 1 ) );
28+
TEST_ASSERT( a[ 6 ].get_array()[ 1 ] == value( 2 ) );
29+
TEST_ASSERT( a[ 6 ].get_array()[ 2 ] == value( 3 ) );
30+
TEST_ASSERT( a[ 7 ].type() == type::OBJECT );
31+
TEST_ASSERT( a[ 7 ].get_object().size() == 2 );
32+
TEST_ASSERT( a[ 7 ].get_object().at("a" ) == "b" );
33+
TEST_ASSERT( a[ 7 ].get_object().at( "c" ) == "d" );
34+
}
35+
36+
void test_object()
37+
{
38+
const auto v = from_string( "{ \"a\" : null, \"b\" : true, \"c\" : false, \"d\" : 42, \"e\" : 43.0, \"f\" : \"foo\", \"g\" : [ 1, 2, 3 ], \"h\" : { \"a\" : \"b\", \"c\" : \"d\" } }" );
39+
40+
TEST_ASSERT( v.type() == type::OBJECT );
41+
42+
const auto & o = v.get_object();
43+
44+
TEST_ASSERT( o.size() == 8 );
45+
TEST_ASSERT( o.at( "a" ) == value( nullptr ) );
46+
TEST_ASSERT( o.at( "b" ) == value( true ) );
47+
TEST_ASSERT( o.at( "c" ) == value( false ) );
48+
TEST_ASSERT( o.at( "d" ) == value( 42 ) );
49+
TEST_ASSERT( o.at( "e" ) == value( 43.0 ) );
50+
TEST_ASSERT( o.at( "f" ) == value( "foo" ) );
51+
TEST_ASSERT( o.at( "g" ).type() == type::ARRAY );
52+
TEST_ASSERT( o.at( "g" ).get_array().size() == 3 );
53+
TEST_ASSERT( o.at( "g" ).get_array()[ 0 ] == value( 1 ) );
54+
TEST_ASSERT( o.at( "g" ).get_array()[ 1 ] == value( 2 ) );
55+
TEST_ASSERT( o.at( "g" ).get_array()[ 2 ] == value( 3 ) );
56+
TEST_ASSERT( o.at( "h" ).type() == type::OBJECT );
57+
TEST_ASSERT( o.at( "h" ).get_object().size() == 2 );
58+
TEST_ASSERT( o.at( "h" ).get_object().at("a" ) == "b" );
59+
TEST_ASSERT( o.at( "h" ).get_object().at( "c" ) == "d" );
60+
}
61+
62+
void unit_test()
63+
{
64+
TEST_ASSERT( from_string( "null" ) == value( nullptr ) );
65+
TEST_ASSERT( from_string( "42" ) == value( 42 ) );
66+
TEST_ASSERT( from_string( "42.0" ) == value( 42.0 ) );
67+
TEST_ASSERT( from_string( "true" ) == value( true ) );
68+
TEST_ASSERT( from_string( "false" ) == value( false ) );
69+
70+
test_array();
71+
test_object();
72+
73+
// More elaborate tests are performed by the "nativejson benchmarks".
74+
}
75+
76+
} // json
77+
78+
} // tao
79+
80+
#include "main.hh"

src/test/json/type.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2015 Dr. Colin Hirsch
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#include "test.hh"
5+
6+
namespace tao
7+
{
8+
namespace json
9+
{
10+
void unit_test()
11+
{
12+
TEST_ASSERT( sizeof( type ) == 1 );
13+
14+
TEST_ASSERT( to_string( type::NULL_ ) == "null" );
15+
TEST_ASSERT( to_string( type::BOOL_ ) == "bool" );
16+
TEST_ASSERT( to_string( type::INT64 ) == "int64" );
17+
TEST_ASSERT( to_string( type::DOUBLE ) == "double" );
18+
19+
TEST_ASSERT( to_string( type::STRING ) == "string" );
20+
TEST_ASSERT( to_string( type::ARRAY ) == "array" );
21+
TEST_ASSERT( to_string( type::OBJECT ) == "object" );
22+
23+
TEST_ASSERT( to_string( type( 42 ) ) == "unknown" );
24+
25+
TEST_ASSERT( needs_destroy( type::NULL_ ) == false );
26+
TEST_ASSERT( needs_destroy( type::BOOL_ ) == false );
27+
TEST_ASSERT( needs_destroy( type::INT64 ) == false );
28+
TEST_ASSERT( needs_destroy( type::DOUBLE ) == false );
29+
30+
TEST_ASSERT( needs_destroy( type::STRING ) == true );
31+
TEST_ASSERT( needs_destroy( type::ARRAY ) == true );
32+
TEST_ASSERT( needs_destroy( type::OBJECT ) == true );
33+
}
34+
35+
} // json
36+
37+
} // tao
38+
39+
#include "main.hh"

0 commit comments

Comments
 (0)