From patchwork Thu Jun 2 21:06:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 629452 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rLK1L661Dz9t84 for ; Fri, 3 Jun 2016 06:43:02 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=VebGv2wy; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; q=dns; s= default; b=mMxQhjA5j1evkPL+A1QbodJR3lLg6tJTIotOJa8OeV5s+eXoW9OYT BuIT0BPmUw9Jck5FCZ/w+fBarfCu4iS3uOk2ojPv0XCKbqt1M4J1vyqL1BKkfRIS w3LcYiEjMV2Zdj6cBc7uVLBF6hFdiGuiCscpTHy+xA6A3l8Ck2VTBI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; s= default; bh=lFAYdnYqM/0EoXLubz/gx6qlYbc=; b=VebGv2wyHJqPWomWwrRh q5rBWvpbqsW/DyK3ecDx6DpNZ+TU3kaCQOr2b8lHXgBX1tzTBT+kZO8+39cuGizV vKFA1JDuc32aYoDCEGVpJ3arTDs7y8AlpUo2iSzFyBQYe+LfRXXuksESLtl9FPfk eSu7uRtEDbaG+W2H82dfiOU= Received: (qmail 36040 invoked by alias); 2 Jun 2016 20:41:19 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 35749 invoked by uid 89); 2 Jun 2016 20:41:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.3 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 02 Jun 2016 20:41:06 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (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 E4C8DC0E7FFC for ; Thu, 2 Jun 2016 20:41:04 +0000 (UTC) Received: from c64.redhat.com (vpn-237-222.phx2.redhat.com [10.3.237.222]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u52Kf2Wu007154; Thu, 2 Jun 2016 16:41:04 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: Bernd Schmidt , Jeff Law , David Malcolm Subject: [PATCH 02/16] diagnostic-show-locus.c: add selftests Date: Thu, 2 Jun 2016 17:06:51 -0400 Message-Id: <1464901625-54547-3-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1464901625-54547-1-git-send-email-dmalcolm@redhat.com> References: <1464874868.11895.41.camel@redhat.com> <1464901625-54547-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes gcc/ChangeLog: * diagnostic-show-locus.c: Include "selftest.h". (make_range): New function. (test_range_contains_point_for_single_point): New function. (test_range_contains_point_test_for_single_line): New function. (test_range_contains_point_for_multiple_lines): New function. (assert_eq): New function. (test_get_line_width_without_trailing_whitespace): New function. (selftest::diagnostic_show_locus_c_tests): New function. --- gcc/diagnostic-show-locus.c | 156 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c index eeccee5..38b424d 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 helper function for testing layout_range::contains_point. */ + +static 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}; + const expanded_location finish_exploc + = {"test.c", end_line, end_col, NULL, false}; + return layout_range (&start_exploc, &finish_exploc, false, + &start_exploc); +} + +/* Selftests for layout_range::contains_point. */ + +static void +test_range_contains_point_for_single_point () +{ + /* A range with start==end. */ + layout_range point = make_range (7, 10, 7, 10); + + /* Before the line. */ + ASSERT_FALSE (point.contains_point (6, 1)); + + /* On the line, but before start. */ + ASSERT_FALSE (point.contains_point (7, 9)); + + /* At the point. */ + ASSERT_TRUE (point.contains_point (7, 10)); + + /* On the line, after the point. */ + ASSERT_FALSE (point.contains_point (7, 11)); + + /* After the line. */ + ASSERT_FALSE (point.contains_point (8, 1)); +} + +static void +test_range_contains_point_test_for_single_line () +{ + /* The single-line example from above. */ + layout_range example_a = make_range (2, 22, 2, 38); + + /* Before the line. */ + ASSERT_FALSE (example_a.contains_point (1, 1)); + + /* On the line, but before start. */ + ASSERT_FALSE (example_a.contains_point (2, 21)); + + /* On the line, at the start. */ + ASSERT_TRUE (example_a.contains_point (2, 22)); + + /* On the line, within the range. */ + ASSERT_TRUE (example_a.contains_point (2, 23)); + + /* On the line, at the end. */ + ASSERT_TRUE (example_a.contains_point (2, 38)); + + /* On the line, after the end. */ + ASSERT_FALSE (example_a.contains_point (2, 39)); + + /* After the line. */ + ASSERT_FALSE (example_a.contains_point (2, 39)); +} + +static void +test_range_contains_point_for_multiple_lines () +{ + /* The multi-line example from above. */ + layout_range example_b = make_range (3, 14, 5, 8); + + /* Before first line. */ + ASSERT_FALSE (example_b.contains_point (1, 1)); + + /* On the first line, but before start. */ + ASSERT_FALSE (example_b.contains_point (3, 13)); + + /* At the start. */ + ASSERT_TRUE (example_b.contains_point (3, 14)); + + /* On the first line, within the range. */ + ASSERT_TRUE (example_b.contains_point (3, 15)); + + /* On an interior line. + The column number should not matter; try various boundary + values. */ + ASSERT_TRUE (example_b.contains_point (4, 1)); + ASSERT_TRUE (example_b.contains_point (4, 7)); + ASSERT_TRUE (example_b.contains_point (4, 8)); + ASSERT_TRUE (example_b.contains_point (4, 9)); + ASSERT_TRUE (example_b.contains_point (4, 13)); + ASSERT_TRUE (example_b.contains_point (4, 14)); + ASSERT_TRUE (example_b.contains_point (4, 15)); + + /* On the final line, before the end. */ + ASSERT_TRUE (example_b.contains_point (5, 7)); + + /* On the final line, at the end. */ + ASSERT_TRUE (example_b.contains_point (5, 8)); + + /* On the final line, after the end. */ + ASSERT_FALSE (example_b.contains_point (5, 9)); + + /* After the line. */ + ASSERT_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. */ @@ -465,6 +578,31 @@ get_line_width_without_trailing_whitespace (const char *line, int line_width) return result; } +#if CHECKING_P + +/* A helper function for testing get_line_width_without_trailing_whitespace. */ + +static void +assert_eq (const char *line, int expected_width) +{ + int actual_value + = get_line_width_without_trailing_whitespace (line, strlen (line)); + ASSERT_EQ (actual_value, expected_width); +} + +static void +test_get_line_width_without_trailing_whitespace () +{ + assert_eq ("", 0); + assert_eq (" ", 0); + assert_eq ("\t", 0); + assert_eq ("hello world", 11); + assert_eq ("hello world ", 11); + assert_eq ("hello world \t\t ", 11); +} + +#endif /* #if CHECKING_P */ + /* Helper function for layout's ctor, for sanitizing locations relative to the primary location within a diagnostic. @@ -1171,3 +1309,21 @@ diagnostic_show_locus (diagnostic_context * context, pp_set_prefix (context->printer, saved_prefix); } + +#if CHECKING_P + +namespace selftest { + +void +diagnostic_show_locus_c_tests () +{ + test_range_contains_point_for_single_point (); + test_range_contains_point_test_for_single_line (); + test_range_contains_point_for_multiple_lines (); + + test_get_line_width_without_trailing_whitespace (); +} + +} // namespace selftest + +#endif /* #if CHECKING_P */