test_json_parse.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <cassert>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <string>
  5. #include <nlohmann/json.hpp>
  6. #include "json_parse.hpp"
  7. using nlohmann::json;
  8. using smartbotic::db::parse_to_nlohmann;
  9. namespace {
  10. void check(bool cond, const char* msg) {
  11. if (!cond) {
  12. std::cerr << "FAIL: " << msg << "\n";
  13. std::abort();
  14. }
  15. }
  16. void test_empty_object() {
  17. auto j = parse_to_nlohmann("{}");
  18. check(j.is_object() && j.empty(), "empty object");
  19. }
  20. void test_empty_array() {
  21. auto j = parse_to_nlohmann("[]");
  22. check(j.is_array() && j.empty(), "empty array");
  23. }
  24. void test_primitives() {
  25. check(parse_to_nlohmann("true").get<bool>() == true, "true");
  26. check(parse_to_nlohmann("false").get<bool>() == false, "false");
  27. check(parse_to_nlohmann("null").is_null(), "null");
  28. check(parse_to_nlohmann("42").get<int64_t>() == 42, "int");
  29. check(parse_to_nlohmann("-7").get<int64_t>() == -7, "negative int");
  30. check(parse_to_nlohmann("3.14").get<double>() == 3.14, "double");
  31. check(parse_to_nlohmann("\"hello\"").get<std::string>() == "hello", "string");
  32. }
  33. void test_nested() {
  34. std::string s = R"({"a":1,"b":[2,3,{"c":"x"}],"d":null,"e":true})";
  35. auto y = parse_to_nlohmann(s);
  36. auto n = json::parse(s);
  37. check(y == n, "nested equality with nlohmann");
  38. }
  39. void test_unicode_string() {
  40. std::string s = R"({"name":"Árvíztűrő tükörfúrógép","emoji":"🚀"})";
  41. auto y = parse_to_nlohmann(s);
  42. auto n = json::parse(s);
  43. check(y == n, "unicode equality");
  44. }
  45. void test_large_int() {
  46. auto j = parse_to_nlohmann("9223372036854775807");
  47. check(j.get<int64_t>() == 9223372036854775807LL, "int64 max");
  48. }
  49. void test_string_view_overload() {
  50. std::string s = "{\"k\":1}";
  51. auto j = parse_to_nlohmann(std::string_view(s));
  52. check(j["k"].get<int>() == 1, "string_view overload");
  53. }
  54. void test_invalid_json_throws() {
  55. bool threw = false;
  56. try {
  57. (void)parse_to_nlohmann("{not json");
  58. } catch (const std::exception&) {
  59. threw = true;
  60. }
  61. check(threw, "invalid json throws");
  62. }
  63. void test_corpus_equivalence() {
  64. // Sample of doc shapes the production service sees.
  65. const char* corpus[] = {
  66. R"({"_id":"abc","name":"x","tags":["a","b"],"count":0})",
  67. R"({"_id":"xyz","_vector":[0.1,-0.2,0.3,0.4],"meta":{"k":"v"}})",
  68. R"({"_id":"e1","_event":"call.ended","at":1731628800123})",
  69. R"([])",
  70. R"([1,2,3,4,5,6,7,8,9,10])",
  71. R"({"deeply":{"nested":{"object":{"is":{"fine":true}}}}})",
  72. };
  73. for (const char* s : corpus) {
  74. check(parse_to_nlohmann(s) == json::parse(s), s);
  75. }
  76. }
  77. } // namespace
  78. int main() {
  79. test_empty_object();
  80. test_empty_array();
  81. test_primitives();
  82. test_nested();
  83. test_unicode_string();
  84. test_large_int();
  85. test_string_view_overload();
  86. test_invalid_json_throws();
  87. test_corpus_equivalence();
  88. std::cout << "test_json_parse: all passed\n";
  89. return 0;
  90. }