--- /dev/null
+#include <set>
+#include <boost/test/auto_unit_test.hpp>
+#include "zypp/base/LogTools.h"
+#include "zypp/base/SetTracker.h"
+
+typedef std::set<int> SetType;
+namespace std
+{
+ inline ostream & operator<<( ostream & str, const SetType & obj )
+ { return zypp::dumpRangeLine( str, obj.begin(), obj.end() ); }
+}
+typedef zypp::base::SetTracker<SetType> Tracker;
+
+std::set<int> s;
+std::set<int> s1 ({1});
+std::set<int> s2 ({2});
+std::set<int> s3 ({3});
+std::set<int> s12 ({1,2});
+std::set<int> s13 ({1,3});
+std::set<int> s23 ({2,3});
+std::set<int> s123 ({1,2,3});
+
+
+BOOST_AUTO_TEST_CASE(basic)
+{
+ Tracker t;
+ BOOST_CHECK_EQUAL( t.current(), s );
+ BOOST_CHECK_EQUAL( t.added(), s );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.add( 1 ) );
+ BOOST_CHECK_EQUAL( t.current(), s1 );
+ BOOST_CHECK_EQUAL( t.added(), s1 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( !t.add( 1 ) );
+ BOOST_CHECK_EQUAL( t.current(), s1 );
+ BOOST_CHECK_EQUAL( t.added(), s1 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.add( 2 ) );
+ BOOST_CHECK_EQUAL( t.current(), s12 );
+ BOOST_CHECK_EQUAL( t.added(), s12 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.remove( 1 ) );
+ BOOST_CHECK_EQUAL( t.current(), s2 );
+ BOOST_CHECK_EQUAL( t.added(), s2 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.set( s3 ) );
+ BOOST_CHECK_EQUAL( t.current(), s3 );
+ BOOST_CHECK_EQUAL( t.added(), s3 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.add( 2 ) );
+ BOOST_CHECK_EQUAL( t.current(), s23 );
+ BOOST_CHECK_EQUAL( t.added(), s23 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.remove( 2 ) );
+ BOOST_CHECK_EQUAL( t.current(), s3 );
+ BOOST_CHECK_EQUAL( t.added(), s3 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( ! t.remove( 2 ) );
+ BOOST_CHECK_EQUAL( t.current(), s3 );
+ BOOST_CHECK_EQUAL( t.added(), s3 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.set( s ) );
+ BOOST_CHECK_EQUAL( t.current(), s );
+ BOOST_CHECK_EQUAL( t.added(), s );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ //----------------------------------------------------------------------
+
+ BOOST_CHECK( t.setInitial( s2 ) );
+ BOOST_CHECK_EQUAL( t.current(), s2 );
+ BOOST_CHECK_EQUAL( t.added(), s );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.set( s13 ) );
+ BOOST_CHECK_EQUAL( t.current(), s13 );
+ BOOST_CHECK_EQUAL( t.added(), s13 );
+ BOOST_CHECK_EQUAL( t.removed(), s2 );
+
+ BOOST_CHECK( t.set( s123 ) );
+ BOOST_CHECK_EQUAL( t.current(), s123 );
+ BOOST_CHECK_EQUAL( t.added(), s13 );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+ BOOST_CHECK( t.set( s ) );
+ BOOST_CHECK_EQUAL( t.current(), s );
+ BOOST_CHECK_EQUAL( t.added(), s );
+ BOOST_CHECK_EQUAL( t.removed(), s2 );
+
+ BOOST_CHECK( t.set( s2 ) );
+ BOOST_CHECK_EQUAL( t.current(), s2 );
+ BOOST_CHECK_EQUAL( t.added(), s );
+ BOOST_CHECK_EQUAL( t.removed(), s );
+
+}
bool changed = ( new_r != _current );
if ( changed )
{
- setInitial(); // clear added/removed
- if ( new_r.empty() )
- {
- _removed.swap( _current );
- }
- else if ( _current.empty() )
- {
- _added.swap( _current );
- }
- else
- {
- setDifference( new_r, _current, _added );
- setDifference( _current, new_r, _removed );
- _current = std::move(new_r);
- }
+ // build the initial (cur-add+rem) set in _current
+ setDifference( _current, _added, _removed );
+ _current.swap( _removed );
+ _added.clear();
+ _removed.clear();
+
+ const set_type & initial( _current );
+ setDifference( initial, new_r, _removed );
+ setDifference( new_r, initial, _added );
+ _current.swap( new_r );
}
return changed;
}
bool done = _current.insert( val_r ).second;
if ( done )
{
- _added.insert( val_r );
- _removed.erase( val_r );
+ if ( ! _removed.erase( val_r ) )
+ _added.insert( val_r );
}
return done;
}
bool done = _current.erase( val_r );
if ( done )
{
- _added.erase( val_r );
- _removed.insert( val_r );
+ if ( ! _added.erase( val_r ) )
+ _removed.insert( val_r );
}
return done;
}
//@}
private:
+
static bool find( const set_type & set_r, const key_type & key_r )
{ return set_r.find( key_r ) != set_r.end(); }