Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/ParallelTest.cpp -----------------------------===// |
Michael J. Spencer | 1eb3b89 | 2013-04-12 18:40:39 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Parallel.h" |
Michael J. Spencer | 1eb3b89 | 2013-04-12 18:40:39 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
Michael J. Spencer | 1eb3b89 | 2013-04-12 18:40:39 +0000 | [diff] [blame] | 17 | #include <array> |
| 18 | #include <random> |
| 19 | |
| 20 | uint32_t array[1024 * 1024]; |
| 21 | |
Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
NAKAMURA Takumi | cf84800 | 2017-05-11 06:35:51 +0000 | [diff] [blame] | 24 | // Tests below are hanging up on mingw. Investigating. |
| 25 | #if !defined(__MINGW32__) |
| 26 | |
Michael J. Spencer | 1eb3b89 | 2013-04-12 18:40:39 +0000 | [diff] [blame] | 27 | TEST(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 Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 34 | sort(parallel::par, std::begin(array), std::end(array)); |
Michael J. Spencer | 1eb3b89 | 2013-04-12 18:40:39 +0000 | [diff] [blame] | 35 | ASSERT_TRUE(std::is_sorted(std::begin(array), std::end(array))); |
| 36 | } |
James Henderson | 8abda20 | 2017-04-07 08:11:28 +0000 | [diff] [blame] | 37 | |
| 38 | TEST(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 Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 44 | for_each_n(parallel::par, 0, 2049, [&range](size_t I) { ++range[I]; }); |
James Henderson | 8abda20 | 2017-04-07 08:11:28 +0000 | [diff] [blame] | 45 | |
| 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 Henderson | db664b6 | 2017-04-07 08:48:17 +0000 | [diff] [blame] | 50 | ASSERT_EQ(range[2049], 1u); |
James Henderson | 8abda20 | 2017-04-07 08:11:28 +0000 | [diff] [blame] | 51 | } |
NAKAMURA Takumi | cf84800 | 2017-05-11 06:35:51 +0000 | [diff] [blame] | 52 | |
| 53 | #endif |