Skip to content

Commit 75ab30f

Browse files
author
Andi Kleen
committed
Add a unit test for random access in the file cache
v2: Remove extra {} gcc/ChangeLog: * input.cc (check_line): New. (test_replacement): New function to test line caching. (input_cc_tests): Call test_replacement
1 parent baf26fc commit 75ab30f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

gcc/input.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,48 @@ test_make_location_nonpure_range_endpoints (const line_table_case &case_)
23012301
ASSERT_FALSE (IS_ADHOC_LOC (get_finish (not_aaa_eq_bbb)));
23022302
}
23032303

2304+
/* Verify reading of a specific line LINENUM in TMP, FC. */
2305+
2306+
static void check_line (temp_source_file &tmp, file_cache &fc, int linenum)
2307+
{
2308+
char_span line = fc.get_source_line (tmp.get_filename (), linenum);
2309+
int n;
2310+
/* get_buffer is not null terminated, but the sscanf stops after a number. */
2311+
ASSERT_TRUE (sscanf (line.get_buffer (), "%d", &n) == 1);
2312+
ASSERT_EQ (n, linenum);
2313+
}
2314+
2315+
/* Test file cache replacement. */
2316+
2317+
static void test_replacement ()
2318+
{
2319+
const int maxline = 1000;
2320+
2321+
char *vec = XNEWVEC (char, maxline * 15);
2322+
char *p = vec;
2323+
int i;
2324+
for (i = 1; i <= maxline; i++)
2325+
p += sprintf (p, "%d\n", i);
2326+
2327+
temp_source_file tmp (SELFTEST_LOCATION, ".txt", vec);
2328+
free (vec);
2329+
file_cache fc;
2330+
2331+
for (i = 2; i <= maxline; i++)
2332+
{
2333+
check_line (tmp, fc, i);
2334+
check_line (tmp, fc, i - 1);
2335+
if (i >= 10)
2336+
check_line (tmp, fc, i - 9);
2337+
if (i >= 350) /* Exceed the look behind cache. */
2338+
check_line (tmp, fc, i - 300);
2339+
}
2340+
for (i = 5; i <= maxline; i += 100)
2341+
check_line (tmp, fc, i);
2342+
for (i = 1; i <= maxline; i++)
2343+
check_line (tmp, fc, i);
2344+
}
2345+
23042346
/* Verify reading of input files (e.g. for caret-based diagnostics). */
23052347

23062348
static void
@@ -4251,6 +4293,7 @@ input_cc_tests ()
42514293

42524294
test_reading_source_line ();
42534295
test_reading_source_buffer ();
4296+
test_replacement ();
42544297

42554298
test_line_offset_overflow ();
42564299

0 commit comments

Comments
 (0)