test_json_archive.cpp 1.77 KB
Newer Older
limm's avatar
limm committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) OpenMMLab. All rights reserved.

#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "catch.hpp"
#include "mmdeploy/archive/json_archive.h"

using ArrayLikeTypes = std::tuple<std::vector<int>, std::deque<int>, std::array<int, 15>,
                                  std::list<int>, std::set<int>, std::unordered_set<int>,
                                  std::multiset<int>, std::unordered_multiset<int> >;

TEMPLATE_LIST_TEST_CASE("test array-like", "[archive]", ArrayLikeTypes) {
  TestType v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4};
  nlohmann::json json;
  mmdeploy::JsonOutputArchive oa(json);
  oa(v);
  mmdeploy::JsonInputArchive ia(json);
  TestType u{};
  ia(u);
  std::cout << json << std::endl;
  REQUIRE(u == v);
}

using MapLikeTypes = std::tuple<
    //        std::map<int, float>
    std::map<int, float>, std::unordered_map<int, float>, std::multimap<int, float>,
    std::unordered_multimap<int, float> >;

TEMPLATE_LIST_TEST_CASE("test map-like", "[archive]", MapLikeTypes) {
  TestType v{{1, 123.456f}, {1, 222.222f}, {2, 111.222f}, {3, 223.332f}, {3, 1.22e10f}};
  nlohmann::json json;
  mmdeploy::JsonOutputArchive oa(json);
  oa(v);
  mmdeploy::JsonInputArchive ia(json);
  TestType u;
  ia(u);
  std::cout << json << std::endl;
  REQUIRE(u == v);
}

struct A {
  std::vector<int> vec;
  std::string str;
  friend bool operator==(const A& a, const A& b) { return a.vec == b.vec && a.str == b.str; }
  MMDEPLOY_ARCHIVE_MEMBERS(vec, str);
};

TEST_CASE("test struct", "[archive]") {
  A a{{1, 2, 3, 4, 5}, "hello"};
  nlohmann::json json;
  mmdeploy::JsonOutputArchive oa(json);
  oa(a);
  mmdeploy::JsonInputArchive ia(json);
  A b;
  ia(b);
  REQUIRE(a == b);
}