blob: 7c4975e82ec308f124b2b8f0f101e4723c1fd911 [file] [log] [blame]
Jacob Erlbeckdfef28d2015-05-11 14:13:47 +02001/*
2 * LListTest.cpp
3 *
4 * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include "cxx_linuxlist.h"
24
25extern "C" {
26#include <osmocom/core/application.h>
27#include <osmocom/core/talloc.h>
28#include <osmocom/core/utils.h>
29}
30
31#include <errno.h>
32
33
34struct TestElem {
35 const char *str;
36 LListHead<TestElem> list;
37
38 TestElem(const char *s) : str(s), list(this) {};
39};
40
41static void test_linux_list()
42{
43 LListHead<TestElem> elems, *pos, *tmp;
44 TestElem elem1("number one");
45 TestElem elem2("number two");
46 TestElem elem3("number three");
47 int count = 0;
48
49 printf("=== start %s ===\n", __func__);
50
Jacob Erlbeckaf387e22015-08-21 12:05:30 +020051 OSMO_ASSERT(llist_empty(&elems));
52
Jacob Erlbeckdfef28d2015-05-11 14:13:47 +020053 llist_add_tail(&elem1.list, &elems);
54 llist_add_tail(&elem2.list, &elems);
55 llist_add_tail(&elem3.list, &elems);
56
Jacob Erlbeckaf387e22015-08-21 12:05:30 +020057 OSMO_ASSERT(!llist_empty(&elems));
58
Jacob Erlbeckdfef28d2015-05-11 14:13:47 +020059 llist_for_each(pos, &elems) {
60 count += 1;
61 printf(" %i -> %s\n", count, pos->entry()->str);
62 }
63 OSMO_ASSERT(count == 3);
64
65 count = 0;
66 llist_for_each_safe(pos, tmp, &elems) {
67 count += 1;
68 if (count == 2)
69 llist_del(pos);
70
71 printf(" %i -> %s\n", count, pos->entry()->str);
72 }
73 OSMO_ASSERT(count == 3);
74
75 count = 0;
76 llist_for_each(pos, &elems) {
77 count += 1;
78 OSMO_ASSERT(pos != &elem2.list);
79 printf(" %i -> %s\n", count, pos->entry()->str);
80 }
81 OSMO_ASSERT(count == 2);
82
83 printf("=== end %s ===\n", __func__);
84}
85
86int main(int argc, char **argv)
87{
88 test_linux_list();
89
90 return EXIT_SUCCESS;
91}