|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "gmock/gmock.h" |
| 21 | +#include "gtest/gtest.h" |
| 22 | + |
| 23 | +#include "UtilAll.h" |
| 24 | + |
| 25 | +using std::string; |
| 26 | + |
| 27 | +using ::testing::InitGoogleMock; |
| 28 | +using ::testing::InitGoogleTest; |
| 29 | +using testing::Return; |
| 30 | + |
| 31 | +using rocketmq::UtilAll; |
| 32 | + |
| 33 | +TEST(UtilAll, startsWith_retry) { |
| 34 | + string source = "testTopic"; |
| 35 | + string retrySource = "%RETRY%testTopic"; |
| 36 | + string noRetrySource = "%DLQ%testTopic"; |
| 37 | + EXPECT_TRUE(UtilAll::startsWith_retry(retrySource)); |
| 38 | + EXPECT_FALSE(UtilAll::startsWith_retry(source)); |
| 39 | + EXPECT_FALSE(UtilAll::startsWith_retry(noRetrySource)); |
| 40 | +} |
| 41 | +TEST(UtilAll, getRetryTopic) { |
| 42 | + string source = "testTopic"; |
| 43 | + string retrySource = "%RETRY%testTopic"; |
| 44 | + EXPECT_EQ(UtilAll::getRetryTopic(source), retrySource); |
| 45 | +} |
| 46 | +TEST(UtilAll, Trim) { |
| 47 | + string source = "testTopic"; |
| 48 | + string preSource = " testTopic"; |
| 49 | + string surSource = "testTopic "; |
| 50 | + string allSource = " testTopic "; |
| 51 | + UtilAll::Trim(preSource); |
| 52 | + UtilAll::Trim(surSource); |
| 53 | + UtilAll::Trim(allSource); |
| 54 | + EXPECT_EQ(preSource, source); |
| 55 | + EXPECT_EQ(surSource, source); |
| 56 | + EXPECT_EQ(allSource, source); |
| 57 | +} |
| 58 | +TEST(UtilAll, hexstr2ull) { |
| 59 | + const char* a = "1"; |
| 60 | + const char* b = "FF"; |
| 61 | + const char* c = "1a"; |
| 62 | + const char* d = "101"; |
| 63 | + EXPECT_EQ(UtilAll::hexstr2ull(a), 1); |
| 64 | + EXPECT_EQ(UtilAll::hexstr2ull(b), 255); |
| 65 | + EXPECT_EQ(UtilAll::hexstr2ull(c), 26); |
| 66 | + EXPECT_EQ(UtilAll::hexstr2ull(d), 257); |
| 67 | +} |
| 68 | +TEST(UtilAll, SplitURL) { |
| 69 | + string source = "127.0.0.1"; |
| 70 | + string source1 = "127.0.0.1:0"; |
| 71 | + string source2 = "127.0.0.1:9876"; |
| 72 | + string addr; |
| 73 | + string addr1; |
| 74 | + string addr2; |
| 75 | + short port; |
| 76 | + EXPECT_FALSE(UtilAll::SplitURL(source, addr, port)); |
| 77 | + EXPECT_FALSE(UtilAll::SplitURL(source1, addr1, port)); |
| 78 | + EXPECT_TRUE(UtilAll::SplitURL(source2, addr2, port)); |
| 79 | + EXPECT_EQ(addr2, "127.0.0.1"); |
| 80 | + EXPECT_EQ(port, 9876); |
| 81 | +} |
| 82 | +TEST(UtilAll, SplitOne) { |
| 83 | + string source = "127.0.0.1:9876"; |
| 84 | + vector<string> ret; |
| 85 | + EXPECT_EQ(UtilAll::Split(ret, source, '.'), 4); |
| 86 | + EXPECT_EQ(ret[0], "127"); |
| 87 | +} |
| 88 | +TEST(UtilAll, SplitStr) { |
| 89 | + string source = "11AA222AA3333AA44444AA5"; |
| 90 | + vector<string> ret; |
| 91 | + EXPECT_EQ(UtilAll::Split(ret, source, "AA"), 5); |
| 92 | + EXPECT_EQ(ret[0], "11"); |
| 93 | +} |
| 94 | +TEST(UtilAll, StringToInt32) { |
| 95 | + string source = "123"; |
| 96 | + int value; |
| 97 | + EXPECT_TRUE(UtilAll::StringToInt32(source, value)); |
| 98 | + EXPECT_EQ(123, value); |
| 99 | + EXPECT_FALSE(UtilAll::StringToInt32("123456789X123456789", value)); |
| 100 | + EXPECT_FALSE(UtilAll::StringToInt32("-1234567890123456789", value)); |
| 101 | + EXPECT_FALSE(UtilAll::StringToInt32("1234567890123456789", value)); |
| 102 | +} |
| 103 | +TEST(UtilAll, StringToInt64) { |
| 104 | + string source = "123"; |
| 105 | + int64_t value; |
| 106 | + EXPECT_TRUE(UtilAll::StringToInt64(source, value)); |
| 107 | + EXPECT_EQ(123, value); |
| 108 | + EXPECT_FALSE(UtilAll::StringToInt64("XXXXXXXXXXX", value)); |
| 109 | + EXPECT_FALSE(UtilAll::StringToInt64("123456789X123456789", value)); |
| 110 | + EXPECT_EQ(123456789, value); |
| 111 | + EXPECT_FALSE(UtilAll::StringToInt64("-123456789012345678901234567890123456789012345678901234567890", value)); |
| 112 | + EXPECT_FALSE(UtilAll::StringToInt64("123456789012345678901234567890123456789012345678901234567890", value)); |
| 113 | +} |
| 114 | +int main(int argc, char* argv[]) { |
| 115 | + InitGoogleMock(&argc, argv); |
| 116 | + testing::GTEST_FLAG(throw_on_failure) = true; |
| 117 | + testing::GTEST_FLAG(filter) = "UtilAll.*"; |
| 118 | + int itestts = RUN_ALL_TESTS(); |
| 119 | + return itestts; |
| 120 | +} |
0 commit comments