2019-11-09 06:39:45 +08:00
|
|
|
#include <cstring>
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
// Microsoft can't be bothered to provide standard utils.
|
|
|
|
#include <dirent_portable.h>
|
|
|
|
#endif
|
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2020-03-03 06:23:19 +08:00
|
|
|
#include "simdjson.h"
|
2019-11-09 06:39:45 +08:00
|
|
|
|
|
|
|
/**
|
2020-03-06 03:05:37 +08:00
|
|
|
* Does the file filename end with the given extension.
|
2019-11-09 06:39:45 +08:00
|
|
|
*/
|
|
|
|
static bool has_extension(const char *filename, const char *extension) {
|
|
|
|
const char *ext = strrchr(filename, '.');
|
|
|
|
return ((ext != nullptr) && (strcmp(ext, extension) == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool starts_with(const char *pre, const char *str) {
|
|
|
|
size_t len_pre = strlen(pre), len_str = strlen(str);
|
|
|
|
return len_str < len_pre ? false : strncmp(pre, str, len_pre) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const char *pre, const char *str) {
|
|
|
|
return (strstr(str, pre) != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validate(const char *dirname) {
|
|
|
|
bool everything_fine = true;
|
|
|
|
const char *extension1 = ".ndjson";
|
|
|
|
const char *extension2 = ".jsonl";
|
2020-01-04 11:22:47 +08:00
|
|
|
const char *extension3 = ".json"; // bad json files shoud fail
|
|
|
|
|
2019-11-09 06:39:45 +08:00
|
|
|
size_t dirlen = strlen(dirname);
|
|
|
|
struct dirent **entry_list;
|
|
|
|
int c = scandir(dirname, &entry_list, nullptr, alphasort);
|
|
|
|
if (c < 0) {
|
|
|
|
fprintf(stderr, "error accessing %s \n", dirname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (c == 0) {
|
|
|
|
printf("nothing in dir %s \n", dirname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool *is_file_as_expected = new bool[c];
|
|
|
|
for (int i = 0; i < c; i++) {
|
|
|
|
is_file_as_expected[i] = true;
|
|
|
|
}
|
|
|
|
size_t how_many = 0;
|
|
|
|
bool needsep = (strlen(dirname) > 1) && (dirname[strlen(dirname) - 1] != '/');
|
|
|
|
|
|
|
|
|
|
|
|
/*For all files in the folder*/
|
|
|
|
for (int i = 0; i < c; i++) {
|
|
|
|
const char *name = entry_list[i]->d_name;
|
2020-01-04 11:22:47 +08:00
|
|
|
if (has_extension(name, extension1) || has_extension(name, extension2) || has_extension(name, extension3)) {
|
2019-11-09 06:39:45 +08:00
|
|
|
|
|
|
|
/* Finding the file path */
|
|
|
|
printf("validating: file %s ", name);
|
|
|
|
fflush(nullptr);
|
|
|
|
size_t filelen = strlen(name);
|
|
|
|
char *fullpath = static_cast<char *>(malloc(dirlen + filelen + 1 + 1));
|
|
|
|
strcpy(fullpath, dirname);
|
|
|
|
if (needsep) {
|
|
|
|
fullpath[dirlen] = '/';
|
|
|
|
strcpy(fullpath + dirlen + 1, name);
|
|
|
|
} else {
|
|
|
|
strcpy(fullpath + dirlen, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The actual test*/
|
2020-03-07 10:14:34 +08:00
|
|
|
simdjson::padded_string json = simdjson::padded_string::load(fullpath);
|
2020-03-06 03:05:37 +08:00
|
|
|
simdjson::document::parser parser;
|
2019-11-09 06:39:45 +08:00
|
|
|
|
|
|
|
++how_many;
|
2020-03-06 03:05:37 +08:00
|
|
|
simdjson::error_code error = simdjson::SUCCESS;
|
|
|
|
for (auto result : parser.parse_many(json)) {
|
|
|
|
error = result.error;
|
2019-11-09 06:39:45 +08:00
|
|
|
}
|
2020-03-06 03:05:37 +08:00
|
|
|
printf("%s\n", error ? "ok" : "invalid");
|
2019-11-09 06:39:45 +08:00
|
|
|
/* Check if the file is supposed to pass or not. Print the results */
|
|
|
|
if (contains("EXCLUDE", name)) {
|
|
|
|
// skipping
|
|
|
|
how_many--;
|
2020-03-06 03:05:37 +08:00
|
|
|
} else if (starts_with("pass", name) and (has_extension(extension1, name) or has_extension(extension2, name)) and error) {
|
2019-11-09 06:39:45 +08:00
|
|
|
is_file_as_expected[i] = false;
|
|
|
|
printf("warning: file %s should pass but it fails. Error is: %s\n",
|
2020-03-07 04:14:23 +08:00
|
|
|
name, error_message(error));
|
2020-03-06 03:05:37 +08:00
|
|
|
printf("size of file in bytes: %zu \n", json.size());
|
2019-11-09 06:39:45 +08:00
|
|
|
everything_fine = false;
|
2020-03-06 03:05:37 +08:00
|
|
|
} else if ( starts_with("fail", name) and (not starts_with("fail10.json", name)) and !error) {
|
2019-11-09 06:39:45 +08:00
|
|
|
is_file_as_expected[i] = false;
|
|
|
|
printf("warning: file %s should fail but it passes.\n", name);
|
2020-03-06 03:05:37 +08:00
|
|
|
printf("size of file in bytes: %zu \n", json.size());
|
2019-11-09 06:39:45 +08:00
|
|
|
everything_fine = false;
|
|
|
|
}
|
|
|
|
free(fullpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%zu files checked.\n", how_many);
|
|
|
|
if (everything_fine) {
|
|
|
|
printf("All ok!\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"There were problems! Consider reviewing the following files:\n");
|
|
|
|
for (int i = 0; i < c; i++) {
|
|
|
|
if (!is_file_as_expected[i]) {
|
|
|
|
fprintf(stderr, "%s \n", entry_list[i]->d_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-04 11:22:47 +08:00
|
|
|
printf("Note that json stream expects sequences of objects and arrays, so otherwise valid json files can fail by design.\n");
|
2019-11-09 06:39:45 +08:00
|
|
|
for (int i = 0; i < c; ++i) {
|
|
|
|
free(entry_list[i]);
|
|
|
|
}
|
|
|
|
free(entry_list);
|
|
|
|
delete[] is_file_as_expected;
|
|
|
|
return everything_fine;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc != 2) {
|
|
|
|
std::cerr << "Usage: " << argv[0] << " <directorywithjsonfiles>"
|
|
|
|
<< std::endl;
|
|
|
|
#ifndef SIMDJSON_TEST_DATA_DIR
|
|
|
|
std::cout
|
|
|
|
<< "We are going to assume you mean to use the 'jsonchecker' directory."
|
|
|
|
<< std::endl;
|
|
|
|
return validate("jsonchecker/") ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
#else
|
|
|
|
std::cout << "We are going to assume you mean to use the '"
|
|
|
|
<< SIMDJSON_TEST_DATA_DIR << "' directory." << std::endl;
|
|
|
|
return validate(SIMDJSON_TEST_DATA_DIR) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return validate(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|