TeaSpeakLibrary/test/ChannelTest.cpp

121 lines
3.2 KiB
C++
Raw Normal View History

2019-06-26 16:11:22 -04:00
#include <src/query/Command.h>
#include <src/BasicChannel.h>
#include <ThreadPool/Thread.h>
#include "channel/TreeView.h"
using namespace std;
using namespace std::chrono;
using namespace ts;
struct TEntry : public ts::TreeEntry {
2020-01-23 20:49:59 -05:00
public:
2019-06-26 16:11:22 -04:00
TEntry(ChannelId channel_id) : channel_id(channel_id), previous_id(0) {}
ChannelId channelId() const override {
return channel_id;
}
ChannelId previousChannelId() const override {
return previous_id;
}
void setParentChannelId(ChannelId id) override {
}
void setPreviousChannelId(ChannelId id) override {
previous_id = id;
}
bool deleted() const override {
return _deleted;
}
void set_deleted(bool b) override {
_deleted = b;
}
bool _deleted = false;
ChannelId channel_id;
ChannelId previous_id;
};
void tree_print_entry(const std::shared_ptr<TreeEntry>& t_entry, int deep) {
auto entry = dynamic_pointer_cast<TEntry>(t_entry);
assert(entry);
string prefix;
while(deep-- > 0) prefix += " ";
cout << prefix << "- " << entry->channel_id << endl;
}
#define PT \
cout << " --------- TREE --------- " << endl; \
tree.print_tree(tree_print_entry); \
cout << " --------- TREE --------- " << endl;
template <typename T>
void print_address(const T& idx) {
2020-01-23 20:49:59 -05:00
cout << &idx << endl;
[idx]() {
cout << &idx << endl;
}();
2019-06-26 16:11:22 -04:00
}
int main() {
auto index = shared_ptr<int>();
print_address(index);
return 0;
2020-01-23 20:49:59 -05:00
/*
BasicChannelTree tree;
auto ch3 = tree.createChannel(0, 0, "test channel");
auto ch2 = tree.createChannel(0, 0, "test channel2");
auto ch = tree.createChannel(ch2->channelId(), 0, "test channel 2");
tree.deleteChannelRoot(ch2);
ch2 = nullptr;
ch = nullptr;
threads::self::sleep_for(seconds(1));
cout << "XX" << endl;
*/
2019-06-26 16:11:22 -04:00
ChannelId channel_id_index = 0;
2020-01-23 20:49:59 -05:00
TreeView tree;
2019-06-26 16:11:22 -04:00
2020-01-23 20:49:59 -05:00
/* Create 10 channels */
while(channel_id_index < 20)
2019-06-26 16:11:22 -04:00
assert(tree.insert_entry(make_shared<TEntry>(channel_id_index++)));
PT
/* Test order id */
for(int i = 0; i < 10000; i++) { //Random test
auto channel_id = tree.find_entry(rand() % channel_id_index);
auto channel_target = tree.find_entry(rand() % channel_id_index);
assert(channel_id);
assert(channel_target);
printf("Move channel %lu after %lu => ", channel_id->channelId(), channel_target->channelId());
printf("%x\n", tree.move_entry(channel_id, nullptr, channel_target));
}
PT
/* Test move */
for(int i = 0; i < 10002; i++) { //Random test
auto channel_id = tree.find_entry(rand() % channel_id_index);
auto channel_target = tree.find_entry(rand() % channel_id_index);
printf("Move channel parent of %lu to %lu => ", channel_id->channelId(), channel_target->channelId());
printf("%x\n", tree.move_entry(channel_id, channel_target));
}
PT
memtrack::statistics();
for(int i = 0; i < 20; i++) {
tree.delete_entry(tree.find_entry(rand() % channel_id_index));
PT
memtrack::statistics();
}
2020-01-23 20:49:59 -05:00
return 0;
2019-06-26 16:11:22 -04:00
}