[PATCH 1/6] [mama] C Unittests


Ian Bell <IBell@...>
 

From 766019e29e6ff6072c662caa2de6adbd3e9df875 Mon Sep 17 00:00:00 2001

Message-Id: <766019e29e6ff6072c662caa2de6adbd3e9df875.1348824474.git.ibell@...>

From: Ian Bell <ibell@...>

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

Subject: [PATCH 1/6] [mama] C Unittests

 

Added some new tests and cleaned up some existing

 

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

---

mama/c_cpp/src/gunittest/c/MainUnitTestC.cpp    |    2 +

mama/c_cpp/src/gunittest/c/Makefile.am          |    4 +-

mama/c_cpp/src/gunittest/c/msgtests.cpp         |  179 ++++++++++++++++++++++-

mama/c_cpp/src/gunittest/c/subscriptiontest.cpp |    3 +-

mama/c_cpp/src/gunittest/c/timertest.cpp        |   18 +--

5 files changed, 189 insertions(+), 17 deletions(-)

 

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

index 06f5797..ad43679 100644

--- a/mama/c_cpp/src/gunittest/c/MainUnitTestC.cpp

+++ b/mama/c_cpp/src/gunittest/c/MainUnitTestC.cpp

@@ -21,6 +21,8 @@

#include <cstdlib>

#include <iostream>

#include <gtest/gtest.h>

+#include <mama/mama.h>

+#include <mama/status.h>

#include <string>

 #ifndef WOMBAT_CONFIG_NO_NAMESPACES

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

index cb88d29..0ff852a 100644

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

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

@@ -25,7 +25,7 @@ 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

@@ -43,7 +43,7 @@ LDADD = -lgtest_main -ldl

 bin_PROGRAMS = MainUnitTestC

-nodist_MainUnitTestC_SOURCES = MainUnitTestC.cpp \

+nodist_UnitTestMamaC_SOURCES = MainUnitTestC.cpp \

                                                                                                               msgtests.cpp \

                                                                                                               iteration.cpp \

                             openclosetest.cpp \

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

index 985e588..21c6584 100755

--- a/mama/c_cpp/src/gunittest/c/msgtests.cpp

+++ b/mama/c_cpp/src/gunittest/c/msgtests.cpp

@@ -23,6 +23,7 @@

#include "mama/msg.h"

#include <gtest/gtest.h>

#include <cstdlib>

+#include "msgimpl.h"

 #include "MainUnitTestC.h"

@@ -31,16 +32,13 @@ class MamaMsgTestC : public ::testing::Test

 

 protected:

-    MamaMsgTestC () {}

+    MamaMsgTestC () : mMamaBridge(NULL) {}

     virtual ~MamaMsgTestC () {}

     virtual void SetUp()

     {

-        mamaBridge mMamaBridge;

-

         mama_loadBridge (&mMamaBridge, getMiddleware());

-

                mama_open();

     }

@@ -50,6 +48,7 @@ protected:

     }

 

+    mamaBridge mMamaBridge;

};

 

@@ -61,10 +60,38 @@ TEST_F (MamaMsgTestC, CreateTest)

     ASSERT_EQ(status, MAMA_STATUS_OK);

     ASSERT_TRUE(msg != NULL);

+    mamaPayloadType payloadType = MAMA_PAYLOAD_UNKNOWN;

+    mamaMsg_getPayloadType (msg, &payloadType);

+    ASSERT_TRUE(payloadType != MAMA_PAYLOAD_UNKNOWN);

+

+    ASSERT_TRUE(mamaMsg_toString(msg));

     mamaMsg_destroy (msg);

}

+TEST_F (MamaMsgTestC, ClearTest)

+{

+    mamaMsg orig = NULL;

+    const char* testString      = "test";

+    mama_status status          = MAMA_STATUS_OK;

+    mama_size_t numFields       = 0;

+

+    mamaMsg_create (&orig);

+

+    status = mamaMsg_addString (orig, "101", 101, testString);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    mamaMsg_getNumFields(orig, &numFields);

+    ASSERT_EQ(1, numFields);

+

+    status = mamaMsg_clear (orig);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    mamaMsg_getNumFields(orig, &numFields);

+    ASSERT_EQ(0, numFields);

+

+    /* Clean up here */

+    mamaMsg_destroy (orig);

+}

+

TEST_F (MamaMsgTestC, CopyTest)

{

     mamaMsg orig = NULL;

@@ -95,6 +122,148 @@ TEST_F (MamaMsgTestC, CopyTest)

     mamaMsg_destroy (copy);

}

+TEST_F (MamaMsgTestC, CopyNotOwnerTest)

+{

+    mamaMsg orig = NULL;

+    mamaMsg copy = NULL;

+    const char* testString      = "test";

+    mama_status status          = MAMA_STATUS_OK;

+    short owner;

+

+    mamaMsg_create (&orig);

+    mamaMsg_create (&copy);

+

+    status = mamaMsg_addString (orig, "101", 101, testString);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+

+    mamaMsgImpl_setMessageOwner (orig, 0);

+    status = mamaMsg_copy (orig, &copy);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    mamaMsgImpl_getMessageOwner (orig, &owner);

+    ASSERT_EQ(0, owner);

+

+    /* Clean up here */

+    mamaMsg_destroy (orig);

+    mamaMsg_destroy (copy);

+}

+

+TEST_F (MamaMsgTestC, TempCopyOwnerTest)

+{

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

+    // the message itself

+

+    mamaMsg orig = NULL;

+    mamaMsg copy1 = NULL;

+    mamaMsg copy2 = NULL;

+    const char* testString      = "test";

+    const char* res1 = NULL;

+    const char* res2 = NULL;

+    mama_status status = MAMA_STATUS_OK;

+    short owner;

+

+    mamaMsg_create (&orig);

+    mamaMsgImpl_getMessageOwner (orig, &owner);

+    ASSERT_TRUE(owner);

+

+    status = mamaMsg_addString (orig, "101", 101, testString);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+

+    status = mamaMsg_getTempCopy (orig, &copy1);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    ASSERT_TRUE(copy1);

+    ASSERT_TRUE(orig == copy1);

+

+    ASSERT_STREQ(mamaMsg_toString (orig), mamaMsg_toString (copy1));

+

+    mamaMsg_addString (copy1, "name1", 101, "copy1");

+

+    /* Try a second copy */

+    status = mamaMsg_getTempCopy (orig, &copy2);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    ASSERT_TRUE(copy2);

+    ASSERT_TRUE(orig == copy2);

+

+    mamaMsg_getString(copy1, "name1", 101, &res1);

+    mamaMsg_getString(copy2, "name1", 101, &res2);

+    ASSERT_STREQ(res1, res2);

+

+    /* String compare second copy */

+    ASSERT_STREQ(mamaMsg_toString (copy1), mamaMsg_toString (copy2));

+

+    /* Clean up here */

+    mamaMsg_destroy (orig);

+}

+

+TEST_F (MamaMsgTestC, TempCopyNotOwnerTest)

+{

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

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

+

+    const void* buffer          = NULL;

+    mama_size_t bufferLength    = 0;

+    mamaMsg msg = NULL;

+    mamaMsg orig = NULL;

+    mamaMsg copy1 = NULL;

+    mamaMsg copy2 = NULL;

+    const char* testString      = "test";

+    const char* res1 = NULL;

+    const char* res2 = NULL;

+    mama_status status = MAMA_STATUS_OK;

+    short owner;

+

+    mamaMsg_create (&msg);

+    status = mamaMsg_addString (msg, NULL, 101, testString);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    status = mamaMsg_getByteBuffer (msg, &buffer, &bufferLength);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+

+    status = mamaMsg_createFromByteBuffer (&orig, buffer, bufferLength);

+

+    mamaMsgImpl_setMessageOwner (orig, 0);

+    mamaMsgImpl_getMessageOwner (orig, &owner);

+    ASSERT_FALSE(owner);

+

+    status = mamaMsg_getTempCopy (orig, &copy1);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    ASSERT_TRUE(copy1);

+    ASSERT_TRUE(orig != copy1);

+    mamaMsgImpl_getMessageOwner (copy1, &owner);

+    ASSERT_TRUE(owner);

+

+//    ASSERT_STREQ(mamaMsg_toString (orig), mamaMsg_toString (copy1));

+

+    status = mamaMsg_getString(copy1, NULL, 101, &res1);

+    ASSERT_EQ(MAMA_STATUS_OK, status);

+    ASSERT_STREQ(testString, res1);

+    status = mamaMsg_addString (copy1, NULL, 102, "copy1");

+    ASSERT_EQ(MAMA_STATUS_OK, status);

+    status = mamaMsg_getString(copy1, NULL, 102, &res1);

+    ASSERT_EQ(MAMA_STATUS_OK, status);

+    status = mamaMsg_getString(orig, NULL, 102, &res1);

+    ASSERT_TRUE(MAMA_STATUS_OK != status);

+

+    /* Try a second copy */

+    status = mamaMsg_getTempCopy (orig, &copy2);

+    ASSERT_EQ(status, MAMA_STATUS_OK);

+    ASSERT_TRUE(copy2);

+    ASSERT_TRUE(copy1 == copy2);

+    mamaMsgImpl_getMessageOwner (copy2, &owner);

+    ASSERT_TRUE(owner);

+

+    mamaMsg_getString(copy1, NULL, 101, &res1);

+    mamaMsg_getString(copy2, NULL, 101, &res2);

+    ASSERT_STREQ(res1, res2);

+

+    /* String compare second copy */

+//    ASSERT_STREQ(mamaMsg_toString (copy1), mamaMsg_toString (copy2));

+

+    mamaMsgImpl_setMessageOwner (orig, 1); // to be able to clear memory

+    mamaMsgImpl_getMessageOwner (orig, &owner);

+    ASSERT_TRUE(owner);

+    /* Clean up here */

+    mamaMsg_destroy (orig);

+    mamaMsg_destroy (msg);

+}

TEST_F (MamaMsgTestC, CreateFromBufferTest)

{

     const void* buffer          = NULL;

@@ -140,4 +309,6 @@ TEST_F (MamaMsgTestC, CreateFromBufferTest)

     mamaMsg_destroy (msg);

     mamaMsg_destroy (createMsg);

+    mamaPrice_destroy(tempPrice);

+    mamaDateTime_destroy(tempDate);

}

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

index 4705d16..f5bb252 100644

--- a/mama/c_cpp/src/gunittest/c/subscriptiontest.cpp

+++ b/mama/c_cpp/src/gunittest/c/subscriptiontest.cpp

@@ -156,13 +156,12 @@ TEST_F(MamaSubscriptionTest, BasicSubscription)

         mamaSource_setId(testSource, "TestSource");

         mamaSource_setTransport(testSource, m_transport);

         mamaSource_setSymbolNamespace(testSource, "WOMBAT");

-        printf("Note: as there is no real source, this test should hang\n");

+        //printf("Note: as there is no real source, this test should hang\n");

         // Create the subscription

         ASSERT_EQ(  MAMA_STATUS_OK,

             mamaSubscription_create(subscription, m_defaultQueue, &simpleCallback, testSource, "TEST_SYMBOL", m_this));

        

-        mama_start(m_bridge);

         // Destroy the subscription

         ASSERT_EQ(MAMA_STATUS_OK, mamaSubscription_destroy(subscription));

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

index 23ffc43..ccdb6ac 100644

--- a/mama/c_cpp/src/gunittest/c/timertest.cpp

+++ b/mama/c_cpp/src/gunittest/c/timertest.cpp

@@ -120,21 +120,21 @@ void MamaTimerTestC::TearDown(void)

 static void MAMACALLTYPE onTimerTick(mamaTimer timer, void* closure)

{

-    printf("Timer ticking....\n");

+    //printf("Timer ticking....\n");

     mamaTimer_destroy(timer);

}

 static void MAMACALLTYPE onTimerDestroy(mamaTimer timer, void* closure)

{

-    printf("Timer destroying...\n");

+    //printf("Timer destroying...\n");

     MamaTimerTestC *fixture = (MamaTimerTestC *)closure;

     fixture->m_numberForCallbacks ++;

-    printf("m_numberForCallbacks = %i\n", fixture->m_numberForCallbacks);

+    //printf("m_numberForCallbacks = %i\n", fixture->m_numberForCallbacks);

     // If all the timers have been destroyed then quit out

     if(fixture->m_numberForCallbacks == fixture->m_numberForTimers)

     {

-        printf("Stopping timer\n");

+      //  printf("Stopping timer\n");

         mama_stop(fixture->m_bridge);

     }

}

@@ -146,7 +146,7 @@ static void MAMACALLTYPE onStopDispatchingTimerTick(mamaTimer timer, void* closu

 static void MAMACALLTYPE onStopDispatchingTimerDestroy(mamaTimer timer, void* closure)

{

-    printf("Stopping Two Timers ticking\n");

+    //printf("Stopping Two Timers ticking\n");

     MamaTimerTestC* fixture = (MamaTimerTestC *)closure;

     mama_stop(fixture->m_bridge);

@@ -166,7 +166,7 @@ static void MAMACALLTYPE onRecursiveTimerTick(mamaTimer timer, void* closure)

static void MAMACALLTYPE onRecursiveTimerDestroy(mamaTimer timer, void* closure)

{

     // Cast the closure to a test fixture

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

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

     MamaTimerTestC *fixture = (MamaTimerTestC *)closure;

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

@@ -187,14 +187,14 @@ static void MAMACALLTYPE onRecursiveTimerDestroy(mamaTimer timer, void* closure)

 static void MAMACALLTYPE onTwoTimerTick(mamaTimer timer, void* closure)

{

-    printf("In TwoTimer Tick\n");

+    //printf("In TwoTimer Tick\n");

 }

 static void MAMACALLTYPE onTwoTimerDestroy(mamaTimer timer, void * closure)

{

     // Cast the closure to a test fixture

-    printf("In TwoTimer Destroy\n");

+    //printf("In TwoTimer Destroy\n");

}

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

/* Test Functions */

@@ -208,7 +208,7 @@ TEST_F(MamaTimerTestC, ForTimer)

     // Create a whole lot of timers

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

     {

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

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

         m_timerInterval = (counter + 1)/100;

         mamaTimer_create2(&mtarray[counter], m_defaultQueue, onTimerTick, onTimerDestroy, m_timerInterval, m_this);

     }

--

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.