File: xdelta.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (235 lines) | stat: -rw-r--r-- 5,993 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "unit_tests.hh"
#include "xdelta.hh"

#include "adler32.hh"
#include <boost/random.hpp>

boost::mt19937 xdelta_prng;
boost::uniform_smallint<char> xdelta_chargen('a', 'z');
boost::uniform_smallint<size_t> xdelta_sizegen(1024, 65536);
boost::uniform_smallint<size_t> xdelta_editgen(3, 10);
boost::uniform_smallint<size_t> xdelta_lengen(1, 256);

using std::string;
using boost::shared_ptr;

UNIT_TEST(basic)
{
  data dat1(string("the first day of spring\nmakes me want to sing\n"),
            origin::internal);
  data dat2(string("the first day of summer\nis a major bummer\n"),
            origin::internal);
  delta del;
  diff(dat1, dat2, del);

  data dat3;
  patch(dat1, del, dat3);
  UNIT_TEST_CHECK(dat3 == dat2);
}

static string
apply_via_normal(string const & base, string const & delta)
{
  string tmp;
  apply_delta(base, delta, tmp);
  return tmp;
}

static string
apply_via_piecewise(string const & base, string const & delta)
{
  shared_ptr<delta_applicator> appl = new_piecewise_applicator();
  appl->begin(base);
  apply_delta(appl, delta);
  appl->next();
  string tmp;
  appl->finish(tmp);
  return tmp;
}

static void
spin(string a, string b)
{
  string ab, ba;
  compute_delta(a, b, ab);
  compute_delta(b, a, ba);
  UNIT_TEST_CHECK(a == apply_via_normal(b, ba));
  UNIT_TEST_CHECK(a == apply_via_piecewise(b, ba));
  UNIT_TEST_CHECK(b == apply_via_normal(a, ab));
  UNIT_TEST_CHECK(b == apply_via_piecewise(a, ab));
  string ab_inverted, ba_inverted;
  invert_xdelta(a, ab, ab_inverted);
  invert_xdelta(b, ba, ba_inverted);
  UNIT_TEST_CHECK(a == apply_via_normal(b, ab_inverted));
  UNIT_TEST_CHECK(a == apply_via_piecewise(b, ab_inverted));
  UNIT_TEST_CHECK(b == apply_via_normal(a, ba_inverted));
  UNIT_TEST_CHECK(b == apply_via_piecewise(a, ba_inverted));
}

UNIT_TEST(simple_cases)
{
  L(FL("empty/empty"));
  spin("", "");
  L(FL("empty/short"));
  spin("", "a");
  L(FL("empty/longer"));
  spin("", "asdfasdf");
  L(FL("two identical strings"));
  spin("same string", "same string");
}

void
xdelta_random_string(string & str)
{
  size_t sz = xdelta_sizegen(xdelta_prng);
  str.clear();
  str.reserve(sz);
  while (sz-- > 0)
    {
      str += xdelta_chargen(xdelta_prng);
    }
}

void
xdelta_randomly_insert(string & str)
{
  size_t nedits = xdelta_editgen(xdelta_prng);
  while (nedits > 0)
    {
      size_t pos = xdelta_sizegen(xdelta_prng) % str.size();
      size_t len = xdelta_lengen(xdelta_prng);
      if (pos+len >= str.size())
        continue;
      string tmp;
      tmp.reserve(len);
      for (size_t i = 0; i < len; ++i)
        tmp += xdelta_chargen(xdelta_prng);
        str.insert(pos, tmp);
      nedits--;
    }
}

void
xdelta_randomly_change(string & str)
{
  size_t nedits = xdelta_editgen(xdelta_prng);
  while (nedits > 0)
    {
      size_t pos = xdelta_sizegen(xdelta_prng) % str.size();
      size_t len = xdelta_lengen(xdelta_prng);
      if (pos+len >= str.size())
        continue;
      for (size_t i = 0; i < len; ++i)
        str[pos+i] = xdelta_chargen(xdelta_prng);
      nedits--;
    }
}

void
xdelta_randomly_delete(string & str)
{
  size_t nedits = xdelta_editgen(xdelta_prng);
  while (nedits > 0)
    {
      size_t pos = xdelta_sizegen(xdelta_prng) % str.size();
      size_t len = xdelta_lengen(xdelta_prng);
      if (pos+len >= str.size())
        continue;
      str.erase(pos, len);
      --nedits;
    }
}

UNIT_TEST(random_simple_delta)
{
  for (int i = 0; i < 100; ++i)
    {
      string a, b;
      xdelta_random_string(a);
      b = a;
      xdelta_randomly_change(b);
      xdelta_randomly_insert(b);
      xdelta_randomly_delete(b);
      spin(a, b);
    }
}

UNIT_TEST(random_piecewise_delta)
{
  for (int i = 0; i < 50; ++i)
    {
      string prev, next, got;
      xdelta_random_string(prev);
      shared_ptr<delta_applicator> appl = new_piecewise_applicator();
      appl->begin(prev);
      for (int j = 0; j < 5; ++j)
        {
          appl->finish(got);
          UNIT_TEST_CHECK(got == prev);
          next = prev;
          xdelta_randomly_change(next);
          xdelta_randomly_insert(next);
          xdelta_randomly_delete(next);
          string delta;
          compute_delta(prev, next, delta);
          apply_delta(appl, delta);
          appl->next();
          prev = next;
        }
      appl->finish(got);
      UNIT_TEST_CHECK(got == prev);
  }
}

UNIT_TEST(rolling_sanity_check)
{
  const unsigned testbufsize = 512;
  static const string::size_type blocksz = 64;
  char testbuf[testbufsize];

  for(unsigned i = 0; i < testbufsize; ++i)
    {
      testbuf[i] = xdelta_chargen(xdelta_prng);
    }
  for(unsigned advanceby = 0; advanceby < testbufsize; ++advanceby)
    {
      adler32 incremental(reinterpret_cast<u8 const *>(testbuf), blocksz);
      for(unsigned i = 0; i < advanceby; ++i)
        {
          incremental.out(static_cast<u8>(testbuf[i]));
          if ((i + blocksz) < testbufsize)
            {
              incremental.in(static_cast<u8>(testbuf[i+blocksz]));
            }
        }
      adler32 skip(reinterpret_cast<u8 const *>(testbuf), blocksz);
      u32 new_lo = advanceby;
      u32 new_hi = new_lo + blocksz;
      if (new_hi > testbufsize)
        {
          new_hi = testbufsize;
        }
      skip.replace_with(reinterpret_cast<u8 const *>(testbuf + new_lo), new_hi - new_lo);

      UNIT_TEST_CHECK(skip.sum() == incremental.sum());
    }
  L(FL("rolling sanity check passed"));
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: