forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShrinkToFitTest.h
54 lines (47 loc) · 1.9 KB
/
ShrinkToFitTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include <string>
#include <vector>
/**
\class ShrinkToFitTest
Verify that the non-binding request shrink_to_fit() does reduce the
container's capacity.
*/
class ShrinkToFitTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static ShrinkToFitTest *createSuite() { return new ShrinkToFitTest(); }
static void destroySuite(ShrinkToFitTest *suite) { delete suite; }
void test_vector() {
// verify that std::vector::shrink_to_fit() reduces the vector's capacity.
std::size_t oldsize{1000};
std::size_t newsize{100};
std::vector<double> original(oldsize);
TS_ASSERT_EQUALS(original.size(), oldsize);
TS_ASSERT_LESS_THAN_EQUALS(oldsize, original.capacity());
original.resize(100);
TS_ASSERT_EQUALS(original.size(), newsize);
std::size_t capacityAfterResize = original.capacity();
original.shrink_to_fit();
TS_ASSERT_EQUALS(original.size(), newsize);
std::size_t capacityAfterShrinkToFit = original.capacity();
TS_ASSERT_LESS_THAN(capacityAfterShrinkToFit, capacityAfterResize);
// Use shrink-to-fit idiom and compare against shrink_to_fit()
original.resize(1000);
TS_ASSERT_EQUALS(original.size(), oldsize);
original.resize(100);
TS_ASSERT_EQUALS(original.size(), newsize);
{
std::vector<double> temp(original);
std::swap(original, temp);
}
TS_ASSERT_LESS_THAN_EQUALS(capacityAfterShrinkToFit, original.capacity());
}
};