ParallelTest.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //===- lld/unittest/ParallelTest.cpp --------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief Parallel.h unit tests.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "gtest/gtest.h"
  15. #include "lld/Core/Parallel.h"
  16. #include <array>
  17. #include <random>
  18. uint32_t array[1024 * 1024];
  19. TEST(Parallel, sort) {
  20. std::mt19937 randEngine;
  21. std::uniform_int_distribution<uint32_t> dist;
  22. for (auto &i : array)
  23. i = dist(randEngine);
  24. sort(lld::parallel::par, std::begin(array), std::end(array));
  25. ASSERT_TRUE(std::is_sorted(std::begin(array), std::end(array)));
  26. }
  27. TEST(Parallel, parallel_for) {
  28. // We need to test the case with a TaskSize > 1. We are white-box testing
  29. // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
  30. // writing.
  31. uint32_t range[2050];
  32. std::fill(range, range + 2050, 1);
  33. for_each_n(lld::parallel::par, 0, 2049, [&range](size_t I) { ++range[I]; });
  34. uint32_t expected[2049];
  35. std::fill(expected, expected + 2049, 2);
  36. ASSERT_TRUE(std::equal(range, range + 2049, expected));
  37. // Check that we don't write past the end of the requested range.
  38. ASSERT_EQ(range[2049], 1u);
  39. }