scclib
Stable Cloud Computing C++ Library
adler32.cc
Go to the documentation of this file.
1 #include <encode/adler32.h>
2 #include <crypto/random.h>
3 #include <gtest/gtest.h>
4 #include <iostream>
5 #include <string>
6 #include <fstream>
7 
23 using namespace std;
25 
27 static string test =
28 "ljpoaweu9uwat7a9g0ujaW219U0U;DSJGEOPUJGAfPVAPUAS:FGJALGJ7804-85," // 64 bytes
29 "G;AKGPTG[ASIGSFDAS[DFSAPDFJASPFJSPADFJPAJPGAJSGSAGJAPJGAPJGPOIOO"; // 64 bytes
30 static uint32_t all_ad = 0xf9932612; // all bytes
31 static uint32_t last64_ad = 0x583e1280; // last 64 bytes
33 
34 TEST(adler32, construct)
35 {
36  Adler32 ad;
37  ASSERT_EQ(ad.val(), 1);
38  ASSERT_EQ(ad.size(), 0);
39 
40  Adler32 ad2(test.data(), test.size());
41  ASSERT_EQ(ad2.val(), all_ad);
42  ASSERT_EQ(ad2.size(), test.size());
43 }
44 
45 TEST(adler32, single_update)
46 {
47  Adler32 ad;
48  ad.update(test.data(), test.size());
49  ASSERT_EQ(ad.val(), all_ad);
50 }
51 
52 TEST(adler32, two_updates)
53 {
54  Adler32 ad;
55  ad.update(test.data(), test.size()/2);
56  ad.update(test.data()+test.size()/2, test.size()-test.size()/2);
57  ASSERT_EQ(ad.val(), all_ad);
58 }
59 
60 TEST(adler32, second_half_update)
61 {
62  Adler32 ad;
63  ad.update(test.data()+test.size()/2, test.size()-test.size()/2);
64  ASSERT_EQ(ad.val(), last64_ad);
65 }
66 
67 TEST(adler32, update_and_reset)
68 {
69  Adler32 ad;
70  ASSERT_EQ(ad.update(test.data(), test.size()), all_ad);
71  ASSERT_EQ(ad.reset(test.data()+test.size()/2, test.size()-test.size()/2), last64_ad);
72  ASSERT_EQ(ad.reset(), 1);
73 }
74 
75 TEST(adler32, combine)
76 {
77  Adler32 first(test.data(), test.size()/2),
78  second(test.data()+test.size()/2, test.size()/2);
79 
80  ASSERT_EQ(first.combine(second), all_ad);
81 }
82 
83 TEST(adler32, rolling_update)
84 {
86  Adler32 ad;
87  size_t sz = test.size(); // 128 bytes
88 
89  ad.reset(test.data(), sz/2); // first 64 bytes, sets window size to 64
90  ASSERT_EQ(ad.size(), 64);
91 
92  // rolling 64 byte window, will calculate the checksum at each 64 byte window
93  for (size_t i = 0; i < 64; i++)
94  {
95  ad.rotate(test[i], test[64+i]); // first byte is 0-63, last is 64-127
96  }
97  ASSERT_EQ(ad, last64_ad); // end up with the last 64 bytes checksum
99 }
100 
101 TEST(adler32, verify_large_blocks)
102 {
103  Adler32 ad;
104  Adler32 vad;
105 
106  int datasz = 1<<14; // 512, block will be up to 5096
107  char dat[datasz];
108 
110 
111  for (int blksz = 1; blksz < datasz>>1; blksz <<= 1)
112  {
113  cout << "testing adler32 rolling checksum for block size: " << blksz << endl;
114 
115  ad.reset(&dat[0], blksz); // reset the checksum we will use for rolling
116 
117  for (int i = 0; i < datasz-blksz; i++)
118  {
119  vad.reset(&dat[i], blksz); // calculate the current block directly
120  ASSERT_EQ(ad, vad);
121  ad.rotate(dat[i], dat[i+blksz]); // calculate the next window via rolling
122  }
123  }
124 }
Adler32 rolling checksum.
static void rand_bytes(void *, int len)
Generate random bytes.
Adler-32 checksum allowing rolling calculation.
Definition: adler32.h:33
uint32_t rotate(unsigned char, unsigned char)
Update the checksum with next byte.
Definition: adler32.cc:36
uint32_t reset()
Reset the checksum.
Definition: adler32.h:61
uint32_t update(const void *, int)
Update the checksum with buffer contents.
Definition: adler32.cc:7
int size()
Size of the current window.
Definition: adler32.h:54
Random number generator.
TEST(adler32, construct)
[Test vars]
Definition: adler32.cc:34