From d55bc438c2d3e9abdfae99243d1cd010eaa984f2 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 27 May 2016 15:52:21 -0400 Subject: [PATCH 17/35] Add selftests for diagnostic-show-locus.c 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