blob: d734e0dd8586afbc7fec1e7b5cf22ac0b47ee8b8 [file] [log] [blame]
Zachary Turner3a57fbd2017-05-11 00:03:52 +00001//===- llvm/unittest/Support/ParallelTest.cpp -----------------------------===//
Michael J. Spencer1eb3b892013-04-12 18:40:39 +00002//
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
Zachary Turner3a57fbd2017-05-11 00:03:52 +000015#include "llvm/Support/Parallel.h"
Michael J. Spencer1eb3b892013-04-12 18:40:39 +000016#include "gtest/gtest.h"
Michael J. Spencer1eb3b892013-04-12 18:40:39 +000017#include <array>
18#include <random>
19
20uint32_t array[1024 * 1024];
21
Zachary Turner3a57fbd2017-05-11 00:03:52 +000022using namespace llvm;
23
NAKAMURA Takumicf848002017-05-11 06:35:51 +000024// Tests below are hanging up on mingw. Investigating.
25#if !defined(__MINGW32__)
26
Michael J. Spencer1eb3b892013-04-12 18:40:39 +000027TEST(Parallel, sort) {
28 std::mt19937 randEngine;
29 std::uniform_int_distribution<uint32_t> dist;
30
31 for (auto &i : array)
32 i = dist(randEngine);
33
Zachary Turner3a57fbd2017-05-11 00:03:52 +000034 sort(parallel::par, std::begin(array), std::end(array));
Michael J. Spencer1eb3b892013-04-12 18:40:39 +000035 ASSERT_TRUE(std::is_sorted(std::begin(array), std::end(array)));
36}
James Henderson8abda202017-04-07 08:11:28 +000037
38TEST(Parallel, parallel_for) {
39 // We need to test the case with a TaskSize > 1. We are white-box testing
40 // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
41 // writing.
42 uint32_t range[2050];
43 std::fill(range, range + 2050, 1);
Zachary Turner3a57fbd2017-05-11 00:03:52 +000044 for_each_n(parallel::par, 0, 2049, [&range](size_t I) { ++range[I]; });
James Henderson8abda202017-04-07 08:11:28 +000045
46 uint32_t expected[2049];
47 std::fill(expected, expected + 2049, 2);
48 ASSERT_TRUE(std::equal(range, range + 2049, expected));
49 // Check that we don't write past the end of the requested range.
James Hendersondb664b62017-04-07 08:48:17 +000050 ASSERT_EQ(range[2049], 1u);
James Henderson8abda202017-04-07 08:11:28 +000051}
NAKAMURA Takumicf848002017-05-11 06:35:51 +000052
53#endif