Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / metrics / compression_utils_unittest.cc
blob797a7fb47ee1860dea0d654734c0f5c3cb6d72af
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/compression_utils.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace metrics {
15 namespace {
17 // The data to be compressed by gzip. This is the hex representation of "hello
18 // world".
19 const uint8 kData[] =
20 {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
21 0x72, 0x6c, 0x64};
23 // This is the string representation of gzip compressed string above. It was
24 // obtained by running echo -n "hello world" | gzip -c | hexdump -e '8 1 ",
25 // 0x%x"' followed by 0'ing out the OS byte (10th byte) in the header. This is
26 // so that the test passes on all platforms (that run various OS'es).
27 const uint8 kCompressedData[] =
28 {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf,
30 0x2f, 0xca, 0x49, 0x01, 0x00, 0x85, 0x11, 0x4a, 0x0d,
31 0x0b, 0x00, 0x00, 0x00};
33 } // namespace
35 TEST(CompressionUtilsTest, GzipCompression) {
36 std::string data(reinterpret_cast<const char*>(kData), arraysize(kData));
37 std::string compressed_data;
38 EXPECT_TRUE(GzipCompress(data, &compressed_data));
39 std::string golden_compressed_data(
40 reinterpret_cast<const char*>(kCompressedData),
41 arraysize(kCompressedData));
42 EXPECT_EQ(golden_compressed_data, compressed_data);
45 TEST(CompressionUtilsTest, GzipUncompression) {
46 std::string compressed_data(reinterpret_cast<const char*>(kCompressedData),
47 arraysize(kCompressedData));
49 std::string uncompressed_data;
50 EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
52 std::string golden_data(reinterpret_cast<const char*>(kData),
53 arraysize(kData));
54 EXPECT_EQ(golden_data, uncompressed_data);
57 // Checks that compressing/decompressing input > 256 bytes works as expected.
58 TEST(CompressionUtilsTest, LargeInput) {
59 const size_t kSize = 32 * 1024;
61 // Generate a data string of |kSize| for testing.
62 std::string data;
63 data.resize(kSize);
64 for (size_t i = 0; i < kSize; ++i)
65 data[i] = static_cast<char>(i & 0xFF);
67 std::string compressed_data;
68 EXPECT_TRUE(GzipCompress(data, &compressed_data));
70 std::string uncompressed_data;
71 EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
73 EXPECT_EQ(data, uncompressed_data);
76 } // namespace metrics