Added testing for number parsing.

This commit is contained in:
Daniel Lemire
2018-09-27 20:26:27 -04:00
parent e4094afe08
commit ecbe1158ed
4 changed files with 48 additions and 14 deletions
+1 -1
View File
@@ -23,8 +23,8 @@ LIBS=$(RAPIDJSON_INCLUDE)
all: $(LIBS) $(EXECUTABLES)
test: jsoncheck numberparsingcheck
-./numberparsingcheck
./jsoncheck
./numberparsingcheck
$(RAPIDJSON_INCLUDE):
git submodule update --init --recursive
+3
View File
@@ -320,6 +320,9 @@ static really_inline bool parse_number(const u8 *const buf, UNUSED size_t len,
result = result * power_of_ten[308 - exppart];
}
}
if (found_minus) {
result = -result;
}
#ifdef JSON_TEST_NUMBERS // for unit testing
foundFloat(result, buf + offset);
#endif
+1 -1
View File
@@ -40,7 +40,7 @@ bool validate(const char *dirname) {
for (int i = 0; i < c; i++) {
const char *name = entry_list[i]->d_name;
if (hasExtension(name, extension)) {
printf("validating: file %s \n", name);
//printf("validating: file %s \n", name);
size_t filelen = strlen(name);
char *fullpath = (char *)malloc(dirlen + filelen + 1 + 1);
strcpy(fullpath, dirname);
+43 -12
View File
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
#ifndef JSON_TEST_NUMBERS
#define JSON_TEST_NUMBERS
@@ -13,17 +14,44 @@
#include "jsonparser/common_defs.h"
int parse_error;
char *fullpath;
enum{PARSE_WARNING, PARSE_ERROR};
inline void foundInvalidNumber(const u8 * buf) {
printf("foundInvalidNumber %.64s \n", buf);
char * endptr;
double expected = strtod((char *)buf, &endptr);
if(endptr != (char *)buf) {
printf("Warning: foundInvalidNumber %.32s whereas strtod parses it to %f, ", buf, expected);
printf(" while parsing %s \n", fullpath);
parse_error |= PARSE_WARNING;
}
}
inline void foundInteger(int64_t result, const u8 * buf) {
printf("parsed %" PRId64 " out of %.64s \n", result, buf);
char * endptr;
long long expected = strtoll((char *)buf, & endptr, 10);
if((endptr == (char *)buf) || (expected != result)) {
printf("Error: parsed %" PRId64 " out of %.32s, ", result, buf);
printf(" while parsing %s \n", fullpath);
parse_error |= PARSE_ERROR;
}
}
inline void foundFloat(double result, const u8 * buf) {
printf("parsed %f from %.64s \n", result, buf);
char * endptr;
double expected = strtod((char *)buf, &endptr);
if(endptr == (char *)buf) {
printf("parsed %f from %.32s whereas strtod refuses to parse a float, ", result, buf);
printf(" while parsing %s \n", fullpath);
parse_error |= PARSE_ERROR;
}
// we want to get some reasonable relative accuracy
if(fabs(expected - result)/fmax(fabs(expected),fabs(result)) > 0.000000000000001) {
printf("parsed %f from %.32s whereas strtod gives %f,", result, buf, expected);
printf(" while parsing %s \n", fullpath);
parse_error |= PARSE_ERROR;
}
}
@@ -45,7 +73,7 @@ bool startsWith(const char *pre, const char *str) {
}
bool validate(const char *dirname) {
bool everythingfine = true;
parse_error = 0;
// init_state_machine(); // no longer necessary
const char *extension = ".json";
size_t dirlen = strlen(dirname);
@@ -63,9 +91,8 @@ bool validate(const char *dirname) {
for (int i = 0; i < c; i++) {
const char *name = entry_list[i]->d_name;
if (hasExtension(name, extension)) {
printf("validating numbers in file %s \n", name);
size_t filelen = strlen(name);
char *fullpath = (char *)malloc(dirlen + filelen + 1 + 1);
fullpath = (char *)malloc(dirlen + filelen + 1 + 1);
strcpy(fullpath, dirname);
if (needsep) {
fullpath[dirlen] = '/';
@@ -81,18 +108,22 @@ bool validate(const char *dirname) {
return false;
}
ParsedJson &pj(*pj_ptr);
bool isok = json_parse(p.first, p.second, pj);
printf("File %s %s.\n", name,
isok ? " is valid JSON " : " is not valid JSON");
//bool isok =
json_parse(p.first, p.second, pj);
//printf("File %s %s.\n", name,
// isok ? " is valid JSON " : " is not valid JSON");
free(p.first);
free(fullpath);
deallocate_ParsedJson(pj_ptr);
}
}
if((parse_error & PARSE_ERROR) != 0) {
printf("NUMBER PARSING FAILS?\n");
}
for (int i = 0; i < c; ++i)
free(entry_list[i]);
free(entry_list);
return everythingfine;
return ((parse_error & PARSE_ERROR) == 0);
}
int main(int argc, char *argv[]) {
@@ -100,9 +131,9 @@ int main(int argc, char *argv[]) {
std::cerr << "Usage: " << argv[0] << " <directorywithjsonfiles>"
<< std::endl;
std::cout
<< "We are going to assume you mean to use the 'jsonchecker' directory."
<< "We are going to assume you mean to use the 'jsonchecker' and 'jsonexamples' directories."
<< std::endl;
return validate("jsonchecker/") ? EXIT_SUCCESS : EXIT_FAILURE;
return validate("jsonchecker/") && validate("jsonexamples/") ? EXIT_SUCCESS : EXIT_FAILURE;
}
return validate(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
}