From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 50755 invoked by alias); 1 Jun 2016 21:21:13 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 50742 invoked by uid 89); 1 Jun 2016 21:21:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 spammy=row, HX-HELO:eggs.gnu.org, Hx-spam-relays-external:208.118.235.92, H*RU:208.118.235.92 X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (208.118.235.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Wed, 01 Jun 2016 21:21:11 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8D9V-0007tf-He for gcc-patches@gcc.gnu.org; Wed, 01 Jun 2016 16:54:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37867) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8D9V-0007tV-8z for gcc-patches@gcc.gnu.org; Wed, 01 Jun 2016 16:54:09 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C818FC05B1CF for ; Wed, 1 Jun 2016 20:54:08 +0000 (UTC) Received: from c64.redhat.com (vpn-239-103.phx2.redhat.com [10.3.239.103]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u51Ks4WO027176; Wed, 1 Jun 2016 16:54:08 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: Bernd Schmidt , Jeff Law , David Malcolm Subject: [PATCH 05/21] Add selftests for diagnostic-show-locus.c Date: Wed, 01 Jun 2016 21:21:00 -0000 Message-Id: <1464816003-35862-6-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1464816003-35862-1-git-send-email-dmalcolm@redhat.com> References: <1447952699-40820-1-git-send-email-dmalcolm@redhat.com> <1464816003-35862-1-git-send-email-dmalcolm@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-IsSubscribed: yes X-SW-Source: 2016-06/txt/msg00101.txt.bz2 Here's another example of unit tests, this time for an implementation detail within diagnostic-show-locus.c. gcc/ChangeLog: * diagnostic-show-locus.c: Include "selftest.h". (class range_contains_point_tests): New class. (TEST_F (range_contains_point_tests, single_point)): New test. (TEST_F (range_contains_point_tests, single_line)): New test. (TEST_F (range_contains_point_tests, multiple_lines)): New test. --- gcc/diagnostic-show-locus.c | 113 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c index eeccee5..283a260 100644 --- a/gcc/diagnostic-show-locus.c +++ b/gcc/diagnostic-show-locus.c @@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see #include "backtrace.h" #include "diagnostic.h" #include "diagnostic-color.h" +#include "selftest.h" #ifdef HAVE_TERMIOS_H # include @@ -442,6 +443,118 @@ layout_range::contains_point (int row, int column) const return column <= m_finish.m_column; } +#if CHECKING_P + +/* A fixture for testing layout_range::contains_point. */ + +class range_contains_point_tests : public ::selftest::test +{ + protected: + layout_range + make_range (int start_line, int start_col, + int end_line, int end_col) + { + const expanded_location start_exploc + = {"test.c", start_line, start_col, NULL, false}; + expanded_location finish_exploc + = {"test.c", end_line, end_col, NULL, false}; + + return layout_range (&start_exploc, &finish_exploc, false, + &start_exploc); + } +}; + +TEST_F (range_contains_point_tests, single_point) +{ + /* A range with start==end. */ + layout_range point = make_range (7, 10, 7, 10); + + /* Before the line. */ + EXPECT_FALSE (point.contains_point (6, 1)); + + /* On the line, but before start. */ + EXPECT_FALSE (point.contains_point (7, 9)); + + /* At the point. */ + EXPECT_TRUE (point.contains_point (7, 10)); + + /* On the line, after the point. */ + EXPECT_FALSE (point.contains_point (7, 11)); + + /* After the line. */ + EXPECT_FALSE (point.contains_point (8, 1)); +} + +TEST_F (range_contains_point_tests, single_line) +{ + /* The single-line example from above. */ + layout_range example_a = make_range (2, 22, 2, 38); + + /* Before the line. */ + EXPECT_FALSE (example_a.contains_point (1, 1)); + + /* On the line, but before start. */ + EXPECT_FALSE (example_a.contains_point (2, 21)); + + /* On the line, at the start. */ + EXPECT_TRUE (example_a.contains_point (2, 22)); + + /* On the line, within the range. */ + EXPECT_TRUE (example_a.contains_point (2, 23)); + + /* On the line, at the end. */ + EXPECT_TRUE (example_a.contains_point (2, 38)); + + /* On the line, after the end. */ + EXPECT_FALSE (example_a.contains_point (2, 39)); + + /* After the line. */ + EXPECT_FALSE (example_a.contains_point (2, 39)); +} + +TEST_F (range_contains_point_tests, multiple_lines) +{ + /* The multi-line example from above. */ + layout_range example_b = make_range (3, 14, 5, 8); + + /* Before first line. */ + EXPECT_FALSE (example_b.contains_point (1, 1)); + + /* On the first line, but before start. */ + EXPECT_FALSE (example_b.contains_point (3, 13)); + + /* At the start. */ + EXPECT_TRUE (example_b.contains_point (3, 14)); + + /* On the first line, within the range. */ + EXPECT_TRUE (example_b.contains_point (3, 15)); + + /* On an interior line. + The column number should not matter; try various boundary + values. */ + EXPECT_TRUE (example_b.contains_point (4, 1)); + EXPECT_TRUE (example_b.contains_point (4, 7)); + EXPECT_TRUE (example_b.contains_point (4, 8)); + EXPECT_TRUE (example_b.contains_point (4, 9)); + EXPECT_TRUE (example_b.contains_point (4, 13)); + EXPECT_TRUE (example_b.contains_point (4, 14)); + EXPECT_TRUE (example_b.contains_point (4, 15)); + + /* On the final line, before the end. */ + EXPECT_TRUE (example_b.contains_point (5, 7)); + + /* On the final line, at the end. */ + EXPECT_TRUE (example_b.contains_point (5, 8)); + + /* On the final line, after the end. */ + EXPECT_FALSE (example_b.contains_point (5, 9)); + + /* After the line. */ + EXPECT_FALSE (example_b.contains_point (6, 1)); +} + +#endif /* #if CHECKING_P */ + /* Given a source line LINE of length LINE_WIDTH, determine the width without any trailing whitespace. */ -- 1.8.5.3