blob: 04f741cffe0e67d2619e69445a3ee2b9ce67d499 [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
51 llist_add_tail(&elem1.list, &elems);
52 llist_add_tail(&elem2.list, &elems);
53 llist_add_tail(&elem3.list, &elems);
54
55 llist_for_each(pos, &elems) {
56 count += 1;
57 printf(" %i -> %s\n", count, pos->entry()->str);
58 }
59 OSMO_ASSERT(count == 3);
60
61 count = 0;
62 llist_for_each_safe(pos, tmp, &elems) {
63 count += 1;
64 if (count == 2)
65 llist_del(pos);
66
67 printf(" %i -> %s\n", count, pos->entry()->str);
68 }
69 OSMO_ASSERT(count == 3);
70
71 count = 0;
72 llist_for_each(pos, &elems) {
73 count += 1;
74 OSMO_ASSERT(pos != &elem2.list);
75 printf(" %i -> %s\n", count, pos->entry()->str);
76 }
77 OSMO_ASSERT(count == 2);
78
79 printf("=== end %s ===\n", __func__);
80}
81
82int main(int argc, char **argv)
83{
84 test_linux_list();
85
86 return EXIT_SUCCESS;
87}