Jeff approved an earlier version of this (as unittests/test-vec.c): https://gcc.gnu.org/ml/gcc-patches/2015-10/msg03308.html > OK if/when prereqs are approved. Minor twiddling if we end up > moving it elsewhere or standardizing/reducing header files is > pre-approved.
gcc/ChangeLog: * vec.c: Include "selftest.h". (safe_push_range): New function. (test_quick_push): New function. (test_safe_push): New function. (test_truncate): New function. (test_safe_grow_cleared): New function. (test_safe_pop): New function. (test_safe_insert): New function. (test_ordered_remove): New function. (test_unordered_remove): New function. (test_block_remove): New function. (reverse_cmp): New function. (test_qsort): New function. (selftest::vec_c_tests): New function.c. --- gcc/vec.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/gcc/vec.c b/gcc/vec.c index a483d5b..b64a012 100644 --- a/gcc/vec.c +++ b/gcc/vec.c @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "system.h" #include "coretypes.h" #include "hash-table.h" +#include "selftest.h" /* vNULL is an empty type with a template cast operation that returns a zero-initialized vec<T, A, L> instance. Use this when you want @@ -188,3 +189,164 @@ dump_vec_loc_statistics (void) { vec_mem_desc.dump (VEC_ORIGIN); } + +#ifndef GENERATOR_FILE +#if CHECKING_P + +static void +safe_push_range (vec <int>&v, int start, int limit) +{ + for (int i = start; i < limit; i++) + v.safe_push (i); +} + +static void +test_quick_push () +{ + auto_vec <int> v; + ASSERT_EQ (0, v.length ()); + v.reserve (3); + ASSERT_EQ (0, v.length ()); + ASSERT_TRUE (v.space (3)); + v.quick_push (5); + v.quick_push (6); + v.quick_push (7); + ASSERT_EQ (3, v.length ()); + ASSERT_EQ (5, v[0]); + ASSERT_EQ (6, v[1]); + ASSERT_EQ (7, v[2]); +} + +static void +test_safe_push () +{ + auto_vec <int> v; + ASSERT_EQ (0, v.length ()); + v.safe_push (5); + v.safe_push (6); + v.safe_push (7); + ASSERT_EQ (3, v.length ()); + ASSERT_EQ (5, v[0]); + ASSERT_EQ (6, v[1]); + ASSERT_EQ (7, v[2]); +} + +static void +test_truncate () +{ + auto_vec <int> v; + ASSERT_EQ (0, v.length ()); + safe_push_range (v, 0, 10); + ASSERT_EQ (10, v.length ()); + + v.truncate (5); + ASSERT_EQ (5, v.length ()); +} + +static void +test_safe_grow_cleared () +{ + auto_vec <int> v; + ASSERT_EQ (0, v.length ()); + v.safe_grow_cleared (50); + ASSERT_EQ (50, v.length ()); + ASSERT_EQ (0, v[0]); + ASSERT_EQ (0, v[49]); +} + +static void +test_safe_pop () +{ + auto_vec <int> v; + safe_push_range (v, 5, 20); + ASSERT_EQ (15, v.length ()); + + int last = v.pop (); + ASSERT_EQ (19, last); + ASSERT_EQ (14, v.length ()); +} + +static void +test_safe_insert () +{ + auto_vec <int> v; + safe_push_range (v, 0, 10); + v.safe_insert (5, 42); + ASSERT_EQ (4, v[4]); + ASSERT_EQ (42, v[5]); + ASSERT_EQ (5, v[6]); + ASSERT_EQ (11, v.length ()); +} + +static void +test_ordered_remove () +{ + auto_vec <int> v; + safe_push_range (v, 0, 10); + v.ordered_remove (5); + ASSERT_EQ (4, v[4]); + ASSERT_EQ (6, v[5]); + ASSERT_EQ (9, v.length ()); +} + +static void +test_unordered_remove () +{ + auto_vec <int> v; + safe_push_range (v, 0, 10); + v.unordered_remove (5); + ASSERT_EQ (9, v.length ()); +} + +static void +test_block_remove () +{ + auto_vec <int> v; + safe_push_range (v, 0, 10); + v.block_remove (5, 3); + ASSERT_EQ (3, v[3]); + ASSERT_EQ (4, v[4]); + ASSERT_EQ (8, v[5]); + ASSERT_EQ (9, v[6]); + ASSERT_EQ (7, v.length ()); +} + +static int reverse_cmp (const void *p_i, const void *p_j) +{ + return *(const int *)p_j - *(const int *)p_i; +} + +static void +test_qsort () +{ + auto_vec <int> v; + safe_push_range (v, 0, 10); + v.qsort (reverse_cmp); + ASSERT_EQ (9, v[0]); + ASSERT_EQ (8, v[1]); + ASSERT_EQ (1, v[8]); + ASSERT_EQ (0, v[9]); + ASSERT_EQ (10, v.length ()); +} + +namespace selftest { + +void +vec_c_tests () +{ + test_quick_push (); + test_safe_push (); + test_truncate (); + test_safe_grow_cleared (); + test_safe_pop (); + test_safe_insert (); + test_ordered_remove (); + test_unordered_remove (); + test_block_remove (); + test_qsort (); +} + +} // namespace selftest + +#endif /* #if CHECKING_P */ +#endif /* #ifndef GENERATOR_FILE */ -- 1.8.5.3