Commit 55a3220a authored by hybrid's avatar hybrid

Add test for string operator+= and fix for long type parameters. Bug and fix found by loneboco.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@3211 dfc29bdd-3216-0410-991c-e03cc46cb475
parent db4abd66
...@@ -188,6 +188,85 @@ public: ...@@ -188,6 +188,85 @@ public:
} }
//! Constructs a string from a long
explicit string(long number)
: array(0), allocated(0), used(0)
{
// store if negative and make positive
bool negative = false;
if (number < 0)
{
number *= -1;
negative = true;
}
// temporary buffer for 16 numbers
c8 tmpbuf[16]={0};
u32 idx = 15;
// special case '0'
if (!number)
{
tmpbuf[14] = '0';
*this = &tmpbuf[14];
return;
}
// add numbers
while(number && idx)
{
--idx;
tmpbuf[idx] = (c8)('0' + (number % 10));
number /= 10;
}
// add sign
if (negative)
{
--idx;
tmpbuf[idx] = '-';
}
*this = &tmpbuf[idx];
}
//! Constructs a string from an unsigned long
explicit string(unsigned long number)
: array(0), allocated(0), used(0)
{
// temporary buffer for 16 numbers
c8 tmpbuf[16]={0};
u32 idx = 15;
// special case '0'
if (!number)
{
tmpbuf[14] = '0';
*this = &tmpbuf[14];
return;
}
// add numbers
while(number && idx)
{
--idx;
tmpbuf[idx] = (c8)('0' + (number % 10));
number /= 10;
}
*this = &tmpbuf[idx];
}
//! Constructor for copying a string from a pointer with a given length //! Constructor for copying a string from a pointer with a given length
template <class B> template <class B>
string(const B* const c, u32 length) string(const B* const c, u32 length)
......
...@@ -41,6 +41,80 @@ static bool testFastAlloc() ...@@ -41,6 +41,80 @@ static bool testFastAlloc()
} }
bool testAppendStringc()
{
core::stringc str;
// Test with character
if (str != "")
return false;
str += 'W';
if (str != "W")
return false;
str += 'i';
if (str != "Wi")
return false;
str="";
if (str != "")
return false;
// Test with C-style string
str += "Another Test";
if (str != "Another Test")
return false;
str="";
str += 'A';
str += "nother Test";
if (str != "Another Test")
return false;
str="";
// Test with int
str += 10;
if (str != "10")
return false;
str += 0;
if (str != "100")
return false;
str="";
str += "-32";
if (str != "-32")
return false;
str="";
// Test with unsigned int
str += 21u;
if (str != "21")
return false;
str += 0u;
if (str != "210")
return false;
str="";
// Test with long int
str += 456l;
if (str != "456")
return false;
str += 0l;
if (str != "4560")
return false;
str="";
str += -456l;
if (str != "-456")
return false;
str="";
// Test with unsigned long
str += 994ul;
if (str != "994")
return false;
str += 0ul;
if (str != "9940")
return false;
str="";
return true;
}
// Test the functionality of irrString // Test the functionality of irrString
/** Validation is done with asserts() against expected results. */ /** Validation is done with asserts() against expected results. */
bool testIrrString(void) bool testIrrString(void)
...@@ -85,6 +159,7 @@ bool testIrrString(void) ...@@ -85,6 +159,7 @@ bool testIrrString(void)
assert(*(empty.c_str())==0); assert(*(empty.c_str())==0);
assert(allExpected &= testSplit()); assert(allExpected &= testSplit());
} }
allExpected &= testAppendStringc();
logTestString("Test io::path\n"); logTestString("Test io::path\n");
{ {
...@@ -106,4 +181,3 @@ bool testIrrString(void) ...@@ -106,4 +181,3 @@ bool testIrrString(void)
return allExpected; return allExpected;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment