Checksum.cc Source File

Back to the index.

Checksum.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 Anders Gavare. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "Checksum.h"
29 
30 
32  : m_value(0)
33 {
34 }
35 
36 
37 uint64_t Checksum::Value() const
38 {
39  return m_value;
40 }
41 
42 
43 void Checksum::Add(uint64_t x)
44 {
45  m_value += x * (((uint64_t)0xc151429 << 32) | 0x517851bf);
46  m_value ^= (((uint64_t)0x9183bfec << 32) | 0x01921947);
47 }
48 
49 
50 void Checksum::Add(const string& str)
51 {
52  size_t n = str.length();
53  Add(10000 + n);
54 
55  for (size_t i=0; i<n; ++i)
56  Add(str[i]);
57 }
58 
59 
60 bool Checksum::operator == (const Checksum& other) const
61 {
62  return m_value == other.m_value;
63 }
64 
65 
66 bool Checksum::operator != (const Checksum& other) const
67 {
68  return m_value != other.m_value;
69 }
70 
71 
72 /*****************************************************************************/
73 
74 
75 #ifdef WITHUNITTESTS
76 
77 static void Test_Checksum_DefaultValue()
78 {
79  Checksum checksum;
80 
81  UnitTest::Assert("value should initially be zero",
82  checksum.Value() == 0);
83 }
84 
85 static void Test_Checksum_Add()
86 {
87  Checksum checksum;
88 
89  checksum.Add(123);
90  uint64_t v1 = checksum.Value();
91 
92  checksum.Add(123);
93  uint64_t v2 = checksum.Value();
94 
95  UnitTest::Assert("v1 should be non-zero", v1 != 0);
96  UnitTest::Assert("v2 should be non-zero", v2 != 0);
97  UnitTest::Assert("v1 and v2 should differ", v1 != v2);
98 }
99 
100 static void Test_Checksum_SameChecksum()
101 {
102  Checksum checksumA;
103  Checksum checksumB;
104 
105  checksumA.Add("testing");
106  checksumB.Add("testing");
107 
108  UnitTest::Assert("checksums should be the same",
109  checksumA.Value() == checksumB.Value());
110 }
111 
112 static void Test_Checksum_OrderIsSignificant_Numeric()
113 {
114  Checksum checksumA;
115  Checksum checksumB;
116 
117  checksumA.Add(123);
118  checksumA.Add(234);
119  uint64_t valueA = checksumA.Value();
120 
121  checksumB.Add(234);
122  checksumB.Add(123);
123  uint64_t valueB = checksumB.Value();
124 
125  UnitTest::Assert("valueA and valueB should differ", valueA != valueB);
126 }
127 
128 static void Test_Checksum_OrderIsSignificant_String()
129 {
130  Checksum checksumA;
131  Checksum checksumB;
132 
133  checksumA.Add("123");
134  checksumA.Add("234");
135  uint64_t valueA = checksumA.Value();
136 
137  checksumB.Add("234");
138  checksumB.Add("123");
139  uint64_t valueB = checksumB.Value();
140 
141  UnitTest::Assert("valueA and valueB should differ", valueA != valueB);
142 }
143 
144 static void Test_Checksum_OrderIsSignificant_String_1()
145 {
146  Checksum checksumA;
147  Checksum checksumB;
148 
149  checksumA.Add("abcdef");
150  uint64_t valueA = checksumA.Value();
151 
152  checksumB.Add("abdcef");
153  uint64_t valueB = checksumB.Value();
154 
155  UnitTest::Assert("valueA and valueB should differ", valueA != valueB);
156 }
157 
158 static void Test_Checksum_OrderIsSignificant_String_2()
159 {
160  Checksum checksumA;
161  Checksum checksumB;
162 
163  checksumA.Add("hello ");
164  checksumA.Add("world");
165  uint64_t valueA = checksumA.Value();
166 
167  checksumB.Add("hello");
168  checksumB.Add(" world");
169  uint64_t valueB = checksumB.Value();
170 
171  UnitTest::Assert("valueA and valueB should differ", valueA != valueB);
172 }
173 
174 static void Test_Checksum_OrderIsSignificant_String_3()
175 {
176  Checksum checksumA;
177  Checksum checksumB;
178 
179  checksumA.Add("string x \"modified\"\n");
180  checksumA.Add("string y \"value 2\"\n");
181  checksumA.Add("string x \"value\\nhello\"\n");
182  uint64_t valueA = checksumA.Value();
183 
184  checksumB.Add("string x \"value 1\"\n");
185  checksumB.Add("string y \"value 2\"\n");
186  checksumB.Add("string x \"value\\nhello\"\n");
187  uint64_t valueB = checksumB.Value();
188 
189  UnitTest::Assert("valueA and valueB should differ", valueA != valueB);
190 }
191 
192 static void Test_Checksum_String_Concatenation()
193 {
194  Checksum checksumA;
195  Checksum checksumB;
196 
197  checksumA.Add("hello");
198  checksumA.Add(" world");
199  uint64_t valueA = checksumA.Value();
200 
201  checksumB.Add("hello world");
202  uint64_t valueB = checksumB.Value();
203 
204  UnitTest::Assert("valueA and valueB should differ", valueA != valueB);
205 }
206 
208 {
209  UNITTEST(Test_Checksum_DefaultValue);
210  UNITTEST(Test_Checksum_Add);
211  UNITTEST(Test_Checksum_SameChecksum);
212  UNITTEST(Test_Checksum_OrderIsSignificant_Numeric);
213  UNITTEST(Test_Checksum_OrderIsSignificant_String);
214  UNITTEST(Test_Checksum_OrderIsSignificant_String_1);
215  UNITTEST(Test_Checksum_OrderIsSignificant_String_2);
216  UNITTEST(Test_Checksum_OrderIsSignificant_String_3);
217  UNITTEST(Test_Checksum_String_Concatenation);
218 }
219 
220 #endif
uint64_t Value() const
Retrieves the value of the checksum, as a uint64_t.
Definition: Checksum.cc:37
A checksum accumulator.
Definition: Checksum.h:47
void Add(uint64_t x)
Add a uint64_t to the checksum.
Definition: Checksum.cc:43
#define UNITTESTS(class)
Helper for unit test case execution.
Definition: UnitTest.h:184
bool operator!=(const Checksum &other) const
Compares one Checksum to another for inequality.
Definition: Checksum.cc:66
bool operator==(const Checksum &other) const
Compares one Checksum to another for equality.
Definition: Checksum.cc:60
Checksum()
Constructs a zeroed checksum.
Definition: Checksum.cc:31
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
Definition: UnitTest.cc:40
#define UNITTEST(functionname)
Helper for unit test case execution.
Definition: UnitTest.h:217

Generated on Sun Sep 30 2018 16:05:18 for GXemul by doxygen 1.8.13