1 /*
2   Copyright (C) 2009 Facundo Domínguez
3 
4   This file is part of Spacejunk.
5 
6   Spacejunk is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   Foobar is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "testdeleteiterator.h"
21 #include "eventlistenerlist.h"
22 #include <assert.h>
23 
24 class EventListener {
25  public:
26   int id;
EventListener(int id)27   EventListener(int id) : id(id) {}
operator int()28   operator int () { return id; }
29 };
30 
31 typedef EventListenerList<EventListener> EventList;
32 
mod(int k,int shift)33 int mod(int k,int shift){
34   return (k + shift) % 3;
35 };
36 
DeleteIteratorTest()37 void DeleteIteratorTest(){
38   EventList l;
39   for(int k=0;k<3;k++) {
40     l.addListener(EventListener(0));
41     l.addListener(EventListener(1));
42     l.addListener(EventListener(2));
43     for(EventList::iterator i=l.begin();i!=l.end();i++){
44       if (*i==mod(k,2))
45         l.deleteListener(*i);
46       for(EventList::iterator i=l.begin();i!=l.end();i++){
47         if (*i==mod(k,1))
48           l.deleteListener(*i);
49       }
50     }
51     int c=0;
52     for(EventList::iterator i=l.begin();i!=l.end();i++){
53       c++;
54       assert(*i==mod(k,0));
55     }
56     assert(c==1);
57     l.deleteListener(EventListener(mod(k,0)));
58     assert(l.begin()==l.end());
59     assert(!l.dic.isIterating());
60     assert(l.listeners.size()==0);
61   }
62 };
63