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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
/*
* Copyright 2019 Patrick Stotko
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <limits>
#include <random>
#include <unordered_set>
#include <stdgpu/functional.h>
#include <test_utils.h>
class stdgpu_functional : public ::testing::Test
{
protected:
// Called before each test
void
SetUp() override
{
}
// Called after each test
void
TearDown() override
{
}
};
// Explicit template instantiations
namespace stdgpu
{
// Instantiation of specialized templates emit no-effect warnings with Clang
/*
template
struct hash<bool>;
template
struct hash<char>;
template
struct hash<signed char>;
template
struct hash<unsigned char>;
template
struct hash<wchar_t>;
template
struct hash<char16_t>;
template
struct hash<char32_t>;
template
struct hash<short>;
template
struct hash<unsigned short>;
template
struct hash<int>;
template
struct hash<unsigned int>;
template
struct hash<long>;
template
struct hash<unsigned long>;
template
struct hash<long long>;
template
struct hash<unsigned long long>;
template
struct hash<float>;
template
struct hash<double>;
template
struct hash<long double>;
*/
template struct plus<int>;
template struct logical_and<int>;
template struct equal_to<int>;
template struct bit_not<unsigned int>;
} // namespace stdgpu
template <typename T>
void
hash_check_integer()
{
stdgpu::index_t N = static_cast<stdgpu::index_t>(std::numeric_limits<T>::max()) -
static_cast<stdgpu::index_t>(std::numeric_limits<T>::lowest());
std::unordered_set<std::size_t> hashes;
hashes.reserve(static_cast<std::size_t>(N));
stdgpu::hash<T> hasher;
for (T i = std::numeric_limits<T>::lowest(); i < std::numeric_limits<T>::max(); ++i)
{
hashes.insert(hasher(i));
}
EXPECT_GT(static_cast<stdgpu::index_t>(hashes.size()), N * 90 / 100);
}
TEST_F(stdgpu_functional, hash_char)
{
hash_check_integer<char>();
}
TEST_F(stdgpu_functional, hash_signed_char)
{
hash_check_integer<signed char>();
}
TEST_F(stdgpu_functional, hash_unsigned_char)
{
hash_check_integer<unsigned char>();
}
TEST_F(stdgpu_functional, short)
{
hash_check_integer<short>();
}
TEST_F(stdgpu_functional, hash_unsigned_short)
{
hash_check_integer<unsigned short>();
}
template <typename T>
void
hash_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
std::unordered_set<std::size_t> hashes;
hashes.reserve(static_cast<std::size_t>(N));
stdgpu::hash<T> hasher;
for (stdgpu::index_t i = 0; i < N; ++i)
{
hashes.insert(hasher(dist(rng)));
}
EXPECT_GT(static_cast<stdgpu::index_t>(hashes.size()), N * 90 / 100);
}
TEST_F(stdgpu_functional, hash_int)
{
hash_check_integer_random<int>();
}
TEST_F(stdgpu_functional, hash_unsigned_int)
{
hash_check_integer_random<unsigned int>();
}
TEST_F(stdgpu_functional, hash_long)
{
hash_check_integer_random<long>();
}
TEST_F(stdgpu_functional, hash_unsigned_long)
{
hash_check_integer_random<unsigned long>();
}
TEST_F(stdgpu_functional, hash_long_long)
{
hash_check_integer_random<long long>();
}
TEST_F(stdgpu_functional, hash_unsigned_long_long)
{
hash_check_integer_random<unsigned long long>();
}
template <typename T>
void
hash_check_floating_point_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
const T bound = static_cast<T>(1e38);
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_real_distribution<T> dist(-bound, bound);
std::unordered_set<std::size_t> hashes;
hashes.reserve(static_cast<std::size_t>(N));
stdgpu::hash<T> hasher;
for (stdgpu::index_t i = 0; i < N; ++i)
{
hashes.insert(hasher(dist(rng)));
}
EXPECT_GT(static_cast<stdgpu::index_t>(hashes.size()), N * 90 / 100);
}
TEST_F(stdgpu_functional, hash_float)
{
hash_check_floating_point_random<float>();
}
TEST_F(stdgpu_functional, hash_double)
{
hash_check_floating_point_random<double>();
}
TEST_F(stdgpu_functional, hash_long_double)
{
hash_check_floating_point_random<long double>();
}
enum old_enum
{
zero = 0,
one = 1,
two = 2,
three = 3
};
// cppcheck-suppress syntaxError
TEST_F(stdgpu_functional, hash_enum)
{
std::unordered_set<std::size_t> hashes;
hashes.reserve(4);
stdgpu::hash<old_enum> hasher;
hashes.insert(hasher(zero));
hashes.insert(hasher(one));
hashes.insert(hasher(two));
hashes.insert(hasher(three));
EXPECT_GT(static_cast<stdgpu::index_t>(hashes.size()), 4 * 90 / 100);
}
enum class scoped_enum
{
zero = 0,
one = 1,
two = 2,
three = 3
};
TEST_F(stdgpu_functional, hash_enum_class)
{
std::unordered_set<std::size_t> hashes;
hashes.reserve(4);
stdgpu::hash<scoped_enum> hasher;
hashes.insert(hasher(scoped_enum::zero));
hashes.insert(hasher(scoped_enum::one));
hashes.insert(hasher(scoped_enum::two));
hashes.insert(hasher(scoped_enum::three));
EXPECT_GT(static_cast<stdgpu::index_t>(hashes.size()), 4 * 90 / 100);
}
template <typename T>
void
identity_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::identity identity_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value = dist(rng);
EXPECT_EQ(identity_function(value), value);
}
}
TEST_F(stdgpu_functional, identity)
{
identity_check_integer_random<int>();
}
template <typename T>
void
plus_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::plus<T> plus_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value_1 = dist(rng);
T value_2 = dist(rng);
EXPECT_EQ(plus_function(value_1, value_2), value_1 + value_2);
}
}
TEST_F(stdgpu_functional, plus_int)
{
plus_check_integer_random<int>();
}
template <typename T, typename U>
void
plus_transparent_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::plus<> plus_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value_1 = dist(rng);
U value_2 = static_cast<U>(dist(rng));
EXPECT_EQ(plus_function(value_1, value_2), value_1 + value_2);
}
}
TEST_F(stdgpu_functional, plus_transparent)
{
plus_transparent_check_integer_random<int, long>();
}
template <typename T>
void
logical_and_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::logical_and<T> logical_and_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value_1 = dist(rng);
T value_2 = dist(rng);
EXPECT_EQ(logical_and_function(value_1, value_2), value_1 && value_2);
}
}
TEST_F(stdgpu_functional, logical_and_int)
{
logical_and_check_integer_random<int>();
}
template <typename T, typename U>
void
logical_and_transparent_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::logical_and<> logical_and_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value_1 = dist(rng);
U value_2 = static_cast<U>(dist(rng));
EXPECT_EQ(logical_and_function(value_1, value_2), value_1 && value_2);
}
}
TEST_F(stdgpu_functional, logical_and_transparent)
{
logical_and_transparent_check_integer_random<int, long>();
}
template <typename T>
void
equal_to_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::equal_to<T> equal_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value = dist(rng);
EXPECT_TRUE(equal_function(value, value));
}
}
TEST_F(stdgpu_functional, equal_to_int)
{
equal_to_check_integer_random<int>();
}
template <typename T, typename U>
void
equal_to_transparent_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::equal_to<> equal_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value_1 = dist(rng);
U value_2 = static_cast<U>(value_1);
EXPECT_TRUE(equal_function(value_1, value_2));
}
}
TEST_F(stdgpu_functional, equal_to_transparent)
{
equal_to_transparent_check_integer_random<int, long>();
}
template <typename T>
void
bit_not_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::bit_not<T> bit_not_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value = dist(rng);
EXPECT_EQ(bit_not_function(value), ~value);
}
}
TEST_F(stdgpu_functional, bit_not_unsigned_int)
{
bit_not_check_integer_random<unsigned int>();
}
template <typename T>
void
bit_not_transparent_check_integer_random()
{
const stdgpu::index_t N = 1000000;
// Generate true random numbers
std::size_t seed = test_utils::random_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<T> dist(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
stdgpu::bit_not<> bit_not_function;
for (stdgpu::index_t i = 0; i < N; ++i)
{
T value = dist(rng);
EXPECT_EQ(bit_not_function(value), ~value);
}
}
TEST_F(stdgpu_functional, bit_not_transparent)
{
bit_not_transparent_check_integer_random<unsigned int>();
}
|