json_buffer.cpp 2.65 KB
Newer Older
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
/** Unit tests for buffer
 *
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
 * @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
 * @license GNU General Public License (version 3)
 *
 * VILLAScommon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#include <cstdlib>
#include <ctime>

#include <criterion/criterion.h>
#include <jansson.h>

#include <villas/json_buffer.hpp>

using namespace villas;

using villas::Buffer;

35
TestSuite(buffer, .description = "Buffer datastructure");
36
37
38
39
40
41
42
43
44
45
46
47

Test(json_buffer, decode)
{
	JsonBuffer buf;
	json_t *j;
	json_t *k;

	const char *e = "{\"id\": \"5a786626-fbc6-4c04-98c2-48027e68c2fa\"}";
	size_t len = strlen(e);

	buf.insert(buf.begin(), e, e+len);

Steffen Vogel's avatar
Steffen Vogel committed
48
	k = json_loads(e, 0, nullptr);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	cr_assert_not_null(k);

	j = buf.decode();
	cr_assert_not_null(j);
	cr_assert(json_equal(j, k));
}

Test(json_buffer, encode)
{
	int ret;
	JsonBuffer buf;
	json_t *k;

	const char *e = "{\"id\": \"5a786626-fbc6-4c04-98c2-48027e68c2fa\"}";

Steffen Vogel's avatar
Steffen Vogel committed
64
	k = json_loads(e, 0, nullptr);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	cr_assert_not_null(k);

	ret = buf.encode(k);
	cr_assert_eq(ret, 0);

	char *f = buf.data();
	cr_assert_not_null(f);

	cr_assert_str_eq(e, f);

	json_decref(k);
}

Test(json_buffer, encode_decode)
{
	int ret;
	JsonBuffer buf;
	json_t *k;
	json_t *j;

	const char *e = "{\"id\": \"5a786626-fbc6-4c04-98c2-48027e68c2fa\"}";

Steffen Vogel's avatar
Steffen Vogel committed
87
	k = json_loads(e, 0, nullptr);
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
	cr_assert_not_null(k);

	ret = buf.encode(k);
	cr_assert_eq(ret, 0);

	j = buf.decode();
	cr_assert_not_null(j);

	cr_assert(json_equal(j, k));

	json_decref(j);
	json_decref(k);
}

Test(json_buffer, multiple)
{
	int ret;
	const int N = 100;

	JsonBuffer buf;
	json_t *k[N];
	json_t *j[N];

	std::srand(std::time(nullptr));

	for (int i = 0; i < N; i++) {
		k[i] = json_pack("{ s: i }", "id", std::rand());
		cr_assert_not_null(k[i]);

		ret = buf.encode(k[i]);
		cr_assert_eq(ret, 0);
	}

	for (int i = 0; i < N; i++) {
		j[i] = buf.decode();
		cr_assert_not_null(j[i]);

		cr_assert(json_equal(k[i], j[i]));

		json_decref(k[i]);
		json_decref(j[i]);
	}
}