diff options
Diffstat (limited to 'testing')
-rwxr-xr-x | testing/Makefile | 15 | ||||
-rw-r--r-- | testing/cmp.h | 64 | ||||
-rw-r--r-- | testing/delta.h | 79 | ||||
-rw-r--r-- | testing/file.h | 367 | ||||
-rw-r--r-- | testing/modify.h | 421 | ||||
-rw-r--r-- | testing/random.h | 140 | ||||
-rw-r--r-- | testing/segment.h | 100 | ||||
-rw-r--r-- | testing/sizes.h | 69 | ||||
-rw-r--r-- | testing/test.h | 110 |
9 files changed, 0 insertions, 1365 deletions
diff --git a/testing/Makefile b/testing/Makefile deleted file mode 100755 index 281ef11..0000000 --- a/testing/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -CFLAGS = -g -Wall -I.. -DXD3_DEBUG=1 -#CFLAGS = -g -Wall -I.. -DXD3_DEBUG=2 -#CFLAGS = -O2 -Wall -I.. -DXD3_DEBUG=0 -DNDEBUG=1 - -DEPS = ../*.h ../*.c *.cc *.h - -TARGETS = xdelta3-regtest - -all: $(TARGETS) - -xdelta3-regtest: $(DEPS) - $(CXX) $(CFLAGS) regtest.cc -o xdelta3-regtest - -clean: - rm -f *.exe *.stackdump $(TARGETS) diff --git a/testing/cmp.h b/testing/cmp.h deleted file mode 100644 index d96c386..0000000 --- a/testing/cmp.h +++ /dev/null @@ -1,64 +0,0 @@ -/* -*- Mode: C++ -*- */ -namespace regtest { - -inline size_t CmpDifferentBlockBytes(const Block &a, const Block &b) { - size_t total = 0; - size_t i = 0; - size_t m = min(a.Size(), b.Size()); - - for (; i < m; i++) { - if (a[i] != b[i]) { - total++; - } - } - - total += a.Size() - i; - total += b.Size() - i; - - return total; -} - -inline xoff_t CmpDifferentBytes(const FileSpec &a, const FileSpec &b) { - Block block_a, block_b; - xoff_t total = 0; - FileSpec::iterator a_i(a), b_i(b); - - for (; !a_i.Done() && !b_i.Done(); a_i.Next(), b_i.Next()) { - - a_i.Get(&block_a); - b_i.Get(&block_b); - - total += CmpDifferentBlockBytes(block_a, block_b); - } - - for (; !a_i.Done(); a_i.Next()) { - total += a_i.BytesOnBlock(); - } - for (; !b_i.Done(); b_i.Next()) { - total += b_i.BytesOnBlock(); - } - - return total; -} - -inline bool ExtFile::EqualsSpec(const FileSpec &spec) const { - main_file t; - main_file_init(&t); - CHECK_EQ(0, main_file_open(&t, Name(), XO_READ)); - - Block tblock; - Block sblock; - for (BlockIterator iter(spec); !iter.Done(); iter.Next()) { - iter.Get(&sblock); - tblock.SetSize(sblock.Size()); - usize_t tread; - CHECK_EQ(0, main_file_read(&t, tblock.Data(), tblock.Size(), &tread, "read failed")); - CHECK_EQ(0, CmpDifferentBlockBytes(tblock, sblock)); - } - - CHECK_EQ(0, main_file_close(&t)); - main_file_cleanup(&t); - return true; -} - -} // namespace regtest diff --git a/testing/delta.h b/testing/delta.h deleted file mode 100644 index 58fbaac..0000000 --- a/testing/delta.h +++ /dev/null @@ -1,79 +0,0 @@ -// Mode: -*- C++ -*- - -namespace regtest { - -class Delta { -public: - Delta(const Block &block); - - ~Delta() { - xd3_free_stream(&stream_); - } - - xoff_t AddedBytes() const { - return stream_.whole_target.addslen; - } - - xoff_t Windows() const { - return stream_.whole_target.wininfolen; - } - - void Print() const; - -private: - xd3_stream stream_; -}; - -Delta::Delta(const Block &block) { - int ret; - xd3_config config; - memset(&stream_, 0, sizeof (stream_)); - memset(&config, 0, sizeof (config)); - - xd3_init_config(&config, XD3_SKIP_EMIT | XD3_ADLER32_NOVER); - - CHECK_EQ(0, xd3_config_stream (&stream_, &config)); - - xd3_avail_input (&stream_, block.Data(), block.Size()); - - bool done = false; - while (!done) { - ret = xd3_decode_input(&stream_); - - switch (ret) { - case XD3_INPUT: - done = true; - break; - case XD3_OUTPUT: - CHECK_EQ(0, xd3_whole_append_window (&stream_)); - break; - case XD3_GOTHEADER: - case XD3_WINSTART: - case XD3_WINFINISH: - break; - default: - DP(RINT "error code %s\n", xd3_strerror (ret)); - abort(); - } - } -} - -void Delta::Print() const { - for (size_t i = 0; i < stream_.whole_target.instlen; i++) { - xd3_winst &winst = stream_.whole_target.inst[i]; - switch (winst.type) { - case XD3_RUN: - DP(RINT "%"Q"u run %u\n", winst.position, winst.size); - break; - case XD3_ADD: - DP(RINT "%"Q"u add %u\n", winst.position, winst.size); - break; - default: - DP(RINT "%"Q"u copy %u @ %"Q"u (mode %u)\n", - winst.position, winst.size, winst.addr, winst.mode); - break; - } - } -} - -} // namespace diff --git a/testing/file.h b/testing/file.h deleted file mode 100644 index 30a8428..0000000 --- a/testing/file.h +++ /dev/null @@ -1,367 +0,0 @@ -/* -*- Mode: C++ -*- */ -namespace regtest { - -class Block; -class BlockIterator; -class TmpFile; - -class FileSpec { - public: - FileSpec(MTRandom *rand) - : rand_(rand) { - } - - // Generates a file with a known size - void GenerateFixedSize(xoff_t size) { - Reset(); - - for (xoff_t p = 0; p < size; ) { - xoff_t t = min(Constants::BLOCK_SIZE, size - p); - table_.insert(make_pair(p, Segment(t, rand_))); - p += t; - } - } - - // Generates a file with exponential-random distributed size - void GenerateRandomSize(xoff_t mean) { - GenerateFixedSize(rand_->ExpRand(mean)); - } - - // Returns the size of the file - xoff_t Size() const { - if (table_.empty()) { - return 0; - } - SegmentMap::const_iterator i = --table_.end(); - return i->first + i->second.Size(); - } - - // Returns the number of blocks - xoff_t Blocks(size_t blksize = Constants::BLOCK_SIZE) const { - if (table_.empty()) { - return 0; - } - return ((Size() - 1) / blksize) + 1; - } - - // Returns the number of segments - xoff_t Segments() const { - return table_.size(); - } - - // Create a mutation according to "what". - void ModifyTo(const Mutator &mutator, - FileSpec *modify) const { - modify->Reset(); - mutator.Mutate(&modify->table_, &table_, rand_); - modify->CheckSegments(); - } - - void CheckSegments() const { - for (SegmentMap::const_iterator iter(table_.begin()); - iter != table_.end(); ) { - SegmentMap::const_iterator iter0(iter++); - if (iter == table_.end()) { - break; - } - CHECK_EQ(iter0->first + iter0->second.Size(), iter->first); - } - } - - void Reset() { - table_.clear(); - } - - void Print() const { - for (SegmentMap::const_iterator iter(table_.begin()); - iter != table_.end(); - ++iter) { - const Segment &seg = iter->second; - cerr << "Segment at " << iter->first << " (" << seg << ")" << endl; - } - } - - void PrintData() const; - - void WriteTmpFile(TmpFile *f) const; - - typedef BlockIterator iterator; - - private: - friend class BlockIterator; - - MTRandom *rand_; - SegmentMap table_; -}; - -class Block { -public: - Block() - : data_(NULL), - data_size_(0), - size_(0) { } - - ~Block() { - if (data_) { - delete [] data_; - } - } - - size_t Size() const { - return size_; - } - - uint8_t operator[](size_t i) const { - CHECK_LT(i, size_); - return data_[i]; - } - - uint8_t* Data() const { - if (data_ == NULL) { - CHECK_EQ(0, size_); - data_size_ = 1; - data_ = new uint8_t[1]; - } - return data_; - } - - // For writing to blocks - void Append(const uint8_t *data, size_t size); - - // For cleaing a block - void Reset() { - size_ = 0; - } - - void Print() const; - - void WriteTmpFile(TmpFile *f) const; - - void SetSize(size_t size) { - size_ = size; - - if (data_size_ < size) { - if (data_) { - delete [] data_; - } - data_ = new uint8_t[size]; - data_size_ = size; - } - } -private: - friend class BlockIterator; - - mutable uint8_t *data_; - mutable size_t data_size_; - size_t size_; -}; - -class BlockIterator { -public: - explicit BlockIterator(const FileSpec& spec) - : spec_(spec), - blkno_(0), - blksize_(Constants::BLOCK_SIZE) { } - - BlockIterator(const FileSpec& spec, - size_t blksize) - : spec_(spec), - blkno_(0), - blksize_(blksize) { } - - bool Done() const { - return blkno_ >= spec_.Blocks(blksize_); - } - - void Next() { - blkno_++; - } - - xoff_t Blkno() const { - return blkno_; - } - - xoff_t Offset() const { - return blkno_ * blksize_; - } - - void SetBlock(xoff_t blkno) { - blkno_ = blkno; - } - - void Get(Block *block) const; - - size_t BytesOnBlock() const { - xoff_t blocks = spec_.Blocks(blksize_); - xoff_t size = spec_.Size(); - - CHECK((blkno_ < blocks) || - (blkno_ == blocks && size % blksize_ == 0)); - - if (blkno_ == blocks) { - return 0; - } - if (blkno_ + 1 == blocks) { - return ((size - 1) % blksize_) + 1; - } - return blksize_; - } - - size_t BlockSize() const { - return blksize_; - } - -private: - const FileSpec& spec_; - xoff_t blkno_; - size_t blksize_; -}; - -class ExtFile { -public: - ExtFile() { - static int static_counter = 0; - char buf[32]; - snprintf(buf, 32, "/tmp/regtest.%d", static_counter++); - filename_.append(buf); - unlink(filename_.c_str()); - } - - ~ExtFile() { - unlink(filename_.c_str()); - } - - const char* Name() const { - return filename_.c_str(); - } - - // Check whether a real file matches a file spec. - bool EqualsSpec(const FileSpec &spec) const; - -protected: - string filename_; -}; - -class TmpFile : public ExtFile { -public: - // TODO this is a little unportable! - TmpFile() { - main_file_init(&file_); - CHECK_EQ(0, main_file_open(&file_, filename_.c_str(), XO_WRITE)); - } - - ~TmpFile() { - main_file_cleanup(&file_); - } - - void Append(const Block *block) { - CHECK_EQ(0, main_file_write(&file_, - block->Data(), block->Size(), - "tmpfile write failed")); - } - - - const char* Name() const { - if (main_file_isopen(&file_)) { - CHECK_EQ(0, main_file_close(&file_)); - } - return ExtFile::Name(); - } - -private: - mutable main_file file_; -}; - -inline void BlockIterator::Get(Block *block) const { - xoff_t offset = blkno_ * blksize_; - const SegmentMap &table = spec_.table_; - size_t got = 0; - block->SetSize(BytesOnBlock()); - - SegmentMap::const_iterator pos = table.upper_bound(offset); - if (pos == table.begin()) { - CHECK_EQ(0, spec_.Size()); - return; - } - --pos; - - while (got < block->size_) { - CHECK(pos != table.end()); - CHECK_GE(offset, pos->first); - - const Segment &seg = pos->second; - - // The position of this segment may start before this block starts, - // and then the position of the data may be offset from the seeding - // position. - size_t seg_offset = offset - pos->first; - size_t advance = min(seg.Size() - seg_offset, - blksize_ - got); - - seg.Fill(seg_offset, advance, block->data_ + got); - - got += advance; - offset += advance; - ++pos; - } -} - -inline void Block::Append(const uint8_t *data, size_t size) { - if (data_ == NULL) { - CHECK_EQ(0, size_); - CHECK_EQ(0, data_size_); - data_ = new uint8_t[Constants::BLOCK_SIZE]; - data_size_ = Constants::BLOCK_SIZE; - } - - if (size_ + size > data_size_) { - uint8_t *tmp = data_; - while (size_ + size > data_size_) { - data_size_ *= 2; - } - data_ = new uint8_t[data_size_]; - memcpy(data_, tmp, size_); - delete tmp; - } - - memcpy(data_ + size_, data, size); - size_ += size; -} - -inline void FileSpec::PrintData() const { - Block block; - for (BlockIterator iter(*this); !iter.Done(); iter.Next()) { - iter.Get(&block); - block.Print(); - } -} - -inline void Block::Print() const { - xoff_t pos = 0; - for (size_t i = 0; i < Size(); i++) { - if (pos % 16 == 0) { - DP(RINT "%5"Q"x: ", pos); - } - DP(RINT "%02x ", (*this)[i]); - if (pos % 16 == 15) { - DP(RINT "\n"); - } - pos++; - } - DP(RINT "\n"); -} - -inline void FileSpec::WriteTmpFile(TmpFile *f) const { - Block block; - for (BlockIterator iter(*this); !iter.Done(); iter.Next()) { - iter.Get(&block); - f->Append(&block); - } -} - -inline void Block::WriteTmpFile(TmpFile *f) const { - f->Append(this); -} - -} // namespace regtest - diff --git a/testing/modify.h b/testing/modify.h deleted file mode 100644 index 67cccd9..0000000 --- a/testing/modify.h +++ /dev/null @@ -1,421 +0,0 @@ -// -*- Mode: C++ -*- -namespace regtest { - -class Mutator { -public: - virtual ~Mutator() { } - virtual void Mutate(SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) const = 0; -}; - -class Change { -public: - enum Kind { - MODIFY = 1, - ADD = 2, - DELETE = 3, - MOVE = 4, - COPY = 5, - OVERWRITE = 6, - }; - - // Constructor for modify, add, delete. - Change(Kind kind, xoff_t size, xoff_t addr1) - : kind(kind), - size(size), - addr1(addr1), - insert(NULL) { - CHECK(kind != MOVE && kind != COPY && kind != OVERWRITE); - } - - // Constructor for modify, add w/ provided data. - Change(Kind kind, xoff_t size, xoff_t addr1, Segment *insert) - : kind(kind), - size(size), - addr1(addr1), - insert(insert) { - CHECK(kind != MOVE && kind != COPY && kind != OVERWRITE); - } - - // Constructor for move - Change(Kind kind, xoff_t size, xoff_t addr1, xoff_t addr2) - : kind(kind), - size(size), - addr1(addr1), - addr2(addr2), - insert(NULL) { - CHECK(kind == MOVE || kind == COPY || kind == OVERWRITE); - } - - Kind kind; - xoff_t size; - xoff_t addr1; - xoff_t addr2; - Segment *insert; // For modify and/or add -}; - -typedef list<Change> ChangeList; - -class ChangeListMutator : public Mutator { -public: - ChangeListMutator(const ChangeList &cl) - : cl_(cl) { } - - ChangeListMutator() { } - - void Mutate(SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) const; - - static void Mutate(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand); - - static void AddChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand); - - static void ModifyChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand); - - static void DeleteChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand); - - static void MoveChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand); - - static void OverwriteChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand); - - static void CopyChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand); - - static void AppendCopy(SegmentMap *table, - const SegmentMap *source_table, - xoff_t copy_offset, - xoff_t append_offset, - xoff_t length); - - ChangeList* Changes() { - return &cl_; - } - - const ChangeList* Changes() const { - return &cl_; - } - -private: - ChangeList cl_; -}; - -void ChangeListMutator::Mutate(SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) const { - // The speed of processing gigabytes of data is so slow compared with - // these table-copy operations, no attempt to make this fast. - SegmentMap tmp; - - for (ChangeList::const_iterator iter(cl_.begin()); iter != cl_.end(); ++iter) { - const Change &ch = *iter; - tmp.clear(); - Mutate(ch, &tmp, source_table, rand); - tmp.swap(*table); - source_table = table; - } -} - -void ChangeListMutator::Mutate(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) { - switch (ch.kind) { - case Change::ADD: - AddChange(ch, table, source_table, rand); - break; - case Change::MODIFY: - ModifyChange(ch, table, source_table, rand); - break; - case Change::DELETE: - DeleteChange(ch, table, source_table, rand); - break; - case Change::COPY: - CopyChange(ch, table, source_table, rand); - break; - case Change::MOVE: - MoveChange(ch, table, source_table, rand); - break; - case Change::OVERWRITE: - OverwriteChange(ch, table, source_table, rand); - break; - } -} - -void ChangeListMutator::ModifyChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) { - xoff_t m_start = ch.addr1; - xoff_t m_end = m_start + ch.size; - xoff_t i_start = 0; - xoff_t i_end = 0; - - for (SegmentMap::const_iterator iter(source_table->begin()); - iter != source_table->end(); - ++iter) { - const Segment &seg = iter->second; - i_start = iter->first; - i_end = i_start + seg.Size(); - - if (i_end <= m_start || i_start >= m_end) { - table->insert(table->end(), make_pair(i_start, seg)); - continue; - } - - if (i_start < m_start) { - table->insert(table->end(), - make_pair(i_start, - seg.Subseg(0, m_start - i_start))); - } - - // Insert the entire segment, even though it may extend into later - // segments. This condition avoids inserting it during later - // segments. - if (m_start >= i_start) { - if (ch.insert != NULL) { - table->insert(table->end(), make_pair(m_start, *ch.insert)); - } else { - Segment part(m_end - m_start, rand); - table->insert(table->end(), make_pair(m_start, part)); - } - } - - if (i_end > m_end) { - table->insert(table->end(), - make_pair(m_end, - seg.Subseg(m_end - i_start, i_end - m_end))); - } - } - - CHECK_LE(m_end, i_end); -} - -void ChangeListMutator::AddChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) { - xoff_t m_start = ch.addr1; - xoff_t i_start = 0; - xoff_t i_end = 0; - - for (SegmentMap::const_iterator iter(source_table->begin()); - iter != source_table->end(); - ++iter) { - const Segment &seg = iter->second; - i_start = iter->first; - i_end = i_start + seg.Size(); - - if (i_end <= m_start) { - table->insert(table->end(), make_pair(i_start, seg)); - continue; - } - - if (i_start > m_start) { - table->insert(table->end(), make_pair(i_start + ch.size, seg)); - continue; - } - - if (i_start < m_start) { - table->insert(table->end(), - make_pair(i_start, - seg.Subseg(0, m_start - i_start))); - } - - if (ch.insert != NULL) { - table->insert(table->end(), make_pair(m_start, *ch.insert)); - } else { - Segment addseg(ch.size, rand); - table->insert(table->end(), make_pair(m_start, addseg)); - } - - if (m_start < i_end) { - table->insert(table->end(), - make_pair(m_start + ch.size, - seg.Subseg(m_start - i_start, i_end - m_start))); - } - } - - CHECK_LE(m_start, i_end); - - // Special case for add at end-of-input. - if (m_start == i_end) { - Segment addseg(ch.size, rand); - table->insert(table->end(), make_pair(m_start, addseg)); - } -} - -void ChangeListMutator::DeleteChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) { - xoff_t m_start = ch.addr1; - xoff_t m_end = m_start + ch.size; - xoff_t i_start = 0; - xoff_t i_end = 0; - - for (SegmentMap::const_iterator iter(source_table->begin()); - iter != source_table->end(); - ++iter) { - const Segment &seg = iter->second; - i_start = iter->first; - i_end = i_start + seg.Size(); - - if (i_end <= m_start) { - table->insert(table->end(), make_pair(i_start, seg)); - continue; - } - - if (i_start >= m_end) { - table->insert(table->end(), make_pair(i_start - ch.size, seg)); - continue; - } - - if (i_start < m_start) { - table->insert(table->end(), - make_pair(i_start, - seg.Subseg(0, m_start - i_start))); - } - - if (i_end > m_end) { - table->insert(table->end(), - make_pair(m_end - ch.size, - seg.Subseg(m_end - i_start, i_end - m_end))); - } - } - - CHECK_LT(m_start, i_end); - CHECK_LE(m_end, i_end); -} - -void ChangeListMutator::MoveChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) { - SegmentMap tmp; - CHECK_NE(ch.addr1, ch.addr2); - CopyChange(ch, &tmp, source_table, rand); - Change d(Change::DELETE, ch.size, - ch.addr1 < ch.addr2 ? ch.addr1 : ch.addr1 + ch.size); - DeleteChange(d, table, &tmp, rand); -} - -void ChangeListMutator::OverwriteChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) { - SegmentMap tmp; - CHECK_NE(ch.addr1, ch.addr2); - CopyChange(ch, &tmp, source_table, rand); - Change d(Change::DELETE, ch.size, ch.addr2 + ch.size); - DeleteChange(d, table, &tmp, rand); -} - -void ChangeListMutator::CopyChange(const Change &ch, - SegmentMap *table, - const SegmentMap *source_table, - MTRandom *ignore) { - xoff_t m_start = ch.addr2; - xoff_t c_start = ch.addr1; - xoff_t i_start = 0; - xoff_t i_end = 0; - - // Like AddChange() with AppendCopy instead of a random segment. - for (SegmentMap::const_iterator iter(source_table->begin()); - iter != source_table->end(); - ++iter) { - const Segment &seg = iter->second; - i_start = iter->first; - i_end = i_start + seg.Size(); - - if (i_end <= m_start) { - table->insert(table->end(), make_pair(i_start, seg)); - continue; - } - - if (i_start > m_start) { - table->insert(table->end(), make_pair(i_start + ch.size, seg)); - continue; - } - - if (i_start < m_start) { - table->insert(table->end(), - make_pair(i_start, - seg.Subseg(0, m_start - i_start))); - } - - AppendCopy(table, source_table, c_start, m_start, ch.size); - - if (m_start < i_end) { - table->insert(table->end(), - make_pair(m_start + ch.size, - seg.Subseg(m_start - i_start, i_end - m_start))); - } - } - - CHECK_LE(m_start, i_end); - - // Special case for copy to end-of-input. - if (m_start == i_end) { - AppendCopy(table, source_table, c_start, m_start, ch.size); - } -} - -void ChangeListMutator::AppendCopy(SegmentMap *table, - const SegmentMap *source_table, - xoff_t copy_offset, - xoff_t append_offset, - xoff_t length) { - SegmentMap::const_iterator pos(source_table->upper_bound(copy_offset)); - --pos; - xoff_t got = 0; - - while (got < length) { - size_t seg_offset = copy_offset - pos->first; - size_t advance = min(pos->second.Size() - seg_offset, - (size_t)(length - got)); - - table->insert(table->end(), - make_pair(append_offset, - pos->second.Subseg(seg_offset, - advance))); - - got += advance; - copy_offset += advance; - append_offset += advance; - ++pos; - } -} - -class Modify1stByte : public Mutator { -public: - void Mutate(SegmentMap *table, - const SegmentMap *source_table, - MTRandom *rand) const { - ChangeListMutator::Mutate(Change(Change::MODIFY, 1, 0), - table, source_table, rand); - } -}; - -} // namespace regtest diff --git a/testing/random.h b/testing/random.h deleted file mode 100644 index f2cb167..0000000 --- a/testing/random.h +++ /dev/null @@ -1,140 +0,0 @@ -/* -*- Mode: C++ -*- */ -/* This is public-domain Mersenne Twister code, - * attributed to Michael Brundage. Thanks! - * http://www.qbrundage.com/michaelb/pubs/essays/random_number_generation.html - */ -#include <math.h> - -namespace regtest { - -class MTRandom { - public: - static const uint32_t TEST_SEED1 = 5489UL; - - static const int MT_LEN = 624; - static const int MT_IA = 397; - static const uint32_t UPPER_MASK = 0x80000000; - static const uint32_t LOWER_MASK = 0x7FFFFFFF; - static const uint32_t MATRIX_A = 0x9908B0DF; - - MTRandom() { - Init(TEST_SEED1); - } - - MTRandom(uint32_t seed) { - Init(seed); - } - - uint32_t Rand32 () { - uint32_t y; - static unsigned long mag01[2] = { - 0 , MATRIX_A - }; - - if (mt_index_ >= MT_LEN) { - int kk; - - for (kk = 0; kk < MT_LEN - MT_IA; kk++) { - y = (mt_buffer_[kk] & UPPER_MASK) | (mt_buffer_[kk + 1] & LOWER_MASK); - mt_buffer_[kk] = mt_buffer_[kk + MT_IA] ^ (y >> 1) ^ mag01[y & 0x1UL]; - } - for (;kk < MT_LEN - 1; kk++) { - y = (mt_buffer_[kk] & UPPER_MASK) | (mt_buffer_[kk + 1] & LOWER_MASK); - mt_buffer_[kk] = mt_buffer_[kk + (MT_IA - MT_LEN)] ^ (y >> 1) ^ mag01[y & 0x1UL]; - } - y = (mt_buffer_[MT_LEN - 1] & UPPER_MASK) | (mt_buffer_[0] & LOWER_MASK); - mt_buffer_[MT_LEN - 1] = mt_buffer_[MT_IA - 1] ^ (y >> 1) ^ mag01[y & 0x1UL]; - - mt_index_ = 0; - } - - y = mt_buffer_[mt_index_++]; - - y ^= (y >> 11); - y ^= (y << 7) & 0x9d2c5680UL; - y ^= (y << 15) & 0xefc60000UL; - y ^= (y >> 18); - - return y; - } - - uint32_t ExpRand32(uint32_t mean) { - double mean_d = mean; - double erand = log (1.0 / (Rand32() / (double)UINT32_MAX)); - uint32_t x = (uint32_t) (mean_d * erand + 0.5); - return x; - } - - uint64_t Rand64() { - return ((uint64_t)Rand32() << 32) | Rand32(); - } - - uint64_t ExpRand64(uint64_t mean) { - double mean_d = mean; - double erand = log (1.0 / (Rand64() / (double)UINT32_MAX)); - uint64_t x = (uint64_t) (mean_d * erand + 0.5); - return x; - } - - template <typename T> - T Rand() { - switch (sizeof(T)) { - case sizeof(uint32_t): - return Rand32(); - case sizeof(uint64_t): - return Rand64(); - default: - cerr << "Invalid sizeof T" << endl; - abort(); - } - } - - template <typename T> - T ExpRand(T mean) { - switch (sizeof(T)) { - case sizeof(uint32_t): - return ExpRand32(mean); - case sizeof(uint64_t): - return ExpRand64(mean); - default: - cerr << "Invalid sizeof T" << endl; - abort(); - } - } - - private: - void Init(uint32_t seed) { - mt_buffer_[0] = seed; - mt_index_ = MT_LEN; - for (int i = 1; i < MT_LEN; i++) { - /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ - /* In the previous versions, MSBs of the seed affect */ - /* only MSBs of the array mt[]. */ - /* 2002/01/09 modified by Makoto Matsumoto */ - mt_buffer_[i] = - (1812433253UL * (mt_buffer_[i-1] ^ (mt_buffer_[i-1] >> 30)) + i); - } - } - - int mt_index_; - uint32_t mt_buffer_[MT_LEN]; -}; - -class MTRandom8 { -public: - MTRandom8(MTRandom *rand) - : rand_(rand) { - } - - uint8_t Rand8() { - uint32_t r = rand_->Rand32(); - - // TODO: make this use a single byte at a time? - return (r & 0xff) ^ (r >> 7) ^ (r >> 15) ^ (r >> 21); - } - -private: - MTRandom *rand_; -}; - -} // namespace regtest diff --git a/testing/segment.h b/testing/segment.h deleted file mode 100644 index 1dabf5c..0000000 --- a/testing/segment.h +++ /dev/null @@ -1,100 +0,0 @@ -// -*- Mode: C++ -*- - -namespace regtest { - -class Segment { - public: - Segment(size_t size, MTRandom *rand) - : size_(size), - seed_(rand->Rand32()), - seed_offset_(0), - data_(NULL) { - CHECK_GT(size_, 0); - } - - Segment(size_t size, uint32_t seed) - : size_(size), - seed_(seed), - seed_offset_(0), - data_(NULL) { - CHECK_GT(size_, 0); - } - - Segment(size_t size, uint8_t *data) - : size_(size), - seed_(0), - seed_offset_(0), - data_(data) { - CHECK_GT(size_, 0); - } - - size_t Size() const { - return size_; - } - - Segment Subseg(size_t start, size_t size) const { - CHECK_LE(start + size, size_); - if (data_) { - return Segment(size, data_ + start); - } else { - return Segment(size, seed_, seed_offset_ + start); - } - } - - void Fill(size_t seg_offset, size_t size, uint8_t *data) const { - CHECK_LE(seg_offset + size, size_); - if (data_) { - memcpy(data, data_ + seg_offset, size); - } else { - size_t skip = seg_offset + seed_offset_; - MTRandom gen(seed_); - MTRandom8 gen8(&gen); - while (skip--) { - gen8.Rand8(); - } - for (size_t i = 0; i < size; i++) { - data[i] = gen8.Rand8(); - } - } - } - -private: - // Used by Subseg() - Segment(size_t size, uint32_t seed, size_t seed_offset) - : size_(size), - seed_(seed), - seed_offset_(seed_offset), - data_(NULL) { - CHECK_GT(size_, 0); - } - - friend ostream& operator<<(ostream& os, const Segment &seg); - - size_t size_; // Size of this segment - - // For random segments - uint32_t seed_; // Seed used for generating byte sequence - size_t seed_offset_; // Seed positions the sequence this many bytes - // before its beginning. - - // For literal segments (data is not owned) - uint8_t *data_; -}; - -ostream& operator<<(ostream& os, const Segment &seg) { - if (seg.data_) { - for (size_t i = 0; i < seg.size_; i++) { - char buf[10]; - sprintf(buf, "%02x ", seg.data_[i]); - os << buf; - } - return os; - } else { - return os << "size=" << seg.size_ << ",seed=" << seg.seed_ - << ",skip=" << seg.seed_offset_; - } -} - -typedef map<xoff_t, Segment> SegmentMap; - -} // namespace regtest diff --git a/testing/sizes.h b/testing/sizes.h deleted file mode 100644 index 6b70892..0000000 --- a/testing/sizes.h +++ /dev/null @@ -1,69 +0,0 @@ -// -*- Mode: C++ -*- -namespace regtest { - -template <typename T, typename U> -class SizeIterator { - public: - SizeIterator(MTRandom *rand, size_t howmany) - : rand_(rand), - count_(0), - fixed_(U::sizes), - fixed_size_(SIZEOF_ARRAY(U::sizes)), - howmany_(howmany) { } - - T Get() { - if (count_ < fixed_size_) { - return fixed_[count_]; - } - return rand_->Rand<T>() % U::max_value; - } - - bool Done() { - return count_ >= fixed_size_ && count_ >= howmany_; - } - - void Next() { - count_++; - } - - private: - MTRandom *rand_; - size_t count_; - T* fixed_; - size_t fixed_size_; - size_t howmany_; -}; - -class SmallSizes { -public: - static size_t sizes[]; - static size_t max_value; -}; - -size_t SmallSizes::sizes[] = { - 0, 1, Constants::BLOCK_SIZE / 4, 3333, - Constants::BLOCK_SIZE - (Constants::BLOCK_SIZE / 3), - Constants::BLOCK_SIZE, - Constants::BLOCK_SIZE + (Constants::BLOCK_SIZE / 3), - 2 * Constants::BLOCK_SIZE - (Constants::BLOCK_SIZE / 3), - 2 * Constants::BLOCK_SIZE, - 2 * Constants::BLOCK_SIZE + (Constants::BLOCK_SIZE / 3), -}; - -size_t SmallSizes::max_value = Constants::BLOCK_SIZE * 3; - -class LargeSizes { -public: - static size_t sizes[]; - static size_t max_value; -}; - -size_t LargeSizes::sizes[] = { - 1 << 20, - 1 << 18, - 1 << 16, -}; - -size_t LargeSizes::max_value = 1<<20; - -} // namespace regtest diff --git a/testing/test.h b/testing/test.h deleted file mode 100644 index f2b46f3..0000000 --- a/testing/test.h +++ /dev/null @@ -1,110 +0,0 @@ -// -*- Mode: C++ -*- - -extern "C" { -#define NOT_MAIN 1 -#define REGRESSION_TEST 0 -#define VCDIFF_TOOLS 1 -#include "../xdelta3.c" -} - -#define CHECK_EQ(x,y) CHECK_OP(x,y,==) -#define CHECK_NE(x,y) CHECK_OP(x,y,!=) -#define CHECK_LT(x,y) CHECK_OP(x,y,<) -#define CHECK_GT(x,y) CHECK_OP(x,y,>) -#define CHECK_LE(x,y) CHECK_OP(x,y,<=) -#define CHECK_GE(x,y) CHECK_OP(x,y,>=) - -#define CHECK_OP(x,y,OP) \ - do { \ - typeof(x) _x(x); \ - typeof(x) _y(y); \ - if (!(_x OP _y)) { \ - cerr << __FILE__ << ":" << __LINE__ << " Check failed: " << #x " " #OP " " #y << endl; \ - cerr << __FILE__ << ":" << __LINE__ << " Expected: " << _x << endl; \ - cerr << __FILE__ << ":" << __LINE__ << " Actual: " << _y << endl; \ - abort(); \ - } } while (false) - -#define CHECK(x) \ - do {if (!(x)) { \ - cerr << __FILE__ << ":" << __LINE__ << " Check failed: " << #x << endl; \ - abort(); \ - } } while (false) - -#include <string> -using std::string; - -#include <vector> -using std::vector; - -inline string CommandToString(const vector<const char*> &v) { - string s(v[0]); - for (size_t i = 1; i < v.size() && v[i] != NULL; i++) { - s.append(" "); - s.append(v[i]); - } - return s; -} - -#include <iostream> -using std::cerr; -using std::endl; -using std::ostream; - -#include <map> -using std::map; -using std::pair; - -#include <ext/hash_map> -using __gnu_cxx::hash_map; - -#include <list> -using std::list; - -template <typename T, typename U> -pair<T, U> make_pair(const T& t, const U& u) { - return pair<T, U>(t, u); -} - -class Constants { -public: - // TODO: need to repeat the tests with different block sizes - // 1 << 7 triggers some bugs, 1 << 20 triggers others. - // - //static const xoff_t BLOCK_SIZE = 1 << 20; - static const xoff_t BLOCK_SIZE = 1 << 7; -}; - -using std::min; - -#include "random.h" -using regtest::MTRandom; -using regtest::MTRandom8; - -#include "segment.h" -using regtest::Segment; - -#include "modify.h" -using regtest::Mutator; -using regtest::ChangeList; -using regtest::Change; -using regtest::ChangeListMutator; -using regtest::Modify1stByte; - -#include "file.h" -using regtest::Block; -using regtest::BlockIterator; -using regtest::ExtFile; -using regtest::FileSpec; -using regtest::TmpFile; - -#include "cmp.h" -using regtest::CmpDifferentBytes; - -#include "sizes.h" -using regtest::SizeIterator; -using regtest::SmallSizes; -using regtest::LargeSizes; - -#include "delta.h" -using regtest::Delta; |