forked from couchbaselabs/libcbio
/
document_unit_tests.cc
263 lines (224 loc) · 7.31 KB
/
document_unit_tests.cc
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2012 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <libcbio/cbio.h>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <gtest/gtest.h>
using namespace std;
class LibcbioCreateDocumentTest : public ::testing::Test
{
public:
LibcbioCreateDocumentTest() {
// Currently we don't use the handle in the create interface
handle = NULL;
}
protected:
libcbio_t handle;
};
TEST_F(LibcbioCreateDocumentTest, createEmptyDocumentWithNilPointer) {
EXPECT_EQ(CBIO_ERROR_EINVAL,
cbio_create_empty_document(handle, NULL));
}
TEST_F(LibcbioCreateDocumentTest, createEmptyDocument) {
libcbio_document_t doc;
EXPECT_EQ(CBIO_SUCCESS,
cbio_create_empty_document(handle, &doc));
cbio_document_release(doc);
}
class LibcbioDocumentMemberTest : public LibcbioCreateDocumentTest
{
public:
LibcbioDocumentMemberTest() : LibcbioCreateDocumentTest() {
}
virtual void SetUp(void) {
EXPECT_EQ(CBIO_SUCCESS,
cbio_create_empty_document(handle, &doc));
}
virtual void TearDown(void) {
cbio_document_release(doc);
}
protected:
libcbio_document_t doc;
};
TEST_F(LibcbioDocumentMemberTest, setId) {
const void *key = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_id(doc, key, 3, 0));
}
TEST_F(LibcbioDocumentMemberTest, setIdAllocate) {
const void *key = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_id(doc, key, 3, 1));
}
TEST_F(LibcbioDocumentMemberTest, getId) {
const void *key = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_id(doc, key, 3, 0));
const void *stored;
size_t nkey;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_id(doc, &stored, &nkey));
EXPECT_EQ(3, nkey);
EXPECT_EQ(key, stored);
}
TEST_F(LibcbioDocumentMemberTest, getIdAllocated) {
const void *key = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_id(doc, key, 3, 1));
const void *stored;
size_t nkey;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_id(doc, &stored, &nkey));
EXPECT_EQ(3, nkey);
EXPECT_NE(key, stored);
}
TEST_F(LibcbioDocumentMemberTest, setMeta) {
const void *meta = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_meta(doc, meta, 3, 0));
}
TEST_F(LibcbioDocumentMemberTest, setMetaAllocate) {
const void *meta = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_meta(doc, meta, 3, 1));
}
TEST_F(LibcbioDocumentMemberTest, getMeta) {
const void *meta = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_meta(doc, meta, 3, 0));
const void *stored;
size_t nmeta;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_meta(doc, &stored, &nmeta));
EXPECT_EQ(3, nmeta);
EXPECT_EQ(meta, stored);
}
TEST_F(LibcbioDocumentMemberTest, getMetaAllocated) {
const void *meta = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_meta(doc, meta, 3, 1));
const void *stored;
size_t nmeta;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_meta(doc, &stored, &nmeta));
EXPECT_EQ(3, nmeta);
EXPECT_NE(meta, stored);
}
TEST_F(LibcbioDocumentMemberTest, setGetRevision) {
for (uint64_t ii = 0; ii < 100; ++ii) {
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_revision(doc, ii));
uint64_t rev;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_revision(doc, &rev));
EXPECT_EQ(ii, rev);
}
}
TEST_F(LibcbioDocumentMemberTest, setGetDeleted) {
for (int ii = 3; ii > -1; ii--) {
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_deleted(doc, ii));
int deleted;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_deleted(doc, &deleted));
EXPECT_EQ(ii, deleted);
}
}
TEST_F(LibcbioDocumentMemberTest, setValue) {
const void *value = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_value(doc, value, 3, 0));
}
TEST_F(LibcbioDocumentMemberTest, setValueAllocate) {
const void *value = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_value(doc, value, 3, 1));
}
TEST_F(LibcbioDocumentMemberTest, getValue) {
const void *value = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_value(doc, value, 3, 0));
const void *stored;
size_t nvalue;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_value(doc, &stored, &nvalue));
EXPECT_EQ(3, nvalue);
EXPECT_EQ(value, stored);
}
TEST_F(LibcbioDocumentMemberTest, getValueAllocated) {
const void *value = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_value(doc, value, 3, 1));
const void *stored;
size_t nvalue;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_value(doc, &stored, &nvalue));
EXPECT_EQ(3, nvalue);
EXPECT_NE(value, stored);
}
TEST_F(LibcbioDocumentMemberTest, getValueLengthAllocated) {
const void *value = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_value(doc, value, 3, 1));
size_t nvalue;
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_value(doc, NULL, &nvalue));
EXPECT_EQ(3, nvalue);
}
TEST_F(LibcbioDocumentMemberTest, setGetContentType) {
uint8_t ct = 0x0, nt;
for (int ii = 0; ii < 0x100; ii++, ct++) {
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_content_type(doc, ct));
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_get_content_type(doc, &nt));
EXPECT_EQ(ct, nt);
}
}
TEST_F(LibcbioDocumentMemberTest, reinitialize) {
const void *data = "foo";
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_id(doc, data, 3, 1));
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_meta(doc, data, 3, 1));
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_value(doc, data, 3, 1));
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_content_type(doc, 0x55));
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_revision(doc, 5));
EXPECT_EQ(CBIO_SUCCESS,
cbio_document_set_deleted(doc, 1));
cbio_document_reinitialize(doc);
size_t nb;
EXPECT_EQ(CBIO_ERROR_EINVAL,
cbio_document_get_id(doc, &data, &nb));
EXPECT_EQ(CBIO_ERROR_EINVAL,
cbio_document_get_meta(doc, &data, &nb));
EXPECT_EQ(CBIO_ERROR_EINVAL,
cbio_document_get_value(doc, &data, &nb));
int deleted;
EXPECT_EQ(CBIO_ERROR_EINVAL,
cbio_document_get_deleted(doc, &deleted));
uint64_t revno;
EXPECT_EQ(CBIO_ERROR_EINVAL,
cbio_document_get_revision(doc, &revno));
uint8_t ct;
EXPECT_EQ(CBIO_ERROR_EINVAL,
cbio_document_get_content_type(doc, &ct));
}