jsoncheckertest.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "unittest.h"
  2. #include "rapidjson/document.h"
  3. using namespace rapidjson;
  4. static char* ReadFile(const char* filename, size_t& length) {
  5. FILE *fp = fopen(filename, "rb");
  6. if (!fp)
  7. fp = fopen(filename, "rb");
  8. if (!fp)
  9. return 0;
  10. fseek(fp, 0, SEEK_END);
  11. length = (size_t)ftell(fp);
  12. fseek(fp, 0, SEEK_SET);
  13. char* json = (char*)malloc(length + 1);
  14. fread(json, 1, length, fp);
  15. json[length] = '\0';
  16. fclose(fp);
  17. return json;
  18. }
  19. TEST(JsonChecker, Reader) {
  20. char filename[256];
  21. // jsonchecker/failXX.json
  22. for (int i = 1; i <= 33; i++) {
  23. if (i == 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting.
  24. continue;
  25. sprintf(filename, "jsonchecker/fail%d.json", i);
  26. size_t length;
  27. char* json = ReadFile(filename, length);
  28. if (!json) {
  29. sprintf(filename, "../../bin/jsonchecker/fail%d.json", i);
  30. json = ReadFile(filename, length);
  31. if (!json) {
  32. printf("jsonchecker file %s not found", filename);
  33. continue;
  34. }
  35. }
  36. GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
  37. if (!document.Parse<0>((const char*)json).HasParseError())
  38. FAIL();
  39. //printf("%s(%u):%s\n", filename, (unsigned)document.GetErrorOffset(), document.GetParseError());
  40. free(json);
  41. }
  42. // passX.json
  43. for (int i = 1; i <= 3; i++) {
  44. sprintf(filename, "jsonchecker/pass%d.json", i);
  45. size_t length;
  46. char* json = ReadFile(filename, length);
  47. if (!json) {
  48. sprintf(filename, "../../bin/jsonchecker/pass%d.json", i);
  49. json = ReadFile(filename, length);
  50. if (!json) {
  51. printf("jsonchecker file %s not found", filename);
  52. continue;
  53. }
  54. }
  55. GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
  56. document.Parse<0>((const char*)json);
  57. EXPECT_TRUE(!document.HasParseError());
  58. free(json);
  59. }
  60. }