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