Mercurial > hg > CbC > CbC_gcc
diff gcc/selftest.c @ 131:84e7813d76e9
gcc-8.2
author | mir3636 |
---|---|
date | Thu, 25 Oct 2018 07:37:49 +0900 |
parents | 04ced10e8804 |
children | 1830386684a0 |
line wrap: on
line diff
--- a/gcc/selftest.c Fri Oct 27 22:46:09 2017 +0900 +++ b/gcc/selftest.c Thu Oct 25 07:37:49 2018 +0900 @@ -1,5 +1,5 @@ /* A self-testing framework, for use by -fself-test. - Copyright (C) 2015-2017 Free Software Foundation, Inc. + Copyright (C) 2015-2018 Free Software Foundation, Inc. This file is part of GCC. @@ -21,6 +21,7 @@ #include "system.h" #include "coretypes.h" #include "selftest.h" +#include "intl.h" #if CHECKING_P @@ -63,27 +64,34 @@ } /* Implementation detail of ASSERT_STREQ. - Compare val_expected and val_actual with strcmp. They ought - to be non-NULL; fail gracefully if either are NULL. */ + Compare val1 and val2 with strcmp. They ought + to be non-NULL; fail gracefully if either or both are NULL. */ void assert_streq (const location &loc, - const char *desc_expected, const char *desc_actual, - const char *val_expected, const char *val_actual) + const char *desc_val1, const char *desc_val2, + const char *val1, const char *val2) { - /* If val_expected is NULL, the test is buggy. Fail gracefully. */ - if (val_expected == NULL) - fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=NULL", - desc_expected, desc_actual); - /* If val_actual is NULL, fail with a custom error message. */ - if (val_actual == NULL) - fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=NULL", - desc_expected, desc_actual, val_expected); - if (0 == strcmp (val_expected, val_actual)) - pass (loc, "ASSERT_STREQ"); + /* If val1 or val2 are NULL, fail with a custom error message. */ + if (val1 == NULL) + if (val2 == NULL) + fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=NULL val2=NULL", + desc_val1, desc_val2); + else + fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=NULL val2=\"%s\"", + desc_val1, desc_val2, val2); else - fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=\"%s\"", - desc_expected, desc_actual, val_expected, val_actual); + if (val2 == NULL) + fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=\"%s\" val2=NULL", + desc_val1, desc_val2, val1); + else + { + if (strcmp (val1, val2) == 0) + pass (loc, "ASSERT_STREQ"); + else + fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=\"%s\" val2=\"%s\"", + desc_val1, desc_val2, val1, val2); + } } /* Implementation detail of ASSERT_STR_CONTAINS. @@ -118,6 +126,40 @@ desc_haystack, desc_needle, val_haystack, val_needle); } +/* Implementation detail of ASSERT_STR_STARTSWITH. + Determine if VAL_STR starts with VAL_PREFIX. + ::selftest::pass if VAL_STR does start with VAL_PREFIX. + ::selftest::fail if it does not, or either is NULL (using + DESC_STR and DESC_PREFIX in the error message). */ + +void +assert_str_startswith (const location &loc, + const char *desc_str, + const char *desc_prefix, + const char *val_str, + const char *val_prefix) +{ + /* If val_str is NULL, fail with a custom error message. */ + if (val_str == NULL) + fail_formatted (loc, "ASSERT_STR_STARTSWITH (%s, %s) str=NULL", + desc_str, desc_prefix); + + /* If val_prefix is NULL, fail with a custom error message. */ + if (val_prefix == NULL) + fail_formatted (loc, + "ASSERT_STR_STARTSWITH (%s, %s) str=\"%s\" prefix=NULL", + desc_str, desc_prefix, val_str); + + const char *test = strstr (val_str, val_prefix); + if (test == val_str) + pass (loc, "ASSERT_STR_STARTSWITH"); + else + fail_formatted + (loc, "ASSERT_STR_STARTSWITH (%s, %s) str=\"%s\" prefix=\"%s\"", + desc_str, desc_prefix, val_str, val_prefix); +} + + /* Constructor. Generate a name for the file. */ named_temp_file::named_temp_file (const char *suffix) @@ -151,6 +193,25 @@ fclose (out); } +/* Avoid introducing locale-specific differences in the results + by hardcoding open_quote and close_quote. */ + +auto_fix_quotes::auto_fix_quotes () +{ + m_saved_open_quote = open_quote; + m_saved_close_quote = close_quote; + open_quote = "`"; + close_quote = "'"; +} + +/* Restore old values of open_quote and close_quote. */ + +auto_fix_quotes::~auto_fix_quotes () +{ + open_quote = m_saved_open_quote; + close_quote = m_saved_close_quote; +} + /* Read the contents of PATH into memory, returning a 0-terminated buffer that must be freed by the caller. Fail (and abort) if there are any problems, with LOC as the reported @@ -213,6 +274,28 @@ return concat (path_to_selftest_files, "/", name, NULL); } +/* selftest::test_runner's ctor. */ + +test_runner::test_runner (const char *name) +: m_name (name), + m_start_time (get_run_time ()) +{ +} + +/* selftest::test_runner's dtor. Print a summary line to stderr. */ + +test_runner::~test_runner () +{ + /* Finished running tests. */ + long finish_time = get_run_time (); + long elapsed_time = finish_time - m_start_time; + + fprintf (stderr, + "%s: %i pass(es) in %ld.%06ld seconds\n", + m_name, num_passes, + elapsed_time / 1000000, elapsed_time % 1000000); +} + /* Selftests for libiberty. */ /* Verify that xstrndup generates EXPECTED when called on SRC and N. */ @@ -266,6 +349,10 @@ ASSERT_EQ (1, 1); ASSERT_EQ_AT (SELFTEST_LOCATION, 1, 1); ASSERT_NE (1, 2); + ASSERT_GT (2, 1); + ASSERT_GT_AT (SELFTEST_LOCATION, 2, 1); + ASSERT_LT (1, 2); + ASSERT_LT_AT (SELFTEST_LOCATION, 1, 2); ASSERT_STREQ ("test", "test"); ASSERT_STREQ_AT (SELFTEST_LOCATION, "test", "test"); ASSERT_STR_CONTAINS ("foo bar baz", "bar");