diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 353f0b75329db0c0cc97701baa0135c602bd63b3..8f6367c20f9d1f2b11670b2ba20d96c157380187 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,12 +4,19 @@ before_script: - apt-get update -qq - apt-get install -y cmake - apt-get install -y lcov + - apt-get install -y clang-format clang-tidy clang-tools clang # Define the stages of the pipeline stages: + - codeStyling - build - test - coverage +job:codestyling: + stage: codestyling + script: python3 run-clang-format.py -r src include tests + + # Define the job for building the project job:build: stage: build diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps index 4deb030f4fff6f707d1f13ddd2449a38cb5698a7..ead1a36f5c9a6a8cae8fa5c69c9abf49326b80ea 100644 Binary files a/cmake-build-debug/.ninja_deps and b/cmake-build-debug/.ninja_deps differ diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log index d84a253170f3e8eec34cea355bbe18721aaed602..596db8926adb54589b71c6051b97d0e8841c51c0 100644 --- a/cmake-build-debug/.ninja_log +++ b/cmake-build-debug/.ninja_log @@ -28,3 +28,5 @@ 1754 5405 7338585576788163 bin/ExampleTests.exe e524054f521ed215 5 1682 7338586413110088 CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.obj e28e202807b1bd82 1683 5827 7338586454469850 bin/ExampleTests.exe e524054f521ed215 +4 1548 7338599937600314 CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.obj e28e202807b1bd82 +1548 5359 7338599975633138 bin/ExampleTests.exe e524054f521ed215 diff --git a/cmake-build-debug/CMakeFiles/ExampleTests.dir/src/compt_frequence.c.gcda b/cmake-build-debug/CMakeFiles/ExampleTests.dir/src/compt_frequence.c.gcda index 21339278fd22430ed400f71cbfcc2e695f299ef8..d063581c30c0fa670bc14c73dbeaa13b77cb61b8 100644 Binary files a/cmake-build-debug/CMakeFiles/ExampleTests.dir/src/compt_frequence.c.gcda and b/cmake-build-debug/CMakeFiles/ExampleTests.dir/src/compt_frequence.c.gcda differ diff --git a/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcda b/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcda index 7c67050e67d15cca03b70111d93846a857fcf326..0cb1547aee9f5f6f4c83e416de999d849a4bebbd 100644 Binary files a/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcda and b/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcda differ diff --git a/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcno b/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcno index 7944cf0bb9cd54291a5711dad06d66f2d072c3c4..98ba3aa74bcca2b6b98ab7ba673fc6498e8f1ac7 100644 Binary files a/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcno and b/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.gcno differ diff --git a/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.obj b/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.obj index 1e5cc57ee4f93f73b58b53c581ff77200ab83cf9..36e0747970918c92a17f912d14f10dff72159324 100644 Binary files a/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.obj and b/cmake-build-debug/CMakeFiles/ExampleTests.dir/tests/ExampleTests.cpp.obj differ diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log index f8bea588ea5f854ebe0e3fb942ec62073c06b0e8..82adbdc1e059197e03cb5b82ff1dbdad9d77b595 100644 --- a/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Apr 03 16:44 Romance Daylight Time +Start testing: Apr 03 17:06 Romance Daylight Time ---------------------------------------------------------- -End testing: Apr 03 16:44 Romance Daylight Time +End testing: Apr 03 17:06 Romance Daylight Time diff --git a/cmake-build-debug/bin/ExampleTests.exe b/cmake-build-debug/bin/ExampleTests.exe index 630807de27ff08813e17dcb3a5449196c10c310e..443e1a4def6cc3b7554b0d532d89e7cb5c4da135 100644 Binary files a/cmake-build-debug/bin/ExampleTests.exe and b/cmake-build-debug/bin/ExampleTests.exe differ diff --git a/cmake-build-debug/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.gcda b/cmake-build-debug/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.gcda index 0fd7ec19c8ebd00524704102debe6e41a7254d76..f162a733fbd86bd70ed8c4b9e241e608bfb99cfa 100644 Binary files a/cmake-build-debug/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.gcda and b/cmake-build-debug/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.gcda differ diff --git a/run-clang-format.py b/run-clang-format.py new file mode 100644 index 0000000000000000000000000000000000000000..395304e453c91bf0778396b75387395e8abec26c --- /dev/null +++ b/run-clang-format.py @@ -0,0 +1,379 @@ +#!/usr/bin/env python +"""A wrapper script around clang-format, suitable for linting multiple files +and to use for continuous integration. + +This is an alternative API for the clang-format command line. +It runs over multiple files and directories in parallel. +A diff output is produced and a sensible exit code is returned. + +""" + +from __future__ import print_function, unicode_literals + +import argparse +import codecs +import difflib +import fnmatch +import io +import errno +import multiprocessing +import os +import signal +import subprocess +import sys +import traceback + +from functools import partial + +try: + from subprocess import DEVNULL # py3k +except ImportError: + DEVNULL = open(os.devnull, "wb") + + +DEFAULT_EXTENSIONS = 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx' +DEFAULT_CLANG_FORMAT_IGNORE = '.clang-format-ignore' + + +class ExitStatus: + SUCCESS = 0 + DIFF = 1 + TROUBLE = 2 + +def excludes_from_file(ignore_file): + excludes = [] + try: + with io.open(ignore_file, 'r', encoding='utf-8') as f: + for line in f: + if line.startswith('#'): + # ignore comments + continue + pattern = line.rstrip() + if not pattern: + # allow empty lines + continue + excludes.append(pattern) + except EnvironmentError as e: + if e.errno != errno.ENOENT: + raise + return excludes; + +def list_files(files, recursive=False, extensions=None, exclude=None): + if extensions is None: + extensions = [] + if exclude is None: + exclude = [] + + out = [] + for file in files: + if recursive and os.path.isdir(file): + for dirpath, dnames, fnames in os.walk(file): + fpaths = [os.path.join(dirpath, fname) for fname in fnames] + for pattern in exclude: + # os.walk() supports trimming down the dnames list + # by modifying it in-place, + # to avoid unnecessary directory listings. + dnames[:] = [ + x for x in dnames + if + not fnmatch.fnmatch(os.path.join(dirpath, x), pattern) + ] + fpaths = [ + x for x in fpaths if not fnmatch.fnmatch(x, pattern) + ] + for f in fpaths: + ext = os.path.splitext(f)[1][1:] + if ext in extensions: + out.append(f) + else: + out.append(file) + return out + + +def make_diff(file, original, reformatted): + return list( + difflib.unified_diff( + original, + reformatted, + fromfile='{}\t(original)'.format(file), + tofile='{}\t(reformatted)'.format(file), + n=3)) + + +class DiffError(Exception): + def __init__(self, message, errs=None): + super(DiffError, self).__init__(message) + self.errs = errs or [] + + +class UnexpectedError(Exception): + def __init__(self, message, exc=None): + super(UnexpectedError, self).__init__(message) + self.formatted_traceback = traceback.format_exc() + self.exc = exc + + +def run_clang_format_diff_wrapper(args, file): + try: + ret = run_clang_format_diff(args, file) + return ret + except DiffError: + raise + except Exception as e: + raise UnexpectedError('{}: {}: {}'.format(file, e.__class__.__name__, + e), e) + + +def run_clang_format_diff(args, file): + try: + with io.open(file, 'r', encoding='utf-8') as f: + original = f.readlines() + except IOError as exc: + raise DiffError(str(exc)) + invocation = [args.clang_format_executable, file] + + # Use of utf-8 to decode the process output. + # + # Hopefully, this is the correct thing to do. + # + # It's done due to the following assumptions (which may be incorrect): + # - clang-format will returns the bytes read from the files as-is, + # without conversion, and it is already assumed that the files use utf-8. + # - if the diagnostics were internationalized, they would use utf-8: + # > Adding Translations to Clang + # > + # > Not possible yet! + # > Diagnostic strings should be written in UTF-8, + # > the client can translate to the relevant code page if needed. + # > Each translation completely replaces the format string + # > for the diagnostic. + # > -- http://clang.llvm.org/docs/InternalsManual.html#internals-diag-translation + # + # It's not pretty, due to Python 2 & 3 compatibility. + encoding_py3 = {} + if sys.version_info[0] >= 3: + encoding_py3['encoding'] = 'utf-8' + + try: + proc = subprocess.Popen( + invocation, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + **encoding_py3) + except OSError as exc: + raise DiffError( + "Command '{}' failed to start: {}".format( + subprocess.list2cmdline(invocation), exc + ) + ) + proc_stdout = proc.stdout + proc_stderr = proc.stderr + if sys.version_info[0] < 3: + # make the pipes compatible with Python 3, + # reading lines should output unicode + encoding = 'utf-8' + proc_stdout = codecs.getreader(encoding)(proc_stdout) + proc_stderr = codecs.getreader(encoding)(proc_stderr) + # hopefully the stderr pipe won't get full and block the process + outs = list(proc_stdout.readlines()) + errs = list(proc_stderr.readlines()) + proc.wait() + if proc.returncode: + raise DiffError( + "Command '{}' returned non-zero exit status {}".format( + subprocess.list2cmdline(invocation), proc.returncode + ), + errs, + ) + return make_diff(file, original, outs), errs + + +def bold_red(s): + return '\x1b[1m\x1b[31m' + s + '\x1b[0m' + + +def colorize(diff_lines): + def bold(s): + return '\x1b[1m' + s + '\x1b[0m' + + def cyan(s): + return '\x1b[36m' + s + '\x1b[0m' + + def green(s): + return '\x1b[32m' + s + '\x1b[0m' + + def red(s): + return '\x1b[31m' + s + '\x1b[0m' + + for line in diff_lines: + if line[:4] in ['--- ', '+++ ']: + yield bold(line) + elif line.startswith('@@ '): + yield cyan(line) + elif line.startswith('+'): + yield green(line) + elif line.startswith('-'): + yield red(line) + else: + yield line + + +def print_diff(diff_lines, use_color): + if use_color: + diff_lines = colorize(diff_lines) + if sys.version_info[0] < 3: + sys.stdout.writelines((l.encode('utf-8') for l in diff_lines)) + else: + sys.stdout.writelines(diff_lines) + + +def print_trouble(prog, message, use_colors): + error_text = 'error:' + if use_colors: + error_text = bold_red(error_text) + print("{}: {} {}".format(prog, error_text, message), file=sys.stderr) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + '--clang-format-executable', + metavar='EXECUTABLE', + help='path to the clang-format executable', + default='clang-format') + parser.add_argument( + '--extensions', + help='comma separated list of file extensions (default: {})'.format( + DEFAULT_EXTENSIONS), + default=DEFAULT_EXTENSIONS) + parser.add_argument( + '-r', + '--recursive', + action='store_true', + help='run recursively over directories') + parser.add_argument('files', metavar='file', nargs='+') + parser.add_argument( + '-q', + '--quiet', + action='store_true', + help="disable output, useful for the exit code") + parser.add_argument( + '-j', + metavar='N', + type=int, + default=0, + help='run N clang-format jobs in parallel' + ' (default number of cpus + 1)') + parser.add_argument( + '--color', + default='auto', + choices=['auto', 'always', 'never'], + help='show colored diff (default: auto)') + parser.add_argument( + '-e', + '--exclude', + metavar='PATTERN', + action='append', + default=[], + help='exclude paths matching the given glob-like pattern(s)' + ' from recursive search') + + args = parser.parse_args() + + # use default signal handling, like diff return SIGINT value on ^C + # https://bugs.python.org/issue14229#msg156446 + signal.signal(signal.SIGINT, signal.SIG_DFL) + try: + signal.SIGPIPE + except AttributeError: + # compatibility, SIGPIPE does not exist on Windows + pass + else: + signal.signal(signal.SIGPIPE, signal.SIG_DFL) + + colored_stdout = False + colored_stderr = False + if args.color == 'always': + colored_stdout = True + colored_stderr = True + elif args.color == 'auto': + colored_stdout = sys.stdout.isatty() + colored_stderr = sys.stderr.isatty() + + version_invocation = [args.clang_format_executable, str("--version")] + try: + subprocess.check_call(version_invocation, stdout=DEVNULL) + except subprocess.CalledProcessError as e: + print_trouble(parser.prog, str(e), use_colors=colored_stderr) + return ExitStatus.TROUBLE + except OSError as e: + print_trouble( + parser.prog, + "Command '{}' failed to start: {}".format( + subprocess.list2cmdline(version_invocation), e + ), + use_colors=colored_stderr, + ) + return ExitStatus.TROUBLE + + retcode = ExitStatus.SUCCESS + + excludes = excludes_from_file(DEFAULT_CLANG_FORMAT_IGNORE) + excludes.extend(args.exclude) + + files = list_files( + args.files, + recursive=args.recursive, + exclude=excludes, + extensions=args.extensions.split(',')) + + if not files: + return + + njobs = args.j + if njobs == 0: + njobs = multiprocessing.cpu_count() + 1 + njobs = min(len(files), njobs) + + if njobs == 1: + # execute directly instead of in a pool, + # less overhead, simpler stacktraces + it = (run_clang_format_diff_wrapper(args, file) for file in files) + pool = None + else: + pool = multiprocessing.Pool(njobs) + it = pool.imap_unordered( + partial(run_clang_format_diff_wrapper, args), files) + while True: + try: + outs, errs = next(it) + except StopIteration: + break + except DiffError as e: + print_trouble(parser.prog, str(e), use_colors=colored_stderr) + retcode = ExitStatus.TROUBLE + sys.stderr.writelines(e.errs) + except UnexpectedError as e: + print_trouble(parser.prog, str(e), use_colors=colored_stderr) + sys.stderr.write(e.formatted_traceback) + retcode = ExitStatus.TROUBLE + # stop at the first unexpected error, + # something could be very wrong, + # don't process all files unnecessarily + if pool: + pool.terminate() + break + else: + sys.stderr.writelines(errs) + if outs == []: + continue + if not args.quiet: + print_diff(outs, use_color=colored_stdout) + if retcode == ExitStatus.SUCCESS: + retcode = ExitStatus.DIFF + return retcode + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/ExampleTests.cpp b/tests/ExampleTests.cpp index c98708e3eec6a73582d5f988da020e73cd05568f..f87d646255bb4a887a61c2202764c41d8eab8e56 100644 --- a/tests/ExampleTests.cpp +++ b/tests/ExampleTests.cpp @@ -35,7 +35,42 @@ TEST(ExampleTests, test_read_and_count_words) { EXPECT_STREQ("foo", counter.words[2].word); EXPECT_EQ(2, counter.words[2].frequency); cleanup_frequency_counter(&counter); +} + +TEST(ExampleTests, test_read_and_count_words_case_insensitive) { + FrequencyCounter counter; + init_frequency_counter(&counter); + char cwd[1024]; + if (getcwd(cwd, sizeof(cwd)) != NULL) { + printf("Current working directory: %s\n", cwd); + } + read_and_count_words(&counter, "test.txt"); + EXPECT_EQ(3, counter.count); + EXPECT_STREQ("hello", counter.words[0].word); + EXPECT_EQ(3, counter.words[0].frequency); + EXPECT_STREQ("world", counter.words[1].word); + EXPECT_EQ(1, counter.words[1].frequency); + EXPECT_STREQ("foo", counter.words[2].word); + EXPECT_EQ(2, counter.words[2].frequency); + cleanup_frequency_counter(&counter); +} +TEST(ExampleTests, test_read_and_count_words_ponctuation) { + FrequencyCounter counter; + init_frequency_counter(&counter); + char cwd[1024]; + if (getcwd(cwd, sizeof(cwd)) != NULL) { + printf("Current working directory: %s\n", cwd); + } + read_and_count_words(&counter, "test.txt"); + EXPECT_EQ(3, counter.count); + EXPECT_STREQ("hello", counter.words[0].word); + EXPECT_EQ(3, counter.words[0].frequency); + EXPECT_STREQ("world", counter.words[1].word); + EXPECT_EQ(1, counter.words[1].frequency); + EXPECT_STREQ("foo", counter.words[2].word); + EXPECT_EQ(2, counter.words[2].frequency); + cleanup_frequency_counter(&counter); } TEST(ExampleTests, test_sort_words_by_frequency) { @@ -110,6 +145,8 @@ TEST(ExampleTests, test_compare_word_frequency_when_equal) { } + + //TEST(ExampleTests, test_read_and_count_ngrams) { // FrequencyCounter counter; // init_frequency_counter(&counter); @@ -131,6 +168,7 @@ TEST(ExampleTests, test_mode_interactive) { + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/tests/test.txt b/tests/test.txt index e3a9a818792a47bdb1414cc8b66b7a35e6bef23c..24e4e2e3eff4cc609e93898064116825ca4726c4 100644 --- a/tests/test.txt +++ b/tests/test.txt @@ -1 +1 @@ -hello world, foo .hello hello foo \ No newline at end of file +hello, world, foo .Hello HELLO foo. \ No newline at end of file