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
|
/*!
@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause
*/
#include <gtest/gtest.h>
#include <pyclustering/interface/pyclustering_package.hpp>
#include <cstring>
#include <memory>
#include <vector>
template <class TypeContainer>
static void template_pyclustering_package(const TypeContainer & container) {
using container_data_t = typename TypeContainer::value_type;
pyclustering_package * package = create_package(&container);
ASSERT_EQ(container.size(), package->size);
if (package->type != PYCLUSTERING_TYPE_LIST) {
for (std::size_t index = 0; index < container.size(); index++) {
ASSERT_EQ(container[index], package->at<container_data_t>(index));
}
}
delete package;
}
TEST(utest_pyclustering, package_integer) {
std::vector<int> container = { 1, 2, 3, 4, 5 };
template_pyclustering_package(container);
}
TEST(utest_pyclustering, package_double) {
std::vector<double> container = { 1.0, 1.5, 2.0, 2.5, 3.0 };
template_pyclustering_package(container);
}
TEST(utest_pyclustering, package_unsigned) {
std::vector<unsigned int> container = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
template_pyclustering_package(container);
}
TEST(utest_pyclustering, package_float) {
std::vector<float> container = { -1.0, -0.5, 0.0, 0.5, 1.0 };
template_pyclustering_package(container);
}
TEST(utest_pyclustering, package_size_t) {
std::vector<std::size_t> container = { 1, 2, 3, 4, 5, 6, 7 };
template_pyclustering_package(container);
}
TEST(utest_pyclustering, package_empty) {
std::vector<int> container = { };
template_pyclustering_package(container);
}
TEST(utest_pyclustering, package_list) {
std::vector<std::vector<int>> container = { { 1, 2 }, { 1, 2, 3, 4 } };
template_pyclustering_package(container);
}
TEST(utest_pyclustering, package_pointer_list) {
std::vector<std::vector<int> *> container;
container.push_back(new std::vector<int>({ 1, 2, 3, 4, 5}));
template_pyclustering_package(container);
delete container[0];
}
TEST(utest_pyclustering, package_char_message) {
const char * message = "message char";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(std::strlen(message) + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_CHAR, package->type);
ASSERT_STREQ(message, (const char *)package->data);
}
TEST(utest_pyclustering, package_empty_char_message) {
const char * message = "";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(std::strlen(message) + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_CHAR, package->type);
ASSERT_STREQ(message, (const char *)package->data);
}
TEST(utest_pyclustering, package_wchar_message) {
const wchar_t * message = L"message wchar_t";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(std::wcslen(message) + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_WCHAR_T, package->type);
ASSERT_EQ(0, std::wcscmp(message, (const wchar_t *)package->data));
}
TEST(utest_pyclustering, package_empty_wchar_message) {
const wchar_t * message = L"";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(std::wcslen(message) + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_WCHAR_T, package->type);
ASSERT_EQ(0, std::wcscmp(message, (const wchar_t *)package->data));
}
TEST(utest_pyclustering, package_string_message) {
std::string message = "message string";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(message.size() + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_CHAR, package->type);
ASSERT_STREQ(message.c_str(), (const char *)package->data);
}
TEST(utest_pyclustering, package_empty_string_message) {
std::string message = "";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(message.size() + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_CHAR, package->type);
ASSERT_STREQ(message.c_str(), (const char *)package->data);
}
TEST(utest_pyclustering, package_wstring_message) {
std::wstring message = L"message wstring";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(message.size() + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_WCHAR_T, package->type);
ASSERT_EQ(0, std::wcscmp(message.c_str(), (const wchar_t *)package->data));
}
TEST(utest_pyclustering, package_empty_wstring_message) {
std::wstring message = L"";
std::shared_ptr<pyclustering_package> package = std::shared_ptr<pyclustering_package>(create_package(message));
ASSERT_NE(nullptr, package);
ASSERT_EQ(message.size() + 1, package->size);
ASSERT_EQ(PYCLUSTERING_TYPE_WCHAR_T, package->type);
ASSERT_EQ(0, std::wcscmp(message.c_str(), (const wchar_t *)package->data));
}
template <class TypeContainer>
static void template_two_dimension_pyclustering_package(const TypeContainer & container) {
using sub_container_t = typename TypeContainer::value_type;
using container_data_t = typename sub_container_t::value_type;
pyclustering_package * package = create_package(&container);
ASSERT_EQ(container.size(), package->size);
for (std::size_t i = 0; i < container.size(); i++) {
pyclustering_package * sub_package = package->at<pyclustering_package *>(i);
ASSERT_EQ(container[i].size(), sub_package->size);
for (std::size_t j = 0; j < container[i].size(); j++) {
ASSERT_EQ(container[i][j], package->at<container_data_t>(i, j));
}
}
delete package;
}
TEST(utest_pyclustering, package_two_dimension_integer) {
std::vector<std::vector<int>> container = { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 } };
template_two_dimension_pyclustering_package(container);
}
TEST(utest_pyclustering, package_two_dimension_double) {
std::vector<std::vector<double>> container = { { 1.0, 2.5, 3.0 }, { 4.5, 5.5 }, { 6.1, 7.2, 8.3, 9.4 } };
template_two_dimension_pyclustering_package(container);
}
TEST(utest_pyclustering, package_two_dimension_empty) {
std::vector<std::vector<long>> container = { { }, { 4, 5 }, { }, { 6 } };
template_two_dimension_pyclustering_package(container);
}
template <class Container>
static void template_pack_unpack(const Container & container) {
pyclustering_package * package = create_package(&container);
Container unpack_container;
package->extract(unpack_container);
ASSERT_EQ(container, unpack_container);
delete package;
}
TEST(utest_pyclustering, package_unpack_int) {
template_pack_unpack(std::vector<int>({ 1, 2, 3, 4 }));
}
TEST(utest_pyclustering, package_unpack_long) {
template_pack_unpack(std::vector<long>({ 1, 2, 3, 4, 5, 6 }));
}
TEST(utest_pyclustering, package_unpack_empty) {
template_pack_unpack(std::vector<float>({ }));
}
TEST(utest_pyclustering, package_unpack_two_dimension) {
template_pack_unpack(std::vector<std::vector<double>>({ { 1.2, 2.4 }, { 3.6, 4.8, 5.0 }, { 6.0 } }));
}
|