-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector1.cc
74 lines (60 loc) · 1.25 KB
/
vector1.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Repeat of `read_and_sum.cc`, but using cpp constructor/destructor.
#include <cstddef>
#include <cmath>
#include <sstream>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
// Vector mimics an STL container.
class Vector
{
public:
Vector(std::size_t sz)
: elems(new double[sz])
, sz(sz)
{ }
~Vector()
{
delete[] elems;
sz = 0;
}
double& operator[](std::size_t i)
{
return elems[i];
}
std::size_t size() const
{
return sz;
}
private:
double *elems;
std::size_t sz;
};
// read_and_sum returns the sum of the values read from input.
double read_and_sum(std::istream& is, std::size_t n)
{
Vector v{n};
for (std::size_t i = 0; i != n; ++i) {
is >> v[i];
}
double sum = 0.;
for (std::size_t i = 0; i != n; ++i) {
sum += v[i];
}
return sum;
}
TEST_CASE("[Vector]")
{
std::size_t n{10};
Vector v(n);
REQUIRE(v.size() == n);
constexpr double pi = 2.*std::acos(0.);
v[n/2] = pi;
CAPTURE(v[n/2]);
REQUIRE(v[n/2] == pi);
}
TEST_CASE("[read_and_sum]")
{
std::istringstream is("1 10 100 1000 10000");
auto rcv = read_and_sum(is, 5);
REQUIRE(rcv == doctest::Approx(11111.));
}