(file) Return to test_string.cpp CVS log (file) (dir) Up to [OMI] / omi / tests / micxx

File: [OMI] / omi / tests / micxx / test_string.cpp (download)
Revision: 1.1, Mon Apr 20 17:20:35 2015 UTC (9 years, 1 month ago) by krisbash
Branch: MAIN
CVS Tags: OMI_1_0_8_2, OMI_1_0_8_1, HEAD
OMI 1.0.8-1

/*
**==============================================================================
**
** Open Management Infrastructure (OMI)
**
** Copyright (c) Microsoft Corporation
** 
** 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 
**
** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
** MERCHANTABLITY OR NON-INFRINGEMENT. 
**
** See the Apache 2 License for the specific language governing permissions 
** and limitations under the License.
**
**==============================================================================
*/

#include <ut/ut.h>
#include "../../micxx/micxx_string.h"
#include "../../common/common.h"
#include <pal/strings.h>

using namespace mi;

namespace tt {
static void setUp4()
{
    char* p = (char*) PAL_Malloc(90);
    PAL_Free(p);
}
}


NitsSetup(TestStringSetup)
{
    tt::setUp4();
}
NitsEndSetup

NitsCleanup(TestStringSetup)
NitsEndCleanup


NitsTestWithSetup(TestStringCtor, TestStringSetup)
{
    String s1(MI_T("string1"));

    UT_ASSERT( Tcscmp(s1.Str(), MI_T("string1")) == 0);

    // reset value
    s1 = 0;
    UT_ASSERT( Tcscmp(s1.Str(), MI_T("string1")) != 0);
    UT_ASSERT( Tcscmp(s1.Str(), MI_T("")) == 0);
}
NitsEndTest

NitsTestWithSetup(TestCharWithSizeCtor, TestStringSetup)
{
    String s1(MI_T("string1"), 3);

    UT_ASSERT( Tcscmp(s1.Str(), MI_T("str")) == 0);
    UT_ASSERT(s1.GetSize() == 3);
    UT_ASSERT(s1 == MI_T("str"));
}
NitsEndTest

NitsTestWithSetup(TestCharWithZeroSizeCtor, TestStringSetup)
{
    String s1(MI_T("string1"), 0);

    UT_ASSERT( Tcscmp(s1.Str(), MI_T("")) == 0);
    UT_ASSERT(s1.GetSize() == 0);
    UT_ASSERT(s1 == MI_T(""));
}
NitsEndTest

NitsTestWithSetup(TestNullCharWithZeroSizeCtor, TestStringSetup)
{
    String s1(0, 0);

    UT_ASSERT( Tcscmp(s1.Str(), MI_T("")) == 0);
    UT_ASSERT(s1.GetSize() == 0);
    UT_ASSERT(s1 == MI_T(""));
}
NitsEndTest

NitsTestWithSetup(TestCopyCtor, TestStringSetup)
{
    String s1(MI_T("str1"));
    String s2 = s1;
    String s3(s1);
    String s4;  s4 = s1;

    // all strings has to be identical and point to the same memory block
    UT_ASSERT( Tcscmp(s1.Str(), MI_T("str1")) == 0);
    UT_ASSERT( s1 == s2 );
    UT_ASSERT( s1 == s3 );
    UT_ASSERT( s1 == s4 );

    UT_ASSERT( s1.Str() == s2.Str() );
    UT_ASSERT( s1.Str() == s3.Str() );
    UT_ASSERT( s1.Str() == s4.Str() );

}
NitsEndTest

NitsTestWithSetup(TestAssignmentToSelf, TestStringSetup)
{
    String s1;

    s1 = s1;
    UT_ASSERT( s1 == MI_T("") );

    s1 = MI_T("*");
    s1 = s1;
    UT_ASSERT( s1 == MI_T("*") );
    
}
NitsEndTest

NitsTestWithSetup(TestRefCounting, TestStringSetup)
{
    String s1(MI_T("str1"));
    String s2 = s1;
    String s3(s1);
    String s4;  s4 = s1;

    // assign the same string again - should re-allocate string, but keep contetn the same
    s1 = MI_T("str1");

    UT_ASSERT( s1 == s2 );
    UT_ASSERT( s1 == s3 );
    UT_ASSERT( s1 == s4 );

    UT_ASSERT(s1.Str() != s2.Str());
    UT_ASSERT(s2.Str() == s3.Str());
    UT_ASSERT(s2.Str() == s4.Str());
}
NitsEndTest

NitsTestWithSetup(TestComparison, TestStringSetup)
{
    // compare == / !=
    String s1 = MI_T("s1");

    // compare to self
    UT_ASSERT( s1 == s1 );
    // even null
    String s_null;

    UT_ASSERT( s_null == s_null );

    String s2 = MI_T("s_2");

    UT_ASSERT( s1 != s2 );

    s2 = MI_T("s1");

    UT_ASSERT(s1 == s2 );


    UT_ASSERT( s1 == MI_T("s1") );
    UT_ASSERT( s1 != MI_T("something else") );
}
NitsEndTest

NitsTestWithSetup(TestComparisonOfEmptyStrings, TestStringSetup)
{
    String s_null, s_empty = MI_T(""), s_not_empty = MI_T("123");

    // compare to self
    UT_ASSERT( s_null == s_null );
    UT_ASSERT( s_empty == s_empty );
    UT_ASSERT( s_not_empty == s_not_empty );

    // compare null
    UT_ASSERT( s_null == s_empty );
    UT_ASSERT( s_null != s_not_empty );

    // compare empty
    UT_ASSERT( s_empty != s_not_empty );

    // compare to char*
    UT_ASSERT( s_null == MI_T("") );
    UT_ASSERT( s_empty == MI_T("") );
    UT_ASSERT( s_not_empty == MI_T("123") );

}
NitsEndTest

NitsTestWithSetup(TestGetSize, TestStringSetup)
{
    String s;

    UT_ASSERT( s.GetSize() == 0 );

    s = MI_T("123");

    UT_ASSERT( s.GetSize() == 3 );
}
NitsEndTest

NitsTestWithSetup(TestConcatString, TestStringSetup)
{
    String s;

    UT_ASSERT( s == MI_T("") );

    s += MI_T("1");
    UT_ASSERT( s == MI_T("1") );
    UT_ASSERT( s.GetSize() == 1 );

    s += String(MI_T("4"));
    UT_ASSERT( s == MI_T("14") );
    UT_ASSERT( s.GetSize() == 2 );
}
NitsEndTest

NitsTestWithSetup(TestConcatStringRefCounter, TestStringSetup)
{
    // check that ref-counter works properly with += operator

    String source1 = MI_T("source");
    String source2 = MI_T("*");

    String target = source2;

    // they point to the same buffer 
    UT_ASSERT( target == source2 );
    UT_ASSERT( target.Str() == source2.Str() );

    const ZChar* source2_p = source2.Str();

    target += source1;
    UT_ASSERT( target == MI_T("*source") );
    UT_ASSERT( target.Str() != source2.Str() );
    UT_ASSERT( source2_p == source2.Str() );
    UT_ASSERT( source2 == MI_T("*") );
}
NitsEndTest

NitsTestWithSetup(TestConcatStringEmptyString, TestStringSetup)
{
    String s1,s2;

    s1 += String(MI_T("1"));
    UT_ASSERT(s1 == MI_T("1"));
    s1 += String(MI_T("234567890123456"));
    UT_ASSERT(s1 == MI_T("1234567890123456"));
    UT_ASSERT(s1.GetSize() == 16);
    s1 += String();
    UT_ASSERT(s1 == MI_T("1234567890123456"));
    UT_ASSERT(s1.GetSize() == 16);

    s2 += MI_T("1");
    UT_ASSERT(s2 == MI_T("1"));
    s2 += MI_T("234567890123456");
    UT_ASSERT(s2 == MI_T("1234567890123456"));
    UT_ASSERT(s2.GetSize() == 16);
    s2 += MI_T("");
    UT_ASSERT(s2 == MI_T("1234567890123456"));
    UT_ASSERT(s2.GetSize() == 16);

    const ZChar* null_str = 0;
    s2 += null_str;
    UT_ASSERT(s2 == MI_T("1234567890123456"));
    UT_ASSERT(s2.GetSize() == 16);
}
NitsEndTest




ViewCVS 0.9.2