lists.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/lists/lists.cpp
  3. // Purpose: wxList unit test
  4. // Author: Vadim Zeitlin, Mattia Barbon
  5. // Created: 2004-12-08
  6. // Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/list.h"
  19. // --------------------------------------------------------------------------
  20. // test class
  21. // --------------------------------------------------------------------------
  22. class ListsTestCase : public CppUnit::TestCase
  23. {
  24. public:
  25. ListsTestCase() { }
  26. private:
  27. CPPUNIT_TEST_SUITE( ListsTestCase );
  28. CPPUNIT_TEST( wxListTest );
  29. CPPUNIT_TEST( wxStdListTest );
  30. CPPUNIT_TEST( wxListCtorTest );
  31. CPPUNIT_TEST_SUITE_END();
  32. void wxListTest();
  33. void wxStdListTest();
  34. void wxListCtorTest();
  35. DECLARE_NO_COPY_CLASS(ListsTestCase)
  36. };
  37. // register in the unnamed registry so that these tests are run by default
  38. CPPUNIT_TEST_SUITE_REGISTRATION( ListsTestCase );
  39. // also include in its own registry so that these tests can be run alone
  40. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListsTestCase, "ListsTestCase" );
  41. class Baz // Foo is already taken in the hash test
  42. {
  43. public:
  44. Baz(const wxString& name) : m_name(name) { ms_bars++; }
  45. Baz(const Baz& bar) : m_name(bar.m_name) { ms_bars++; }
  46. ~Baz() { ms_bars--; }
  47. static size_t GetNumber() { return ms_bars; }
  48. const wxChar *GetName() const { return m_name.c_str(); }
  49. private:
  50. wxString m_name;
  51. static size_t ms_bars;
  52. };
  53. size_t Baz::ms_bars = 0;
  54. #include "wx/list.h"
  55. WX_DECLARE_LIST(Baz, wxListBazs);
  56. #include "wx/listimpl.cpp"
  57. WX_DEFINE_LIST(wxListBazs)
  58. WX_DECLARE_LIST(int, wxListInt);
  59. WX_DEFINE_LIST(wxListInt)
  60. void ListsTestCase::wxListTest()
  61. {
  62. wxListInt list1;
  63. int dummy[5];
  64. size_t i;
  65. for ( i = 0; i < WXSIZEOF(dummy); ++i )
  66. list1.Append(dummy + i);
  67. CPPUNIT_ASSERT_EQUAL( WXSIZEOF(dummy), list1.GetCount() );
  68. CPPUNIT_ASSERT_EQUAL( dummy + 3, list1.Item(3)->GetData() );
  69. CPPUNIT_ASSERT( list1.Find(dummy + 4) );
  70. wxListInt::compatibility_iterator node;
  71. for ( i = 0, node = list1.GetFirst(); node; ++i, node = node->GetNext() )
  72. {
  73. CPPUNIT_ASSERT_EQUAL( dummy + i, node->GetData() );
  74. }
  75. CPPUNIT_ASSERT_EQUAL( i, list1.GetCount() );
  76. list1.Insert(dummy + 0);
  77. list1.Insert(1, dummy + 1);
  78. list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2);
  79. for ( i = 0, node = list1.GetFirst(); i < 3; ++i, node = node->GetNext() )
  80. {
  81. int* t = node->GetData();
  82. CPPUNIT_ASSERT_EQUAL( dummy + i, t );
  83. }
  84. }
  85. void ListsTestCase::wxStdListTest()
  86. {
  87. wxListInt list1;
  88. wxListInt::iterator it, en;
  89. wxListInt::reverse_iterator rit, ren;
  90. int i;
  91. for ( i = 0; i < 5; ++i )
  92. list1.push_back(i + &i);
  93. for ( it = list1.begin(), en = list1.end(), i = 0;
  94. it != en; ++it, ++i )
  95. {
  96. CPPUNIT_ASSERT( *it == i + &i );
  97. }
  98. for ( rit = list1.rbegin(), ren = list1.rend(), i = 4;
  99. rit != ren; ++rit, --i )
  100. {
  101. CPPUNIT_ASSERT( *rit == i + &i );
  102. }
  103. CPPUNIT_ASSERT( *list1.rbegin() == *--list1.end() &&
  104. *list1.begin() == *--list1.rend() );
  105. CPPUNIT_ASSERT( *list1.begin() == *--++list1.begin() &&
  106. *list1.rbegin() == *--++list1.rbegin() );
  107. CPPUNIT_ASSERT( list1.front() == &i && list1.back() == &i + 4 );
  108. list1.erase(list1.begin());
  109. list1.erase(--list1.end());
  110. for ( it = list1.begin(), en = list1.end(), i = 1;
  111. it != en; ++it, ++i )
  112. {
  113. CPPUNIT_ASSERT( *it == i + &i );
  114. }
  115. list1.clear();
  116. CPPUNIT_ASSERT( list1.empty() );
  117. it = list1.insert(list1.end(), (int *)1);
  118. CPPUNIT_ASSERT_EQUAL( (int *)1, *it );
  119. CPPUNIT_ASSERT( it == list1.begin() );
  120. CPPUNIT_ASSERT_EQUAL( (int *)1, list1.front() );
  121. it = list1.insert(list1.end(), (int *)2);
  122. CPPUNIT_ASSERT_EQUAL( (int *)2, *it );
  123. CPPUNIT_ASSERT( ++it == list1.end() );
  124. CPPUNIT_ASSERT_EQUAL( (int *)2, list1.back() );
  125. it = list1.begin();
  126. wxListInt::iterator it2 = list1.insert(++it, (int *)3);
  127. CPPUNIT_ASSERT_EQUAL( (int *)3, *it2 );
  128. it = list1.begin();
  129. it = list1.erase(++it, list1.end());
  130. CPPUNIT_ASSERT_EQUAL( 1, list1.size() );
  131. CPPUNIT_ASSERT( it == list1.end() );
  132. wxListInt list2;
  133. list2.push_back((int *)3);
  134. list2.push_back((int *)4);
  135. list1.insert(list1.begin(), list2.begin(), list2.end());
  136. CPPUNIT_ASSERT_EQUAL( 3, list1.size() );
  137. CPPUNIT_ASSERT_EQUAL( (int *)3, list1.front() );
  138. list1.insert(list1.end(), list2.begin(), list2.end());
  139. CPPUNIT_ASSERT_EQUAL( 5, list1.size() );
  140. CPPUNIT_ASSERT_EQUAL( (int *)4, list1.back() );
  141. }
  142. void ListsTestCase::wxListCtorTest()
  143. {
  144. {
  145. wxListBazs list1;
  146. list1.Append(new Baz(wxT("first")));
  147. list1.Append(new Baz(wxT("second")));
  148. CPPUNIT_ASSERT( list1.GetCount() == 2 );
  149. CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
  150. wxListBazs list2;
  151. list2 = list1;
  152. CPPUNIT_ASSERT( list1.GetCount() == 2 );
  153. CPPUNIT_ASSERT( list2.GetCount() == 2 );
  154. CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
  155. #if !wxUSE_STL
  156. list1.DeleteContents(true);
  157. #else
  158. WX_CLEAR_LIST(wxListBazs, list1);
  159. #endif
  160. }
  161. CPPUNIT_ASSERT( Baz::GetNumber() == 0 );
  162. }