crc32_test.cc
Go to the documentation of this file.
1 #include "catch2/catch.hpp"
2 
3 #include "cetlib/crc32.h"
4 
5 #include <string>
6 
7 #include "CRC32Calculator.h"
8 
9 using namespace std::string_literals;
10 
11 using cet::crc32;
12 
13 namespace {
14  auto const TESTA = "abc"s;
15  auto const TESTB =
16  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"s;
17  auto const TESTC = "\
18 1 First God made heaven & earth \
19 2 The earth was without form and void, and darkness was upon the face of the deep; and the Spirit of God was moving over the face of the waters. \
20 3 And God said, \"Let there be light\"; and there was light. \
21 4 And God saw that the light was good; and God separated the light from the darkness. \
22 5 God called the light Day, and the darkness he called Night. And there was evening and there was morning, one day. \
23 6 And God said, \"Let there be a firmament in the midst of the waters, and let it separate the waters from the waters.\" \
24 7 And God made the firmament and separated the waters which were under the firmament from the waters which were above the firmament. And it was so. \
25 8 And God called the firmament Heaven. And there was evening and there was morning, a second day. \
26 9 And God said, \"Let the waters under the heavens be gathered together into one place, and let the dry land appear.\" And it was so. \
27 10 God called the dry land Earth, and the waters that were gathered together he called Seas. And God saw that it was good. \
28 11 And God said, \"Let the earth put forth vegetation, plants yielding seed, and fruit trees bearing fruit in which is their seed, each according to its kind, upon the earth.\" And it was so. \
29 12 The earth brought forth vegetation, plants yielding seed according to their own kinds, and trees bearing fruit in which is their seed, each according to its kind. And God saw that it was good. \
30 13 And there was evening and there was morning, a third day. \
31 14 And God said, \"Let there be lights in the firmament of the heavens to separate the day from the night; and let them be for signs and for seasons and for days and years, \
32 15 and let them be lights in the firmament of the heavens to give light upon the earth.\" And it was so. \
33 16 And God made the two great lights, the greater light to rule the day, and the lesser light to rule the night; he made the stars also. \
34 17 And God set them in the firmament of the heavens to give light upon the earth, \
35 18 to rule over the day and over the night, and to separate the light from the darkness. And God saw that it was good. \
36 19 And there was evening and there was morning, a fourth day. \
37 20 And God said, \"Let the waters bring forth swarms of living creatures, and let birds fly above the earth across the firmament of the heavens.\" \
38 21 So God created the great sea monsters and every living creature that moves, with which the waters swarm, according to their kinds, and every winged bird according to its kind. And God saw that it was good. \
39 22 And God blessed them, saying, \"Be fruitful and multiply and fill the waters in the seas, and let birds multiply on the earth.\" \
40 23 And there was evening and there was morning, a fifth day. \
41 24 And God said, \"Let the earth bring forth living creatures according to their kinds: cattle and creeping things and beasts of the earth according to their kinds.\" And it was so. \
42 25 And God made the beasts of the earth according to their kinds and the cattle according to their kinds, and everything that creeps upon the ground according to its kind. And God saw that it was good. \
43 26 Then God said, \"Let us make man in our image, after our likeness; and let them have dominion over the fish of the sea, and over the birds of the air, and over the cattle, and over all the earth, and over every creeping thing that creeps upon the earth.\" \
44 27 So God created man in his own image, in the image of God he created him; male and female he created them. \
45 28 And God blessed them, and God said to them, \"Be fruitful and multiply, and fill the earth and subdue it; and have dominion over the fish of the sea and over the birds of the air and over every living thing that moves upon the earth.\" \
46 29 And God said, \"Behold, I have given you every plant yielding seed which is upon the face of all the earth, and every tree with seed in its fruit; you shall have them for food. \
47 30 And to every beast of the earth, and to every bird of the air, and to everything that creeps on the earth, everything that has the breath of life, I have given every green plant for food.\" And it was so. \
48 31 And God saw everything that he had made, and behold, it was very good. And there was evening and there was morning, a sixth day.\
49 "s;
50 }
51 
52 SCENARIO("We can produce CRC32 checksums accurately")
53 {
54  crc32 crc;
55 
56  GIVEN("We have a short string")
57  {
58  WHEN("We create CRC32 digests in two different ways")
59  {
60  crc << TESTA;
61  cet::CRC32Calculator const refMaker(TESTA);
62 
63  auto const digest = crc.digest();
64  auto const ref = refMaker.checksum();
65 
66  THEN("The checksums compare equal") { CHECK(digest == ref); }
67  }
68  }
69 
70  GIVEN("We have a longer string")
71  {
72  WHEN("We create CRC32 digests in two different ways")
73  {
74  crc << TESTB;
75  cet::CRC32Calculator const refMaker(TESTB);
76 
77  auto const digest = crc.digest();
78  auto const ref = refMaker.checksum();
79 
80  THEN("The checksums compare equal") { CHECK(digest == ref); }
81  }
82  }
83 
84  GIVEN("We have a *much* longer string")
85  {
86  WHEN("We create CRC32 digests in two different ways")
87  {
88  crc << TESTC;
89  cet::CRC32Calculator const refMaker(TESTC);
90 
91  auto const digest = crc.digest();
92  auto const ref = refMaker.checksum();
93 
94  THEN("The checksums compare equal") { CHECK(digest == ref); }
95  }
96  }
97 
98  GIVEN("We have a string with a known, externally calculated CRC32 checksum")
99  {
100  WHEN("We create CRC32 digests in two different ways")
101  {
102  auto const testString = "type_label_instance"s;
103  auto const process_suffix = "_process"s;
104  auto const knownResult = 1215348599u;
105 
106  crc << (testString + process_suffix);
107  cet::CRC32Calculator const refMaker(testString + process_suffix);
108 
109  auto const digest = crc.digest();
110  auto const ref = refMaker.checksum();
111 
112  THEN("All three checksums compare equal")
113  {
114  CHECK(digest == knownResult);
115  CHECK(digest == ref);
116  }
117  WHEN("We insert more text before calculating a digest")
118  {
119  crc32 crc_insert(testString);
120  crc_insert << process_suffix;
121 
122  THEN("We get the same answer")
123  {
124  CHECK(crc_insert.digest() == knownResult);
125  }
126  }
127  }
128  }
129 
130  GIVEN("We have an empty string")
131  {
132  WHEN("We calculate its CRC32 checksum")
133  {
134  crc << "";
135 
136  THEN("It should be 0") { CHECK(crc.digest() == 0); }
137  }
138  }
139 }
#define TESTC
Definition: sha1_test_2.cc:8
SCENARIO("We can produce CRC32 checksums accurately")
Definition: crc32_test.cc:52
#define TESTB
Definition: sha1_test_2.cc:7
#define TESTA
Definition: sha1_test_2.cc:6
std::uint32_t checksum() const
constexpr digest_t digest() const
Definition: crc32.h:138
static QCString * s
Definition: config.cpp:1042