JsonTestUtil.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/test/JsonTestUtil.h>
  17. #include <algorithm>
  18. #include <cmath>
  19. #include <folly/Conv.h>
  20. #include <folly/json.h>
  21. #include <folly/lang/Assume.h>
  22. namespace folly {
  23. bool compareJson(StringPiece json1, StringPiece json2) {
  24. auto obj1 = parseJson(json1);
  25. auto obj2 = parseJson(json2);
  26. return obj1 == obj2;
  27. }
  28. namespace {
  29. bool isClose(double x, double y, double tolerance) {
  30. return std::abs(x - y) <= tolerance;
  31. }
  32. } // namespace
  33. bool compareDynamicWithTolerance(
  34. const dynamic& obj1,
  35. const dynamic& obj2,
  36. double tolerance) {
  37. if (obj1.type() != obj2.type()) {
  38. if (obj1.isNumber() && obj2.isNumber()) {
  39. const auto& integ = obj1.isInt() ? obj1 : obj2;
  40. const auto& doubl = obj1.isInt() ? obj2 : obj1;
  41. // Use to<double> to fail on precision loss for very large
  42. // integers (in which case the comparison does not make sense).
  43. return isClose(to<double>(integ.asInt()), doubl.asDouble(), tolerance);
  44. }
  45. return false;
  46. }
  47. switch (obj1.type()) {
  48. case dynamic::Type::NULLT:
  49. return true;
  50. case dynamic::Type::ARRAY:
  51. if (obj1.size() != obj2.size()) {
  52. return false;
  53. }
  54. for (auto i1 = obj1.begin(), i2 = obj2.begin(); i1 != obj1.end();
  55. ++i1, ++i2) {
  56. if (!compareDynamicWithTolerance(*i1, *i2, tolerance)) {
  57. return false;
  58. }
  59. }
  60. return true;
  61. case dynamic::Type::BOOL:
  62. return obj1.asBool() == obj2.asBool();
  63. case dynamic::Type::DOUBLE:
  64. return isClose(obj1.asDouble(), obj2.asDouble(), tolerance);
  65. case dynamic::Type::INT64:
  66. return obj1.asInt() == obj2.asInt();
  67. case dynamic::Type::OBJECT:
  68. if (obj1.size() != obj2.size()) {
  69. return false;
  70. }
  71. return std::all_of(
  72. obj1.items().begin(), obj1.items().end(), [&](const auto& item) {
  73. const auto& value1 = item.second;
  74. const auto value2 = obj2.get_ptr(item.first);
  75. return value2 &&
  76. compareDynamicWithTolerance(value1, *value2, tolerance);
  77. });
  78. case dynamic::Type::STRING:
  79. return obj1.asString() == obj2.asString();
  80. }
  81. assume_unreachable();
  82. }
  83. bool compareJsonWithTolerance(
  84. StringPiece json1,
  85. StringPiece json2,
  86. double tolerance) {
  87. auto obj1 = parseJson(json1);
  88. auto obj2 = parseJson(json2);
  89. return compareDynamicWithTolerance(obj1, obj2, tolerance);
  90. }
  91. } // namespace folly