-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing_number.cpp
More file actions
45 lines (38 loc) · 1.24 KB
/
Copy pathmissing_number.cpp
File metadata and controls
45 lines (38 loc) · 1.24 KB
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
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
#include <random>
std::size_t sum_of_n(std::size_t n) {
return n * (n + 1) / 2;
}
size_t missing_number(auto const& numbers) {
size_t expected_sum = sum_of_n(numbers.size());
size_t current_sum = std::accumulate(numbers.begin(), numbers.end(), 0zu);
return expected_sum - current_sum;
}
namespace {
const size_t size = 100;
std::array<int, size> numbers;
std::mt19937 gen{std::random_device{}()};
std::uniform_int_distribution<size_t> distributor(0, size - 1);
} // namespace
TEST_CASE("missing_number") {
SECTION("missing number should be equal to index + 1") {
std::iota(numbers.begin(), numbers.end(), 1);
auto index = distributor(gen);
numbers[index] = 0;
REQUIRE(missing_number(numbers) == index + 1);
}
SECTION("missing number should be equal to index + 1") {
std::iota(numbers.begin(), numbers.end(), 1);
auto index = distributor(gen);
numbers[index] = 0;
REQUIRE(missing_number(numbers) == index + 1);
}
}
TEST_CASE("missing_number", "[!benchmark]") {
std::iota(numbers.begin(), numbers.end(), 1);
numbers[distributor(gen)] = 0;
BENCHMARK("missing_number(numbers)") {
return missing_number(numbers);
};
}