[PATCH 2/6] [mama] CPP Unittests


Ian Bell <IBell@...>
 

From 099d090882594f6975540cc5ffd3076f8d4e4433 Mon Sep 17 00:00:00 2001

Message-Id: <099d090882594f6975540cc5ffd3076f8d4e4433.1348824474.git.ibell@...>

In-Reply-To: <766019e29e6ff6072c662caa2de6adbd3e9df875.1348824474.git.ibell@...>

References: <766019e29e6ff6072c662caa2de6adbd3e9df875.1348824474.git.ibell@...>

From: Ian Bell <ibell@...>

Date: Fri, 28 Sep 2012 10:07:32 +0100

Subject: [PATCH 2/6] [mama] CPP Unittests

 

Added some new unittests and cleaned up some existing

 

Signed-off-by: Ian Bell <ibell@...>

---

mama/c_cpp/src/gunittest/cpp/Makefile.am           |    6 +-

mama/c_cpp/src/gunittest/cpp/MamaDateTimeTest.cpp  |    2 +-

mama/c_cpp/src/gunittest/cpp/MamaMsgTest.cpp       |  106 ++++++++++++++++++++

mama/c_cpp/src/gunittest/cpp/MamaOpenCloseTest.cpp |   12 +--

.../src/gunittest/cpp/MamaSubscriptionTest.cpp     |    2 +-

mama/c_cpp/src/gunittest/cpp/MamaTimerTest.cpp     |   10 +-

6 files changed, 122 insertions(+), 16 deletions(-)

 

diff --git a/mama/c_cpp/src/gunittest/cpp/Makefile.am b/mama/c_cpp/src/gunittest/cpp/Makefile.am

index f08c945..1518e01 100644

--- a/mama/c_cpp/src/gunittest/cpp/Makefile.am

+++ b/mama/c_cpp/src/gunittest/cpp/Makefile.am

@@ -23,7 +23,7 @@ VPATH  = @srcdir@

blddir=@builddir@

 if USE_GCC_FLAGS

-CFLAGS   += -pedantic -Wno-long-long -O2 -pthread -fPIC

+CFLAGS   += -std=gnu99 -pedantic -Wno-long-long -O2 -pthread -fPIC

CPPFLAGS += -pedantic -Wno-long-long -O2 -pthread -fPIC

endif

@@ -38,9 +38,9 @@ LDADD = -lgtest_main -ldl

 

 #if WITH_UNITTESTS

-bin_PROGRAMS = MainUnitTestCPP

+bin_PROGRAMS = UnitTestMamaCPP

-nodist_MainUnitTestCPP_SOURCES =  MainUnitTestCpp.cpp \

+nodist_UnitTestMamaCPP_SOURCES =  MainUnitTestCpp.cpp \

     MamaDateTimeTest.cpp \

     MamaOpenCloseTest.cpp \

     MamaPriceTest.cpp \

diff --git a/mama/c_cpp/src/gunittest/cpp/MamaDateTimeTest.cpp b/mama/c_cpp/src/gunittest/cpp/MamaDateTimeTest.cpp

index cc6a9e3..6b9f7ba 100644

--- a/mama/c_cpp/src/gunittest/cpp/MamaDateTimeTest.cpp

+++ b/mama/c_cpp/src/gunittest/cpp/MamaDateTimeTest.cpp

@@ -99,7 +99,7 @@ TEST_F(MamaDateTimeTest, CompareDates)

               // Get the string representation of the data

               char stringDate[100] = "";

               //ASSERT_EQ(mamaDateTime_getAsFormattedString(today, stringDate, 100, "%Y-%m-%d"), MAMA_STATUS_OK);

-    today->getAsFormattedString(stringDate, 100, "%Y-%m-\%\d");

+    today->getAsFormattedString(stringDate, 100, "%Y-%m-%d");

                // Destroy the date

               //ASSERT_EQ(mamaDateTime_destroy(today), MAMA_STATUS_OK);

diff --git a/mama/c_cpp/src/gunittest/cpp/MamaMsgTest.cpp b/mama/c_cpp/src/gunittest/cpp/MamaMsgTest.cpp

index 55b6cf6..c3d54ee 100644

--- a/mama/c_cpp/src/gunittest/cpp/MamaMsgTest.cpp

+++ b/mama/c_cpp/src/gunittest/cpp/MamaMsgTest.cpp

@@ -19,6 +19,9 @@

  * 02110-1301 USA

  */

#include "MamaMsgTest.h"

+#include "mama/types.h"

+#include "mama/MamaReservedFields.h"

+#include "../c/msgimpl.h"

#include <cstdlib>

 using namespace Wombat;

@@ -34,6 +37,8 @@ void MamaMsgTestCPP::SetUp(void)

 void MamaMsgTestCPP::TearDown(void)

{

+    Mama::close();

+    MamaReservedFields::uninitReservedFields();

}

 TEST_F (MamaMsgTestCPP, CreateTest)

@@ -80,6 +85,83 @@ TEST_F (MamaMsgTestCPP, CopyTest)

     delete copy2;

}

+TEST_F (MamaMsgTestCPP, TempCopyOwnerTest)

+{

+    // getTempCopy: when the message can be modified, getTempCopy returns

+    // the message itself, so any changes will be applied to the Msg as well.

+

+    MamaMsg* orig = new MamaMsg;

+    MamaMsg* copy1 = NULL;

+    MamaMsg* copy2 = NULL;

+    const char* testString      = "test";

+

+    orig->create ();

+    orig->addString (NULL, 101, testString);

+

+    copy1 = orig->getTempCopy ();

+    ASSERT_TRUE(copy1);

+

+    ASSERT_STREQ(orig->toString (), copy1->toString ());

+

+    /* Try a second copy */

+    copy2 = orig->getTempCopy ();

+    ASSERT_TRUE(copy2);

+

+    /* String compare second copy */

+    ASSERT_STREQ(orig->toString (), copy2->toString ());

+

+    ASSERT_EQ(1, orig->getNumFields());

+

+    copy1->clear();

+    ASSERT_EQ(0, orig->getNumFields());

+

+    /* Clean up here */

+    delete orig;

+}

+

+TEST_F (MamaMsgTestCPP, TempCopyNotOwnerTest)

+{

+    // getTempCopy: when the message cannot be modified, getTempCopy returns

+    // a copy of the message and always the same copy.

+

+    const void*       buffer          = NULL;

+    size_t            bufferLength    = 0;

+    MamaMsg*          msg             = new MamaMsg;

+    MamaMsg* orig = new MamaMsg;

+    MamaMsg* copy1 = NULL;

+    MamaMsg* copy2 = NULL;

+    const char* testString      = "test";

+    short owner;

+

+    msg->create();

+    msg->addString (NULL, 101, testString);

+    msg->getByteBuffer (buffer, bufferLength);

+

+    orig->createFromBuffer (buffer, bufferLength);

+    mamaMsgImpl_setMessageOwner (orig->getUnderlyingMsg(), 0);

+    mamaMsgImpl_getMessageOwner (orig->getUnderlyingMsg(), &owner);

+    ASSERT_FALSE(owner);

+

+    copy1 = orig->getTempCopy ();

+    ASSERT_TRUE(copy1);

+    ASSERT_TRUE(orig != copy1);

+

+    ASSERT_STREQ(testString, copy1->getString(NULL, 101));

+    copy1->addString(NULL, 102, "copy1");

+    ASSERT_THROW(orig->getString(NULL, 102), MamaStatus);

+

+    /* Try a second copy */

+    copy2 = orig->getTempCopy ();

+    ASSERT_TRUE(copy2);

+    ASSERT_EQ(copy1, copy2);

+    ASSERT_STREQ(copy1->getString(NULL, 102), copy2->getString(NULL, 102));

+

+    mamaMsgImpl_setMessageOwner (orig->getUnderlyingMsg(), 1);

+    mamaMsgImpl_getMessageOwner (orig->getUnderlyingMsg(), &owner);

+    /* Clean up here */

+    delete orig;

+    delete msg;

+}

TEST_F (MamaMsgTestCPP, CreateFromBufferTest)

{

     const void*       buffer          = NULL;

@@ -100,3 +182,27 @@ TEST_F (MamaMsgTestCPP, CreateFromBufferTest)

     delete msg;

     delete createMsg;

}

+

+TEST_F (MamaMsgTestCPP, IteratorTest)

+{

+    MamaMsg msg;

+    msg.create();

+

+    msg.addChar("char", 1, 'a');

+    msg.addBoolean("boolean", 2, true);

+    msg.addI32("i32", 3, 123);

+    msg.addString("string", 4, "hello");

+

+    MamaMsgIterator it;

+    msg.begin(it);

+

+    int counter = 0;

+    while (*it != NULL)

+    {

+        mama_fid_t fid = it->getFid();

+        ASSERT_TRUE(0 < fid && fid < 5);

+        ++it;

+        ++counter;

+    }

+    ASSERT_EQ(4, counter);

+}

\ No newline at end of file

diff --git a/mama/c_cpp/src/gunittest/cpp/MamaOpenCloseTest.cpp b/mama/c_cpp/src/gunittest/cpp/MamaOpenCloseTest.cpp

index 9268c50..7021721 100644

--- a/mama/c_cpp/src/gunittest/cpp/MamaOpenCloseTest.cpp

+++ b/mama/c_cpp/src/gunittest/cpp/MamaOpenCloseTest.cpp

@@ -73,19 +73,19 @@ TEST_F(MamaOpenCloseTest, NestedOpenClose)

     // Load the bridge

     Mama::loadBridge(getMiddleware());

-    printf("Attempt first open\n");

+    //printf("Attempt first open\n");

     Mama::open();

-    printf("Attempt second open\n");

+    //printf("Attempt second open\n");

     Mama::open();

-    printf("Attempt first close\n");

+    //printf("Attempt first close\n");

     Mama::close();

-    printf("Attempt second close\n");

+    //printf("Attempt second close\n");

     Mama::close();

    

-    printf("Finished nested testing\n");

+    //printf("Finished nested testing\n");

}

 TEST_F(MamaOpenCloseTest, OpenCloseReopenSameBridge)

@@ -138,7 +138,7 @@ TEST_F(MamaOpenCloseTest, OpenCloseReopenNewBridge)

//CPP Test

void MamaOpenCloseTest::StartBackgroundCallback::onStartComplete(MamaStatus status)

{

-    printf("Start completed\n");

+    //printf("Start completed\n");

}

 TEST_F(MamaOpenCloseTest, StartStopDifferentThreads)

diff --git a/mama/c_cpp/src/gunittest/cpp/MamaSubscriptionTest.cpp b/mama/c_cpp/src/gunittest/cpp/MamaSubscriptionTest.cpp

index dae66ad..05fc191 100644

--- a/mama/c_cpp/src/gunittest/cpp/MamaSubscriptionTest.cpp

+++ b/mama/c_cpp/src/gunittest/cpp/MamaSubscriptionTest.cpp

@@ -792,7 +792,7 @@ TEST_F(MamaSubscriptionTest, BasicSubscription)

             // Create the subscription

             //basicSubscription->createBasic(m_transport, queue, testCallback, "CTA_3.IBM");

             basicSubscription->createBasic(&m_transport, queue, testCallback, "JGRAY");

-            printf("subscription created for JGRAY-  start dispatching thread on default queue\n");

+            //printf("subscription created for JGRAY-  start dispatching thread on default queue\n");

             // Process messages until the first message is received

            

             Mama::start(m_bridge);

diff --git a/mama/c_cpp/src/gunittest/cpp/MamaTimerTest.cpp b/mama/c_cpp/src/gunittest/cpp/MamaTimerTest.cpp

index 8e4345d..71b4f0d 100644

--- a/mama/c_cpp/src/gunittest/cpp/MamaTimerTest.cpp

+++ b/mama/c_cpp/src/gunittest/cpp/MamaTimerTest.cpp

@@ -130,7 +130,7 @@ void MamaTimerTestCPP::OnForRecursiveTick::onTimer(MamaTimer* timer)

void MamaTimerTestCPP::OnForRecursiveTick::onDestroy(MamaTimer* timer, void * closure)

{

     // Cast the closure to a test fixture

-    printf("in recursive onDestroy\n");

+    //printf("in recursive onDestroy\n");

     MamaTimerTestCPP *fixture = (MamaTimerTestCPP *)closure;

     // Increment the number of times this function has been called

@@ -150,14 +150,14 @@ void MamaTimerTestCPP::OnForRecursiveTick::onDestroy(MamaTimer* timer, void * cl

 void MamaTimerTestCPP::OnForTwoTimerTick::onTimer(MamaTimer* timer)

{

-    printf("on timer tick\n");

+    //printf("on timer tick\n");

 }

 void MamaTimerTestCPP::OnForTwoTimerTick::onDestroy(MamaTimer* timer, void * closure)

{

     // Cast the closure to a test fixture

-    printf("in TwoTick onDestroy\n");

+    //printf("in TwoTick onDestroy\n");

}

 /* ************************************************************************* */

@@ -173,12 +173,12 @@ TEST_F(MamaTimerTestCPP, ForTimer)

     MamaTimerTestCPP::OnForTimerTick timerCb;

     for(int counter=0; counter<m_numberForTimers; counter++)

     {

-        printf("Creating new timer\n");

+        //printf("Creating new timer\n");

         m_timerInterval = (counter + 1)/100;

         mtarray[counter].create(m_defaultQueue, &timerCb, m_timerInterval, m_this);

     }

     Mama::start(m_bridge);

-    printf("Timers started\n");

+    //printf("Timers started\n");

 

 }   

 

--

1.7.9.5

 




Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.