gcc/gcc/fibonacci_heap.cc
David Malcolm d5148d4faa Update per-file selftest and finalization hooks for .c to .cc renaming
This is mostly a mechanical change, apart from:
- fix the name of attribute_c_tests to match its filename (attribs.cc)
- fix the name of opt_proposer_c to match its filename (opt-suggestions.cc)
- delete a bogus "modref_c_tests" decl from ipa-modref-tree.h that's been
  present since the initial commit of that file
  (d119f34c95)

gcc/ChangeLog:
	* attribs.cc (attribute_c_tests): Rename to...
	(attribs_cc_tests): ...this.
	* bitmap.cc (bitmap_c_tests): Rename to...
	(bitmap_cc_tests): ...this.
	* cgraph.cc (cgraph_c_finalize): Rename to...
	(cgraph_cc_finalize): ...this.
	(cgraph_c_tests): Rename to...
	(cgraph_cc_tests): ...this.
	* cgraph.h (cgraph_c_finalize): Rename to...
	(cgraph_cc_finalize): ...this.
	(cgraphunit_c_finalize): Rename to...
	(cgraphunit_cc_finalize): ...this.
	* cgraphunit.cc (cgraphunit_c_finalize): Rename to...
	(cgraphunit_cc_finalize): ...this.
	* convert.cc (convert_c_tests): Rename to...
	(convert_cc_tests): ...this.
	* dbgcnt.cc (dbgcnt_c_tests): Rename to...
	(dbgcnt_cc_tests): ...this.
	* diagnostic-show-locus.cc (diagnostic_show_locus_c_tests): Rename to...
	(diagnostic_show_locus_cc_tests): ...this.
	* diagnostic.cc (diagnostic_c_tests): Rename to...
	(diagnostic_cc_tests): ...this.
	* dumpfile.cc (dumpfile_c_tests): Rename to...
	(dumpfile_cc_tests): ...this.
	* dwarf2out.cc (dwarf2out_c_finalize): Rename to...
	(dwarf2out_cc_finalize): ...this.
	* dwarf2out.h (dwarf2out_c_finalize): Rename to...
	(dwarf2out_cc_finalize): ...this.
	* edit-context.cc (edit_context_c_tests): Rename to...
	(edit_context_cc_tests): ...this.
	* et-forest.cc (et_forest_c_tests): Rename to...
	(et_forest_cc_tests): ...this.
	* fibonacci_heap.cc (fibonacci_heap_c_tests): Rename to...
	(fibonacci_heap_cc_tests): ...this.
	* fold-const.cc (fold_const_c_tests): Rename to...
	(fold_const_cc_tests): ...this.
	* function-tests.cc (function_tests_c_tests): Rename to...
	(function_tests_cc_tests): ...this.
	* gcse.cc (gcse_c_finalize): Rename to...
	(gcse_cc_finalize): ...this.
	* gcse.h (gcse_c_finalize): Rename to...
	(gcse_cc_finalize): ...this.
	* ggc-tests.cc (ggc_tests_c_tests): Rename to...
	(ggc_tests_cc_tests): ...this.
	* gimple-ssa-store-merging.cc (store_merging_c_tests): Rename to...
	(store_merging_cc_tests): ...this.
	* gimple.cc (gimple_c_tests): Rename to...
	(gimple_cc_tests): ...this.
	* hash-map-tests.cc (hash_map_tests_c_tests): Rename to...
	(hash_map_tests_cc_tests): ...this.
	* hash-set-tests.cc (hash_set_tests_c_tests): Rename to...
	(hash_set_tests_cc_tests): ...this.
	* input.cc (input_c_tests): Rename to...
	(input_cc_tests): ...this.
	* ipa-cp.cc (ipa_cp_c_finalize): Rename to...
	(ipa_cp_cc_finalize): ...this.
	* ipa-fnsummary.cc (ipa_fnsummary_c_finalize): Rename to...
	(ipa_fnsummary_cc_finalize): ...this.
	* ipa-fnsummary.h (ipa_fnsummary_c_finalize): Rename to...
	(ipa_fnsummary_cc_finalize): ...this.
	* ipa-modref-tree.cc (ipa_modref_tree_c_tests): Rename to...
	(ipa_modref_tree_cc_tests): ...this.
	* ipa-modref-tree.h (modref_c_tests): Delete bogus decl.
	* ipa-modref.cc (ipa_modref_c_finalize): Rename to...
	(ipa_modref_cc_finalize): ...this.
	* ipa-modref.h (ipa_modref_c_finalize): Rename to...
	(ipa_modref_cc_finalize): ...this.
	* ipa-prop.h (ipa_cp_c_finalize): Rename to...
	(ipa_cp_cc_finalize): ...this.
	* ipa-reference.cc (ipa_reference_c_finalize): Rename to...
	(ipa_reference_cc_finalize): ...this.
	* ipa-reference.h (ipa_reference_c_finalize): Rename to...
	(ipa_reference_cc_finalize): ...this.
	* ira-costs.cc (ira_costs_c_finalize): Rename to...
	(ira_costs_cc_finalize): ...this.
	* ira.h (ira_costs_c_finalize): Rename to...
	(ira_costs_cc_finalize): ...this.
	* opt-suggestions.cc (opt_proposer_c_tests): Rename to...
	(opt_suggestions_cc_tests): ...this.
	* opts.cc (opts_c_tests): Rename to...
	(opts_cc_tests): ...this.
	* predict.cc (predict_c_tests): Rename to...
	(predict_cc_tests): ...this.
	* pretty-print.cc (pretty_print_c_tests): Rename to...
	(pretty_print_cc_tests): ...this.
	* read-rtl-function.cc (read_rtl_function_c_tests): Rename to...
	(read_rtl_function_cc_tests): ...this.
	* rtl-tests.cc (rtl_tests_c_tests): Rename to...
	(rtl_tests_cc_tests): ...this.
	* sbitmap.cc (sbitmap_c_tests): Rename to...
	(sbitmap_cc_tests): ...this.
	* selftest-run-tests.cc (selftest::run_tests): Update calls for
	_c_ to _cc_ function renamings; fix names of attribs and
	opt-suggestions tests.
	* selftest.cc (selftest_c_tests): Rename to...
	(selftest_cc_tests): ...this.
	* selftest.h (attribute_c_tests): Rename to...
	(attribs_cc_tests): ...this.
	(bitmap_c_tests): Rename to...
	(bitmap_cc_tests): ...this.
	(cgraph_c_tests): Rename to...
	(cgraph_cc_tests): ...this.
	(convert_c_tests): Rename to...
	(convert_cc_tests): ...this.
	(diagnostic_c_tests): Rename to...
	(diagnostic_cc_tests): ...this.
	(diagnostic_show_locus_c_tests): Rename to...
	(diagnostic_show_locus_cc_tests): ...this.
	(dumpfile_c_tests): Rename to...
	(dumpfile_cc_tests): ...this.
	(edit_context_c_tests): Rename to...
	(edit_context_cc_tests): ...this.
	(et_forest_c_tests): Rename to...
	(et_forest_cc_tests): ...this.
	(fibonacci_heap_c_tests): Rename to...
	(fibonacci_heap_cc_tests): ...this.
	(fold_const_c_tests): Rename to...
	(fold_const_cc_tests): ...this.
	(function_tests_c_tests): Rename to...
	(function_tests_cc_tests): ...this.
	(ggc_tests_c_tests): Rename to...
	(ggc_tests_cc_tests): ...this.
	(gimple_c_tests): Rename to...
	(gimple_cc_tests): ...this.
	(hash_map_tests_c_tests): Rename to...
	(hash_map_tests_cc_tests): ...this.
	(hash_set_tests_c_tests): Rename to...
	(hash_set_tests_cc_tests): ...this.
	(input_c_tests): Rename to...
	(input_cc_tests): ...this.
	(opts_c_tests): Rename to...
	(opts_cc_tests): ...this.
	(predict_c_tests): Rename to...
	(predict_cc_tests): ...this.
	(pretty_print_c_tests): Rename to...
	(pretty_print_cc_tests): ...this.
	(read_rtl_function_c_tests): Rename to...
	(read_rtl_function_cc_tests): ...this.
	(rtl_tests_c_tests): Rename to...
	(rtl_tests_cc_tests): ...this.
	(sbitmap_c_tests): Rename to...
	(sbitmap_cc_tests): ...this.
	(selftest_c_tests): Rename to...
	(selftest_cc_tests): ...this.
	(simplify_rtx_c_tests): Rename to...
	(simplify_rtx_cc_tests): ...this.
	(spellcheck_c_tests): Rename to...
	(spellcheck_cc_tests): ...this.
	(spellcheck_tree_c_tests): Rename to...
	(spellcheck_tree_cc_tests): ...this.
	(sreal_c_tests): Rename to...
	(sreal_cc_tests): ...this.
	(store_merging_c_tests): Rename to...
	(store_merging_cc_tests): ...this.
	(tree_c_tests): Rename to...
	(tree_cc_tests): ...this.
	(tree_cfg_c_tests): Rename to...
	(tree_cfg_cc_tests): ...this.
	(typed_splay_tree_c_tests): Rename to...
	(typed_splay_tree_cc_tests): ...this.
	(vec_c_tests): Rename to...
	(vec_cc_tests): ...this.
	(vec_perm_indices_c_tests): Rename to...
	(vec_perm_indices_cc_tests): ..this.
	(opt_proposer_c_tests): Rename to...
	(opt_suggestions_cc_tests): ...this.
	(dbgcnt_c_tests): Rename to...
	(dbgcnt_cc_tests): ...this.
	(ipa_modref_tree_c_tests): Rename to...
	(ipa_modref_tree_cc_tests): ...this.
	* simplify-rtx.cc (simplify_rtx_c_tests): Rename to...
	(simplify_rtx_cc_tests): ...this.
	* spellcheck-tree.cc (spellcheck_tree_c_tests): Rename to...
	(spellcheck_tree_cc_tests): ...this.
	* spellcheck.cc (spellcheck_c_tests): Rename to...
	(spellcheck_cc_tests): ...this.
	* sreal.cc (sreal_c_tests): Rename to...
	(sreal_cc_tests): ...this.
	* toplev.cc (toplev::finalize): Update calls for _c_ to _cc_
	function renamings.
	* tree-cfg.cc (tree_cfg_c_tests): Rename to...
	(tree_cfg_cc_tests): ...this.
	* tree.cc (tree_c_tests): Rename to...
	(tree_cc_tests): ...this.
	* typed-splay-tree.cc (typed_splay_tree_c_tests): Rename to...
	(typed_splay_tree_cc_tests): ...this.
	* vec-perm-indices.cc (vec_perm_indices_c_tests): Rename to...
	(vec_perm_indices_cc_tests): ...this.
	* vec.cc (vec_c_tests): Rename to...
	(vec_cc_tests): ...this.

gcc/c-family/ChangeLog:
	* c-common.cc (c_common_c_tests): Rename to...
	(c_common_cc_tests): ...this.
	(c_family_tests): Update calls for .c to .cc renaming.
	* c-common.h (c_format_c_tests): Rename to...
	(c_format_cc_tests): ...this.
	(c_indentation_c_tests): Rename to...
	(c_indentation_cc_tests): ...this.
	(c_pretty_print_c_tests): Rename to...
	(c_pretty_print_cc_tests): ...this.
	* c-format.cc (c_format_c_tests): Rename to...
	(c_format_cc_tests): ...this.
	* c-indentation.cc (c_indentation_c_tests): Rename to...
	(c_indentation_cc_tests): ...this.
	* c-pretty-print.cc (c_pretty_print_c_tests): Rename to...
	(c_pretty_print_cc_tests): ...this.

gcc/cp/ChangeLog:
	* cp-lang.cc (selftest::run_cp_tests): Update calls for .c to .cc
	renaming.
	* cp-tree.h (cp_pt_c_tests): Rename to...
	(cp_pt_cc_tests): ...this.
	(cp_tree_c_tests): Rename to...
	(cp_tree_cc_tests): ...this.
	* pt.cc (cp_pt_c_tests): Rename to...
	(cp_pt_cc_tests): ...this.
	* tree.cc (cp_tree_c_tests): Rename to...
	(cp_tree_cc_tests): ...this.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2022-01-19 11:33:53 -05:00

296 lines
6.7 KiB
C++

/* Fibonacci heap for GNU compiler.
Copyright (C) 2016-2022 Free Software Foundation, Inc.
Contributed by Martin Liska <mliska@suse.cz>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "alloc-pool.h"
#include "fibonacci_heap.h"
#include "selftest.h"
#if CHECKING_P
namespace selftest {
/* Selftests. */
/* Verify that operations with empty heap work. */
typedef fibonacci_node <int, int> int_heap_node_t;
typedef fibonacci_heap <int, int> int_heap_t;
static void
test_empty_heap ()
{
pool_allocator allocator ("fibheap test", sizeof (int_heap_node_t));
int_heap_t *h1 = new int_heap_t (INT_MIN, &allocator);
ASSERT_TRUE (h1->empty ());
ASSERT_EQ (0, h1->nodes ());
ASSERT_EQ (NULL, h1->min ());
int_heap_t *h2 = new int_heap_t (INT_MIN, &allocator);
int_heap_t *r = h1->union_with (h2);
ASSERT_TRUE (r->empty ());
ASSERT_EQ (0, r->nodes ());
ASSERT_EQ (NULL, r->min ());
delete r;
}
#define TEST_HEAP_N 100
#define TEST_CALCULATE_VALUE(i) ((3 * i) + 10000)
/* Verify heap basic operations. */
static void
test_basic_heap_operations ()
{
int values[TEST_HEAP_N];
int_heap_t *h1 = new int_heap_t (INT_MIN);
for (unsigned i = 0; i < TEST_HEAP_N; i++)
{
values[i] = TEST_CALCULATE_VALUE (i);
ASSERT_EQ (i, h1->nodes ());
h1->insert (i, &values[i]);
ASSERT_EQ (0, h1->min_key ());
ASSERT_EQ (values[0], *h1->min ());
}
for (unsigned i = 0; i < TEST_HEAP_N; i++)
{
ASSERT_EQ (TEST_HEAP_N - i, h1->nodes ());
ASSERT_EQ ((int)i, h1->min_key ());
ASSERT_EQ (values[i], *h1->min ());
h1->extract_min ();
}
ASSERT_TRUE (h1->empty ());
delete h1;
}
/* Builds a simple heap with values in interval 0..TEST_HEAP_N-1, where values
of each key is equal to 3 * key + 10000. BUFFER is used as a storage
of values and NODES points to inserted nodes. */
static int_heap_t *
build_simple_heap (int *buffer, int_heap_node_t **nodes)
{
int_heap_t *h = new int_heap_t (INT_MIN);
for (unsigned i = 0; i < TEST_HEAP_N; i++)
{
buffer[i] = TEST_CALCULATE_VALUE (i);
nodes[i] = h->insert (i, &buffer[i]);
}
return h;
}
/* Verify that fibonacci_heap::replace_key works. */
static void
test_replace_key ()
{
int values[TEST_HEAP_N];
int_heap_node_t *nodes[TEST_HEAP_N];
int_heap_t *heap = build_simple_heap (values, nodes);
int N = 10;
for (unsigned i = 0; i < (unsigned)N; i++)
heap->replace_key (nodes[i], 100 * 1000 + i);
ASSERT_EQ (TEST_HEAP_N, heap->nodes ());
ASSERT_EQ (N, heap->min_key ());
ASSERT_EQ (TEST_CALCULATE_VALUE (N), *heap->min ());
for (int i = 0; i < TEST_HEAP_N - 1; i++)
heap->extract_min ();
ASSERT_EQ (1, heap->nodes ());
ASSERT_EQ (100 * 1000 + N - 1, heap->min_key ());
delete heap;
}
/* Verify that heap can handle duplicate keys. */
static void
test_duplicate_keys ()
{
int values[3 * TEST_HEAP_N];
int_heap_t *heap = new int_heap_t (INT_MIN);
for (unsigned i = 0; i < 3 * TEST_HEAP_N; i++)
{
values[i] = TEST_CALCULATE_VALUE (i);
heap->insert (i / 3, &values[i]);
}
ASSERT_EQ (3 * TEST_HEAP_N, heap->nodes ());
ASSERT_EQ (0, heap->min_key ());
ASSERT_EQ (TEST_CALCULATE_VALUE (0), *heap->min ());
for (unsigned i = 0; i < 9; i++)
heap->extract_min ();
for (unsigned i = 0; i < 3; i++)
{
ASSERT_EQ (3, heap->min_key ());
heap->extract_min ();
}
delete heap;
}
/* Verify that heap can handle union. */
static void
test_union ()
{
int value = 777;
pool_allocator allocator ("fibheap test", sizeof (int_heap_node_t));
int_heap_t *heap1 = new int_heap_t (INT_MIN, &allocator);
for (unsigned i = 0; i < 2 * TEST_HEAP_N; i++)
heap1->insert (i, &value);
int_heap_t *heap2 = new int_heap_t (INT_MIN, &allocator);
for (unsigned i = 2 * TEST_HEAP_N; i < 3 * TEST_HEAP_N; i++)
heap2->insert (i, &value);
int_heap_t *union_heap = heap1->union_with (heap2);
for (int i = 0; i < 3 * TEST_HEAP_N; i++)
{
ASSERT_EQ (i, union_heap->min_key ());
union_heap->extract_min ();
}
delete union_heap;
}
/* Verify that heap can handle union with a heap having exactly the same
keys. */
static void
test_union_of_equal_heaps ()
{
int value = 777;
pool_allocator allocator ("fibheap test", sizeof (int_heap_node_t));
int_heap_t *heap1 = new int_heap_t (INT_MIN, &allocator);
for (unsigned i = 0; i < TEST_HEAP_N; i++)
heap1->insert (i, &value);
int_heap_t *heap2 = new int_heap_t (INT_MIN, &allocator);
for (unsigned i = 0; i < TEST_HEAP_N; i++)
heap2->insert (i, &value);
int_heap_t *union_heap = heap1->union_with (heap2);
for (int i = 0; i < TEST_HEAP_N; i++)
for (int j = 0; j < 2; j++)
{
ASSERT_EQ (i, union_heap->min_key ());
union_heap->extract_min ();
}
delete union_heap;
}
/* Dummy struct for testing. */
class heap_key
{
public:
heap_key (int k): key (k)
{
}
int key;
bool operator< (const heap_key &other) const
{
return key > other.key;
}
bool operator== (const heap_key &other) const
{
return key == other.key;
}
bool operator> (const heap_key &other) const
{
return !(*this == other || *this < other);
}
};
typedef fibonacci_heap<heap_key, int> class_fibonacci_heap_t;
/* Verify that heap can handle a struct as key type. */
static void
test_struct_key ()
{
int value = 123456;
class_fibonacci_heap_t *heap = new class_fibonacci_heap_t (INT_MIN);
heap->insert (heap_key (1), &value);
heap->insert (heap_key (10), &value);
heap->insert (heap_key (100), &value);
heap->insert (heap_key (1000), &value);
ASSERT_EQ (1000, heap->min_key ().key);
ASSERT_EQ (4, heap->nodes ());
heap->extract_min ();
heap->extract_min ();
ASSERT_EQ (10, heap->min_key ().key);
heap->extract_min ();
ASSERT_EQ (&value, heap->min ());
heap->extract_min ();
ASSERT_TRUE (heap->empty ());
delete heap;
}
/* Run all of the selftests within this file. */
void
fibonacci_heap_cc_tests ()
{
test_empty_heap ();
test_basic_heap_operations ();
test_replace_key ();
test_duplicate_keys ();
test_union ();
test_union_of_equal_heaps ();
test_struct_key ();
}
} // namespace selftest
#endif /* #if CHECKING_P */