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 
14 #include "llvm/Support/Parallel.h"
15 #include "gtest/gtest.h"
16 #include <array>
17 #include <random>
18 
19 uint32_t array[1024 * 1024];
20 
21 using namespace llvm;
22 
23 // Tests below are hanging up on mingw. Investigating.
24 #if !defined(__MINGW32__)
25 
26 TEST(Parallel, sort) {
27   std::mt19937 randEngine;
28   std::uniform_int_distribution<uint32_t> dist;
29 
30   for (auto &i : array)
31     i = dist(randEngine);
32 
33   parallelSort(std::begin(array), std::end(array));
34   ASSERT_TRUE(llvm::is_sorted(array));
35 }
36 
37 TEST(Parallel, parallel_for) {
38   // We need to test the case with a TaskSize > 1. We are white-box testing
39   // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
40   // writing.
41   uint32_t range[2050];
42   std::fill(range, range + 2050, 1);
43   parallelForEachN(0, 2049, [&range](size_t I) { ++range[I]; });
44 
45   uint32_t expected[2049];
46   std::fill(expected, expected + 2049, 2);
47   ASSERT_TRUE(std::equal(range, range + 2049, expected));
48   // Check that we don't write past the end of the requested range.
49   ASSERT_EQ(range[2049], 1u);
50 }
51 
52 #endif
53