| Stan Rokita | aed1e97 | 2019-08-08 14:05:33 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "chre/util/system/debug_dump.h" |
| 22 | |
| 23 | using chre::DebugDumpWrapper; |
| 24 | |
| 25 | static constexpr size_t kStandardBufferSize = 4000; |
| 26 | |
| 27 | TEST(DebugDumpWrapper, ZeroBuffersInitially) { |
| 28 | DebugDumpWrapper debugDump(kStandardBufferSize); |
| 29 | const auto &buffers = debugDump.getBuffers(); |
| 30 | EXPECT_TRUE(buffers.empty()); |
| 31 | } |
| 32 | |
| 33 | TEST(DebugDumpWrapper, OneBufferForOneString) { |
| 34 | DebugDumpWrapper debugDump(kStandardBufferSize); |
| 35 | const char *str = "Lorem ipsum"; |
| 36 | debugDump.print("%s", str); |
| 37 | const auto &buffers = debugDump.getBuffers(); |
| 38 | EXPECT_EQ(buffers.size(), 1); |
| 39 | EXPECT_TRUE(strcmp(buffers.front().get(), str) == 0); |
| 40 | } |
| 41 | |
| 42 | TEST(DebugDumpWrapper, TwoStringsFitPerfectlyInOneBuffer) { |
| 43 | DebugDumpWrapper debugDump(5); |
| 44 | const char *str1 = "ab"; |
| 45 | const char *str2 = "cd"; |
| 46 | debugDump.print("%s", str1); |
| 47 | debugDump.print("%s", str2); |
| 48 | const auto &buffers = debugDump.getBuffers(); |
| 49 | EXPECT_EQ(buffers.size(), 1); |
| 50 | char bothStr[5]; |
| 51 | strcpy(bothStr, str1); |
| 52 | strcat(bothStr, str2); |
| 53 | EXPECT_TRUE(strcmp(buffers.front().get(), bothStr) == 0); |
| 54 | } |
| 55 | |
| 56 | TEST(DebugDumpWrapper, TooLargeOfStringToFit) { |
| 57 | DebugDumpWrapper debugDump(1); |
| 58 | const char *str = "a"; |
| 59 | debugDump.print("%s", str); |
| 60 | const auto &buffers = debugDump.getBuffers(); |
| 61 | EXPECT_TRUE(buffers.empty()); |
| 62 | } |
| 63 | |
| 64 | TEST(DebugDumpWrapper, TooLargeOfStringWithPartlyFilledBuffer) { |
| 65 | DebugDumpWrapper debugDump(2); |
| 66 | const char *str1 = "a"; |
| 67 | const char *str2 = "bc"; |
| 68 | debugDump.print("%s", str1); |
| 69 | const auto &buffers = debugDump.getBuffers(); |
| 70 | EXPECT_EQ(buffers.size(), 1); |
| 71 | debugDump.print("%s", str2); |
| 72 | EXPECT_EQ(buffers.size(), 1); |
| 73 | EXPECT_TRUE(strcmp(buffers.front().get(), str1) == 0); |
| 74 | } |
| 75 | |
| 76 | TEST(DebugDumpWrapper, StringForcesNewBufferWithPartlyFilledBuffer) { |
| 77 | DebugDumpWrapper debugDump(4); |
| 78 | const char *str1 = "ab"; |
| 79 | const char *str2 = "bc"; |
| 80 | debugDump.print("%s", str1); |
| 81 | debugDump.print("%s", str2); |
| 82 | const auto &buffers = debugDump.getBuffers(); |
| 83 | EXPECT_EQ(buffers.size(), 2); |
| 84 | EXPECT_TRUE(strcmp(buffers.front().get(), str1) == 0); |
| 85 | EXPECT_TRUE(strcmp(buffers.back().get(), str2) == 0); |
| 86 | } |
| 87 | |
| 88 | TEST(DebugDumpWrapper, ManyNewBuffersAllocated) { |
| 89 | DebugDumpWrapper debugDump(kStandardBufferSize); |
| 90 | constexpr size_t kSizeStrings = 10; |
| 91 | constexpr size_t kNumPrints = 1200; |
| 92 | // Should be about 12000 chars added to debugDump |
| 93 | char str[kSizeStrings]; |
| 94 | memset(str, 'a', sizeof(char) * kSizeStrings); |
| 95 | str[kSizeStrings - 1] = '\0'; |
| 96 | for (size_t i = 0; i < kNumPrints; i++) { |
| 97 | debugDump.print("%s", str); |
| 98 | } |
| 99 | const auto &buffers = debugDump.getBuffers(); |
| 100 | EXPECT_EQ(buffers.size(), 3); |
| 101 | } |
| 102 | |
| 103 | TEST(DebugDumpWrapper, EmptyStringAllocsOneBuffer) { |
| 104 | DebugDumpWrapper debugDump(kStandardBufferSize); |
| 105 | debugDump.print("%s", ""); |
| 106 | const auto &buffers = debugDump.getBuffers(); |
| 107 | EXPECT_EQ(buffers.size(), 1); |
| 108 | } |