Newer
Older
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
// 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
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef MANTID_KERNEL_BINARYSTREAMWRITERTEST_H_
#define MANTID_KERNEL_BINARYSTREAMWRITERTEST_H_
#include <cxxtest/TestSuite.h>
#include <cxxtest/ValueTraits.h>
#include "MantidKernel/BinaryStreamReader.h"
#include "MantidKernel/BinaryStreamWriter.h"
#include "MantidKernel/Matrix.h"
#include <sstream>
using Mantid::Kernel::BinaryStreamReader;
using Mantid::Kernel::BinaryStreamWriter;
using Mantid::Kernel::Matrix;
class BinaryStreamWriterTest : 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 BinaryStreamWriterTest *createSuite() {
return new BinaryStreamWriterTest();
}
static void destroySuite(BinaryStreamWriterTest *suite) { delete suite; }
BinaryStreamWriterTest() : CxxTest::TestSuite(), m_bytes() {}
//----------------------------------------------------------------------------
// Successes cases
//----------------------------------------------------------------------------
// single values
void test_Constructor_With_Good_Stream_Does_Not_Touch_Stream() {
BinaryStreamReader reader(m_bytes);
TS_ASSERT_EQUALS(std::ios_base::beg, m_bytes.tellg());
}
void test_Write_int16_t_Gives_Correct_Value() {
doWriteSingleValueTest<int16_t>(6);
}
void test_Write_int32_t_Gives_Correct_Value() {
doWriteSingleValueTest<int32_t>(580);
}
void test_Write_int64_t_Gives_Correct_Value() {
;
doWriteSingleValueTest<int64_t>(200);
}
void test_Write_uint16_t_Gives_Correct_Value() {
doWriteSingleValueTest<uint16_t>(111);
}
void test_Write_uint32_t_Gives_Correct_Value() {
doWriteSingleValueTest<uint32_t>(231);
}
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
void test_Write_float_Gives_Correct_Value() {
doWriteSingleValueTest<float>(787.0f);
}
void test_Write_double_Gives_Correct_Value() {
doWriteSingleValueTest<double>(2.0);
}
void test_Write_String_Gives_Expected_String() {
doWriteSingleValueTest<std::string>("mantid");
}
// vectors of values
void test_Write_Vector_int16_t() {
const size_t nvals(3);
std::vector<int16_t> expectedValue{2, 0, 4};
doWriteArrayValueTest(nvals, expectedValue);
}
void test_Write_Vector_int32_t() {
const size_t nvals(3);
std::vector<int32_t> expectedValue{2, 4, 6};
doWriteArrayValueTest(nvals, expectedValue);
}
void test_Write_Vector_int64_t() {
std::vector<int64_t> expectedValue{200, 400, 600, 900};
doWriteArrayValueTest(expectedValue.size(), expectedValue);
}
void test_Write_Vector_float() {
std::vector<float> expectedValue{0.0f, 5.0f, 10.0f};
doWriteArrayValueTest(expectedValue.size(), expectedValue);
}
void test_Write_Vector_double() {
std::vector<double> expectedValue{10.0, 15.0, 20.0, 25.0};
doWriteArrayValueTest(expectedValue.size(), expectedValue);
}
private:
template <typename T> void doWriteSingleValueTest(T value) {
BinaryStreamWriter writer(m_bytes);
BinaryStreamReader reader(m_bytes);
T readVal;
writer << value;
reader >> readVal;
TS_ASSERT_EQUALS(readVal, value);
}
template <typename T>
void doWriteArrayValueTest(const size_t nvals, const std::vector<T> &values) {
BinaryStreamWriter writer(m_bytes);
BinaryStreamReader reader(m_bytes);
std::vector<T> readValues;
writer.write(values, nvals);
reader.read(readValues, nvals);
TS_ASSERT_EQUALS(readValues, values);
TS_ASSERT_EQUALS(nvals, values.size());
}
void resetStreamToStart() {
m_bytes.clear();
m_bytes.seekg(std::ios_base::beg);
}
std::stringstream m_bytes;
};
#endif