Changeset View
Changeset View
Standalone View
Standalone View
src/univalue/test/unitester.cpp
Show All 11 Lines | |||||
#ifndef JSON_TEST_SRC | #ifndef JSON_TEST_SRC | ||||
#error JSON_TEST_SRC must point to test source directory | #error JSON_TEST_SRC must point to test source directory | ||||
#endif | #endif | ||||
#ifndef ARRAY_SIZE | #ifndef ARRAY_SIZE | ||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | ||||
#endif | #endif | ||||
std::string srcdir(JSON_TEST_SRC); | using namespace std; | ||||
string srcdir(JSON_TEST_SRC); | |||||
static bool test_failed = false; | static bool test_failed = false; | ||||
#define d_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", filename.c_str()); } } | #define d_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", filename.c_str()); } } | ||||
#define f_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", __func__); } } | #define f_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", __func__); } } | ||||
static std::string rtrim(std::string s) | static std::string rtrim(std::string s) | ||||
{ | { | ||||
s.erase(s.find_last_not_of(" \n\r\t")+1); | s.erase(s.find_last_not_of(" \n\r\t")+1); | ||||
return s; | return s; | ||||
} | } | ||||
static void runtest(std::string filename, const std::string& jdata) | static void runtest(string filename, const string& jdata) | ||||
{ | { | ||||
std::string prefix = filename.substr(0, 4); | string prefix = filename.substr(0, 4); | ||||
bool wantPass = (prefix == "pass") || (prefix == "roun"); | bool wantPass = (prefix == "pass") || (prefix == "roun"); | ||||
bool wantFail = (prefix == "fail"); | bool wantFail = (prefix == "fail"); | ||||
bool wantRoundTrip = (prefix == "roun"); | bool wantRoundTrip = (prefix == "roun"); | ||||
assert(wantPass || wantFail); | assert(wantPass || wantFail); | ||||
UniValue val; | UniValue val; | ||||
bool testResult = val.read(jdata); | bool testResult = val.read(jdata); | ||||
if (wantPass) { | if (wantPass) { | ||||
d_assert(testResult == true); | d_assert(testResult == true); | ||||
} else { | } else { | ||||
d_assert(testResult == false); | d_assert(testResult == false); | ||||
} | } | ||||
if (wantRoundTrip) { | if (wantRoundTrip) { | ||||
std::string odata = val.write(0, 0); | std::string odata = val.write(0, 0); | ||||
assert(odata == rtrim(jdata)); | assert(odata == rtrim(jdata)); | ||||
} | } | ||||
} | } | ||||
static void runtest_file(const char *filename_) | static void runtest_file(const char *filename_) | ||||
{ | { | ||||
std::string basename(filename_); | string basename(filename_); | ||||
std::string filename = srcdir + "/" + basename; | string filename = srcdir + "/" + basename; | ||||
FILE *f = fopen(filename.c_str(), "r"); | FILE *f = fopen(filename.c_str(), "r"); | ||||
assert(f != NULL); | assert(f != NULL); | ||||
std::string jdata; | string jdata; | ||||
char buf[4096]; | char buf[4096]; | ||||
while (!feof(f)) { | while (!feof(f)) { | ||||
int bread = fread(buf, 1, sizeof(buf), f); | int bread = fread(buf, 1, sizeof(buf), f); | ||||
assert(!ferror(f)); | assert(!ferror(f)); | ||||
std::string s(buf, bread); | string s(buf, bread); | ||||
jdata += s; | jdata += s; | ||||
} | } | ||||
assert(!ferror(f)); | assert(!ferror(f)); | ||||
fclose(f); | fclose(f); | ||||
runtest(basename, jdata); | runtest(basename, jdata); | ||||
} | } | ||||
Show All 28 Lines | static const char *filenames[] = { | ||||
"fail34.json", | "fail34.json", | ||||
"fail35.json", | "fail35.json", | ||||
"fail36.json", | "fail36.json", | ||||
"fail37.json", | "fail37.json", | ||||
"fail38.json", // invalid unicode: only first half of surrogate pair | "fail38.json", // invalid unicode: only first half of surrogate pair | ||||
"fail39.json", // invalid unicode: only second half of surrogate pair | "fail39.json", // invalid unicode: only second half of surrogate pair | ||||
"fail40.json", // invalid unicode: broken UTF-8 | "fail40.json", // invalid unicode: broken UTF-8 | ||||
"fail41.json", // invalid unicode: unfinished UTF-8 | "fail41.json", // invalid unicode: unfinished UTF-8 | ||||
"fail42.json", // valid json with garbage following a nul byte | |||||
"fail44.json", // unterminated string | |||||
"fail3.json", | "fail3.json", | ||||
"fail4.json", // extra comma | "fail4.json", // extra comma | ||||
"fail5.json", | "fail5.json", | ||||
"fail6.json", | "fail6.json", | ||||
"fail7.json", | "fail7.json", | ||||
"fail8.json", | "fail8.json", | ||||
"fail9.json", // extra comma | "fail9.json", // extra comma | ||||
"pass1.json", | "pass1.json", | ||||
"pass2.json", | "pass2.json", | ||||
"pass3.json", | "pass3.json", | ||||
"round1.json", // round-trip test | "round1.json", // round-trip test | ||||
"round2.json", // unicode | "round2.json", // unicode | ||||
"round3.json", // bare string | |||||
"round4.json", // bare number | |||||
"round5.json", // bare true | |||||
"round6.json", // bare false | |||||
"round7.json", // bare null | |||||
}; | }; | ||||
// Test \u handling | // Test \u handling | ||||
void unescape_unicode_test() | void unescape_unicode_test() | ||||
{ | { | ||||
UniValue val; | UniValue val; | ||||
bool testResult; | bool testResult; | ||||
// Escaped ASCII (quote) | // Escaped ASCII (quote) | ||||
Show All 28 Lines |