unit-test-common-2 (#116)


diff --git a/src/common/NamesrvConfig.h b/src/common/NamesrvConfig.h
index 6c0259f..4bf8eb1 100755
--- a/src/common/NamesrvConfig.h
+++ b/src/common/NamesrvConfig.h
@@ -19,38 +19,36 @@
 
 #include <stdlib.h>
 #include <string>
+#include "UtilAll.h"
+
 namespace rocketmq {
 //<!***************************************************************************
 class NamesrvConfig {
- public:
-  NamesrvConfig() {
-    m_kvConfigPath = "";
+   public:
+    NamesrvConfig() {
+        m_kvConfigPath = "";
 
-    char* home = getenv(ROCKETMQ_HOME_ENV.c_str());
-    if (home) {
-      m_rocketmqHome = home;
-    } else {
-      m_rocketmqHome = "";
+        char *home = getenv(rocketmq::ROCKETMQ_HOME_ENV.c_str());
+        if (home) {
+            m_rocketmqHome = home;
+        } else {
+            m_rocketmqHome = "";
+        }
     }
-  }
 
-  const string& getRocketmqHome() const { return m_rocketmqHome; }
+    const string &getRocketmqHome() const { return m_rocketmqHome; }
 
-  void setRocketmqHome(const string& rocketmqHome) {
-    m_rocketmqHome = rocketmqHome;
-  }
+    void setRocketmqHome(const string &rocketmqHome) { m_rocketmqHome = rocketmqHome; }
 
-  const string& getKvConfigPath() const { return m_kvConfigPath; }
+    const string &getKvConfigPath() const { return m_kvConfigPath; }
 
-  void setKvConfigPath(const string& kvConfigPath) {
-    m_kvConfigPath = kvConfigPath;
-  }
+    void setKvConfigPath(const string &kvConfigPath) { m_kvConfigPath = kvConfigPath; }
 
- private:
-  string m_rocketmqHome;
-  string m_kvConfigPath;
+   private:
+    string m_rocketmqHome;
+    string m_kvConfigPath;
 };
 
 //<!***************************************************************************
-}  //<!end namespace;
+}  // namespace rocketmq
 #endif
diff --git a/test/src/common/MemoryOutputStreamTest.cpp b/test/src/common/MemoryOutputStreamTest.cpp
new file mode 100644
index 0000000..6cf1015
--- /dev/null
+++ b/test/src/common/MemoryOutputStreamTest.cpp
@@ -0,0 +1,224 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "string.h"
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "MemoryInputStream.h"
+#include "MemoryOutputStream.h"
+#include "dataBlock.h"
+
+using std::string;
+
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Return;
+
+using rocketmq::MemoryBlock;
+using rocketmq::MemoryInputStream;
+using rocketmq::MemoryOutputStream;
+
+TEST(memoryOutputStream, init) {
+    MemoryOutputStream memoryOutput;
+    EXPECT_EQ(memoryOutput.getMemoryBlock().getSize(), 0);
+    EXPECT_TRUE(memoryOutput.getData() != nullptr);
+
+    EXPECT_EQ(memoryOutput.getPosition(), 0);
+    EXPECT_EQ(memoryOutput.getDataSize(), 0);
+
+    MemoryOutputStream twoMemoryOutput(512);
+    EXPECT_EQ(memoryOutput.getMemoryBlock().getSize(), 0);
+
+    MemoryBlock memoryBlock(12, false);
+    MemoryOutputStream threeMemoryOutput(memoryBlock, false);
+    EXPECT_EQ(threeMemoryOutput.getPosition(), 0);
+    EXPECT_EQ(threeMemoryOutput.getDataSize(), 0);
+
+    MemoryOutputStream frouMemoryOutput(memoryBlock, true);
+    EXPECT_EQ(frouMemoryOutput.getPosition(), memoryBlock.getSize());
+    EXPECT_EQ(frouMemoryOutput.getDataSize(), memoryBlock.getSize());
+
+    char *buf = (char *) malloc(sizeof(char) * 9);
+    strcpy(buf, "RocketMQ");
+    MemoryOutputStream fiveMemoryOutputStream(buf, 8);
+    EXPECT_EQ(fiveMemoryOutputStream.getData(), buf);
+
+    fiveMemoryOutputStream.reset();
+    EXPECT_EQ(memoryOutput.getPosition(), 0);
+    EXPECT_EQ(memoryOutput.getDataSize(), 0);
+    free(buf);
+}
+
+TEST(memoryOutputStream, flush) {
+    char *buf = (char *) malloc(sizeof(char) * 9);
+    strcpy(buf, "RocketMQ");
+    MemoryOutputStream memoryOutput;
+    memoryOutput.write(buf, 9);
+    memoryOutput.flush();
+    EXPECT_FALSE(memoryOutput.getData() == buf);
+    free(buf);
+}
+
+TEST(memoryOutputStream, preallocate) {
+    MemoryOutputStream memoryOutput;
+    memoryOutput.preallocate(250);
+
+    memoryOutput.preallocate(256);
+}
+
+TEST(memoryOutputStream, getMemoryBlock) {
+    char *buf = (char *) malloc(sizeof(char) * 9);
+    strcpy(buf, "RocketMQ");
+    MemoryOutputStream memoryOutput;
+    memoryOutput.write(buf, 9);
+    MemoryBlock memoryBlock = memoryOutput.getMemoryBlock();
+    EXPECT_EQ(memoryBlock.getSize(), memoryOutput.getDataSize());
+    free(buf);
+}
+
+TEST(memoryOutputStream, prepareToWriteAndGetData) {
+    char *buf = (char *) malloc(sizeof(char) * 9);
+    strcpy(buf, "RocketMQ");
+    MemoryOutputStream memoryOutput(buf, 9);
+    EXPECT_EQ(memoryOutput.getData(), buf);
+
+    // prepareToWrite
+    // EXPECT_TRUE(memoryOutput.writeIntBigEndian(123));
+    EXPECT_TRUE(memoryOutput.writeByte('r'));
+    const char *data = static_cast<const char *>(memoryOutput.getData());
+    EXPECT_EQ(string(data), "rocketMQ");
+
+    MemoryOutputStream blockMmoryOutput(8);
+    char *memoryData = (char *) blockMmoryOutput.getData();
+
+    EXPECT_EQ(memoryData[blockMmoryOutput.getDataSize()], 0);
+
+    blockMmoryOutput.write(buf, 8);
+    blockMmoryOutput.write(buf, 8);
+    data = static_cast<const char *>(blockMmoryOutput.getData());
+    EXPECT_EQ(string(data), "rocketMQrocketMQ");
+    free(buf);
+}
+
+TEST(memoryOutputStream, position) {
+    char *buf = (char *) malloc(sizeof(char) * 9);
+    strcpy(buf, "RocketMQ");
+
+    MemoryOutputStream memoryOutput;
+    EXPECT_EQ(memoryOutput.getPosition(), 0);
+
+    memoryOutput.write(buf, 8);
+    EXPECT_EQ(memoryOutput.getPosition(), 8);
+
+    EXPECT_FALSE(memoryOutput.setPosition(9));
+
+    EXPECT_TRUE(memoryOutput.setPosition(-1));
+    EXPECT_EQ(memoryOutput.getPosition(), 0);
+
+    EXPECT_TRUE(memoryOutput.setPosition(8));
+    EXPECT_EQ(memoryOutput.getPosition(), 8);
+
+    EXPECT_TRUE(memoryOutput.setPosition(7));
+    EXPECT_EQ(memoryOutput.getPosition(), 7);
+    free(buf);
+}
+
+TEST(memoryOutputStream, write) {
+    MemoryOutputStream memoryOutput;
+    MemoryInputStream memoryInput(memoryOutput.getData(), 256, false);
+
+    EXPECT_TRUE(memoryOutput.writeBool(true));
+    EXPECT_TRUE(memoryInput.readBool());
+
+    EXPECT_TRUE(memoryOutput.writeBool(false));
+    EXPECT_FALSE(memoryInput.readBool());
+
+    EXPECT_TRUE(memoryOutput.writeByte('a'));
+    EXPECT_EQ(memoryInput.readByte(), 'a');
+
+    EXPECT_TRUE(memoryOutput.writeShortBigEndian(128));
+    EXPECT_EQ(memoryInput.readShortBigEndian(), 128);
+
+    EXPECT_TRUE(memoryOutput.writeIntBigEndian(123));
+    EXPECT_EQ(memoryInput.readIntBigEndian(), 123);
+
+    EXPECT_TRUE(memoryOutput.writeInt64BigEndian(123123));
+    EXPECT_EQ(memoryInput.readInt64BigEndian(), 123123);
+
+    EXPECT_TRUE(memoryOutput.writeDoubleBigEndian(12.71));
+    EXPECT_EQ(memoryInput.readDoubleBigEndian(), 12.71);
+
+    EXPECT_TRUE(memoryOutput.writeFloatBigEndian(12.1));
+    float f = 12.1;
+    EXPECT_EQ(memoryInput.readFloatBigEndian(), f);
+
+    // EXPECT_TRUE(memoryOutput.writeRepeatedByte(8 , 8));
+}
+
+TEST(memoryInputStream, info) {
+    char *buf = (char *) malloc(sizeof(char) * 9);
+    strcpy(buf, "RocketMQ");
+
+    MemoryInputStream memoryInput(buf, 8, false);
+
+    char *memoryData = (char *) memoryInput.getData();
+    EXPECT_EQ(memoryData, buf);
+
+    EXPECT_EQ(memoryInput.getTotalLength(), 8);
+
+    MemoryInputStream twoMemoryInput(buf, 8, true);
+    EXPECT_NE(twoMemoryInput.getData(), buf);
+
+    memoryData = (char *) twoMemoryInput.getData();
+    EXPECT_NE(&memoryData, &buf);
+
+    MemoryBlock memoryBlock(buf, 8);
+    MemoryInputStream threeMemoryInput(memoryBlock, false);
+    memoryData = (char *) threeMemoryInput.getData();
+    EXPECT_EQ(memoryData, threeMemoryInput.getData());
+    EXPECT_EQ(threeMemoryInput.getTotalLength(), 8);
+
+    MemoryInputStream frouMemoryInput(memoryBlock, true);
+    EXPECT_NE(frouMemoryInput.getData(), memoryBlock.getData());
+    free(buf);
+}
+
+TEST(memoryInputStream, position) {
+    char *buf = (char *) malloc(sizeof(char) * 9);
+    strcpy(buf, "RocketMQ");
+
+    MemoryInputStream memoryInput(buf, 8, false);
+    EXPECT_EQ(memoryInput.getPosition(), 0);
+    EXPECT_FALSE(memoryInput.isExhausted());
+
+    memoryInput.setPosition(9);
+    EXPECT_EQ(memoryInput.getPosition(), 8);
+    EXPECT_TRUE(memoryInput.isExhausted());
+
+    memoryInput.setPosition(-1);
+    EXPECT_EQ(memoryInput.getPosition(), 0);
+    free(buf);
+}
+
+int main(int argc, char *argv[]) {
+    InitGoogleMock(&argc, argv);
+    testing::GTEST_FLAG(throw_on_failure) = true;
+    testing::GTEST_FLAG(filter) = "*.*";
+    int itestts = RUN_ALL_TESTS();
+    return itestts;
+}
diff --git a/test/src/common/NamesrvConfigTest.cpp b/test/src/common/NamesrvConfigTest.cpp
new file mode 100644
index 0000000..1ac2c13
--- /dev/null
+++ b/test/src/common/NamesrvConfigTest.cpp
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "NamesrvConfig.h"
+
+using std::string;
+
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Return;
+
+using rocketmq::NamesrvConfig;
+
+TEST(namesrvConfig, init) {
+    NamesrvConfig namesrvConfig;
+
+    const string home = "/home/rocketmq";
+    namesrvConfig.setRocketmqHome(home);
+    EXPECT_EQ(namesrvConfig.getRocketmqHome(), "/home/rocketmq");
+
+    namesrvConfig.setKvConfigPath("/home/rocketmq");
+    EXPECT_EQ(namesrvConfig.getKvConfigPath(), "/home/rocketmq");
+}
+
+int main(int argc, char *argv[]) {
+    InitGoogleMock(&argc, argv);
+    testing::GTEST_FLAG(throw_on_failure) = true;
+    testing::GTEST_FLAG(filter) = "namesrvConfig.init";
+    int itestts = RUN_ALL_TESTS();
+    return itestts;
+}
diff --git a/test/src/common/PermNametTest.cpp b/test/src/common/PermNametTest.cpp
new file mode 100644
index 0000000..1a98e31
--- /dev/null
+++ b/test/src/common/PermNametTest.cpp
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "PermName.h"
+
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Return;
+
+using rocketmq::PermName;
+
+TEST(permName, perm2String) {
+    EXPECT_EQ(PermName::perm2String(0), "---");
+    EXPECT_EQ(PermName::perm2String(1), "--X");
+    EXPECT_EQ(PermName::perm2String(2), "-W");
+    EXPECT_EQ(PermName::perm2String(3), "-WX");
+    EXPECT_EQ(PermName::perm2String(4), "R--");
+    EXPECT_EQ(PermName::perm2String(5), "R-X");
+    EXPECT_EQ(PermName::perm2String(6), "RW");
+    EXPECT_EQ(PermName::perm2String(7), "RWX");
+    EXPECT_EQ(PermName::perm2String(8), "---");
+}
+
+int main(int argc, char *argv[]) {
+    InitGoogleMock(&argc, argv);
+    testing::GTEST_FLAG(throw_on_failure) = true;
+    testing::GTEST_FLAG(filter) = "permName.perm2String";
+    int itestts = RUN_ALL_TESTS();
+    return itestts;
+}
diff --git a/test/src/common/PullSysFlagTest.cpp b/test/src/common/PullSysFlagTest.cpp
new file mode 100644
index 0000000..4e5ebd5
--- /dev/null
+++ b/test/src/common/PullSysFlagTest.cpp
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "PullSysFlag.h"
+
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Return;
+
+using rocketmq::PullSysFlag;
+
+TEST(pullSysFlag, flag) {
+    EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, false, false), 0);
+
+    EXPECT_EQ(PullSysFlag::buildSysFlag(true, false, false, false), 1);
+    EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, false, false), 3);
+    EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, true, false), 7);
+    EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, true, true), 15);
+
+    EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, false, false), 2);
+    EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, true, false), 6);
+    EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, true, true), 14);
+
+    EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, true, false), 4);
+    EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, true, true), 12);
+
+    EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, false, true), 8);
+
+    int FLAG_COMMIT_OFFSET = 0x1 << 0;
+    int FLAG_SUSPEND = 0x1 << 1;
+    int FLAG_SUBSCRIPTION = 0x1 << 2;
+    int FLAG_CLASS_FILTER = 0x1 << 3;
+
+    for (int i = 0; i < 16; i++) {
+        if ((i & FLAG_COMMIT_OFFSET) == FLAG_COMMIT_OFFSET) {
+            EXPECT_TRUE(PullSysFlag::hasCommitOffsetFlag(i));
+        } else {
+            EXPECT_FALSE(PullSysFlag::hasCommitOffsetFlag(i));
+        }
+
+        if ((i & FLAG_SUSPEND) == FLAG_SUSPEND) {
+            EXPECT_TRUE(PullSysFlag::hasSuspendFlag(i));
+        } else {
+            EXPECT_FALSE(PullSysFlag::hasSuspendFlag(i));
+        }
+
+        if ((i & FLAG_SUBSCRIPTION) == FLAG_SUBSCRIPTION) {
+            EXPECT_TRUE(PullSysFlag::hasSubscriptionFlag(i));
+        } else {
+            EXPECT_FALSE(PullSysFlag::hasSubscriptionFlag(i));
+        }
+
+        if ((i & FLAG_CLASS_FILTER) == FLAG_CLASS_FILTER) {
+            EXPECT_TRUE(PullSysFlag::hasClassFilterFlag(i));
+        } else {
+            EXPECT_FALSE(PullSysFlag::hasClassFilterFlag(i));
+        }
+
+        if ((i & FLAG_COMMIT_OFFSET) == FLAG_COMMIT_OFFSET) {
+            EXPECT_TRUE(PullSysFlag::hasCommitOffsetFlag(i));
+        } else {
+            EXPECT_FALSE(PullSysFlag::hasCommitOffsetFlag(i));
+        }
+
+        if (i == 0 || i == 1) {
+            EXPECT_EQ(PullSysFlag::clearCommitOffsetFlag(i), 0);
+        } else {
+            EXPECT_TRUE(PullSysFlag::clearCommitOffsetFlag(i) > 0);
+        }
+    }
+}
+
+int main(int argc, char *argv[]) {
+    InitGoogleMock(&argc, argv);
+    testing::GTEST_FLAG(throw_on_failure) = true;
+    testing::GTEST_FLAG(filter) = "pullSysFlag.flag";
+    int itestts = RUN_ALL_TESTS();
+    return itestts;
+}