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
|
#include <forstio/test/suite.h>
#include <forstio/codec/json/json.h>
#include <iostream>
namespace {
namespace schema {
using namespace saw::schema;
using TestTuple = Tuple<
String,
Int32
>;
using TestArray = Array<
String, 1
>;
using TestMultiArray = Array<
String, 3
>;
using TestStruct = Struct<
Member<Int32, "foo">,
Member<String, "bar">
>;
}
SAW_TEST("Int32 write"){
using namespace saw;
data<schema::Int32, encode::Native> native_int;
data<schema::Int32, encode::Json> json_int;
native_int.set(44123);
codec<schema::Int32, encode::Json> json_codec;
error_or<void> eov = json_codec.encode(native_int, json_int);
SAW_EXPECT(eov.is_value(), "Encoding error");
/**
* Currently doing this manually. Technically I should convert to std::string for tests
*/
std::string_view str_v = "44123";
std::string enc_val = convert_to_string(json_int.get_buffer());
SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val );
}
SAW_TEST("String write"){
using namespace saw;
data<schema::String, encode::Native> nat_str;
data<schema::String, encode::Json> json_str;
nat_str.set("foo");
codec<schema::String, encode::Json> json_codec;
error_or<void> eov = json_codec.encode(nat_str, json_str);
SAW_EXPECT(eov.is_value(), "Encoding error");
/**
* Currently doing this manually. Technically I should convert to std::string for tests
*/
std::string_view str_view = "\"foo\"";
std::string encoded_value = convert_to_string(json_str.get_buffer());
SAW_EXPECT(encoded_value == str_view, "String not encoded correctly");
}
SAW_TEST("Tuple write"){
using namespace saw;
data<schema::TestTuple, encode::Native> native_tup;
data<schema::TestTuple, encode::Json> json_tup;
auto& nat_zero = native_tup.template get<0>();
auto& nat_one = native_tup.template get<1>();
nat_zero.set("bar");
nat_one.set(34);
codec<schema::TestTuple, encode::Json> json_codec;
error_or<void> eov = json_codec.encode(native_tup, json_tup);
SAW_EXPECT(eov.is_value(), "Encoding error");
std::string_view str_v = "[\"bar\",34]";
std::string enc_val = convert_to_string(json_tup.get_buffer());
SAW_EXPECT(enc_val == str_v, std::string{"Tuple not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v});
}
SAW_TEST("Array write"){
using namespace saw;
data<schema::TestArray, encode::Native> native{3u};
data<schema::TestArray, encode::Json> json;
native.at(0).set("foo");
native.at(1).set("bar");
native.at(2).set("baz");
codec<schema::TestArray, encode::Json> json_codec;
error_or<void> eov = json_codec.encode(native, json);
SAW_EXPECT(eov.is_value(), "Encoding error");
std::string_view str_v = "[\"foo\",\"bar\",\"baz\"]";
std::string enc_val = convert_to_string(json.get_buffer());
SAW_EXPECT(enc_val == str_v, std::string{"Array not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v});
}
SAW_TEST("Three Dim Array write"){
using namespace saw;
data<schema::TestMultiArray, encode::Native> native{2,1,2};
data<schema::TestMultiArray, encode::Json> json;
native.at(0,0,0).set("multi");
native.at(0,0,1).set("baz");
native.at(1,0,0).set("foo");
native.at(1,0,1).set("bar");
codec<schema::TestMultiArray, encode::Json> json_codec;
error_or<void> eov = json_codec.encode(native, json);
SAW_EXPECT(eov.is_value(), "Encoding error");
std::string_view str_v = "[[[\"multi\",\"baz\"]],[[\"foo\",\"bar\"]]]";
std::string enc_val = convert_to_string(json.get_buffer());
SAW_EXPECT(enc_val == str_v, std::string{"Array not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v});
}
SAW_TEST("Struct write"){
using namespace saw;
data<schema::TestStruct, encode::Native> native;
data<schema::TestStruct, encode::Json> json;
native.get<"foo">().set(5);
native.get<"bar">().set("baz");
codec<schema::TestStruct, encode::Json> json_codec;
error_or<void> eov = json_codec.encode(native, json);
SAW_EXPECT(eov.is_value(), "Encoding error");
std::string_view str_v = "{\"foo\":5,\"bar\":\"baz\"}";
std::string enc_val = convert_to_string(json.get_buffer());
SAW_EXPECT(enc_val == str_v, std::string{"Struct not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v});
}
}
|