From c8f80d27586a190927d491c618f48a4253246728 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 31 Mar 2008 15:22:10 +0000 Subject: [PATCH 1/1] throw away the now invalid items in wxSelectionStore::SetItemCount() (bug 1929823) and added a unit test for this class git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@52939 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/selstore.h | 4 +- src/generic/selstore.cpp | 16 ++++ tests/Makefile.in | 6 +- tests/makefile.bcc | 6 +- tests/makefile.gcc | 6 +- tests/makefile.vc | 6 +- tests/makefile.wat | 6 +- tests/misc/selstoretest.cpp | 134 +++++++++++++++++++++++++++++++++ tests/test.bkl | 1 + tests/test_test_gui.dsp | 4 + tests/test_vc7_test_gui.vcproj | 2 + tests/test_vc8_test_gui.vcproj | 3 + 12 files changed, 187 insertions(+), 7 deletions(-) create mode 100644 tests/misc/selstoretest.cpp diff --git a/include/wx/selstore.h b/include/wx/selstore.h index d987111259..b72d10f990 100644 --- a/include/wx/selstore.h +++ b/include/wx/selstore.h @@ -43,7 +43,7 @@ public: wxSelectionStore() : m_itemsSel(wxUIntCmp) { Init(); } // set the total number of items we handle - void SetItemCount(unsigned count) { m_count = count; } + void SetItemCount(unsigned count); // special case of SetItemCount(0) void Clear() { m_itemsSel.Clear(); m_count = 0; m_defaultState = false; } @@ -59,7 +59,7 @@ public: // returns true if the items selection really changed bool SelectItem(unsigned item, bool select = true); - // select the range of items + // select the range of items (inclusive) // // return true and fill the itemsChanged array with the indices of items // which have changed state if "few" of them did, otherwise return false diff --git a/src/generic/selstore.cpp b/src/generic/selstore.cpp index 2422bd8057..4333aeaeaa 100644 --- a/src/generic/selstore.cpp +++ b/src/generic/selstore.cpp @@ -214,3 +214,19 @@ void wxSelectionStore::OnItemDelete(unsigned item) } } +void wxSelectionStore::SetItemCount(unsigned count) +{ + // forget about all items whose indices are now invalid if the size + // decreased + if ( count < m_count ) + { + for ( size_t i = m_itemsSel.GetCount(); i > 0; i-- ) + { + if ( m_itemsSel[i - 1] >= count ) + m_itemsSel.RemoveAt(i - 1); + } + } + + // remember the new number of items + m_count = count; +} diff --git a/tests/Makefile.in b/tests/Makefile.in index 4827514091..950d9703af 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -116,7 +116,8 @@ TEST_GUI_OBJECTS = \ test_gui_point.o \ test_gui_config.o \ test_gui_clientsize.o \ - test_gui_textctrltest.o + test_gui_textctrltest.o \ + test_gui_selstoretest.o TEST_GUI_ODEP = $(___pch_testprec_test_gui_testprec_h_gch___depname) PRINTFBENCH_CXXFLAGS = $(__printfbench_PCH_INC) -D__WX$(TOOLKIT)__ \ $(__WXUNIV_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \ @@ -502,6 +503,9 @@ test_gui_clientsize.o: $(srcdir)/controls/clientsize.cpp $(TEST_GUI_ODEP) test_gui_textctrltest.o: $(srcdir)/controls/textctrltest.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/controls/textctrltest.cpp +test_gui_selstoretest.o: $(srcdir)/misc/selstoretest.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/misc/selstoretest.cpp + printfbench_printfbench.o: $(srcdir)/benchmarks/printfbench.cpp $(PRINTFBENCH_ODEP) $(CXXC) -c -o $@ $(PRINTFBENCH_CXXFLAGS) $(srcdir)/benchmarks/printfbench.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 0d5d0eca2c..ecf68202c7 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -103,7 +103,8 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_point.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_clientsize.obj \ - $(OBJS)\test_gui_textctrltest.obj + $(OBJS)\test_gui_textctrltest.obj \ + $(OBJS)\test_gui_selstoretest.obj PRINTFBENCH_CXXFLAGS = $(__RUNTIME_LIBS) -I$(BCCDIR)\include $(__DEBUGINFO) \ $(__OPTIMIZEFLAG) $(__THREADSFLAG_1) -D__WXMSW__ $(__WXUNIV_DEFINE_p) \ $(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \ @@ -537,6 +538,9 @@ $(OBJS)\test_gui_clientsize.obj: .\controls\clientsize.cpp $(OBJS)\test_gui_textctrltest.obj: .\controls\textctrltest.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\controls\textctrltest.cpp +$(OBJS)\test_gui_selstoretest.obj: .\misc\selstoretest.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\misc\selstoretest.cpp + $(OBJS)\printfbench_dummy.obj: .\dummy.cpp $(CXX) -q -c -P -o$@ $(PRINTFBENCH_CXXFLAGS) -H .\dummy.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index c5f6814f66..1678a05f72 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -96,7 +96,8 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_point.o \ $(OBJS)\test_gui_config.o \ $(OBJS)\test_gui_clientsize.o \ - $(OBJS)\test_gui_textctrltest.o + $(OBJS)\test_gui_textctrltest.o \ + $(OBJS)\test_gui_selstoretest.o PRINTFBENCH_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG) $(__THREADSFLAG) \ $(GCCFLAGS) -DHAVE_W32API_H -D__WXMSW__ $(__WXUNIV_DEFINE_p) \ $(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \ @@ -515,6 +516,9 @@ $(OBJS)\test_gui_clientsize.o: ./controls/clientsize.cpp $(OBJS)\test_gui_textctrltest.o: ./controls/textctrltest.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_gui_selstoretest.o: ./misc/selstoretest.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\printfbench_dummy.o: ./dummy.cpp $(CXX) -c -o $@ $(PRINTFBENCH_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index e7b7ebde77..6477522b04 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -99,7 +99,8 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_point.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_clientsize.obj \ - $(OBJS)\test_gui_textctrltest.obj + $(OBJS)\test_gui_textctrltest.obj \ + $(OBJS)\test_gui_selstoretest.obj PRINTFBENCH_CXXFLAGS = /M$(__RUNTIME_LIBS_38)$(__DEBUGRUNTIME) /DWIN32 \ $(__DEBUGINFO) /Fd$(OBJS)\printfbench.pdb $(____DEBUGRUNTIME) \ $(__OPTIMIZEFLAG) $(__NO_VC_CRTDBG_p) /D__WXMSW__ $(__WXUNIV_DEFINE_p) \ @@ -622,6 +623,9 @@ $(OBJS)\test_gui_clientsize.obj: .\controls\clientsize.cpp $(OBJS)\test_gui_textctrltest.obj: .\controls\textctrltest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\controls\textctrltest.cpp +$(OBJS)\test_gui_selstoretest.obj: .\misc\selstoretest.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\misc\selstoretest.cpp + $(OBJS)\printfbench_dummy.obj: .\dummy.cpp $(CXX) /c /nologo /TP /Fo$@ $(PRINTFBENCH_CXXFLAGS) /Yctestprec.h .\dummy.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index 4f1da2621c..875a094a6c 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -308,7 +308,8 @@ TEST_GUI_OBJECTS = & $(OBJS)\test_gui_point.obj & $(OBJS)\test_gui_config.obj & $(OBJS)\test_gui_clientsize.obj & - $(OBJS)\test_gui_textctrltest.obj + $(OBJS)\test_gui_textctrltest.obj & + $(OBJS)\test_gui_selstoretest.obj PRINTFBENCH_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG) $(__THREADSFLAG) & $(__RUNTIME_LIBS) -d__WXMSW__ $(__WXUNIV_DEFINE_p) $(__DEBUG_DEFINE_p) & $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) $(__THREAD_DEFINE_p) & @@ -568,6 +569,9 @@ $(OBJS)\test_gui_clientsize.obj : .AUTODEPEND .\controls\clientsize.cpp $(OBJS)\test_gui_textctrltest.obj : .AUTODEPEND .\controls\textctrltest.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< +$(OBJS)\test_gui_selstoretest.obj : .AUTODEPEND .\misc\selstoretest.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< + $(OBJS)\printfbench_dummy.obj : .AUTODEPEND .\dummy.cpp $(CXX) -bt=nt -zq -fo=$^@ $(PRINTFBENCH_CXXFLAGS) $< diff --git a/tests/misc/selstoretest.cpp b/tests/misc/selstoretest.cpp new file mode 100644 index 0000000000..881940923d --- /dev/null +++ b/tests/misc/selstoretest.cpp @@ -0,0 +1,134 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/misc/selstoretest.cpp +// Purpose: wxSelectionStore unit test +// Author: Vadim Zeitlin +// Created: 2008-03-31 +// RCS-ID: $Id$ +// Copyright: (c) 2008 Vadim Zeitlin +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#include "wx/selstore.h" + +// ---------------------------------------------------------------------------- +// test class +// ---------------------------------------------------------------------------- + +class SelStoreTestCase : public CppUnit::TestCase +{ +public: + SelStoreTestCase() { m_store = NULL; } + + virtual void setUp() + { + m_store = new wxSelectionStore; + m_store->SetItemCount(NUM_ITEMS); + } + + virtual void tearDown() + { + delete m_store; + m_store = NULL; + } + +private: + CPPUNIT_TEST_SUITE( SelStoreTestCase ); + CPPUNIT_TEST( SelectItem ); + CPPUNIT_TEST( SelectRange ); + CPPUNIT_TEST( SetItemCount ); + CPPUNIT_TEST( Clear ); + CPPUNIT_TEST_SUITE_END(); + + void SelectItem(); + void SelectRange(); + void SetItemCount(); + void Clear(); + + // NB: must be even + static const unsigned NUM_ITEMS; + + wxSelectionStore *m_store; + + DECLARE_NO_COPY_CLASS(SelStoreTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( SelStoreTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SelStoreTestCase, "SelStoreTestCase" ); + +const unsigned SelStoreTestCase::NUM_ITEMS = 10; // NB: must be even + +void SelStoreTestCase::SelectItem() +{ + m_store->SelectItem(0); + CPPUNIT_ASSERT_EQUAL( 1u, m_store->GetSelectedCount() ); + CPPUNIT_ASSERT( m_store->IsSelected(0) ); + + m_store->SelectItem(NUM_ITEMS - 1); + CPPUNIT_ASSERT_EQUAL( 2u, m_store->GetSelectedCount() ); + CPPUNIT_ASSERT( m_store->IsSelected(NUM_ITEMS - 1) ); + + m_store->SelectItem(0, false); + CPPUNIT_ASSERT_EQUAL( 1u, m_store->GetSelectedCount() ); + CPPUNIT_ASSERT( !m_store->IsSelected(0) ); +} + +void SelStoreTestCase::SelectRange() +{ + m_store->SelectRange(0, NUM_ITEMS/2); + CPPUNIT_ASSERT_EQUAL( NUM_ITEMS/2 + 1, m_store->GetSelectedCount() ); + CPPUNIT_ASSERT( m_store->IsSelected(0) ); + CPPUNIT_ASSERT( !m_store->IsSelected(NUM_ITEMS - 1) ); + + m_store->SelectRange(NUM_ITEMS/2, NUM_ITEMS - 1); + CPPUNIT_ASSERT_EQUAL( NUM_ITEMS, m_store->GetSelectedCount() ); + CPPUNIT_ASSERT( m_store->IsSelected(0) ); + CPPUNIT_ASSERT( m_store->IsSelected(NUM_ITEMS - 1) ); + + m_store->SelectRange(1, NUM_ITEMS - 2, false); + CPPUNIT_ASSERT_EQUAL( 2u, m_store->GetSelectedCount() ); + CPPUNIT_ASSERT( m_store->IsSelected(0) ); + CPPUNIT_ASSERT( !m_store->IsSelected(NUM_ITEMS/2) ); + CPPUNIT_ASSERT( m_store->IsSelected(NUM_ITEMS - 1) ); +} + +void SelStoreTestCase::SetItemCount() +{ + m_store->SelectRange(1, NUM_ITEMS - 2); + CPPUNIT_ASSERT_EQUAL( NUM_ITEMS - 2, m_store->GetSelectedCount() ); + + m_store->SetItemCount(NUM_ITEMS/2); + CPPUNIT_ASSERT_EQUAL( NUM_ITEMS/2 - 1, m_store->GetSelectedCount() ); + + + m_store->Clear(); + m_store->SetItemCount(NUM_ITEMS); + + + m_store->SelectItem(NUM_ITEMS/2 - 1); + m_store->SelectItem(NUM_ITEMS/2 + 1); + m_store->SetItemCount(NUM_ITEMS/2); + CPPUNIT_ASSERT_EQUAL( 1u, m_store->GetSelectedCount() ); +} + +void SelStoreTestCase::Clear() +{ + CPPUNIT_ASSERT_EQUAL( 0u, m_store->GetSelectedCount() ); + + m_store->SelectItem(0); + m_store->Clear(); + + CPPUNIT_ASSERT_EQUAL( 0u, m_store->GetSelectedCount() ); +} + diff --git a/tests/test.bkl b/tests/test.bkl index 6080b7b75a..b9acc86aff 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -96,6 +96,7 @@ config/config.cpp controls/clientsize.cpp controls/textctrltest.cpp + misc/selstoretest.cpp core base diff --git a/tests/test_test_gui.dsp b/tests/test_test_gui.dsp index e64be615d8..a7dc53936e 100644 --- a/tests/test_test_gui.dsp +++ b/tests/test_test_gui.dsp @@ -261,6 +261,10 @@ SOURCE=.\..\samples\sample.rc # End Source File # Begin Source File +SOURCE=.\misc\selstoretest.cpp +# End Source File +# Begin Source File + SOURCE=.\geometry\size.cpp # End Source File # Begin Source File diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index 73c760d662..48f2e35941 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -708,6 +708,8 @@ RelativePath=".\geometry\point.cpp"/> + + -- 2.45.2