8156852: Convert JSON_test to Gtest

Convert test from InternalVMTests to Gtest

Reviewed-by: kvn, kzhaldyb
This commit is contained in:
Pavel Punegov 2016-08-17 18:48:34 +03:00
parent 4347d96655
commit 8474269d18
3 changed files with 516 additions and 285 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -686,286 +686,3 @@ void JSON::error(JSON_ERROR e, const char* format, ...) {
}
}
#ifndef PRODUCT
class JSONTest : public JSON {
public:
static void test();
private:
JSONTest(const char* text);
static void test(const char* json, bool valid);
void log(uint level, const char* format, ...) ATTRIBUTE_PRINTF(3, 4);
bool callback(JSON_TYPE t, JSON_VAL* v, uint level);
JSON_TYPE prev;
};
void JSON_test() {
JSONTest::test();
}
void JSONTest::test(const char* text, bool should_pass) {
JSONTest json(text);
if (should_pass) {
assert(json.valid() == true, "failed on a valid json string");
if (VerboseInternalVMTests) {
tty->print_cr("-- json test passed as expected --");
}
} else {
assert(json.valid() == false, "succeeded on an invalid json string");
if (VerboseInternalVMTests) {
tty->print_cr("-- json test failed as expected --");
}
}
}
JSONTest::JSONTest(const char* text) : JSON(text, !VerboseInternalVMTests, tty) {
prev = JSON_NONE;
parse();
}
void JSONTest::test() {
JSONTest::test("{}", true);
JSONTest::test("[]", true);
JSONTest::test(" { } ", true);
JSONTest::test(" [ ] ", true);
JSONTest::test("\"error\"", false);
JSONTest::test("error", false);
JSONTest::test("1", false);
JSONTest::test("1.2", false);
JSONTest::test("true", false);
JSONTest::test("false", false);
JSONTest::test("null", false);
JSONTest::test("[ 1 ]", true);
JSONTest::test("[ 1, ]", true);
JSONTest::test("[ true ]", true);
JSONTest::test("[ true, ]", true);
JSONTest::test("[ false ]", true);
JSONTest::test("[ false, ]", true);
JSONTest::test("[ null ]", true);
JSONTest::test("[ null, ]", true);
JSONTest::test("[ \"\" ]", true);
JSONTest::test("[ \"\", ]", true);
JSONTest::test("[ \"elem1\" ]", true);
JSONTest::test("[ \"elem1\", ]", true);
JSONTest::test("[ \"elem1\", ]", true);
JSONTest::test("[ \"elem1\" ]", true);
JSONTest::test("[ \"elem1\", \"elem2\" ]", true);
JSONTest::test("[ \"elem1\", \"elem2\", ]", true);
JSONTest::test("[ \"elem1\" ] { }", false);
JSONTest::test("[ elem1, \"elem2\" ]", false);
JSONTest::test("[ \"elem1\"", false);
JSONTest::test("[ \"elem1 ]", false);
JSONTest::test("[ \"elem1\", \"elem2\"", false);
JSONTest::test("[ truefoo ]", false);
JSONTest::test("[ falsefoo ]", false);
JSONTest::test("[ nullfoo ]", false);
JSONTest::test("{ key : 1 }", true);
JSONTest::test("{ key : 1, }", true);
JSONTest::test("{ key : true }", true);
JSONTest::test("{ key : true, }", true);
JSONTest::test("{ key : false }", true);
JSONTest::test("{ key : false, }", true);
JSONTest::test("{ key : null }", true);
JSONTest::test("{ key : null, }", true);
JSONTest::test("{ \"\" : \"\" }", true);
JSONTest::test("{ \"\" : \"\", }", true);
JSONTest::test("{ \"key1\" : \"val1\" }", true);
JSONTest::test("{ \"key1\" : \"val1\", }", true);
JSONTest::test("{ \"key1\" : \"val1\", \"key2\" : \"val2\" }", true);
JSONTest::test("{ \"key1\" : \"val1\", \"key2\" : \"val2\", }", true);
JSONTest::test("{ \"key\" : \"val\" } [ \"error\" ]", false);
JSONTest::test("{ \"key\" : \"val\" ", false);
JSONTest::test("/**/ { }", true);
JSONTest::test("/* */ { }", true);
JSONTest::test("/*foo*/ { }", true);
JSONTest::test("/* *foo */ { }", true);
JSONTest::test("/* *foo* */ { }", true);
JSONTest::test("/* /*foo */ { }", true);
JSONTest::test("{ } /* foo */", true);
JSONTest::test("{ } /* foo */ ", true);
JSONTest::test("{ } //", true);
JSONTest::test("{ } // ", true);
JSONTest::test("{ } // foo", true);
JSONTest::test("/* * / { }", false);
JSONTest::test("/ * */ { }", false);
JSONTest::test("// { }", false);
JSONTest::test("/* { } */", false);
JSONTest::test("/* { } */ ", false);
JSONTest::test("/* { } ", false);
JSONTest::test("{ } /* ", false);
JSONTest::test("/* { } *", false);
JSONTest::test("{ /* } */", false);
JSONTest::test("[ /* ] */", false);
JSONTest::test("{ key : \"val\", /* } */", false);
JSONTest::test("[ \"val\", /* ] */", false);
JSONTest::test("/* comment */{ key1 : { \"key2\" : { \"key3\" : [ \"elem1\", \"elem2\", { \"key4\" : null }, 3 , 2 , 1 , 0 , -1 , -2 , -3 , true, false, null, ] }, \"key5\" : true }, \"key6\" : [ \"\" ], key7 : \"val\",}", true);
JSONTest::test("/* comment */ { \"key1\" : { \"key2\" : { \"key3\" : [ \"elem1\", \"elem2\", { \"key4\" : null }, 3 , 2 , 1 , 0 , -1 , -2 , -3 , true, false, null, ] }, \"key5\" : true }, \"key6\" : [ \"\" ], key7 : \"val\",}", true);
JSONTest::test("/*comment*/{\"ff1 fsd\":{\"\":{\"\":[\"\",\"\"]},\"\":true},\"\":[\"\"],\"foo\":\"\",}", true);
JSONTest::test("/* comment */ { key1 error : { \"\" : { \"\" : [ \"\", \"\" ] }, \"\" : true }, \"baz\" : [ \"\" ], foo : \"\",}", false); // first key needs to be quoted since it contains a space
JSONTest::test("[\n]", true);
JSONTest::test(
"[" "\n"
" {"
" // pattern to match against class+method+signature" "\n"
" // leading and trailing wildcard (*) allowed" "\n"
" match: \"foo.bar.*\"," "\n"
" " "\n"
" // override defaults for specified compiler" "\n"
" // we may differentiate between levels too. TBD." "\n"
" c1: {" "\n"
" //override c1 presets " "\n"
" array_bounds_check_removal: false" "\n"
" }," "\n"
"" "\n"
" c2: {" "\n"
" // control inlining of method" "\n"
" // + force inline, - dont inline" "\n"
" inline : [ \"+java.util.*\", \"-com.sun.*\"]," "\n"
" }," "\n"
"" "\n"
" // directives outside a specific preset applies to all compilers" "\n"
" inline : [ \"+java.util.*\", \"-com.sun.*\"]," "\n"
" print_assembly: true," "\n"
" verify_oopmaps: true," "\n"
" max_loop_unrolling: 5" "\n"
" }," "\n"
" {" "\n"
" // matching several patterns require an array" "\n"
" match: [\"baz.*\",\"frob*\"]," "\n"
"" "\n"
" // only enable c1 for this directive" "\n"
" // all enabled by default. Command disables all not listed" "\n"
" enable: \"c1\"," "\n"
"" "\n"
" // applies to all compilers" "\n"
" // + force inline, - dont inline" "\n"
" inline : [ \"+java.util.*\", \"-com.sun.*\"]," "\n"
" print_inlining: true," "\n"
"" "\n"
" // force matching compiles to be blocking/syncronous" "\n"
" blocking_compile: true" "\n"
" }," "\n"
"]" "\n", true);
}
void JSONTest::log(uint indent, const char* format, ...) {
if (VerboseInternalVMTests) {
if (prev != JSON_KEY) {
for (uint i = 0; i < indent; i++) {
_st->print(" ");
}
}
va_list args;
va_start(args, format);
_st->vprint(format, args);
va_end(args);
}
}
bool JSONTest::callback(JSON_TYPE t, JSON_VAL* v, uint rlevel) {
switch (t) {
case JSON_OBJECT_BEGIN:
log(rlevel, "{\n");
prev = JSON_NONE; // Only care about JSON_KEY, to indent correctly
return true;
case JSON_OBJECT_END:
log(rlevel, "},\n");
prev = JSON_NONE;
return true;
case JSON_ARRAY_BEGIN:
log(rlevel, "[\n");
prev = JSON_NONE;
return true;
case JSON_ARRAY_END:
log(rlevel, "],\n");
prev = JSON_NONE;
return true;
case JSON_KEY:
if (VerboseInternalVMTests) {
for (uint i = 0; i < rlevel; i++) {
_st->print(" ");
}
_st->print("<key>");
for (size_t i = 0; i < v->str.length; i++) {
u_char c = v->str.start[i];
assert(c != 0, "string overrun");
if (c == 0) {
return false;
}
_st->print("%c", c);
}
_st->print(" : ");
}
prev = JSON_KEY;
return true;
case JSON_STRING:
if (VerboseInternalVMTests) {
if (prev != JSON_KEY) {
for (uint i = 0; i < rlevel; i++) {
_st->print(" ");
}
}
_st->print("<str>");
for (size_t i = 0; i < v->str.length; i++) {
u_char c = v->str.start[i];
assert(c != 0, "string overrun");
if (c == 0) {
return false;
}
_st->print("%c", c);
}
_st->print(",\n");
}
prev = JSON_NONE;
return true;
case JSON_NUMBER_INT:
log(rlevel, "<int>%" PRId64 ",\n", v->int_value);
prev = JSON_NONE;
return true;
case JSON_NUMBER_FLOAT:
log(rlevel, "<double>%lf,\n", v->double_value);
prev = JSON_NONE;
return true;
case JSON_TRUE:
log(rlevel, "<true>,\n");
prev = JSON_NONE;
return true;
case JSON_FALSE:
log(rlevel, "<false>,\n");
prev = JSON_NONE;
return true;
case JSON_NULL:
log(rlevel, "<null>,\n");
prev = JSON_NONE;
return true;
default:
error(INTERNAL_ERROR, "unknown JSON type");
return false;
}
}
#endif