]> git.saurik.com Git - wxWidgets.git/commitdiff
throw away the now invalid items in wxSelectionStore::SetItemCount() (bug 1929823...
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 31 Mar 2008 15:22:10 +0000 (15:22 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 31 Mar 2008 15:22:10 +0000 (15:22 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@52939 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

12 files changed:
include/wx/selstore.h
src/generic/selstore.cpp
tests/Makefile.in
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/misc/selstoretest.cpp [new file with mode: 0644]
tests/test.bkl
tests/test_test_gui.dsp
tests/test_vc7_test_gui.vcproj
tests/test_vc8_test_gui.vcproj

index d987111259a1f6af8c7b5cf120eb6ec6e934e4ea..b72d10f9905c65a79140f42ec5144f074f24d942 100644 (file)
@@ -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
index 2422bd8057f3388fefca039f2fb17ca3d55fdf79..4333aeaeaac99d1d5ec53c96d34f21725aa93c44 100644 (file)
@@ -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;
+}
index 48275140914f5b9489762e10c7af73669a65880a..950d9703af34ed610013a1f4ec8660fbce125765 100644 (file)
@@ -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
 
index 0d5d0eca2c0d6b9438562cba6f88074414318d1c..ecf68202c7df1173ff308fb1ddf9aa0d75ca29f9 100644 (file)
@@ -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
 
index c5f6814f665390a18746ea6161ac95aaf0a8d83d..1678a05f72a4d60409acc36d6ec6359fc6e77113 100644 (file)
@@ -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) $<
 
index e7b7ebde77ca51beec3b1b2b5946d58708e2a5a0..6477522b046da3dd5cba4a27197af2f4fb307215 100644 (file)
@@ -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
 
index 4f1da2621ccb08b38105b3aa33688c4a0c152a92..875a094a6c0613eab7950c6d3f5f457d41359c8e 100644 (file)
@@ -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 (file)
index 0000000..8819409
--- /dev/null
@@ -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() );
+}
+
index 6080b7b75acebcbaa5b7f91d845522629b0e0a49..b9acc86aff25f3673924e0b34c612140c14c62a7 100644 (file)
@@ -96,6 +96,7 @@
             config/config.cpp
             controls/clientsize.cpp
             controls/textctrltest.cpp
+            misc/selstoretest.cpp
         </sources>
         <wx-lib>core</wx-lib>
         <wx-lib>base</wx-lib>
index e64be615d8d2acef5f8fb20257c77de323b2c45f..a7dc53936ea4ce39d6ef5953f84623b4219fc05a 100644 (file)
@@ -261,6 +261,10 @@ SOURCE=.\..\samples\sample.rc
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\misc\selstoretest.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\geometry\size.cpp\r
 # End Source File\r
 # Begin Source File\r
index 73c760d662d8371a3a53e84733d8a49716b7fe0b..48f2e3594108762a01a12f7f88a64627b2fe8bc2 100644 (file)
                                RelativePath=".\geometry\point.cpp"/>\r
                        <File\r
                                RelativePath=".\geometry\rect.cpp"/>\r
+                       <File\r
+                               RelativePath=".\misc\selstoretest.cpp"/>\r
                        <File\r
                                RelativePath=".\geometry\size.cpp"/>\r
                        <File\r
index 3501c978c652cb5aab41bd0539f40f3cf47d5035..7e2bf795753d3b4d3393f930dc4c6f7a07b36209 100644 (file)
                        <File\r
                                RelativePath=".\geometry\rect.cpp"\r
                        />\r
+                       <File\r
+                               RelativePath=".\misc\selstoretest.cpp"\r
+                       />\r
                        <File\r
                                RelativePath=".\geometry\size.cpp"\r
                        />\r