Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: components/metrics/compression_utils_unittest.cc

Issue 1304563003: Move //components/metrics/compression_utils into standalone component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/metrics/compression_utils.cc ('k') | components/metrics/metrics_service_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "components/metrics/compression_utils.h"
6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace metrics {
14
15 namespace {
16
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};
22
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};
32
33 } // namespace
34
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);
43 }
44
45 TEST(CompressionUtilsTest, GzipUncompression) {
46 std::string compressed_data(reinterpret_cast<const char*>(kCompressedData),
47 arraysize(kCompressedData));
48
49 std::string uncompressed_data;
50 EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
51
52 std::string golden_data(reinterpret_cast<const char*>(kData),
53 arraysize(kData));
54 EXPECT_EQ(golden_data, uncompressed_data);
55 }
56
57 // Checks that compressing/decompressing input > 256 bytes works as expected.
58 TEST(CompressionUtilsTest, LargeInput) {
59 const size_t kSize = 32 * 1024;
60
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);
66
67 std::string compressed_data;
68 EXPECT_TRUE(GzipCompress(data, &compressed_data));
69
70 std::string uncompressed_data;
71 EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
72
73 EXPECT_EQ(data, uncompressed_data);
74 }
75
76 TEST(CompressionUtilsTest, InPlace) {
77 const std::string original_data(reinterpret_cast<const char*>(kData),
78 arraysize(kData));
79 const std::string golden_compressed_data(
80 reinterpret_cast<const char*>(kCompressedData),
81 arraysize(kCompressedData));
82
83 std::string data(original_data);
84 EXPECT_TRUE(GzipCompress(data, &data));
85 EXPECT_EQ(golden_compressed_data, data);
86 EXPECT_TRUE(GzipUncompress(data, &data));
87 EXPECT_EQ(original_data, data);
88 }
89
90 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/compression_utils.cc ('k') | components/metrics/metrics_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698