2013-06-26 15:43:16 +04:00
/***
This file is part of systemd
Copyright 2013 Jan Janssen
systemd is free software ; you can redistribute it and / or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
( at your option ) any later version .
systemd is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
# include "list.h"
# include "util.h"
int main ( int argc , const char * argv [ ] ) {
size_t i ;
typedef struct list_item {
LIST_FIELDS ( struct list_item , item ) ;
} list_item ;
LIST_HEAD ( list_item , head ) ;
list_item items [ 4 ] ;
list_item * cursor ;
2013-10-14 08:10:14 +04:00
LIST_HEAD_INIT ( head ) ;
2013-06-26 15:43:16 +04:00
assert_se ( head = = NULL ) ;
for ( i = 0 ; i < ELEMENTSOF ( items ) ; i + + ) {
2013-10-14 08:10:14 +04:00
LIST_INIT ( item , & items [ i ] ) ;
2013-06-26 15:43:16 +04:00
assert_se ( LIST_JUST_US ( item , & items [ i ] ) ) ;
2013-10-14 08:10:14 +04:00
LIST_PREPEND ( item , head , & items [ i ] ) ;
2013-06-26 15:43:16 +04:00
}
2015-01-28 02:34:58 +03:00
i = 0 ;
LIST_FOREACH_OTHERS ( item , cursor , & items [ 2 ] ) {
i + + ;
assert_se ( cursor ! = & items [ 2 ] ) ;
}
assert_se ( i = = ELEMENTSOF ( items ) - 1 ) ;
2015-01-28 16:22:13 +03:00
i = 0 ;
LIST_FOREACH_OTHERS ( item , cursor , & items [ 0 ] ) {
i + + ;
assert_se ( cursor ! = & items [ 0 ] ) ;
}
assert_se ( i = = ELEMENTSOF ( items ) - 1 ) ;
i = 0 ;
LIST_FOREACH_OTHERS ( item , cursor , & items [ 3 ] ) {
i + + ;
assert_se ( cursor ! = & items [ 3 ] ) ;
}
assert_se ( i = = ELEMENTSOF ( items ) - 1 ) ;
2013-06-26 15:43:16 +04:00
assert_se ( ! LIST_JUST_US ( item , head ) ) ;
assert_se ( items [ 0 ] . item_next = = NULL ) ;
assert_se ( items [ 1 ] . item_next = = & items [ 0 ] ) ;
assert_se ( items [ 2 ] . item_next = = & items [ 1 ] ) ;
assert_se ( items [ 3 ] . item_next = = & items [ 2 ] ) ;
assert_se ( items [ 0 ] . item_prev = = & items [ 1 ] ) ;
assert_se ( items [ 1 ] . item_prev = = & items [ 2 ] ) ;
assert_se ( items [ 2 ] . item_prev = = & items [ 3 ] ) ;
assert_se ( items [ 3 ] . item_prev = = NULL ) ;
2013-10-14 08:10:14 +04:00
LIST_FIND_HEAD ( item , & items [ 0 ] , cursor ) ;
2013-06-26 15:43:16 +04:00
assert_se ( cursor = = & items [ 3 ] ) ;
2013-10-14 08:10:14 +04:00
LIST_FIND_TAIL ( item , & items [ 3 ] , cursor ) ;
2013-06-26 15:43:16 +04:00
assert_se ( cursor = = & items [ 0 ] ) ;
2013-10-14 08:10:14 +04:00
LIST_REMOVE ( item , head , & items [ 1 ] ) ;
2013-06-26 15:43:16 +04:00
assert_se ( LIST_JUST_US ( item , & items [ 1 ] ) ) ;
assert_se ( items [ 0 ] . item_next = = NULL ) ;
assert_se ( items [ 2 ] . item_next = = & items [ 0 ] ) ;
assert_se ( items [ 3 ] . item_next = = & items [ 2 ] ) ;
assert_se ( items [ 0 ] . item_prev = = & items [ 2 ] ) ;
assert_se ( items [ 2 ] . item_prev = = & items [ 3 ] ) ;
assert_se ( items [ 3 ] . item_prev = = NULL ) ;
2013-10-14 08:10:14 +04:00
LIST_INSERT_AFTER ( item , head , & items [ 3 ] , & items [ 1 ] ) ;
2013-06-26 15:43:16 +04:00
assert_se ( items [ 0 ] . item_next = = NULL ) ;
assert_se ( items [ 2 ] . item_next = = & items [ 0 ] ) ;
assert_se ( items [ 1 ] . item_next = = & items [ 2 ] ) ;
assert_se ( items [ 3 ] . item_next = = & items [ 1 ] ) ;
assert_se ( items [ 0 ] . item_prev = = & items [ 2 ] ) ;
assert_se ( items [ 2 ] . item_prev = = & items [ 1 ] ) ;
assert_se ( items [ 1 ] . item_prev = = & items [ 3 ] ) ;
assert_se ( items [ 3 ] . item_prev = = NULL ) ;
2013-10-14 08:10:14 +04:00
LIST_REMOVE ( item , head , & items [ 0 ] ) ;
2013-06-26 15:43:16 +04:00
assert_se ( LIST_JUST_US ( item , & items [ 0 ] ) ) ;
assert_se ( items [ 2 ] . item_next = = NULL ) ;
assert_se ( items [ 1 ] . item_next = = & items [ 2 ] ) ;
assert_se ( items [ 3 ] . item_next = = & items [ 1 ] ) ;
assert_se ( items [ 2 ] . item_prev = = & items [ 1 ] ) ;
assert_se ( items [ 1 ] . item_prev = = & items [ 3 ] ) ;
assert_se ( items [ 3 ] . item_prev = = NULL ) ;
2013-10-14 08:10:14 +04:00
LIST_REMOVE ( item , head , & items [ 1 ] ) ;
2013-06-26 15:43:16 +04:00
assert_se ( LIST_JUST_US ( item , & items [ 1 ] ) ) ;
assert_se ( items [ 2 ] . item_next = = NULL ) ;
assert_se ( items [ 3 ] . item_next = = & items [ 2 ] ) ;
assert_se ( items [ 2 ] . item_prev = = & items [ 3 ] ) ;
assert_se ( items [ 3 ] . item_prev = = NULL ) ;
2013-10-14 08:10:14 +04:00
LIST_REMOVE ( item , head , & items [ 2 ] ) ;
2013-06-26 15:43:16 +04:00
assert_se ( LIST_JUST_US ( item , & items [ 2 ] ) ) ;
assert_se ( LIST_JUST_US ( item , head ) ) ;
2013-10-14 08:10:14 +04:00
LIST_REMOVE ( item , head , & items [ 3 ] ) ;
2013-06-26 15:43:16 +04:00
assert_se ( LIST_JUST_US ( item , & items [ 3 ] ) ) ;
2015-01-09 21:34:01 +03:00
assert_se ( head = = NULL ) ;
for ( i = 0 ; i < ELEMENTSOF ( items ) ; i + + ) {
assert_se ( LIST_JUST_US ( item , & items [ i ] ) ) ;
LIST_APPEND ( item , head , & items [ i ] ) ;
}
assert_se ( ! LIST_JUST_US ( item , head ) ) ;
assert_se ( items [ 0 ] . item_next = = & items [ 1 ] ) ;
assert_se ( items [ 1 ] . item_next = = & items [ 2 ] ) ;
assert_se ( items [ 2 ] . item_next = = & items [ 3 ] ) ;
assert_se ( items [ 3 ] . item_next = = NULL ) ;
assert_se ( items [ 0 ] . item_prev = = NULL ) ;
assert_se ( items [ 1 ] . item_prev = = & items [ 0 ] ) ;
assert_se ( items [ 2 ] . item_prev = = & items [ 1 ] ) ;
assert_se ( items [ 3 ] . item_prev = = & items [ 2 ] ) ;
for ( i = 0 ; i < ELEMENTSOF ( items ) ; i + + )
2015-01-28 02:34:58 +03:00
LIST_REMOVE ( item , head , & items [ i ] ) ;
2015-01-09 21:34:01 +03:00
assert_se ( head = = NULL ) ;
2013-06-26 15:43:16 +04:00
return 0 ;
}