/Users/deen/code/yugabyte-db/src/yb/util/inline_slice-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <vector> |
34 | | |
35 | | #include <gtest/gtest.h> |
36 | | |
37 | | #include "yb/util/inline_slice.h" |
38 | | #include "yb/util/memory/arena.h" |
39 | | |
40 | | namespace yb { |
41 | | |
42 | | template<size_t N> |
43 | | static void TestRoundTrip(InlineSlice<N> *slice, |
44 | | Arena *arena, |
45 | 6.00k | size_t test_size) { |
46 | 6.00k | std::unique_ptr<uint8_t[]> buf(new uint8_t[test_size]); |
47 | 3.00M | for (size_t i = 0; i < test_size; i++) { |
48 | 3.00M | buf[i] = i & 0xff; |
49 | 3.00M | } |
50 | | |
51 | 6.00k | Slice test_input(buf.get(), test_size); |
52 | | |
53 | 6.00k | slice->set(test_input, arena); |
54 | 6.00k | Slice ret = slice->as_slice(); |
55 | 12.0k | ASSERT_TRUE(ret == test_input) |
56 | 12.0k | << "test_size =" << test_size << "\n" |
57 | 12.0k | << "ret = " << ret.ToDebugString() << "\n" |
58 | 12.0k | << "test_input = " << test_input.ToDebugString(); |
59 | | |
60 | | // If the data is small enough to fit inline, then |
61 | | // the returned slice should point directly into the |
62 | | // InlineSlice object. |
63 | 6.00k | if (test_size < N) { |
64 | 69 | ASSERT_EQ(reinterpret_cast<const uint8_t *>(slice) + 1, |
65 | 69 | ret.data()); |
66 | 69 | } |
67 | 6.00k | } inline_slice-test.cc:_ZN2ybL13TestRoundTripILm8EEEvPNS_11InlineSliceIXT_ELb0EEEPNS_8internal9ArenaBaseINS4_11ArenaTraitsEEEm Line | Count | Source | 45 | 2.00k | size_t test_size) { | 46 | 2.00k | std::unique_ptr<uint8_t[]> buf(new uint8_t[test_size]); | 47 | 1.00M | for (size_t i = 0; i < test_size; i++) { | 48 | 1.00M | buf[i] = i & 0xff; | 49 | 1.00M | } | 50 | | | 51 | 2.00k | Slice test_input(buf.get(), test_size); | 52 | | | 53 | 2.00k | slice->set(test_input, arena); | 54 | 2.00k | Slice ret = slice->as_slice(); | 55 | 4.00k | ASSERT_TRUE(ret == test_input) | 56 | 4.00k | << "test_size =" << test_size << "\n" | 57 | 4.00k | << "ret = " << ret.ToDebugString() << "\n" | 58 | 4.00k | << "test_input = " << test_input.ToDebugString(); | 59 | | | 60 | | // If the data is small enough to fit inline, then | 61 | | // the returned slice should point directly into the | 62 | | // InlineSlice object. | 63 | 2.00k | if (test_size < N) { | 64 | 15 | ASSERT_EQ(reinterpret_cast<const uint8_t *>(slice) + 1, | 65 | 15 | ret.data()); | 66 | 15 | } | 67 | 2.00k | } |
inline_slice-test.cc:_ZN2ybL13TestRoundTripILm12EEEvPNS_11InlineSliceIXT_ELb0EEEPNS_8internal9ArenaBaseINS4_11ArenaTraitsEEEm Line | Count | Source | 45 | 2.00k | size_t test_size) { | 46 | 2.00k | std::unique_ptr<uint8_t[]> buf(new uint8_t[test_size]); | 47 | 1.00M | for (size_t i = 0; i < test_size; i++) { | 48 | 1.00M | buf[i] = i & 0xff; | 49 | 1.00M | } | 50 | | | 51 | 2.00k | Slice test_input(buf.get(), test_size); | 52 | | | 53 | 2.00k | slice->set(test_input, arena); | 54 | 2.00k | Slice ret = slice->as_slice(); | 55 | 4.00k | ASSERT_TRUE(ret == test_input) | 56 | 4.00k | << "test_size =" << test_size << "\n" | 57 | 4.00k | << "ret = " << ret.ToDebugString() << "\n" | 58 | 4.00k | << "test_input = " << test_input.ToDebugString(); | 59 | | | 60 | | // If the data is small enough to fit inline, then | 61 | | // the returned slice should point directly into the | 62 | | // InlineSlice object. | 63 | 2.00k | if (test_size < N) { | 64 | 23 | ASSERT_EQ(reinterpret_cast<const uint8_t *>(slice) + 1, | 65 | 23 | ret.data()); | 66 | 23 | } | 67 | 2.00k | } |
inline_slice-test.cc:_ZN2ybL13TestRoundTripILm16EEEvPNS_11InlineSliceIXT_ELb0EEEPNS_8internal9ArenaBaseINS4_11ArenaTraitsEEEm Line | Count | Source | 45 | 2.00k | size_t test_size) { | 46 | 2.00k | std::unique_ptr<uint8_t[]> buf(new uint8_t[test_size]); | 47 | 1.00M | for (size_t i = 0; i < test_size; i++) { | 48 | 1.00M | buf[i] = i & 0xff; | 49 | 1.00M | } | 50 | | | 51 | 2.00k | Slice test_input(buf.get(), test_size); | 52 | | | 53 | 2.00k | slice->set(test_input, arena); | 54 | 2.00k | Slice ret = slice->as_slice(); | 55 | 4.00k | ASSERT_TRUE(ret == test_input) | 56 | 4.00k | << "test_size =" << test_size << "\n" | 57 | 4.00k | << "ret = " << ret.ToDebugString() << "\n" | 58 | 4.00k | << "test_input = " << test_input.ToDebugString(); | 59 | | | 60 | | // If the data is small enough to fit inline, then | 61 | | // the returned slice should point directly into the | 62 | | // InlineSlice object. | 63 | 2.00k | if (test_size < N) { | 64 | 31 | ASSERT_EQ(reinterpret_cast<const uint8_t *>(slice) + 1, | 65 | 31 | ret.data()); | 66 | 31 | } | 67 | 2.00k | } |
|
68 | | |
69 | | // Sweep a variety of inputs for a given size of inline |
70 | | // data |
71 | | template<size_t N> |
72 | 3 | static void DoTest() { |
73 | 3 | Arena arena(1024, 4096); |
74 | | |
75 | | // Test a range of inputs both growing and shrinking |
76 | 3 | InlineSlice<N> my_slice; |
77 | 3 | ASSERT_EQ(N, sizeof(my_slice)); |
78 | | |
79 | 3.00k | for (size_t to_test = 0; to_test < 1000; to_test++) { |
80 | 3.00k | TestRoundTrip(&my_slice, &arena, to_test); |
81 | 3.00k | } |
82 | 3.00k | for (size_t to_test = 1000; to_test > 0; to_test--) { |
83 | 3.00k | TestRoundTrip(&my_slice, &arena, to_test); |
84 | 3.00k | } |
85 | 3 | } inline_slice-test.cc:_ZN2ybL6DoTestILm8EEEvv Line | Count | Source | 72 | 1 | static void DoTest() { | 73 | 1 | Arena arena(1024, 4096); | 74 | | | 75 | | // Test a range of inputs both growing and shrinking | 76 | 1 | InlineSlice<N> my_slice; | 77 | 1 | ASSERT_EQ(N, sizeof(my_slice)); | 78 | | | 79 | 1.00k | for (size_t to_test = 0; to_test < 1000; to_test++) { | 80 | 1.00k | TestRoundTrip(&my_slice, &arena, to_test); | 81 | 1.00k | } | 82 | 1.00k | for (size_t to_test = 1000; to_test > 0; to_test--) { | 83 | 1.00k | TestRoundTrip(&my_slice, &arena, to_test); | 84 | 1.00k | } | 85 | 1 | } |
inline_slice-test.cc:_ZN2ybL6DoTestILm12EEEvv Line | Count | Source | 72 | 1 | static void DoTest() { | 73 | 1 | Arena arena(1024, 4096); | 74 | | | 75 | | // Test a range of inputs both growing and shrinking | 76 | 1 | InlineSlice<N> my_slice; | 77 | 1 | ASSERT_EQ(N, sizeof(my_slice)); | 78 | | | 79 | 1.00k | for (size_t to_test = 0; to_test < 1000; to_test++) { | 80 | 1.00k | TestRoundTrip(&my_slice, &arena, to_test); | 81 | 1.00k | } | 82 | 1.00k | for (size_t to_test = 1000; to_test > 0; to_test--) { | 83 | 1.00k | TestRoundTrip(&my_slice, &arena, to_test); | 84 | 1.00k | } | 85 | 1 | } |
inline_slice-test.cc:_ZN2ybL6DoTestILm16EEEvv Line | Count | Source | 72 | 1 | static void DoTest() { | 73 | 1 | Arena arena(1024, 4096); | 74 | | | 75 | | // Test a range of inputs both growing and shrinking | 76 | 1 | InlineSlice<N> my_slice; | 77 | 1 | ASSERT_EQ(N, sizeof(my_slice)); | 78 | | | 79 | 1.00k | for (size_t to_test = 0; to_test < 1000; to_test++) { | 80 | 1.00k | TestRoundTrip(&my_slice, &arena, to_test); | 81 | 1.00k | } | 82 | 1.00k | for (size_t to_test = 1000; to_test > 0; to_test--) { | 83 | 1.00k | TestRoundTrip(&my_slice, &arena, to_test); | 84 | 1.00k | } | 85 | 1 | } |
|
86 | | |
87 | 1 | TEST(TestInlineSlice, Test8ByteInline) { |
88 | 1 | DoTest<8>(); |
89 | 1 | } |
90 | | |
91 | 1 | TEST(TestInlineSlice, Test12ByteInline) { |
92 | 1 | DoTest<12>(); |
93 | 1 | } |
94 | | |
95 | 1 | TEST(TestInlineSlice, Test16ByteInline) { |
96 | 1 | DoTest<16>(); |
97 | 1 | } |
98 | | |
99 | | } // namespace yb |