Ai
2 Star 0 Fork 0

mirrors_android_source/bsdiff

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bsdiff_unittest.cc 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
Alex Deymo 提交于 2018-02-08 22:50 +08:00 . Allow to set the minimum required match length.
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "bsdiff/bsdiff.h"
#include <gtest/gtest.h>
#include <algorithm>
#include <random>
#include <string>
#include <vector>
#include "bsdiff/fake_patch_writer.h"
namespace {
// Generate deterministic random data in the output buffer. The buffer must be
// already allocated with the desired size. The data generated depends on the
// selected size.
void GenerateRandomBuffer(std::vector<uint8_t>* buffer) {
std::minstd_rand prng(1234 + buffer->size());
std::generate(buffer->begin(), buffer->end(), prng);
}
} // namespace
namespace bsdiff {
class BsdiffTest : public testing::Test {
protected:
BsdiffTest() = default;
~BsdiffTest() override = default;
void RunBsdiff() {
EXPECT_EQ(0, bsdiff(old_file_.data(), old_file_.size(), new_file_.data(),
new_file_.size(), min_len_, &patch_writer_, nullptr));
}
std::vector<uint8_t> old_file_;
std::vector<uint8_t> new_file_;
size_t min_len_ = 0; // 0 means the default.
FakePatchWriter patch_writer_;
};
// Check that a file with no changes has a very small patch (no extra data).
TEST_F(BsdiffTest, EqualEmptyFiles) {
// Empty old and new files.
RunBsdiff();
// No entries should be generated on an empty new file.
EXPECT_TRUE(patch_writer_.entries().empty());
}
TEST_F(BsdiffTest, EqualSmallFiles) {
std::string some_text = "Hello world!";
old_file_.insert(old_file_.begin(), some_text.begin(), some_text.end());
new_file_.insert(new_file_.begin(), some_text.begin(), some_text.end());
RunBsdiff();
EXPECT_EQ(1U, patch_writer_.entries().size());
ControlEntry entry = patch_writer_.entries()[0];
EXPECT_EQ(some_text.size(), entry.diff_size);
EXPECT_EQ(0U, entry.extra_size);
}
TEST_F(BsdiffTest, FileWithSmallErrorsTest) {
old_file_.resize(100);
GenerateRandomBuffer(&old_file_);
new_file_ = old_file_;
// Break a few bytes somewhere in the middle.
new_file_[20]++;
new_file_[30] += 2;
new_file_[31] += 2;
RunBsdiff();
// We expect that the result has only one entry with all in the diff stream
// since the two files are very similar.
EXPECT_EQ(1U, patch_writer_.entries().size());
ControlEntry entry = patch_writer_.entries()[0];
EXPECT_EQ(100U, entry.diff_size);
EXPECT_EQ(0U, entry.extra_size);
}
TEST_F(BsdiffTest, MinLengthConsideredTest) {
old_file_.resize(100);
GenerateRandomBuffer(&old_file_);
new_file_ = old_file_;
// Copy the first 10 bytes to the middle.
for (size_t i = 0; i < 10; i++) {
new_file_[50 + i] = old_file_[i];
}
min_len_ = 12;
RunBsdiff();
// We expect that the 10 bytes in the middle that match the beginning are
// ignored and just emitted as diff data because the min_len is bigger than
// 10.
EXPECT_EQ(1U, patch_writer_.entries().size());
ControlEntry entry = patch_writer_.entries()[0];
EXPECT_EQ(100U, entry.diff_size);
EXPECT_EQ(0U, entry.extra_size);
}
} // namespace bsdiff
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_android_source/bsdiff.git
git@gitee.com:mirrors_android_source/bsdiff.git
mirrors_android_source
bsdiff
bsdiff
main

搜索帮助