From 3f566d80dbaaa6c8b4dd4f455dd38e16d2cc7cf9 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 10 Jun 2015 06:08:11 -0400
Subject: [PATCH 15/16] Add test-vec.c to unittests

gcc/testsuite/ChangeLog:
	* unittests/test-vec.c: New file.
---
 gcc/testsuite/unittests/test-vec.c | 161 +++++++++++++++++++++++++++++++++++++
 1 file changed, 161 insertions(+)
 create mode 100644 gcc/testsuite/unittests/test-vec.c

diff --git a/gcc/testsuite/unittests/test-vec.c b/gcc/testsuite/unittests/test-vec.c
new file mode 100644
index 0000000..c5b9d78
--- /dev/null
+++ b/gcc/testsuite/unittests/test-vec.c
@@ -0,0 +1,161 @@
+/* Unit tests for vec<>.
+   Copyright (C) 2015 Free Software Foundation, Inc.
+
+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 "gtest/gtest.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "opts.h"
+#include "vec.h"
+
+namespace {
+
+class vec_test : public ::testing::Test
+{
+ protected:
+  /* Add the range [START..LIMIT) to V.  */
+  void
+  safe_push_range (vec <int>&v, int start, int limit)
+  {
+    for (int i = start; i < limit; i++)
+      v.safe_push (i);
+  }
+};
+
+TEST_F (vec_test, quick_push)
+{
+  auto_vec <int> v;
+  EXPECT_EQ (0, v.length ());
+  v.reserve (3);
+  EXPECT_EQ (0, v.length ());
+  EXPECT_TRUE (v.space (3));
+  v.quick_push (5);
+  v.quick_push (6);
+  v.quick_push (7);
+  EXPECT_EQ (3, v.length ());
+  EXPECT_EQ (5, v[0]);
+  EXPECT_EQ (6, v[1]);
+  EXPECT_EQ (7, v[2]);
+}
+
+TEST_F (vec_test, safe_push)
+{
+  auto_vec <int> v;
+  EXPECT_EQ (0, v.length ());
+  v.safe_push (5);
+  v.safe_push (6);
+  v.safe_push (7);
+  EXPECT_EQ (3, v.length ());
+  EXPECT_EQ (5, v[0]);
+  EXPECT_EQ (6, v[1]);
+  EXPECT_EQ (7, v[2]);
+}
+
+TEST_F (vec_test, truncate)
+{
+  auto_vec <int> v;
+  EXPECT_EQ (0, v.length ());
+  safe_push_range (v, 0, 10);
+  EXPECT_EQ (10, v.length ());
+
+  v.truncate (5);
+  EXPECT_EQ (5, v.length ());
+}
+
+TEST_F (vec_test, safe_grow_cleared)
+{
+  auto_vec <int> v;
+  EXPECT_EQ (0, v.length ());
+  v.safe_grow_cleared (50);
+  EXPECT_EQ (50, v.length ());
+  EXPECT_EQ (0, v[0]);
+  EXPECT_EQ (0, v[49]);
+}
+
+TEST_F (vec_test, pop)
+{
+  auto_vec <int> v;
+  safe_push_range (v, 5, 20);
+  EXPECT_EQ (15, v.length ());
+
+  int last = v.pop ();
+  EXPECT_EQ (19, last);
+  EXPECT_EQ (14, v.length ());
+}
+
+TEST_F (vec_test, safe_insert)
+{
+  auto_vec <int> v;
+  safe_push_range (v, 0, 10);
+  v.safe_insert (5, 42);
+  EXPECT_EQ (4, v[4]);
+  EXPECT_EQ (42, v[5]);
+  EXPECT_EQ (5, v[6]);
+  EXPECT_EQ (11, v.length ());
+}
+
+TEST_F (vec_test, ordered_remove)
+{
+  auto_vec <int> v;
+  safe_push_range (v, 0, 10);
+  v.ordered_remove (5);
+  EXPECT_EQ (4, v[4]);
+  EXPECT_EQ (6, v[5]);
+  EXPECT_EQ (9, v.length ());
+}
+
+TEST_F (vec_test, unordered_remove)
+{
+  auto_vec <int> v;
+  safe_push_range (v, 0, 10);
+  v.unordered_remove (5);
+  EXPECT_EQ (9, v.length ());
+}
+
+TEST_F (vec_test, block_remove)
+{
+  auto_vec <int> v;
+  safe_push_range (v, 0, 10);
+  v.block_remove (5, 3);
+  EXPECT_EQ (3, v[3]);
+  EXPECT_EQ (4, v[4]);
+  EXPECT_EQ (8, v[5]);
+  EXPECT_EQ (9, v[6]);
+  EXPECT_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;
+}
+
+TEST_F (vec_test, qsort)
+{
+  auto_vec <int> v;
+  safe_push_range (v, 0, 10);
+  v.qsort (reverse_cmp);
+  EXPECT_EQ (9, v[0]);
+  EXPECT_EQ (8, v[1]);
+  EXPECT_EQ (1, v[8]);
+  EXPECT_EQ (0, v[9]);
+  EXPECT_EQ (10, v.length ());
+}
+
+}  // anon namespace
-- 
1.8.5.3

