ParallelTest.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===- llvm/unittest/Support/ParallelTest.cpp -----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// Parallel.h unit tests.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/Parallel.h"
  14. #include "gtest/gtest.h"
  15. #include <array>
  16. #include <random>
  17. uint32_t array[1024 * 1024];
  18. using namespace llvm;
  19. // Tests below are hanging up on mingw. Investigating.
  20. #if !defined(__MINGW32__)
  21. TEST(Parallel, sort) {
  22. std::mt19937 randEngine;
  23. std::uniform_int_distribution<uint32_t> dist;
  24. for (auto &i : array)
  25. i = dist(randEngine);
  26. sort(parallel::par, std::begin(array), std::end(array));
  27. ASSERT_TRUE(std::is_sorted(std::begin(array), std::end(array)));
  28. }
  29. TEST(Parallel, parallel_for) {
  30. // We need to test the case with a TaskSize > 1. We are white-box testing
  31. // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
  32. // writing.
  33. uint32_t range[2050];
  34. std::fill(range, range + 2050, 1);
  35. for_each_n(parallel::par, 0, 2049, [&range](size_t I) { ++range[I]; });
  36. uint32_t expected[2049];
  37. std::fill(expected, expected + 2049, 2);
  38. ASSERT_TRUE(std::equal(range, range + 2049, expected));
  39. // Check that we don't write past the end of the requested range.
  40. ASSERT_EQ(range[2049], 1u);
  41. }
  42. #endif