File: static_reflection_enum_tests.cpp

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (238 lines) | stat: -rw-r--r-- 6,443 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "simdjson.h"
#include "test_builder.h"
#include <string>

using namespace simdjson;

namespace builder_tests {

  bool test_enum_serialization() {
    TEST_START();
#if SIMDJSON_STATIC_REFLECTION
    enum class Color {
      Red,
      Green,
      Blue
    };

    struct EnumStruct {
      Color color;
      int value;
    };

    EnumStruct test{Color::Red, 42};
    std::string json;
    ASSERT_SUCCESS(builder::to_json_string(test).get(json));
    // Enum should be serialized as string (Red)
    ASSERT_TRUE(json.find("\"color\":\"Red\"") != std::string::npos);
    ASSERT_TRUE(json.find("\"value\":42") != std::string::npos);

    // Test different enum values
    test.color = Color::Green;
    std::string json2;
    ASSERT_SUCCESS(builder::to_json_string(test).get(json2));
    ASSERT_TRUE(json2.find("\"color\":\"Green\"") != std::string::npos);

    test.color = Color::Blue;
    std::string json3;
    ASSERT_SUCCESS(builder::to_json_string(test).get(json3));
    ASSERT_TRUE(json3.find("\"color\":\"Blue\"") != std::string::npos);
#endif
    TEST_SUCCEED();
  }

  bool test_enum_deserialization() {
    TEST_START();
#if SIMDJSON_STATIC_REFLECTION
    enum class Status {
      Active,
      Inactive,
      Pending
    };

    struct StatusStruct {
      Status status;
      std::string name;
    };

    // Test deserialization of different enum values with string representation
    auto json1 = R"({"status":"Active","name":"test1"})"_padded;
    ondemand::parser parser1;
    ondemand::document doc_result1;
    ASSERT_SUCCESS(parser1.iterate(json1).get(doc_result1));
    StatusStruct deserialized1;
    ASSERT_SUCCESS(doc_result1.get<StatusStruct>().get(deserialized1));

    ASSERT_TRUE(deserialized1.status == Status::Active);
    ASSERT_EQUAL(deserialized1.name, "test1");

    // Test Status::Inactive
    auto json2 = R"({"status":"Inactive","name":"test2"})"_padded;
    ondemand::parser parser2;
    ondemand::document doc_result2;
    ASSERT_SUCCESS(parser2.iterate(json2).get(doc_result2));

    StatusStruct deserialized2;
    ASSERT_SUCCESS(doc_result2.get<StatusStruct>().get(deserialized2));

    ASSERT_TRUE(deserialized2.status == Status::Inactive);
    ASSERT_EQUAL(deserialized2.name, "test2");

    // Test Status::Pending
    std::string json3 = "{\"status\":\"Pending\",\"name\":\"test3\"}";
    ondemand::parser parser3;
    ondemand::document doc_result3;
    ASSERT_SUCCESS(parser3.iterate(pad(json3)).get(doc_result3));

    StatusStruct deserialized3;
    ASSERT_SUCCESS(doc_result3.get<StatusStruct>().get(deserialized3));

    ASSERT_TRUE(deserialized3.status == Status::Pending);
    ASSERT_EQUAL(deserialized3.name, "test3");
#endif
    TEST_SUCCEED();
  }

  bool test_enum_round_trip() {
    TEST_START();
#if SIMDJSON_STATIC_REFLECTION
    enum class Priority {
      Low,
      Medium,
      High,
      Critical
    };

    struct Task {
      Priority priority;
      std::string description;
      int id;
    };

    Task original{Priority::High, "Important task", 123};

    // Serialize
    std::string json;
    ASSERT_SUCCESS(builder::to_json_string(original).get(json));
    ASSERT_TRUE(json.find("\"priority\":\"High\"") != std::string::npos); // High as string
    ASSERT_TRUE(json.find("\"description\":\"Important task\"") != std::string::npos);
    ASSERT_TRUE(json.find("\"id\":123") != std::string::npos);

    // Deserialize
    ondemand::parser parser;
    std::cout << json << std::endl;
    ondemand::document doc_result;
    ASSERT_SUCCESS(parser.iterate(pad(json)).get(doc_result));

    Task deserialized;
    ASSERT_SUCCESS(doc_result.get<Task>().get(deserialized));

    ASSERT_TRUE(deserialized.priority == Priority::High);
    ASSERT_EQUAL(deserialized.description, "Important task");
    ASSERT_EQUAL(deserialized.id, 123);
#endif
    TEST_SUCCEED();
  }

  bool test_enum_with_underlying_type() {
    TEST_START();
#if SIMDJSON_STATIC_REFLECTION
    enum class ErrorCode : int {
      Success = 0,
      NotFound = 404,
      ServerError = 500
    };

    struct Response {
      ErrorCode error;
      std::string message;
    };

    Response test{ErrorCode::NotFound, "Resource not found"};

    std::string json;
    ASSERT_SUCCESS(builder::to_json_string(test).get(json));
    ASSERT_TRUE(json.find("\"error\":\"NotFound\"") != std::string::npos);
    ASSERT_TRUE(json.find("\"message\":\"Resource not found\"") != std::string::npos);

    // Test round-trip
    ondemand::parser parser;
    ondemand::document doc_result;
    ASSERT_SUCCESS(parser.iterate(pad(json)).get(doc_result));
    Response deserialized;
    ASSERT_SUCCESS(doc_result.get<Response>().get(deserialized));

    ASSERT_TRUE(deserialized.error == ErrorCode::NotFound);
    ASSERT_EQUAL(deserialized.message, "Resource not found");
#endif
    TEST_SUCCEED();
  }

  bool test_multiple_enums() {
    TEST_START();
#if SIMDJSON_STATIC_REFLECTION
    enum class Day {
      Monday,
      Tuesday,
      Wednesday,
      Thursday,
      Friday,
      Saturday,
      Sunday
    };

    enum class Month {
      January,
      February,
      March,
      April,
      May,
      June,
      July,
      August,
      September,
      October,
      November,
      December
    };

    struct Date {
      Day day;
      Month month;
      int year;
    };

    Date test{Day::Friday, Month::July, 2024};

    std::string json;
    ASSERT_SUCCESS(builder::to_json_string(test).get(json));
    ASSERT_TRUE(json.find("\"day\":\"Friday\"") != std::string::npos);      // Friday as string
    ASSERT_TRUE(json.find("\"month\":\"July\"") != std::string::npos);    // July as string
    ASSERT_TRUE(json.find("\"year\":2024") != std::string::npos);

    // Test round-trip
    ondemand::parser parser;
    auto doc_result = parser.iterate(pad(json));
    ASSERT_SUCCESS(doc_result);
    Date deserialized;
    ASSERT_SUCCESS(doc_result.get<Date>().get(deserialized));
    ASSERT_TRUE(deserialized.day == Day::Friday);
    ASSERT_TRUE(deserialized.month == Month::July);
    ASSERT_EQUAL(deserialized.year, 2024);
#endif
    TEST_SUCCEED();
  }

  bool run() {
    return test_enum_serialization() &&
           test_enum_deserialization() &&
           test_enum_round_trip() &&
           test_enum_with_underlying_type() &&
           test_multiple_enums();
  }

} // namespace builder_tests

int main(int argc, char *argv[]) {
  return test_main(argc, argv, builder_tests::run);
}