Counting numbers.

This commit is contained in:
Daniel Lemire 2018-10-04 09:48:00 -04:00
parent aeacd26366
commit e2a3f751cf
1 changed files with 18 additions and 5 deletions

View File

@ -10,7 +10,7 @@
#ifndef JSON_TEST_NUMBERS
#define JSON_TEST_NUMBERS
#endif
#endif
#include "jsonparser/common_defs.h"
@ -18,7 +18,12 @@ int parse_error;
char *fullpath;
enum{PARSE_WARNING, PARSE_ERROR};
size_t float_count;
size_t int_count;
size_t invalid_count;
inline void foundInvalidNumber(const u8 * buf) {
invalid_count++;
char * endptr;
double expected = strtod((char *)buf, &endptr);
if(endptr != (char *)buf) {
@ -29,6 +34,7 @@ inline void foundInvalidNumber(const u8 * buf) {
}
inline void foundInteger(int64_t result, const u8 * buf) {
int_count++;
char * endptr;
long long expected = strtoll((char *)buf, & endptr, 10);
if((endptr == (char *)buf) || (expected != result)) {
@ -40,9 +46,10 @@ inline void foundInteger(int64_t result, const u8 * buf) {
inline void foundFloat(double result, const u8 * buf) {
char * endptr;
float_count++;
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("parsed %f from %.32s whereas strtod refuses to parse a float, ", result, buf);
printf(" while parsing %s \n", fullpath);
parse_error |= PARSE_ERROR;
}
@ -107,11 +114,17 @@ bool validate(const char *dirname) {
std::cerr<< "can't allocate memory"<<std::endl;
return false;
}
float_count = 0;
int_count = 0;
invalid_count = 0;
ParsedJson &pj(*pj_ptr);
//bool isok =
bool isok =
json_parse(p.first, p.second, pj);
//printf("File %s %s.\n", name,
// isok ? " is valid JSON " : " is not valid JSON");
if(int_count+float_count+invalid_count > 0) {
printf("File %40s %s --- integers: %10zu floats: %10zu invalid: %10zu total numbers: %10zu \n", name,
isok ? " is valid " :
" is not valid ",int_count, float_count, invalid_count, int_count+float_count+invalid_count);
}
free(p.first);
free(fullpath);
deallocate_ParsedJson(pj_ptr);