OTest2
A C++ testing framework
testmarkhash.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 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 <testmarkhash.h>
21 
22 #include <cstring>
23 
24 namespace OTest2 {
25 
27  hash(0xcbf29ce484222325ULL) {
28 
29 }
30 
32  TestMarkHash&& other_) noexcept :
33  hash(other_.hash) {
34 
35 }
36 
38 
39 }
40 
42  const std::uint8_t* data_,
43  std::size_t size_) {
44  std::uint8_t const* const end_(data_ + size_);
45  for(; data_ != end_; ++data_) {
46  hash *= 0x100000001b3ULL;
47  hash ^= static_cast<std::uint64_t>(*data_);
48  }
49 }
50 
52  std::uint8_t term_(0);
53  addData(&term_, sizeof(term_));
54 }
55 
57  TestMarkHashCode code_) {
58  addData(reinterpret_cast<const std::uint8_t*>(&code_), sizeof(code_));
59 }
60 
62  const char* string_) {
63  addData(
64  reinterpret_cast<const std::uint8_t*>(string_),
65  std::strlen(string_));
66 }
67 
69  return hash;
70 }
71 
73  const char* type_name_,
74  const std::uint8_t* data_,
75  std::size_t size_) {
76  addString(type_name_);
77  addTerminator();
78  addData(data_, size_);
79 }
80 
82  const char* type_name_,
83  const std::string& value_) {
85  type_name_,
86  reinterpret_cast<const std::uint8_t*>(value_.c_str()),
87  value_.size());
88 
89 }
90 
92  const char* type_name_,
93  const std::string& value_) {
94  TestMarkHash hash_;
95  hash_.addBasicType(type_name_, value_);
96  return hash_.getHashCode();
97 }
98 
99 } /* -- namespace OTest2 */
OTest2::TestMarkHash::addString
void addString(const char *string_)
Append a string literal.
Definition: testmarkhash.cpp:61
OTest2::TestMarkHash::addTerminator
void addTerminator()
Append a terminataor character into the hash.
Definition: testmarkhash.cpp:51
OTest2::TestMarkHash::getHashCode
TestMarkHashCode getHashCode() const noexcept
Get currently computed hash code.
Definition: testmarkhash.cpp:68
testmarkhash.h
OTest2::TestMarkHashCode
std::uint64_t TestMarkHashCode
Definition: testmarkhashcode.h:28
OTest2::TestMarkHash::hashBasicType
static TestMarkHashCode hashBasicType(const char *type_name_, Type_ value_)
Definition: testmarkhash.h:122
OTest2::TestMarkHash::TestMarkHash
TestMarkHash()
Ctor.
Definition: testmarkhash.cpp:26
OTest2::TestMarkHash::~TestMarkHash
~TestMarkHash()
Dtor.
Definition: testmarkhash.cpp:37
OTest2
Definition: assertbean.h:25
OTest2::TestMarkHash
A simple generator of a testmark hash.
Definition: testmarkhash.h:40
OTest2::TestMarkHash::addData
void addData(const std::uint8_t *data_, std::size_t size_)
Append piece of data.
Definition: testmarkhash.cpp:41
OTest2::TestMarkHash::addBasicType
void addBasicType(const char *type_name_, const std::uint8_t *data_, std::size_t size_)
Add hash for a value of a basic type.
Definition: testmarkhash.cpp:72
OTest2::TestMarkHash::addHashCode
void addHashCode(TestMarkHashCode code_)
Append a hash code of another testmark.
Definition: testmarkhash.cpp:56