1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "RdbBigIntTest"
16 #include <gtest/gtest.h>
17
18 #include "common.h"
19 #include "rdb_errno.h"
20 #include "rdb_helper.h"
21 using namespace testing::ext;
22 using namespace OHOS::NativeRdb;
23 namespace Test {
24 class RdbBigIntTest : public testing::Test {
25 public:
26 static void SetUpTestCase(void);
27 static void TearDownTestCase(void);
28 void SetUp();
29 void TearDown();
30
31 protected:
32 class RdbCallback : public RdbOpenCallback {
33 public:
OnCreate(RdbStore & store)34 int OnCreate(RdbStore &store) override
35 {
36 return E_OK;
37 }
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)38 int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override
39 {
40 return E_OK;
41 }
42 };
43 using Floats = ValueObject::FloatVector;
44 static constexpr const char* PATH_NAME = "/data/test/bigint_test.db";
45 static constexpr const char* DATABASE_NAME = "bigint_test.db";
46 static constexpr const char* CREATE_TABLE =
47 "CREATE TABLE IF NOT EXISTS bigint_table(id INTEGER PRIMARY KEY AUTOINCREMENT, "
48 "value1 UNLIMITED INT NOT NULL, value2 UNLIMITED INT, value3 FLOATVECTOR)";
49 static constexpr const char* DROP_TABLE = "DROP TABLE IF EXISTS bigint_table";
50 static std::shared_ptr<RdbStore> rdbStore_;
51 };
52 std::shared_ptr<RdbStore> RdbBigIntTest::rdbStore_;
SetUpTestCase(void)53 void RdbBigIntTest::SetUpTestCase(void)
54 {
55 int errCode = E_OK;
56 RdbCallback callback;
57 RdbStoreConfig config(PATH_NAME);
58 config.SetName(DATABASE_NAME);
59 RdbHelper::DeleteRdbStore(PATH_NAME);
60 rdbStore_ = RdbHelper::GetRdbStore(config, 1, callback, errCode);
61 EXPECT_NE(rdbStore_, nullptr);
62 }
63
TearDownTestCase(void)64 void RdbBigIntTest::TearDownTestCase(void)
65 {
66 rdbStore_ = nullptr;
67 RdbHelper::DeleteRdbStore(PATH_NAME);
68 }
69
SetUp(void)70 void RdbBigIntTest::SetUp(void)
71 {
72 rdbStore_->ExecuteSql(DROP_TABLE);
73 rdbStore_->ExecuteSql(CREATE_TABLE);
74 }
75
TearDown(void)76 void RdbBigIntTest::TearDown(void)
77 {
78 rdbStore_->ExecuteSql(DROP_TABLE);
79 }
80
81 /**
82 * @tc.name: Insert_BigInt_INT64
83 * @tc.desc: test insert bigint to rdb store
84 * @tc.type: FUNC
85 */
86 HWTEST_F(RdbBigIntTest, Insert_BigInt_INT64, TestSize.Level1)
87 {
88 int64_t outRowId = -1;
89 ValuesBucket bucket;
90 bucket.Put("value1", BigInteger(158));
91 bucket.Put("value2", BigInteger(-158));
92 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
93 EXPECT_EQ(status, E_OK);
94 auto resultSet = rdbStore_->QuerySql("select value1, value2 from bigint_table");
95 EXPECT_NE(resultSet, nullptr);
96 while (resultSet->GoToNextRow() == E_OK) {
97 RowEntity entity;
98 status = resultSet->GetRow(entity);
99 EXPECT_EQ(status, E_OK);
100 auto value = entity.Get("value1");
101 auto val = std::get_if<BigInteger>(&value.value);
102 EXPECT_NE(val, nullptr);
103 if (val != nullptr) {
104 EXPECT_TRUE(*val == BigInteger(158));
105 }
106 value = entity.Get("value2");
107 val = std::get_if<BigInteger>(&value.value);
108 EXPECT_NE(val, nullptr);
109 if (val != nullptr) {
110 EXPECT_TRUE(*val == BigInteger(-158));
111 }
112 }
113 }
114
115 /**
116 * @tc.name: Insert_Step_BigInt_INT64
117 * @tc.desc: test insert bigint to rdb store
118 * @tc.type: FUNC
119 */
120 HWTEST_F(RdbBigIntTest, Insert_Step_BigInt_INT64, TestSize.Level1)
121 {
122 int64_t outRowId = -1;
123 ValuesBucket bucket;
124 bucket.Put("value1", BigInteger(158));
125 bucket.Put("value2", BigInteger(-158));
126 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
127 EXPECT_EQ(status, E_OK);
128 auto resultSet = rdbStore_->QueryByStep("select value1, value2 from bigint_table");
129 EXPECT_NE(resultSet, nullptr);
130 while (resultSet->GoToNextRow() == E_OK) {
131 RowEntity entity;
132 status = resultSet->GetRow(entity);
133 EXPECT_EQ(status, E_OK);
134 auto value = entity.Get("value1");
135 auto val = std::get_if<BigInteger>(&value.value);
136 EXPECT_NE(val, nullptr);
137 if (val != nullptr) {
138 EXPECT_TRUE(*val == BigInteger(158));
139 }
140 value = entity.Get("value2");
141 val = std::get_if<BigInteger>(&value.value);
142 EXPECT_NE(val, nullptr);
143 if (val != nullptr) {
144 EXPECT_TRUE(*val == BigInteger(-158));
145 }
146 }
147 }
148
149 /**
150 * @tc.name: Insert_BigInt_INT128
151 * @tc.desc: test insert bigint to rdb store
152 * @tc.type: FUNC
153 */
154 HWTEST_F(RdbBigIntTest, Insert_BigInt_INT128, TestSize.Level1)
155 {
156 int64_t outRowId = -1;
157 BigInteger value1 = BigInteger(0, std::vector<uint64_t>{ 158, 0xDEADDEADDEADDEAD });
158 BigInteger value2 = BigInteger(1, std::vector<uint64_t>{ 158, 0xDEADDEADDEADDEAD });
159 ValuesBucket bucket;
160 bucket.Put("value1", value1);
161 bucket.Put("value2", value2);
162 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
163 EXPECT_EQ(status, E_OK);
164 auto resultSet = rdbStore_->QuerySql("select value1, value2 from bigint_table");
165 EXPECT_NE(resultSet, nullptr);
166 while (resultSet->GoToNextRow() == E_OK) {
167 RowEntity entity;
168 status = resultSet->GetRow(entity);
169 EXPECT_EQ(status, E_OK);
170 auto value = entity.Get("value1");
171 auto val = std::get_if<BigInteger>(&value.value);
172 EXPECT_NE(val, nullptr);
173 if (val != nullptr) {
174 EXPECT_TRUE(*val == value1);
175 }
176 value = entity.Get("value2");
177 val = std::get_if<BigInteger>(&value.value);
178 EXPECT_NE(val, nullptr);
179 if (val != nullptr) {
180 EXPECT_TRUE(*val == value2);
181 }
182 }
183 }
184
185 /**
186 * @tc.name: Insert_BigInt_INT128
187 * @tc.desc: test insert bigint to rdb store
188 * @tc.type: FUNC
189 */
190 HWTEST_F(RdbBigIntTest, GetValue_BigInt_INT128, TestSize.Level1)
191 {
192 int64_t outRowId = -1;
193 BigInteger value1 = BigInteger(0, std::vector<uint64_t>{ 158, 0xDEADDEADDEADDEAD });
194 BigInteger value2 = BigInteger(1, std::vector<uint64_t>{ 158, 0xDEADDEADDEADDEAD });
195 ValuesBucket bucket;
196 bucket.Put("value1", value1);
197 bucket.Put("value2", value2);
198 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
199 EXPECT_EQ(status, E_OK);
200 auto resultSet = rdbStore_->QuerySql("select value1, value2 from bigint_table");
201 EXPECT_NE(resultSet, nullptr);
202 while (resultSet->GoToNextRow() == E_OK) {
203 ValueObject object;
204 status = resultSet->Get(0, object);
205 EXPECT_EQ(status, E_OK);
206 auto val = std::get_if<BigInteger>(&object.value);
207 EXPECT_NE(val, nullptr);
208 if (val != nullptr) {
209 EXPECT_TRUE(*val == value1);
210 }
211 status = resultSet->Get(1, object);
212 EXPECT_EQ(status, E_OK);
213 val = std::get_if<BigInteger>(&object.value);
214 EXPECT_NE(val, nullptr);
215 if (val != nullptr) {
216 EXPECT_TRUE(*val == value2);
217 }
218 }
219 }
220
221 /**
222 * @tc.name: Insert_Step_BigInt_INT128
223 * @tc.desc: test insert bigint to rdb store
224 * @tc.type: FUNC
225 */
226 HWTEST_F(RdbBigIntTest, Insert_Step_BigInt_INT128, TestSize.Level1)
227 {
228 int64_t outRowId = -1;
229 BigInteger value1 = BigInteger(0, std::vector<uint64_t>{ 158, 0xDEADDEADDEADDEAD });
230 BigInteger value2 = BigInteger(1, std::vector<uint64_t>{ 158, 0xDEADDEADDEADDEAD });
231 ValuesBucket bucket;
232 bucket.Put("value1", value1);
233 bucket.Put("value2", value2);
234 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
235 EXPECT_EQ(status, E_OK);
236 auto resultSet = rdbStore_->QueryByStep("select value1, value2 from bigint_table");
237 EXPECT_NE(resultSet, nullptr);
238 while (resultSet->GoToNextRow() == E_OK) {
239 RowEntity entity;
240 status = resultSet->GetRow(entity);
241 EXPECT_EQ(status, E_OK);
242 auto value = entity.Get("value1");
243 auto val = std::get_if<BigInteger>(&value.value);
244 EXPECT_NE(val, nullptr);
245 if (val != nullptr) {
246 EXPECT_TRUE(*val == value1);
247 }
248 value = entity.Get("value2");
249 val = std::get_if<BigInteger>(&value.value);
250 EXPECT_NE(val, nullptr);
251 if (val != nullptr) {
252 EXPECT_TRUE(*val == value2);
253 }
254 }
255 }
256
257 /**
258 * @tc.name: Insert_BigInt_INTRand
259 * @tc.desc: test insert bigint to rdb store
260 * @tc.type: FUNC
261 */
262 HWTEST_F(RdbBigIntTest, Insert_BigInt_INTRand, TestSize.Level1)
263 {
264 int64_t outRowId = -1;
265 std::vector<uint64_t> u64Val(2 + rand() % 100, 0);
266 for (int i = 0; i < u64Val.size(); ++i) {
267 uint64_t high = uint64_t(rand());
268 uint64_t low = uint64_t(rand());
269 u64Val[i] = (high << 32) | low;
270 }
271 BigInteger value1 = BigInteger(0, std::vector<uint64_t>(u64Val));
272 BigInteger value2 = BigInteger(1, std::vector<uint64_t>(u64Val));
273 ValuesBucket bucket;
274 bucket.Put("value1", value1);
275 bucket.Put("value2", value2);
276 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
277 EXPECT_EQ(status, E_OK);
278 auto resultSet = rdbStore_->QuerySql("select value1, value2 from bigint_table");
279 EXPECT_NE(resultSet, nullptr);
280 while (resultSet->GoToNextRow() == E_OK) {
281 RowEntity entity;
282 status = resultSet->GetRow(entity);
283 EXPECT_EQ(status, E_OK);
284 auto value = entity.Get("value1");
285 auto val = std::get_if<BigInteger>(&value.value);
286 EXPECT_NE(val, nullptr);
287 if (val != nullptr) {
288 EXPECT_TRUE(*val == value1);
289 }
290 value = entity.Get("value2");
291 val = std::get_if<BigInteger>(&value.value);
292 EXPECT_NE(val, nullptr);
293 if (val != nullptr) {
294 EXPECT_TRUE(*val == value2);
295 }
296 }
297 }
298
299 /**
300 * @tc.name: Insert_Step_BigInt_INTRand
301 * @tc.desc: test insert bigint to rdb store
302 * @tc.type: FUNC
303 */
304 HWTEST_F(RdbBigIntTest, Insert_Step_BigInt_INTRand, TestSize.Level1)
305 {
306 int64_t outRowId = -1;
307 std::vector<uint64_t> u64Val(2 + rand() % 100, 0);
308 for (int i = 0; i < u64Val.size(); ++i) {
309 uint64_t high = uint64_t(rand());
310 uint64_t low = uint64_t(rand());
311 u64Val[i] = (high << 32) | low;
312 }
313 BigInteger value1 = BigInteger(0, std::vector<uint64_t>(u64Val));
314 BigInteger value2 = BigInteger(1, std::vector<uint64_t>(u64Val));
315 ValuesBucket bucket;
316 bucket.Put("value1", value1);
317 bucket.Put("value2", value2);
318 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
319 EXPECT_EQ(status, E_OK);
320 auto resultSet = rdbStore_->QueryByStep("select value1, value2 from bigint_table");
321 EXPECT_NE(resultSet, nullptr);
322 while (resultSet->GoToNextRow() == E_OK) {
323 RowEntity entity;
324 status = resultSet->GetRow(entity);
325 EXPECT_EQ(status, E_OK);
326 auto value = entity.Get("value1");
327 auto val = std::get_if<BigInteger>(&value.value);
328 EXPECT_NE(val, nullptr);
329 if (val != nullptr) {
330 EXPECT_TRUE(*val == value1);
331 }
332 value = entity.Get("value2");
333 val = std::get_if<BigInteger>(&value.value);
334 EXPECT_NE(val, nullptr);
335 if (val != nullptr) {
336 EXPECT_TRUE(*val == value2);
337 }
338 }
339 }
340
341 /**
342 * @tc.name: Insert_Floats
343 * @tc.desc: test insert bigint to rdb store
344 * @tc.type: FUNC
345 */
346 HWTEST_F(RdbBigIntTest, Insert_Floats, TestSize.Level1)
347 {
348 int64_t outRowId = -1;
349 std::vector<uint64_t> u64Val(2 + rand() % 100, 0);
350 for (int i = 0; i < u64Val.size(); ++i) {
351 uint64_t high = uint64_t(rand());
352 uint64_t low = uint64_t(rand());
353 u64Val[i] = (high << 32) | low;
354 }
355 BigInteger value1 = BigInteger(0, std::vector<uint64_t>(u64Val));
356 BigInteger value2 = BigInteger(1, std::vector<uint64_t>(u64Val));
357 Floats value3 = { 0.1, 2.2, 3.3, 0.5 };
358 ValuesBucket bucket;
359 bucket.Put("value1", value1);
360 bucket.Put("value2", value2);
361 bucket.Put("value3", value3);
362 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
363 EXPECT_EQ(status, E_OK);
364 auto resultSet = rdbStore_->QuerySql("select * from bigint_table");
365 EXPECT_NE(resultSet, nullptr);
366 while (resultSet->GoToNextRow() == E_OK) {
367 RowEntity entity;
368 status = resultSet->GetRow(entity);
369 EXPECT_EQ(status, E_OK);
370 auto value = entity.Get("value3");
371 auto val = std::get_if<Floats>(&value.value);
372 EXPECT_NE(val, nullptr);
373 if (val != nullptr) {
374 EXPECT_EQ(*val, value3);
375 }
376 }
377 }
378
379 /**
380 * @tc.name: Insert_Step_Floats
381 * @tc.desc: test insert bigint to rdb store
382 * @tc.type: FUNC
383 */
384 HWTEST_F(RdbBigIntTest, Insert_Step_Floats, TestSize.Level1)
385 {
386 int64_t outRowId = -1;
387 std::vector<uint64_t> u64Val(2 + rand() % 100, 0);
388 for (int i = 0; i < u64Val.size(); ++i) {
389 uint64_t high = uint64_t(rand());
390 uint64_t low = uint64_t(rand());
391 u64Val[i] = (high << 32) | low;
392 }
393 BigInteger value1 = BigInteger(0, std::vector<uint64_t>(u64Val));
394 BigInteger value2 = BigInteger(1, std::vector<uint64_t>(u64Val));
395 Floats value3 = { 0.1, 2.2, 3.3, 0.5 };
396 ValuesBucket bucket;
397 bucket.Put("value1", value1);
398 bucket.Put("value2", value2);
399 bucket.Put("value3", value3);
400 auto status = rdbStore_->Insert(outRowId, "bigint_table", bucket);
401 EXPECT_EQ(status, E_OK);
402 auto resultSet = rdbStore_->QueryByStep("select * from bigint_table");
403 EXPECT_NE(resultSet, nullptr);
404 while (resultSet->GoToNextRow() == E_OK) {
405 RowEntity entity;
406 status = resultSet->GetRow(entity);
407 EXPECT_EQ(status, E_OK);
408 auto value = entity.Get("value3");
409 auto val = std::get_if<Floats>(&value.value);
410 EXPECT_NE(val, nullptr);
411 if (val != nullptr) {
412 EXPECT_EQ(*val, value3);
413 }
414 }
415 }
416 } // namespace Test
417