summaryrefslogtreecommitdiff
path: root/Homework/cs3460/array_perf/TestPerformance.cpp
blob: 302ab648b7f0349780ff1e9d8915d8ae549f38a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "sortutils.hpp"

#include "gtest/gtest.h"
#include <algorithm>
#include <random>

constexpr auto MIN_VALUE = -10'000'000;
constexpr auto MAX_VALUE = 10'000'000;

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

SourceArray generateRandomArray()
{
    std::default_random_engine engine{ 0u };
    std::uniform_int_distribution<> dist{ MIN_VALUE, MAX_VALUE };

    SourceArray array;
    std::generate(array.begin(), array.end(), [&] { return dist(engine); });
    return array;
}

TEST(OrganPipeArray, DoesGenerateCorrectly)
{
    auto sourceArray = generateRandomArray();
    std::sort(sourceArray.begin(), sourceArray.end());
    auto array = sourceArray;
    organPipeStdArray(array);

    for (auto i = 0ul; i < sourceArray.size() / 2; i++)
    {
        ASSERT_EQ(sourceArray[i], array[i]) << "Organ pipe changed first half of array at position " << i;
    }

    for (std::size_t first = 0ul, second = array.size() - 1; first < second; first++, second--)
    {
        ASSERT_EQ(array[first], array[second]) << "Organ pipe not symmetric at positions " << first << " and " << second;
    }
}

TEST(RawArray, DoesInitializeFromStdArrayCorrectly)
{
    const auto sourceArray = generateRandomArray();
    int dest[HOW_MANY_ELEMENTS];
    initializeRawArrayFromStdArray(sourceArray, dest);

    for (auto i = 0ul; i < sourceArray.size(); i++)
    {
        ASSERT_EQ(sourceArray[i], dest[i]) << "Raw array differs from source array at position " << i;
    }
}