/*
**==============================================================================
**
** 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/string.h"
#include "../../common/common.h"
#include <base/strings.h>
using namespace mi;
namespace tt {
static void setUp4()
{
char* p = (char*) malloc(90);
free(p);
}
}
static void setUp()
{
tt::setUp4();
}
static void cleanup()
{
}
static void TestCtor()
{
String s1(MI_T("string1"));
UT_ASSERT( Zcmp(s1.Str(), MI_T("string1")) == 0);
// reset value
s1 = 0;
UT_ASSERT( Zcmp(s1.Str(), MI_T("string1")) != 0);
UT_ASSERT( Zcmp(s1.Str(), MI_T("")) == 0);
}
static void TestCharWithSizeCtor()
{
String s1(MI_T("string1"), 3);
UT_ASSERT( Zcmp(s1.Str(), MI_T("str")) == 0);
UT_ASSERT(s1.GetSize() == 3);
UT_ASSERT(s1 == MI_T("str"));
}
static void TestCharWithZeroSizeCtor()
{
String s1(MI_T("string1"), 0);
UT_ASSERT( Zcmp(s1.Str(), MI_T("")) == 0);
UT_ASSERT(s1.GetSize() == 0);
UT_ASSERT(s1 == MI_T(""));
}
static void TestNullCharWithZeroSizeCtor()
{
String s1(0, 0);
UT_ASSERT( Zcmp(s1.Str(), MI_T("")) == 0);
UT_ASSERT(s1.GetSize() == 0);
UT_ASSERT(s1 == MI_T(""));
}
static void TestCopyCtor()
{
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( Zcmp(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() );
}
static void TestAssignmentToSelf()
{
String s1;
s1 = s1;
UT_ASSERT( s1 == MI_T("") );
s1 = MI_T("*");
s1 = s1;
UT_ASSERT( s1 == MI_T("*") );
}
static void TestRefCounting()
{
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());
}
static void TestComparison()
{
// 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") );
}
static void TestComparisonOfEmptyStrings()
{
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") );
}
static void TestGetSize()
{
String s;
UT_ASSERT( s.GetSize() == 0 );
s = MI_T("123");
UT_ASSERT( s.GetSize() == 3 );
}
static void TestConcatString()
{
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 );
}
static void TestConcatStringRefCounter()
{
// 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 MI_Char* 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("*") );
}
static void TestConcatStringEmptyString()
{
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 MI_Char* null_str = 0;
s2 += null_str;
UT_ASSERT(s2 == MI_T("1234567890123456"));
UT_ASSERT(s2.GetSize() == 16);
}
static void RunTests()
{
UT_TEST(TestCtor);
UT_TEST(TestCopyCtor);
UT_TEST(TestCharWithSizeCtor);
UT_TEST(TestCharWithZeroSizeCtor);
UT_TEST(TestNullCharWithZeroSizeCtor);
UT_TEST(TestAssignmentToSelf);
UT_TEST(TestRefCounting);
UT_TEST(TestComparison);
UT_TEST(TestComparisonOfEmptyStrings);
UT_TEST(TestGetSize);
UT_TEST(TestConcatString);
UT_TEST(TestConcatStringRefCounter);
UT_TEST(TestConcatStringEmptyString);
}
UT_ENTRY_POINT(RunTests);