2017-11-04 Thomas Koenig <tkoenig@gcc.gnu.org>
[official-gcc.git] / gcc / typed-splay-tree.c
blob0f1536fa1bac49352b0e796a8036e25ff7c9affd
1 /* Selftests for typed-splay-tree.h.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "typed-splay-tree.h"
24 #include "selftest.h"
26 #if CHECKING_P
28 namespace selftest {
30 /* Callback for use by test_str_to_int. */
32 static int
33 append_cb (const char *, int value, void *user_data)
35 auto_vec <int> *vec = (auto_vec <int> *)user_data;
36 vec->safe_push (value);
37 return 0;
40 /* Test of typed_splay_tree <const char *, int>. */
42 static void
43 test_str_to_int ()
45 typed_splay_tree <const char *, int> t (strcmp, NULL, NULL);
47 t.insert ("a", 1);
48 t.insert ("b", 2);
49 t.insert ("c", 3);
51 ASSERT_EQ (1, t.lookup ("a"));
52 ASSERT_EQ (2, t.lookup ("b"));
53 ASSERT_EQ (3, t.lookup ("c"));
55 ASSERT_EQ (2, t.predecessor ("c"));
56 ASSERT_EQ (3, t.successor ("b"));
57 ASSERT_EQ (1, t.min ());
58 ASSERT_EQ (3, t.max ());
60 /* Test foreach by appending to a vec, and verifying the vec. */
61 auto_vec <int> v;
62 t.foreach (append_cb, &v);
63 ASSERT_EQ (3, v.length ());
64 ASSERT_EQ (1, v[0]);
65 ASSERT_EQ (2, v[1]);
66 ASSERT_EQ (3, v[2]);
69 /* Run all of the selftests within this file. */
71 void
72 typed_splay_tree_c_tests ()
74 test_str_to_int ();
77 } // namespace selftest
79 #endif /* #if CHECKING_P */