2004-12-08 17:39:22 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/lists/lists.cpp
|
|
|
|
// Purpose: wxList unit test
|
|
|
|
// Author: Vadim Zeitlin, Mattia Barbon
|
|
|
|
// Created: 2004-12-08
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "wx/list.h"
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class ListsTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ListsTestCase() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( ListsTestCase );
|
|
|
|
CPPUNIT_TEST( wxListTest );
|
|
|
|
CPPUNIT_TEST( wxStdListTest );
|
|
|
|
CPPUNIT_TEST( wxListCtorTest );
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void wxListTest();
|
|
|
|
void wxStdListTest();
|
|
|
|
void wxListCtorTest();
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(ListsTestCase)
|
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( ListsTestCase );
|
|
|
|
|
|
|
|
// also include in it's own registry so that these tests can be run alone
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListsTestCase, "ListsTestCase" );
|
|
|
|
|
|
|
|
class Baz // Foo is already taken in the hash test
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Baz(const wxString& name) : m_name(name) { ms_bars++; }
|
|
|
|
Baz(const Baz& bar) : m_name(bar.m_name) { ms_bars++; }
|
|
|
|
~Baz() { ms_bars--; }
|
|
|
|
|
|
|
|
static size_t GetNumber() { return ms_bars; }
|
|
|
|
|
2007-06-10 13:42:41 -04:00
|
|
|
const wxChar *GetName() const { return m_name.c_str(); }
|
2004-12-08 17:39:22 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
wxString m_name;
|
|
|
|
|
|
|
|
static size_t ms_bars;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t Baz::ms_bars = 0;
|
|
|
|
|
|
|
|
#include "wx/list.h"
|
|
|
|
|
|
|
|
WX_DECLARE_LIST(Baz, wxListBazs);
|
|
|
|
#include "wx/listimpl.cpp"
|
2005-11-20 16:55:35 -05:00
|
|
|
WX_DEFINE_LIST(wxListBazs)
|
2004-12-08 17:39:22 -05:00
|
|
|
|
|
|
|
WX_DECLARE_LIST(int, wxListInt);
|
2005-11-20 16:55:35 -05:00
|
|
|
WX_DEFINE_LIST(wxListInt)
|
2004-12-08 17:39:22 -05:00
|
|
|
|
|
|
|
void ListsTestCase::wxListTest()
|
|
|
|
{
|
|
|
|
wxListInt list1;
|
|
|
|
int dummy[5];
|
2008-12-17 04:47:39 -05:00
|
|
|
size_t i;
|
2004-12-08 17:39:22 -05:00
|
|
|
|
2008-12-17 04:47:39 -05:00
|
|
|
for ( i = 0; i < WXSIZEOF(dummy); ++i )
|
2004-12-08 17:39:22 -05:00
|
|
|
list1.Append(dummy + i);
|
|
|
|
|
2008-12-17 04:47:39 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL( WXSIZEOF(dummy), list1.GetCount() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( dummy + 3, list1.Item(3)->GetData() );
|
2004-12-08 17:39:22 -05:00
|
|
|
CPPUNIT_ASSERT( list1.Find(dummy + 4) );
|
|
|
|
|
2008-12-17 04:47:39 -05:00
|
|
|
wxListInt::compatibility_iterator node;
|
|
|
|
for ( i = 0, node = list1.GetFirst(); node; ++i, node = node->GetNext() )
|
2004-12-08 17:39:22 -05:00
|
|
|
{
|
2008-12-17 04:47:39 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL( dummy + i, node->GetData() );
|
2004-12-08 17:39:22 -05:00
|
|
|
}
|
|
|
|
|
2008-12-17 04:47:39 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL( i, list1.GetCount() );
|
2004-12-08 17:39:22 -05:00
|
|
|
|
|
|
|
list1.Insert(dummy + 0);
|
|
|
|
list1.Insert(1, dummy + 1);
|
|
|
|
list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2);
|
|
|
|
|
2008-12-17 04:47:39 -05:00
|
|
|
for ( i = 0, node = list1.GetFirst(); i < 3; ++i, node = node->GetNext() )
|
2004-12-08 17:39:22 -05:00
|
|
|
{
|
|
|
|
int* t = node->GetData();
|
2008-12-17 04:47:39 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL( dummy + i, t );
|
2004-12-08 17:39:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListsTestCase::wxStdListTest()
|
|
|
|
{
|
|
|
|
wxListInt list1;
|
|
|
|
wxListInt::iterator it, en;
|
|
|
|
wxListInt::reverse_iterator rit, ren;
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < 5; ++i )
|
|
|
|
list1.push_back(i + &i);
|
|
|
|
|
|
|
|
for ( it = list1.begin(), en = list1.end(), i = 0;
|
|
|
|
it != en; ++it, ++i )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT( *it == i + &i );
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( rit = list1.rbegin(), ren = list1.rend(), i = 4;
|
|
|
|
rit != ren; ++rit, --i )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT( *rit == i + &i );
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( *list1.rbegin() == *--list1.end() &&
|
|
|
|
*list1.begin() == *--list1.rend() );
|
|
|
|
CPPUNIT_ASSERT( *list1.begin() == *--++list1.begin() &&
|
|
|
|
*list1.rbegin() == *--++list1.rbegin() );
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( list1.front() == &i && list1.back() == &i + 4 );
|
|
|
|
|
|
|
|
list1.erase(list1.begin());
|
|
|
|
list1.erase(--list1.end());
|
|
|
|
|
|
|
|
for ( it = list1.begin(), en = list1.end(), i = 1;
|
|
|
|
it != en; ++it, ++i )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT( *it == i + &i );
|
|
|
|
}
|
2008-10-14 05:04:52 -04:00
|
|
|
|
|
|
|
list1.clear();
|
|
|
|
CPPUNIT_ASSERT( list1.empty() );
|
|
|
|
|
2010-04-11 20:36:36 -04:00
|
|
|
it = list1.insert(list1.end(), (int *)1);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( (int *)1, *it );
|
|
|
|
CPPUNIT_ASSERT( it == list1.begin() );
|
2008-10-14 05:04:52 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( (int *)1, list1.front() );
|
2010-04-11 20:36:36 -04:00
|
|
|
|
|
|
|
it = list1.insert(list1.end(), (int *)2);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( (int *)2, *it );
|
|
|
|
CPPUNIT_ASSERT( ++it == list1.end() );
|
2008-10-14 05:04:52 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( (int *)2, list1.back() );
|
2009-02-07 18:10:56 -05:00
|
|
|
|
2010-04-11 20:36:36 -04:00
|
|
|
it = list1.begin();
|
|
|
|
wxListInt::iterator it2 = list1.insert(++it, (int *)3);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( (int *)3, *it2 );
|
|
|
|
|
2009-02-07 18:10:56 -05:00
|
|
|
it = list1.begin();
|
|
|
|
it = list1.erase(++it, list1.end());
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, list1.size() );
|
|
|
|
CPPUNIT_ASSERT( it == list1.end() );
|
2009-02-07 18:46:53 -05:00
|
|
|
|
|
|
|
wxListInt list2;
|
|
|
|
list2.push_back((int *)3);
|
|
|
|
list2.push_back((int *)4);
|
|
|
|
list1.insert(list1.begin(), list2.begin(), list2.end());
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 3, list1.size() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( (int *)3, list1.front() );
|
|
|
|
|
|
|
|
list1.insert(list1.end(), list2.begin(), list2.end());
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 5, list1.size() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( (int *)4, list1.back() );
|
2004-12-08 17:39:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListsTestCase::wxListCtorTest()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
wxListBazs list1;
|
2009-07-23 16:30:22 -04:00
|
|
|
list1.Append(new Baz(wxT("first")));
|
|
|
|
list1.Append(new Baz(wxT("second")));
|
2004-12-08 17:39:22 -05:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT( list1.GetCount() == 2 );
|
|
|
|
CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
|
|
|
|
|
|
|
|
wxListBazs list2;
|
|
|
|
list2 = list1;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( list1.GetCount() == 2 );
|
|
|
|
CPPUNIT_ASSERT( list2.GetCount() == 2 );
|
|
|
|
CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
|
|
|
|
|
|
|
|
#if !wxUSE_STL
|
|
|
|
list1.DeleteContents(true);
|
|
|
|
#else
|
|
|
|
WX_CLEAR_LIST(wxListBazs, list1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( Baz::GetNumber() == 0 );
|
|
|
|
}
|
|
|
|
|