2019-08-12 13:45:57 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2019-08-12 13:45:57 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/port/thread.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
#include "td/utils/SharedSlice.h"
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/tests.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
|
2019-11-17 21:42:48 +01:00
|
|
|
char disable_linker_warning_about_empty_file_tdutils_test_shared_slice_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
2019-08-12 13:45:57 +02:00
|
|
|
TEST(SharedSlice, Hands) {
|
|
|
|
{
|
2019-08-13 22:52:54 +02:00
|
|
|
td::SharedSlice h("hello");
|
2019-08-12 13:45:57 +02:00
|
|
|
ASSERT_EQ("hello", h.as_slice());
|
|
|
|
// auto g = h; // CE
|
|
|
|
auto g = h.clone();
|
2019-08-13 22:52:54 +02:00
|
|
|
ASSERT_EQ("hello", h.as_slice());
|
2019-08-12 13:45:57 +02:00
|
|
|
ASSERT_EQ("hello", g.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-13 22:52:54 +02:00
|
|
|
td::SharedSlice h("hello");
|
|
|
|
td::UniqueSharedSlice g(std::move(h));
|
2019-08-12 13:45:57 +02:00
|
|
|
ASSERT_EQ("", h.as_slice());
|
|
|
|
ASSERT_EQ("hello", g.as_slice());
|
|
|
|
}
|
|
|
|
{
|
2019-08-13 22:52:54 +02:00
|
|
|
td::SharedSlice h("hello");
|
|
|
|
td::SharedSlice t = h.clone();
|
|
|
|
td::UniqueSharedSlice g(std::move(h));
|
2019-08-12 13:45:57 +02:00
|
|
|
ASSERT_EQ("", h.as_slice());
|
|
|
|
ASSERT_EQ("hello", g.as_slice());
|
|
|
|
ASSERT_EQ("hello", t.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-13 22:52:54 +02:00
|
|
|
td::UniqueSharedSlice g(5);
|
2019-08-12 13:45:57 +02:00
|
|
|
g.as_mutable_slice().copy_from("hello");
|
2019-08-13 22:52:54 +02:00
|
|
|
td::SharedSlice h(std::move(g));
|
2019-08-12 13:45:57 +02:00
|
|
|
ASSERT_EQ("hello", h);
|
|
|
|
ASSERT_EQ("", g);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-13 22:52:54 +02:00
|
|
|
td::UniqueSlice h("hello");
|
|
|
|
td::UniqueSlice g(std::move(h));
|
2019-08-12 13:45:57 +02:00
|
|
|
ASSERT_EQ("", h.as_slice());
|
|
|
|
ASSERT_EQ("hello", g.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-13 22:52:54 +02:00
|
|
|
td::SecureString h("hello");
|
|
|
|
td::SecureString g(std::move(h));
|
2019-08-12 13:45:57 +02:00
|
|
|
ASSERT_EQ("", h.as_slice());
|
|
|
|
ASSERT_EQ("hello", g.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-13 22:52:54 +02:00
|
|
|
td::Stage stage;
|
2021-10-21 11:51:16 +02:00
|
|
|
td::SharedSlice a;
|
|
|
|
td::SharedSlice b;
|
2019-08-13 22:52:54 +02:00
|
|
|
td::vector<td::thread> threads(2);
|
2019-08-12 13:45:57 +02: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) {
|
2019-08-13 22:52:54 +02:00
|
|
|
a = td::SharedSlice("hello");
|
2019-08-12 13:45:57 +02:00
|
|
|
b = a.clone();
|
|
|
|
}
|
|
|
|
stage.wait((2 * j + 1) * 2);
|
|
|
|
if (i == 0) {
|
|
|
|
ASSERT_EQ('h', a[0]);
|
|
|
|
a.clear();
|
|
|
|
} else {
|
2019-08-13 22:52:54 +02:00
|
|
|
td::UniqueSharedSlice c(std::move(b));
|
2019-08-12 13:45:57 +02:00
|
|
|
c.as_mutable_slice()[0] = '!';
|
|
|
|
}
|
|
|
|
stage.wait((2 * j + 2) * 2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 21:42:48 +01:00
|
|
|
#endif
|