Update of /cvsroot/objecthandler/log4cxx-0.9.7/tests/src/helpers
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25784/tests/src/helpers
Added Files:
Makefile.am boundedfifotestcase.cpp cyclicbuffertestcase.cpp
optionconvertertestcase.cpp
Log Message:
--- NEW FILE: cyclicbuffertestcase.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/logger.h>
#include <log4cxx/helpers/cyclicbuffer.h>
#include <log4cxx/spi/loggingevent.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::spi;
#define MAX 1000
class CyclicBufferTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(CyclicBufferTestCase);
CPPUNIT_TEST(test0);
CPPUNIT_TEST(test1);
CPPUNIT_TEST(testResize);
CPPUNIT_TEST_SUITE_END();
LoggerPtr logger;
std::vector<LoggingEventPtr> e;
public:
void setUp()
{
logger = Logger::getLogger(_T("x"));
e.reserve(1000);
for (int i = 0; i < MAX; i++)
{
e.push_back(
new LoggingEvent(_T(""), logger, Level::DEBUG, _T("e")));
}
}
void tearDown()
{
LogManager::shutdown();
}
void test0()
{
int size = 2;
CyclicBuffer cb(size);
CPPUNIT_ASSERT_EQUAL(size, cb.getMaxSize());
cb.add(e[0]);
CPPUNIT_ASSERT_EQUAL(1, cb.length());
CPPUNIT_ASSERT_EQUAL(e[0], cb.get());
CPPUNIT_ASSERT_EQUAL(0, cb.length());
CPPUNIT_ASSERT(cb.get() == 0);
CPPUNIT_ASSERT_EQUAL(0, cb.length());
CyclicBuffer cb2(size);
cb2.add(e[0]);
cb2.add(e[1]);
CPPUNIT_ASSERT_EQUAL(2, cb2.length());
CPPUNIT_ASSERT_EQUAL(e[0], cb2.get());
CPPUNIT_ASSERT_EQUAL(1, cb2.length());
CPPUNIT_ASSERT_EQUAL(e[1], cb2.get());
CPPUNIT_ASSERT_EQUAL(0, cb2.length());
CPPUNIT_ASSERT(cb2.get() == 0);
CPPUNIT_ASSERT_EQUAL(0, cb2.length());
}
void test1()
{
for (int bufSize = 1; bufSize <= 128; bufSize *= 2)
doTest1(bufSize);
}
void doTest1(int size)
{
//System.out.println("Doing test with size = "+size);
CyclicBuffer cb(size);
CPPUNIT_ASSERT_EQUAL(size, cb.getMaxSize());
int i;
for (i = -(size + 10); i < (size + 10); i++)
{
CPPUNIT_ASSERT(cb.get(i) == 0);
}
for (i = 0; i < MAX; i++)
{
cb.add(e[i]);
int limit = (i < (size - 1)) ? i : (size - 1);
//System.out.println("\nLimit is " + limit + ", i="+i);
for (int j = limit; j >= 0; j--)
{
//System.out.println("i= "+i+", j="+j);
CPPUNIT_ASSERT_EQUAL(e[i - (limit - j)], cb.get(j));
}
CPPUNIT_ASSERT(cb.get(-1) == 0);
CPPUNIT_ASSERT(cb.get(limit + 1) == 0);
}
}
void testResize()
{
for (int isize = 1; isize <= 128; isize *= 2)
{
doTestResize(isize, (isize / 2) + 1, (isize / 2) + 1);
doTestResize(isize, (isize / 2) + 1, isize + 10);
doTestResize(isize, isize + 10, (isize / 2) + 1);
doTestResize(isize, isize + 10, isize + 10);
}
}
void doTestResize(int initialSize, int numberOfAdds, int newSize)
{
//System.out.println("initialSize = "+initialSize+", numberOfAdds="
// +numberOfAdds+", newSize="+newSize);
CyclicBuffer cb(initialSize);
for (int i = 0; i < numberOfAdds; i++)
{
cb.add(e[i]);
}
cb.resize(newSize);
int offset = numberOfAdds - initialSize;
if (offset < 0)
{
offset = 0;
}
int len = (newSize < numberOfAdds) ? newSize : numberOfAdds;
len = (len < initialSize) ? len : initialSize;
//System.out.println("Len = "+len+", offset="+offset);
for (int j = 0; j < len; j++)
{
CPPUNIT_ASSERT_EQUAL(e[offset + j], cb.get(j));
}
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(CyclicBufferTestCase);
--- NEW FILE: boundedfifotestcase.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/logger.h>
#include <log4cxx/helpers/boundedfifo.h>
#include <log4cxx/spi/loggingevent.h>
#include <log4cxx/helpers/strictmath.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::spi;
#define MAX 1000
class BoundedFIFOTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(BoundedFIFOTestCase);
CPPUNIT_TEST(test1);
CPPUNIT_TEST(test2);
CPPUNIT_TEST(testResize1);
CPPUNIT_TEST(testResize2);
CPPUNIT_TEST(testResize3);
CPPUNIT_TEST_SUITE_END();
LoggerPtr logger;
std::vector<LoggingEventPtr> e;
public:
void setUp()
{
logger = Logger::getLogger(_T("x"));
e.reserve(1000);
for (int i = 0; i < MAX; i++)
{
e.push_back(
new LoggingEvent(_T(""), logger, Level::DEBUG, _T("e")));
}
}
void tearDown()
{
LogManager::shutdown();
}
void test1()
{
for (int size = 1; size <= 128; size *= 2)
{
BoundedFIFO bf(size);
CPPUNIT_ASSERT_EQUAL(size, bf.getMaxSize());
CPPUNIT_ASSERT(bf.get() == 0);
int i;
int j;
int k;
for (i = 1; i < (2 * size); i++)
{
for (j = 0; j < i; j++)
{
//tcout << _T("Putting ") << e[j] << std::endl;
bf.put(e[j]);
CPPUNIT_ASSERT_EQUAL((j < size) ? (j + 1) : size, bf.length());
}
int max = (size < j) ? size : j;
j--;
for (k = 0; k <= j; k++)
{
//tcout << _T("max=") << max << _T(", j=") << j
// << _T(", k="= << k << std::endl;
CPPUNIT_ASSERT_EQUAL(((max - k) > 0) ? (max - k) : 0, bf.length());
LoggingEventPtr r = bf.get();
//tcout << _t("Got ") << r << std::endl;
if (k >= size)
{
CPPUNIT_ASSERT(r == 0);
}
else
{
CPPUNIT_ASSERT(r == e[k]);
}
}
}
//tcout << _T("Passed size=") << size << std::endl;
}
}
void test2()
{
int size = 3;
BoundedFIFO bf(size);
bf.put(e[0]);
CPPUNIT_ASSERT_EQUAL(e[0], bf.get());
CPPUNIT_ASSERT(bf.get() == 0);
bf.put(e[1]);
CPPUNIT_ASSERT_EQUAL(1, bf.length());
bf.put(e[2]);
CPPUNIT_ASSERT_EQUAL(2, bf.length());
bf.put(e[3]);
CPPUNIT_ASSERT_EQUAL(3, bf.length());
CPPUNIT_ASSERT_EQUAL(e[1], bf.get());
CPPUNIT_ASSERT_EQUAL(2, bf.length());
CPPUNIT_ASSERT_EQUAL(e[2], bf.get());
CPPUNIT_ASSERT_EQUAL(1, bf.length());
CPPUNIT_ASSERT_EQUAL(e[3], bf.get());
CPPUNIT_ASSERT_EQUAL(0, bf.length());
CPPUNIT_ASSERT(bf.get() == 0);
CPPUNIT_ASSERT_EQUAL(0, bf.length());
}
void testResize1()
{
int size = 10;
for (int n = 1; n < (size * 2); n++)
{
for (int i = 0; i < (size * 2); i++)
{
BoundedFIFO bf(size);
for (int f = 0; f < i; f++)
{
bf.put(e[f]);
}
bf.resize(n);
int expectedSize = StrictMath::minimum(n, StrictMath::minimum(i, size));
CPPUNIT_ASSERT_EQUAL(expectedSize, bf.length());
for (int c = 0; c < expectedSize; c++)
{
CPPUNIT_ASSERT_EQUAL(e[c], bf.get());
}
}
}
}
void testResize2()
{
int size = 10;
for (int n = 1; n < (size * 2); n++)
{
for (int i = 0; i < (size * 2); i++)
{
for (int d = 0; d < StrictMath::minimum(i, size); d++)
{
BoundedFIFO bf(size);
for (int p = 0; p < i; p++)
{
bf.put(e[p]);
}
for (int g = 0; g < d; g++)
{
bf.get();
}
// x = the number of elems in
int x = bf.length();
bf.resize(n);
int expectedSize = StrictMath::minimum(n, x);
CPPUNIT_ASSERT_EQUAL(expectedSize, bf.length());
for (int c = 0; c < expectedSize; c++)
{
CPPUNIT_ASSERT_EQUAL(e[c + d], bf.get());
}
CPPUNIT_ASSERT(bf.get() == 0);
}
}
}
}
void testResize3()
{
int size = 10;
for (int n = 1; n < (size * 2); n++)
{
for (int i = 0; i < size; i++)
{
for (int d = 0; d < i; d++)
{
for (int r = 0; r < d; r++)
{
BoundedFIFO bf(size);
for (int p0 = 0; p0 < i; p0++)
bf.put(e[p0]);
for (int g = 0; g < d; g++)
bf.get();
for (int p1 = 0; p1 < r; p1++)
bf.put(e[i + p1]);
int x = bf.length();
bf.resize(n);
int expectedSize = StrictMath::minimum(n, x);
CPPUNIT_ASSERT_EQUAL(expectedSize, bf.length());
for (int c = 0; c < expectedSize; c++)
{
CPPUNIT_ASSERT_EQUAL(e[c + d], bf.get());
}
//CPPUNIT_ASSERT(bf.get() == 0);
}
}
}
}
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(BoundedFIFOTestCase);
--- NEW FILE: Makefile.am ---
EXTRA_DIST = *.cpp
if TESTS
noinst_LIBRARIES = libhelpers.a
INCLUDES = -I$(top_srcdir)/include
libhelpers_a_SOURCES = \
boundedfifotestcase.cpp\
cyclicbuffertestcase.cpp\
optionconvertertestcase.cpp
check: libhelpers.a
endif
--- NEW FILE: optionconvertertestcase.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <log4cxx/helpers/optionconverter.h>
#include <log4cxx/helpers/properties.h>
#include <log4cxx/helpers/system.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::spi;
#define MAX 1000
class OptionConverterTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(OptionConverterTestCase);
CPPUNIT_TEST(varSubstTest1);
CPPUNIT_TEST(varSubstTest2);
CPPUNIT_TEST(varSubstTest3);
CPPUNIT_TEST(varSubstTest4);
CPPUNIT_TEST(varSubstTest5);
CPPUNIT_TEST_SUITE_END();
Properties props;
Properties nullProperties;
public:
void setUp()
{
props.setProperty(_T("TOTO"), _T("wonderful"));
props.setProperty(_T("key1"), _T("value1"));
props.setProperty(_T("key2"), _T("value2"));
System::setProperties(props);
}
void tearDown()
{
}
void varSubstTest1()
{
String r;
r = OptionConverter::substVars(_T("hello world."), nullProperties);
CPPUNIT_ASSERT(r == _T("hello world."));
r = OptionConverter::substVars(_T("hello ${TOTO} world."), nullProperties);
CPPUNIT_ASSERT(r == _T("hello wonderful world."));
}
void varSubstTest2()
{
String r;
r = OptionConverter::substVars(_T("Test2 ${key1} mid ${key2} end."),
nullProperties);
CPPUNIT_ASSERT(r == _T("Test2 value1 mid value2 end."));
}
void varSubstTest3()
{
String r;
r = OptionConverter::substVars(
_T("Test3 ${unset} mid ${key1} end."), nullProperties);
CPPUNIT_ASSERT(r == _T("Test3 mid value1 end."));
}
void varSubstTest4()
{
String res;
String val = _T("Test4 ${incomplete ");
try
{
res = OptionConverter::substVars(val, nullProperties);
}
catch(IllegalArgumentException& e)
{
String witness = String(_T("\""))+val
+ _T("\" has no closing brace. Opening brace at position 6.");
String errorMsg = e.getMessage();
CPPUNIT_ASSERT(errorMsg == witness);
}
}
void varSubstTest5()
{
Properties props;
props.setProperty(_T("p1"), _T("x1"));
props.setProperty(_T("p2"), _T("${p1}"));
String res = OptionConverter::substVars(_T("${p2}"), props);
CPPUNIT_ASSERT(res == _T("x1"));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(OptionConverterTestCase);
|