OTest2
A C++ testing framework
testmarkoutbin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <testmarkoutbin.h>
21 
22 #include <assert.h>
23 #include <boost/endian/conversion.hpp>
24 #include <limits>
25 #include <cstring>
26 #include <sstream>
27 #include <string>
28 
29 #include "testmarkoutbintags.h"
30 
31 namespace OTest2 {
32 
34 
35 }
36 
38 
39 }
40 
41 void TestMarkOutBin::writeTag(
42  TestMarkOutBinTag tag_) {
43  writeBinaryData(sizeof(tag_), reinterpret_cast<const std::uint8_t*>(&tag_));
44 }
45 
47  const char* typemark_) {
48  assert(typemark_ != nullptr);
49 
50  writeTag(TestMarkOutBinTag::TYPE);
51 
52  const std::uint64_t length_(std::strlen(typemark_));
53  assert(length_ <= std::numeric_limits<std::uint8_t>::max());
54  const std::uint8_t mark_len_(static_cast<std::uint8_t>(length_));
55  writeBinaryData(sizeof(mark_len_), reinterpret_cast<const std::uint8_t*>(&mark_len_));
56  writeBinaryData(length_, reinterpret_cast<const std::uint8_t*>(typemark_));
57 }
58 
60  std::int64_t value_) {
61  if(value_ >= std::numeric_limits<std::int8_t>::min()
62  && value_ <= std::numeric_limits<std::int8_t>::max()) {
64  std::uint8_t be_value_(static_cast<std::uint8_t>(static_cast<std::int8_t>(value_)));
65  writeBinaryData(sizeof(be_value_), &be_value_);
66  }
67  else if(value_ >= std::numeric_limits<std::int16_t>::min()
68  && value_ <= std::numeric_limits<std::int16_t>::max()) {
70  std::uint16_t be_value_(boost::endian::native_to_big(
71  static_cast<std::uint16_t>(static_cast<std::int16_t>(value_))));
72  writeBinaryData(sizeof(be_value_), reinterpret_cast<const std::uint8_t*>(&be_value_));
73  }
74  else {
76  std::uint64_t be_value_(
77  boost::endian::native_to_big(static_cast<std::uint64_t>(value_)));
78  writeBinaryData(sizeof(be_value_), reinterpret_cast<const std::uint8_t*>(&be_value_));
79  }
80 }
81 
83  long double value_) {
84  writeTag(TestMarkOutBinTag::FLOAT);
85 
86  /* -- There is no platform independent way how to store floating points
87  * in binary form. Hence, I print the float in the hexadecimal
88  * format and I store the float as a string. */
89  std::ostringstream oss_;
90  oss_ << std::hexfloat << value_;
91  const std::string& str_value_(oss_.str());
92 
93  /* -- write the string */
94  const std::uint64_t length_(str_value_.size());
95  assert(length_ <= std::numeric_limits<std::uint8_t>::max());
96  const std::uint8_t be_length_(static_cast<std::uint8_t>(length_));
97  writeBinaryData(sizeof(be_length_), reinterpret_cast<const std::uint8_t*>(&be_length_));
98  writeBinaryData(length_, reinterpret_cast<const std::uint8_t*>(str_value_.c_str()));
99 }
100 
102  const std::string& string_) {
103  const std::uint64_t length_(string_.size());
104 
105  if(length_ <= std::numeric_limits<std::uint8_t>::max()) {
107  const std::uint8_t be_length_(static_cast<std::uint8_t>(length_));
108  writeBinaryData(
109  sizeof(be_length_), reinterpret_cast<const std::uint8_t*>(&be_length_));
110  }
111  else if(length_ <= std::numeric_limits<std::uint16_t>::max()) {
113  const std::uint16_t be_length_(
114  boost::endian::native_to_big(static_cast<std::uint8_t>(length_)));
115  writeBinaryData(
116  sizeof(be_length_), reinterpret_cast<const std::uint8_t*>(&be_length_));
117  }
118  else {
120  const std::uint64_t be_length_(boost::endian::native_to_big(length_));
121  writeBinaryData(
122  sizeof(be_length_), reinterpret_cast<const std::uint8_t*>(&be_length_));
123  }
124  writeBinaryData(length_, reinterpret_cast<const std::uint8_t*>(string_.c_str()));
125 }
126 
127 } /* -- namespace OTest2 */
OTest2::TestMarkOutBinTag
TestMarkOutBinTag
Definition: testmarkoutbintags.h:27
OTest2::TestMarkOutBin::TestMarkOutBin
TestMarkOutBin()
Ctor.
Definition: testmarkoutbin.cpp:33
testmarkoutbin.h
OTest2::TestMarkOutBin::~TestMarkOutBin
virtual ~TestMarkOutBin()
Dtor.
Definition: testmarkoutbin.cpp:37
OTest2::TestMarkOutBinTag::STRING_SHORT
@ STRING_SHORT
OTest2::TestMarkOutBinTag::INT_NORMAL
@ INT_NORMAL
OTest2::TestMarkOutBinTag::INT_SHORT
@ INT_SHORT
OTest2::TestMarkOutBin::writeFloat
virtual void writeFloat(long double value_) override
Write 64-bit float.
Definition: testmarkoutbin.cpp:82
OTest2
Definition: assertbean.h:25
OTest2::TestMarkOutBin::writeTypeMark
virtual void writeTypeMark(const char *typemark_) override
Write type mark of currently stored testmark.
Definition: testmarkoutbin.cpp:46
OTest2::TestMarkOutBin::writeInt
virtual void writeInt(std::int64_t value_) override
Write a 64 bit integer.
Definition: testmarkoutbin.cpp:59
OTest2::TestMarkOutBinTag::INT_HUGE
@ INT_HUGE
OTest2::TestMarkOutBinTag::FLOAT
@ FLOAT
OTest2::TestMarkOutBinTag::STRING_NORMAL
@ STRING_NORMAL
OTest2::TestMarkOutBinTag::TYPE
@ TYPE
testmarkoutbintags.h
OTest2::TestMarkOutBin::writeString
virtual void writeString(const std::string &string_) override
Write text string.
Definition: testmarkoutbin.cpp:101
OTest2::TestMarkOutBinTag::STRING_HUGE
@ STRING_HUGE