You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_loader_test.cpp 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * \file sdk/load-and-run/test/test_json_loader.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include <cfloat>
  12. #include <cstdint>
  13. #include <cstdio>
  14. #include <cmath>
  15. #include "../src/json_loader.h"
  16. using namespace mgb;
  17. void test_number(double real, std::string str) {
  18. JsonLoader json;
  19. auto root = json.load(str.data(), str.size());
  20. mgb_assert(root->is_number());
  21. mgb_assert(std::fabs(real - root->number()) <= DBL_EPSILON);
  22. }
  23. void test_string(std::string str, std::string json_str) {
  24. JsonLoader json;
  25. auto root = json.load(json_str.data(), json_str.size());
  26. mgb_assert(root->is_str());
  27. mgb_assert(str == root->str());
  28. }
  29. void test_array(size_t num, std::string str) {
  30. JsonLoader json;
  31. auto root = json.load(str.data(), str.size());
  32. mgb_assert(root->is_array());
  33. mgb_assert(root->len() == num);
  34. }
  35. void test_object(size_t num, std::string str) {
  36. JsonLoader json;
  37. auto root = json.load(str.data(), str.size());
  38. mgb_assert(root->is_object());
  39. mgb_assert(root->len() == num);
  40. }
  41. int main() {
  42. test_number(1.0, "1.0");
  43. test_number(1e10, "1e10");
  44. test_number(0.2345678, "0.02345678e1");
  45. test_number(-10086, "-1.0086E4");
  46. test_number(1.7976931348623157e+308,
  47. "1.7976931348623157e+308"); // max double
  48. test_string("a", "\"a\"");
  49. test_string("\\table", "\"\\table\"");
  50. test_array(0, " [ ] ");
  51. test_array(4, " [ 0.1, 0.2,0.3, 1990 ] ");
  52. test_array(2, " [ 0.1, \"hello-world\"]");
  53. test_array(3, " [ 0.1, \"hello-world\", [2.0, 33]]");
  54. test_array(1, " [ [ [ [2020] ], [2021], [[2022]] ] ]");
  55. test_object(0, " { } ");
  56. test_object(1, "{\"key1\": 2023}");
  57. test_object(1,
  58. "{\"key1\": { \"key2\": { "
  59. "\"key3\": \"value\" } } }");
  60. test_object(1, "{\"key1\":{\"key2\":{}}}");
  61. printf("test passed\n");
  62. return 0;
  63. }

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台