test-list.c revision 3ad85e3775d018f32098a2957ce25e1f53440da1
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen/***
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen This file is part of systemd
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen Copyright 2013 Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen systemd is free software; you can redistribute it and/or modify it
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen under the terms of the GNU Lesser General Public License as published by
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen the Free Software Foundation; either version 2.1 of the License, or
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen (at your option) any later version.
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen systemd is distributed in the hope that it will be useful, but
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen WITHOUT ANY WARRANTY; without even the implied warranty of
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen Lesser General Public License for more details.
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen You should have received a copy of the GNU Lesser General Public License
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen along with systemd; If not, see <http://www.gnu.org/licenses/>.
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen***/
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen#include "list.h"
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen#include "util.h"
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssenint main(int argc, const char *argv[]) {
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen size_t i;
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen typedef struct list_item {
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen LIST_FIELDS(struct list_item, item);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen } list_item;
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen LIST_HEAD(list_item, head);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen list_item items[4];
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen list_item *cursor;
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_HEAD_INIT(head);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(head == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen for (i = 0; i < ELEMENTSOF(items); i++) {
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_INIT(item, &items[i]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(LIST_JUST_US(item, &items[i]));
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_PREPEND(item, head, &items[i]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen }
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
7663df377016cf7b95001aec893006647175ae4aLennart Poettering i = 0;
7663df377016cf7b95001aec893006647175ae4aLennart Poettering LIST_FOREACH_OTHERS(item, cursor, &items[2]) {
7663df377016cf7b95001aec893006647175ae4aLennart Poettering i++;
7663df377016cf7b95001aec893006647175ae4aLennart Poettering assert_se(cursor != &items[2]);
7663df377016cf7b95001aec893006647175ae4aLennart Poettering }
7663df377016cf7b95001aec893006647175ae4aLennart Poettering assert_se(i == ELEMENTSOF(items)-1);
7663df377016cf7b95001aec893006647175ae4aLennart Poettering
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering i = 0;
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering LIST_FOREACH_OTHERS(item, cursor, &items[0]) {
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering i++;
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering assert_se(cursor != &items[0]);
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering }
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering assert_se(i == ELEMENTSOF(items)-1);
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering i = 0;
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering LIST_FOREACH_OTHERS(item, cursor, &items[3]) {
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering i++;
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering assert_se(cursor != &items[3]);
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering }
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering assert_se(i == ELEMENTSOF(items)-1);
3ad85e3775d018f32098a2957ce25e1f53440da1Lennart Poettering
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(!LIST_JUST_US(item, head));
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[0].item_next == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[1].item_next == &items[0]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_next == &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_next == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[0].item_prev == &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[1].item_prev == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_prev == &items[3]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_prev == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_FIND_HEAD(item, &items[0], cursor);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(cursor == &items[3]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_FIND_TAIL(item, &items[3], cursor);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(cursor == &items[0]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_REMOVE(item, head, &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(LIST_JUST_US(item, &items[1]));
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[0].item_next == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_next == &items[0]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_next == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[0].item_prev == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_prev == &items[3]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_prev == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_INSERT_AFTER(item, head, &items[3], &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[0].item_next == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_next == &items[0]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[1].item_next == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_next == &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[0].item_prev == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_prev == &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[1].item_prev == &items[3]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_prev == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_REMOVE(item, head, &items[0]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(LIST_JUST_US(item, &items[0]));
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_next == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[1].item_next == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_next == &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_prev == &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[1].item_prev == &items[3]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_prev == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_REMOVE(item, head, &items[1]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(LIST_JUST_US(item, &items[1]));
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_next == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_next == &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[2].item_prev == &items[3]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(items[3].item_prev == NULL);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_REMOVE(item, head, &items[2]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(LIST_JUST_US(item, &items[2]));
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(LIST_JUST_US(item, head));
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_REMOVE(item, head, &items[3]);
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen assert_se(LIST_JUST_US(item, &items[3]));
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(head == NULL);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek for (i = 0; i < ELEMENTSOF(items); i++) {
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(LIST_JUST_US(item, &items[i]));
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek LIST_APPEND(item, head, &items[i]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek }
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(!LIST_JUST_US(item, head));
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[0].item_next == &items[1]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[1].item_next == &items[2]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[2].item_next == &items[3]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[3].item_next == NULL);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[0].item_prev == NULL);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[1].item_prev == &items[0]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[2].item_prev == &items[1]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(items[3].item_prev == &items[2]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek for (i = 0; i < ELEMENTSOF(items); i++)
7663df377016cf7b95001aec893006647175ae4aLennart Poettering LIST_REMOVE(item, head, &items[i]);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek assert_se(head == NULL);
502f1733e320b1339beafa949a41db3027c46ec6Zbigniew Jędrzejewski-Szmek
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen return 0;
3b18ae6866043ac4d96ee9bcada1a42469c80129Jan Janssen}