OSDN Git Service

[Support] Move Parallel algorithms from LLD to LLVM.
[android-x86/external-llvm.git] / unittests / Support / ParallelTest.cpp
1 //===- llvm/unittest/Support/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
15 #include "llvm/Support/Parallel.h"
16 #include "gtest/gtest.h"
17 #include <array>
18 #include <random>
19
20 uint32_t array[1024 * 1024];
21
22 using namespace llvm;
23
24 TEST(Parallel, sort) {
25   std::mt19937 randEngine;
26   std::uniform_int_distribution<uint32_t> dist;
27
28   for (auto &i : array)
29     i = dist(randEngine);
30
31   sort(parallel::par, std::begin(array), std::end(array));
32   ASSERT_TRUE(std::is_sorted(std::begin(array), std::end(array)));
33 }
34
35 TEST(Parallel, parallel_for) {
36   // We need to test the case with a TaskSize > 1. We are white-box testing
37   // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
38   // writing.
39   uint32_t range[2050];
40   std::fill(range, range + 2050, 1);
41   for_each_n(parallel::par, 0, 2049, [&range](size_t I) { ++range[I]; });
42
43   uint32_t expected[2049];
44   std::fill(expected, expected + 2049, 2);
45   ASSERT_TRUE(std::equal(range, range + 2049, expected));
46   // Check that we don't write past the end of the requested range.
47   ASSERT_EQ(range[2049], 1u);
48 }