Mercurial > hg > CbC > CbC_gcc
diff gcc/vec.c @ 131:84e7813d76e9
gcc-8.2
author | mir3636 |
---|---|
date | Thu, 25 Oct 2018 07:37:49 +0900 |
parents | 04ced10e8804 |
children | 1830386684a0 |
line wrap: on
line diff
--- a/gcc/vec.c Fri Oct 27 22:46:09 2017 +0900 +++ b/gcc/vec.c Thu Oct 25 07:37:49 2018 +0900 @@ -1,5 +1,5 @@ /* Vector API for GNU compiler. - Copyright (C) 2004-2017 Free Software Foundation, Inc. + Copyright (C) 2004-2018 Free Software Foundation, Inc. Contributed by Nathan Sidwell <nathan@codesourcery.com> Re-implemented in C++ by Diego Novillo <dnovillo@google.com> @@ -60,15 +60,6 @@ : mem_usage (allocated, times, peak), m_items (items), m_items_peak (items_peak) {} - /* Comparison operator. */ - inline bool - operator< (const vec_usage &second) const - { - return (m_allocated == second.m_allocated ? - (m_peak == second.m_peak ? m_times < second.m_times - : m_peak < second.m_peak) : m_allocated < second.m_allocated); - } - /* Sum the usage with SECOND usage. */ vec_usage operator+ (const vec_usage &second) @@ -115,18 +106,6 @@ print_dash_line (); } - /* Compare wrapper used by qsort method. */ - static int - compare (const void *first, const void *second) - { - typedef std::pair<mem_location *, vec_usage *> mem_pair_t; - - const mem_pair_t f = *(const mem_pair_t *)first; - const mem_pair_t s = *(const mem_pair_t *)second; - - return (*f.second) < (*s.second); - } - /* Current number of items allocated. */ size_t m_items; /* Peak value of number of allocated items. */ @@ -222,21 +201,12 @@ internal_error ("qsort checking failed"); } -/* Wrapper around qsort with checking that CMP is consistent on given input. - - Strictly speaking, passing invalid (non-transitive, non-anti-commutative) - comparators to libc qsort can result in undefined behavior. Therefore we - should ideally perform consistency checks prior to invoking qsort, but in - order to do that optimally we'd need to sort the array ourselves beforehand - with a sorting routine known to be "safe". Instead, we expect that most - implementations in practice will still produce some permutation of input - array even for invalid comparators, which enables us to perform checks on - the output array. */ +/* Verify anti-symmetry and transitivity for comparator CMP on sorted array + of N SIZE-sized elements pointed to by BASE. */ void qsort_chk (void *base, size_t n, size_t size, int (*cmp)(const void *, const void *)) { - (qsort) (base, n, size, cmp); #if 0 #define LIM(n) (n) #else @@ -403,6 +373,51 @@ ASSERT_EQ (9, v.length ()); } +/* Verify that vec::ordered_remove_if works correctly. */ + +static void +test_ordered_remove_if (void) +{ + auto_vec <int> v; + safe_push_range (v, 0, 10); + unsigned ix, ix2; + int *elem_ptr; + VEC_ORDERED_REMOVE_IF (v, ix, ix2, elem_ptr, + *elem_ptr == 5 || *elem_ptr == 7); + ASSERT_EQ (4, v[4]); + ASSERT_EQ (6, v[5]); + ASSERT_EQ (8, v[6]); + ASSERT_EQ (8, v.length ()); + + v.truncate (0); + safe_push_range (v, 0, 10); + VEC_ORDERED_REMOVE_IF_FROM_TO (v, ix, ix2, elem_ptr, 0, 6, + *elem_ptr == 5 || *elem_ptr == 7); + ASSERT_EQ (4, v[4]); + ASSERT_EQ (6, v[5]); + ASSERT_EQ (7, v[6]); + ASSERT_EQ (9, v.length ()); + + v.truncate (0); + safe_push_range (v, 0, 10); + VEC_ORDERED_REMOVE_IF_FROM_TO (v, ix, ix2, elem_ptr, 0, 5, + *elem_ptr == 5 || *elem_ptr == 7); + VEC_ORDERED_REMOVE_IF_FROM_TO (v, ix, ix2, elem_ptr, 8, 10, + *elem_ptr == 5 || *elem_ptr == 7); + ASSERT_EQ (4, v[4]); + ASSERT_EQ (5, v[5]); + ASSERT_EQ (6, v[6]); + ASSERT_EQ (10, v.length ()); + + v.truncate (0); + safe_push_range (v, 0, 10); + VEC_ORDERED_REMOVE_IF (v, ix, ix2, elem_ptr, *elem_ptr == 5); + ASSERT_EQ (4, v[4]); + ASSERT_EQ (6, v[5]); + ASSERT_EQ (7, v[6]); + ASSERT_EQ (9, v.length ()); +} + /* Verify that vec::unordered_remove works correctly. */ static void @@ -452,6 +467,43 @@ ASSERT_EQ (10, v.length ()); } +/* Verify that vec::reverse works correctly. */ + +static void +test_reverse () +{ + /* Reversing an empty vec ought to be a no-op. */ + { + auto_vec <int> v; + ASSERT_EQ (0, v.length ()); + v.reverse (); + ASSERT_EQ (0, v.length ()); + } + + /* Verify reversing a vec with even length. */ + { + auto_vec <int> v; + safe_push_range (v, 0, 4); + v.reverse (); + ASSERT_EQ (3, v[0]); + ASSERT_EQ (2, v[1]); + ASSERT_EQ (1, v[2]); + ASSERT_EQ (0, v[3]); + ASSERT_EQ (4, v.length ()); + } + + /* Verify reversing a vec with odd length. */ + { + auto_vec <int> v; + safe_push_range (v, 0, 3); + v.reverse (); + ASSERT_EQ (2, v[0]); + ASSERT_EQ (1, v[1]); + ASSERT_EQ (0, v[2]); + ASSERT_EQ (3, v.length ()); + } +} + /* Run all of the selftests within this file. */ void @@ -464,9 +516,11 @@ test_pop (); test_safe_insert (); test_ordered_remove (); + test_ordered_remove_if (); test_unordered_remove (); test_block_remove (); test_qsort (); + test_reverse (); } } // namespace selftest