Quellcode durchsuchen

core: more string concatenation and comparison methods, plus a lot

of unit tests for the String class.
legacy
Sam Hocevar sam vor 12 Jahren
Ursprung
Commit
46f7e750c0
4 geänderte Dateien mit 169 neuen und 2 gelöschten Zeilen
  1. +1
    -0
      src/Makefile.am
  2. +41
    -1
      src/lol/core/string.h
  3. +1
    -1
      test/Makefile.am
  4. +126
    -0
      test/unit/string.cpp

+ 1
- 0
src/Makefile.am Datei anzeigen

@@ -17,6 +17,7 @@ liblol_a_SOURCES = \
platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \
\
lol/unit.h lol/debug.h \
lol/core/array.h lol/core/string.h \
lol/math/vector.h lol/math/half.h lol/math/real.h lol/math/remez.h \
lol/math/math.h \
\


+ 41
- 1
src/lol/core/string.h Datei anzeigen

@@ -44,6 +44,11 @@ public:
while (*str++);
}

inline String(String const &s)
: Super((Super const &)s)
{
}

inline char &operator [](int n)
{
return ((Super &)*this)[n];
@@ -54,7 +59,12 @@ public:
return ((Super const &)*this)[n];
}

inline String operator +(String const &s)
inline int Count() const
{
return ((Super const &)*this).Count() - 1;
}

inline String operator +(String const &s) const
{
String ret(*this);
return ret += s;
@@ -67,6 +77,36 @@ public:
(Super &)*this += (Super const &)s;
return *this;
}

inline String operator +(char c) const
{
String ret(*this);
return ret += c;
}

inline String operator +=(char c)
{
((Super &)*this).Last() = c;
((Super &)*this).Push('\0');
return *this;
}

inline bool operator ==(String const &s) const
{
if (this->m_count != s.m_count)
return false;

for (int i = 0; i < this->m_count; ++i)
if ((*this)[i] != s[i])
return false;

return true;
}

inline bool operator !=(String const &s) const
{
return !(*this == s);
}
};

} /* namespace lol */


+ 1
- 1
test/Makefile.am Datei anzeigen

@@ -22,7 +22,7 @@ endif
testsuite_SOURCES = testsuite.cpp \
unit/vector.cpp unit/matrix.cpp unit/half.cpp unit/trig.cpp \
unit/build.cpp unit/real.cpp unit/image.cpp unit/quat.cpp unit/cmplx.cpp \
unit/array.cpp unit/rotation.cpp
unit/array.cpp unit/rotation.cpp unit/string.cpp
testsuite_CPPFLAGS = @LOL_CFLAGS@
testsuite_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@
testsuite_DEPENDENCIES = $(top_builddir)/src/liblol.a


+ 126
- 0
test/unit/string.cpp Datei anzeigen

@@ -0,0 +1,126 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include "core.h"
#include "lol/unit.h"

namespace lol
{

LOLUNIT_FIXTURE(StringTest)
{
void SetUp() {}

void TearDown() {}

LOLUNIT_TEST(StringBuild)
{
String s1;
LOLUNIT_ASSERT_EQUAL(s1.Count(), 0);
LOLUNIT_ASSERT_EQUAL(s1[0], '\0');

String s2("");
LOLUNIT_ASSERT_EQUAL(s2.Count(), 0);
LOLUNIT_ASSERT_EQUAL(s2[0], '\0');

String s3("a");
LOLUNIT_ASSERT_EQUAL(s3.Count(), 1);
LOLUNIT_ASSERT_EQUAL(s3[0], 'a');
LOLUNIT_ASSERT_EQUAL(s3[1], '\0');

String s4(s3);
LOLUNIT_ASSERT_EQUAL(s4.Count(), 1);
LOLUNIT_ASSERT_EQUAL(s4[0], 'a');
LOLUNIT_ASSERT_EQUAL(s4[1], '\0');
}

LOLUNIT_TEST(StringAppendChar)
{
String s;
s += 'a';
s += 'b';
s += 'c';

LOLUNIT_ASSERT_EQUAL(s[0], 'a');
LOLUNIT_ASSERT_EQUAL(s[1], 'b');
LOLUNIT_ASSERT_EQUAL(s[2], 'c');
LOLUNIT_ASSERT_EQUAL(s[3], '\0');
}

LOLUNIT_TEST(StringCopy)
{
String s1 = "abc";

String s2 = s1;

LOLUNIT_ASSERT_EQUAL(s1[0], s2[0]);
LOLUNIT_ASSERT_EQUAL(s1[1], s2[1]);
LOLUNIT_ASSERT_EQUAL(s1[2], s2[2]);
LOLUNIT_ASSERT_EQUAL(s1[3], s2[3]);
}

LOLUNIT_TEST(StringConcat)
{
String s1("ab"), s2("cd");

String s3 = s1 + s2;
LOLUNIT_ASSERT_EQUAL(s3[0], 'a');
LOLUNIT_ASSERT_EQUAL(s3[1], 'b');
LOLUNIT_ASSERT_EQUAL(s3[2], 'c');
LOLUNIT_ASSERT_EQUAL(s3[3], 'd');
LOLUNIT_ASSERT_EQUAL(s3[4], '\0');
}

LOLUNIT_TEST(StringAppendString)
{
String s1("ab"), s2("cd");

s1 += s2;
LOLUNIT_ASSERT_EQUAL(s1[0], 'a');
LOLUNIT_ASSERT_EQUAL(s1[1], 'b');
LOLUNIT_ASSERT_EQUAL(s1[2], 'c');
LOLUNIT_ASSERT_EQUAL(s1[3], 'd');
LOLUNIT_ASSERT_EQUAL(s1[4], '\0');

s2 += s2;
LOLUNIT_ASSERT_EQUAL(s2[0], 'c');
LOLUNIT_ASSERT_EQUAL(s2[1], 'd');
LOLUNIT_ASSERT_EQUAL(s2[2], 'c');
LOLUNIT_ASSERT_EQUAL(s2[3], 'd');
LOLUNIT_ASSERT_EQUAL(s2[4], '\0');
}

LOLUNIT_TEST(StringEqual)
{
String s1("abc");
String s2("abc");
String s3("ab");

LOLUNIT_ASSERT(s1 == s2);
LOLUNIT_ASSERT(!(s1 == s3));
}

LOLUNIT_TEST(StringDifferent)
{
String s1("abc");
String s2("ab");
String s3("abc");

LOLUNIT_ASSERT(s1 != s2);
LOLUNIT_ASSERT(!(s1 != s3));
}
};

} /* namespace lol */


Laden…
Abbrechen
Speichern