diff options
author | Philip Withnall <withnall@endlessm.com> | 2020-06-09 15:41:45 +0100 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2020-06-09 15:43:04 +0100 |
commit | dd7a711244e3d33e3e960cd990e9afccc6877820 (patch) | |
tree | 6cdd86fb2578dee5f5626091cdcbd78a6c45df0a /json-glib/tests/parser.c | |
parent | 13142637c8132f49e1bd012786eb5f7f7c6dc986 (diff) | |
download | json-glib-dd7a711244e3d33e3e960cd990e9afccc6877820.tar.gz |
json-parser: Support loading files via memory mapping
Add a new `json_parser_load_from_mapped_file()` to load JSON from
files via memory mapping. It’s otherwise similar to
`json_parser_load_from_file()`. It’s in the right position to be able
to memory map the file it’s reading from: it reads the input once
before building a `JsonNode` structure to represent it, doesn’t write
to the file, and often deals with large input files.
This should speed things up slightly due to reducing time spent
allocating a large chunk of heap memory to load the file into, if a
caller can support that.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Diffstat (limited to 'json-glib/tests/parser.c')
-rw-r--r-- | json-glib/tests/parser.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/json-glib/tests/parser.c b/json-glib/tests/parser.c index acc6276..ddec577 100644 --- a/json-glib/tests/parser.c +++ b/json-glib/tests/parser.c @@ -754,6 +754,59 @@ test_stream_async (void) g_free (path); } +/* Test json_parser_load_from_mapped_file() succeeds. */ +static void +test_mapped (void) +{ + GError *error = NULL; + JsonParser *parser = json_parser_new (); + char *path; + + path = g_test_build_filename (G_TEST_DIST, "stream-load.json", NULL); + + json_parser_load_from_mapped_file (parser, path, &error); + g_assert_no_error (error); + + assert_stream_load_json_correct (parser); + + g_object_unref (parser); + g_free (path); +} + +/* Test json_parser_load_from_mapped_file() error handling for file I/O. */ +static void +test_mapped_file_error (void) +{ + GError *error = NULL; + JsonParser *parser = json_parser_new (); + + json_parser_load_from_mapped_file (parser, "nope.json", &error); + g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); + + g_assert_null (json_parser_get_root (parser)); + + g_object_unref (parser); +} + +/* Test json_parser_load_from_mapped_file() error handling for JSON parsing. */ +static void +test_mapped_json_error (void) +{ + GError *error = NULL; + JsonParser *parser = json_parser_new (); + char *path; + + path = g_test_build_filename (G_TEST_DIST, "invalid.json", NULL); + + json_parser_load_from_mapped_file (parser, path, &error); + g_assert_error (error, JSON_PARSER_ERROR, JSON_PARSER_ERROR_INVALID_BAREWORD); + + g_assert_null (json_parser_get_root (parser)); + + g_object_unref (parser); + g_free (path); +} + int main (int argc, char *argv[]) @@ -772,6 +825,9 @@ main (int argc, g_test_add_func ("/parser/unicode-escape", test_unicode_escape); g_test_add_func ("/parser/stream-sync", test_stream_sync); g_test_add_func ("/parser/stream-async", test_stream_async); + g_test_add_func ("/parser/mapped", test_mapped); + g_test_add_func ("/parser/mapped/file-error", test_mapped_file_error); + g_test_add_func ("/parser/mapped/json-error", test_mapped_json_error); return g_test_run (); } |