1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-13 03:32:22 +00:00
ton/tdutils/test/SharedSlice.cpp

104 lines
2.8 KiB
C++
Raw Normal View History

2019-09-07 10:03:22 +00:00
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
2020-04-10 19:06:01 +00:00
Copyright 2017-2020 Telegram Systems LLP
2019-09-07 10:03:22 +00:00
*/
2020-07-06 14:07:20 +00:00
#include "td/utils/common.h"
#include "td/utils/port/thread.h"
2019-09-07 10:03:22 +00:00
#include "td/utils/SharedSlice.h"
2020-07-06 14:07:20 +00:00
#include "td/utils/tests.h"
2019-09-07 10:03:22 +00:00
2020-07-06 14:07:20 +00:00
char disable_linker_warning_about_empty_file_tdutils_test_shared_slice_cpp TD_UNUSED;
2019-09-07 10:03:22 +00:00
2020-07-06 14:07:20 +00:00
#if !TD_THREAD_UNSUPPORTED
2019-09-07 10:03:22 +00:00
TEST(SharedSlice, Hands) {
{
2020-07-06 14:07:20 +00:00
td::SharedSlice h("hello");
2019-09-07 10:03:22 +00:00
ASSERT_EQ("hello", h.as_slice());
// auto g = h; // CE
auto g = h.clone();
2020-07-06 14:07:20 +00:00
ASSERT_EQ("hello", h.as_slice());
2019-09-07 10:03:22 +00:00
ASSERT_EQ("hello", g.as_slice());
}
{
2020-07-06 14:07:20 +00:00
td::SharedSlice h("hello");
td::UniqueSharedSlice g(std::move(h));
2019-09-07 10:03:22 +00:00
ASSERT_EQ("", h.as_slice());
ASSERT_EQ("hello", g.as_slice());
}
{
2020-07-06 14:07:20 +00:00
td::SharedSlice h("hello");
td::SharedSlice t = h.clone();
td::UniqueSharedSlice g(std::move(h));
2019-09-07 10:03:22 +00:00
ASSERT_EQ("", h.as_slice());
ASSERT_EQ("hello", g.as_slice());
ASSERT_EQ("hello", t.as_slice());
}
{
2020-07-06 14:07:20 +00:00
td::UniqueSharedSlice g(5);
2019-09-07 10:03:22 +00:00
g.as_mutable_slice().copy_from("hello");
2020-07-06 14:07:20 +00:00
td::SharedSlice h(std::move(g));
2019-09-07 10:03:22 +00:00
ASSERT_EQ("hello", h);
ASSERT_EQ("", g);
}
{
2020-07-06 14:07:20 +00:00
td::UniqueSlice h("hello");
td::UniqueSlice g(std::move(h));
2019-09-07 10:03:22 +00:00
ASSERT_EQ("", h.as_slice());
ASSERT_EQ("hello", g.as_slice());
}
{
2020-07-06 14:07:20 +00:00
td::SecureString h("hello");
td::SecureString g(std::move(h));
2019-09-07 10:03:22 +00:00
ASSERT_EQ("", h.as_slice());
ASSERT_EQ("hello", g.as_slice());
}
{
2020-07-06 14:07:20 +00:00
td::Stage stage;
td::SharedSlice a, b;
td::vector<td::thread> threads(2);
2019-09-07 10:03:22 +00:00
for (int i = 0; i < 2; i++) {
threads[i] = td::thread([i, &stage, &a, &b] {
for (int j = 0; j < 10000; j++) {
if (i == 0) {
2020-07-06 14:07:20 +00:00
a = td::SharedSlice("hello");
2019-09-07 10:03:22 +00:00
b = a.clone();
}
stage.wait((2 * j + 1) * 2);
if (i == 0) {
ASSERT_EQ('h', a[0]);
a.clear();
} else {
2020-07-06 14:07:20 +00:00
td::UniqueSharedSlice c(std::move(b));
2019-09-07 10:03:22 +00:00
c.as_mutable_slice()[0] = '!';
}
stage.wait((2 * j + 2) * 2);
}
});
}
for (auto &thread : threads) {
thread.join();
}
}
}
2020-07-06 14:07:20 +00:00
#endif