Add BinaryQueue unit tests 39/234239/9
authorZofia Abramowska <z.abramowska@samsung.com>
Fri, 22 May 2020 16:07:25 +0000 (18:07 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Thu, 9 Jul 2020 14:08:29 +0000 (16:08 +0200)
* add unit tests
* name test cases so they contain positive/negative information

Change-Id: I085c416bcc8fdd1cda1eeaed6778306f24ce5f52

test/CMakeLists.txt
test/common/containers/binaryqueue.cpp [new file with mode: 0644]

index 11fe30a86ff06179820a16af0b46a47e2b6243c2..a3dbf12e65fc5829f71ac862a7de3eab98160eba 100644 (file)
@@ -109,6 +109,7 @@ SET(CYNARA_TESTS_SOURCES
     chsgen/checksumgenerator.cpp
     client-async/sequence/sequencecontainer.cpp
     common/cache/monitorcache.cpp
+    common/containers/binaryqueue.cpp
     common/exceptions/bucketrecordcorrupted.cpp
     common/protocols/admin/admincheckrequest.cpp
     common/protocols/admin/admincheckresponse.cpp
diff --git a/test/common/containers/binaryqueue.cpp b/test/common/containers/binaryqueue.cpp
new file mode 100644 (file)
index 0000000..eefaf71
--- /dev/null
@@ -0,0 +1,233 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed 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.
+ */
+/**
+ * @file        test/common/containers/binaryqueue.cpp
+ * @author      Zofia Abramowska <z.abramowska>
+ * @version     1.0
+ * @brief       Tests for BinaryQueue class
+ */
+
+#include <string_view>
+
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+#include <common/containers/BinaryQueue.h>
+#include <common/exceptions/OutOfDataException.h>
+#include <common/exceptions/NullPointerException.h>
+
+using namespace Cynara;
+
+TEST(BinaryQueue, emptyPositive)
+{
+    BinaryQueue bbq;
+    ASSERT_TRUE(bbq.empty());
+    ASSERT_EQ(bbq.size(), 0);
+}
+
+TEST(BinaryQueue, copyEmptyPositive)
+{
+    BinaryQueue bbq;
+    const char data[] = "";
+
+    BinaryQueue other;
+    other = bbq;
+    ASSERT_TRUE(other.empty());
+
+    BinaryQueue copy(bbq);
+    ASSERT_TRUE(copy.empty());
+
+    BinaryQueue empty;
+    ASSERT_NO_THROW(bbq.appendCopyFrom(empty));
+    ASSERT_TRUE(bbq.empty());
+    ASSERT_NO_THROW(bbq.appendCopyTo(empty));
+    ASSERT_TRUE(empty.empty());
+    ASSERT_NO_THROW(bbq.appendMoveFrom(empty));
+    ASSERT_TRUE(bbq.empty());
+    ASSERT_NO_THROW(bbq.appendMoveTo(empty));
+    ASSERT_TRUE(empty.empty());
+    ASSERT_NO_THROW(bbq.appendCopy(nullptr, 0));
+    ASSERT_TRUE(bbq.empty());
+    ASSERT_NO_THROW(bbq.appendCopy(data, 0));
+    ASSERT_TRUE(bbq.empty());
+    ASSERT_NO_THROW(bbq.appendUnmanaged(data, 0, [](const void *, size_t, void *){}, nullptr));
+}
+
+TEST(BinaryQueue, emptyFlattenConsumePositive)
+{
+    BinaryQueue bbq;
+
+    ASSERT_NO_THROW(bbq.consume(0));
+    ASSERT_NO_THROW(bbq.flatten(nullptr, 0));
+    ASSERT_NO_THROW(bbq.flattenConsume(nullptr, 0));
+}
+
+TEST(BinaryQueue, emptyConsumeNegative)
+{
+    BinaryQueue bbq;
+    ASSERT_THROW(bbq.consume(42), OutOfDataException);
+}
+
+TEST(BinaryQueue, emptyFlattenNegative)
+{
+    BinaryQueue bbq;
+    char buffer[42];
+    ASSERT_THROW(bbq.flatten(buffer, 42), OutOfDataException);
+}
+
+
+TEST(BinaryQueue, emptyFlattenConsumeNegative)
+{
+    BinaryQueue bbq;
+    char buffer[42];
+    ASSERT_THROW(bbq.flattenConsume(buffer, 42), OutOfDataException);
+}
+
+TEST(BinaryQueue, flattenNullNegative)
+{
+    BinaryQueue bbq;
+    const char data[] = "somedata";
+    ASSERT_NO_THROW(bbq.appendCopy(data, sizeof(data)));
+    ASSERT_THROW(bbq.flatten(nullptr, sizeof(data)), NullPointerException);
+}
+
+TEST(BinaryQueue, flattenConsumeNullNegative)
+{
+    BinaryQueue bbq;
+    const char data[] = "somedata";
+    ASSERT_NO_THROW(bbq.appendCopy(data, sizeof(data)));
+    ASSERT_THROW(bbq.flattenConsume(nullptr, sizeof(data)), NullPointerException);
+}
+
+TEST(BinaryQueue, appendCopyNullNegative)
+{
+    BinaryQueue bbq;
+    ASSERT_THROW(bbq.appendCopy(nullptr, 42), NullPointerException);
+}
+
+TEST(BinaryQueue, appendUnmanagedNullBufferNegative)
+{
+    BinaryQueue bbq;
+    ASSERT_THROW(bbq.appendUnmanaged(nullptr, 42, [](const void *, size_t, void *){}, nullptr),
+                 NullPointerException);
+}
+
+TEST(BinaryQueue, appendUnmanagedNullDeleterNegative)
+{
+    BinaryQueue bbq;
+    const char data[] = "somedata";
+    ASSERT_THROW(bbq.appendUnmanaged(data, sizeof(data), nullptr, nullptr),
+                 NullPointerException);
+}
+
+TEST(BinaryQueue, appendFlattenAndConsumePositive)
+{
+    BinaryQueue bbq;
+    const std::vector<std::string> dataVector = {"some", "data", "to", "be", "consumed",
+                                                 "abcdefghijklmnopqrstuwvxyz",
+                                                 "1q2w3e$R%T^Y7u8i9O)P-[=]"};
+    std::string::size_type size = 0;
+    for (auto &data: dataVector) {
+        ASSERT_NO_THROW(bbq.appendCopy(data.c_str(), data.size()));
+        size += data.size();
+        ASSERT_EQ(bbq.size(), size);
+    }
+
+    for (auto &data: dataVector) {
+        char buffer[255] = {};
+        ASSERT_NO_THROW(bbq.flatten(buffer, data.size()));
+        ASSERT_EQ(std::string(buffer), data);
+        ASSERT_EQ(bbq.size(), size);
+        ASSERT_NO_THROW(bbq.consume(data.size()));
+        size -= data.size();
+        ASSERT_EQ(bbq.size(), size);
+    }
+}
+
+TEST(BinaryQueue, appendFlattenConsumePositive)
+{
+    BinaryQueue bbq;
+    const std::vector<std::string> dataVector = {"some", "different", "data", "to", "be",
+                                                 "flattenConsumed", "abcdefghijklmnopqrstuwvxyz",
+                                                 ",jk>LOP:8776irkL*76$%^$%"};
+    std::string::size_type size = 0;
+    for (auto &data: dataVector) {
+        ASSERT_NO_THROW(bbq.appendCopy(data.c_str(), data.size()));
+        size += data.size();
+        ASSERT_EQ(bbq.size(), size);
+    }
+
+    for (auto &data: dataVector) {
+        char buffer[255] = {};
+        ASSERT_NO_THROW(bbq.flattenConsume(buffer, data.size()));
+        ASSERT_EQ(std::string(buffer), data);
+        size -= data.size();
+        ASSERT_EQ(bbq.size(), size);
+    }
+}
+
+TEST(BinaryQueue, appendFlattenConsumeAsymetricPositive)
+{
+    BinaryQueue bbq;
+    const std::vector<std::string> dataVec = {"Iwan","","ttobe","properlyflatten","ed",
+                                              "and","c","onsumed"};
+    const std::vector<std::string::size_type> wordsLen = {1, 4, 2, 2, 8, 9, 3, 8};
+
+    std::string wholeString;
+    for (auto &data: dataVec) {
+        wholeString += data;
+    }
+
+    for (auto &data: dataVec) {
+        ASSERT_NO_THROW(bbq.appendCopy(data.c_str(), data.size()));
+    }
+
+    int pos = 0;
+    for (std::string::size_type i = 0; i < dataVec.size(); i++) {
+        char buffer[255] = {};
+        int len = wordsLen[i];
+        ASSERT_NO_THROW(bbq.flattenConsume(buffer, len));
+        ASSERT_EQ(std::string_view(buffer, len),
+                  std::string_view(wholeString).substr(pos, len));
+        pos += len;
+    }
+}
+
+TEST(BinaryQueue, consumeOneLessPositive)
+{
+    BinaryQueue bbq;
+    const char data[] = "somedata";
+
+    ASSERT_NO_THROW(bbq.appendCopy(data, sizeof(data)));
+
+    char buffer[255] = {};
+    ASSERT_NO_THROW(bbq.flattenConsume(buffer, sizeof(data) - 1));
+    ASSERT_NO_THROW(bbq.flattenConsume(buffer + (sizeof(data) - 1), 1));
+    ASSERT_EQ(std::string_view(data, sizeof(data)), std::string(buffer, sizeof(data)));
+}
+
+TEST(BinaryQueue, consumeOneTooMuchNegative)
+{
+    BinaryQueue bbq;
+    const char data[] = "somedata";
+
+    ASSERT_NO_THROW(bbq.appendCopy(data, sizeof(data)));
+
+    char buffer[255] = {};
+    ASSERT_NO_THROW(bbq.flattenConsume(buffer, sizeof(data)));
+    ASSERT_THROW(bbq.flattenConsume(buffer + sizeof(data), 1), OutOfDataException);
+}
+