Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force run pre-commit #110

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions codebasin/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

import os
from pathlib import Path
from typing import Union


def is_source_file(filename: Union[str, os.PathLike]) -> bool:
def is_source_file(filename: str | os.PathLike) -> bool:
"""
Parameters
----------
Expand Down
12 changes: 7 additions & 5 deletions tests/comments/test_comments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright (C) 2019 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

import unittest
import logging
import os
from codebasin import preprocessor, file_parser
import unittest

from codebasin import file_parser


class TestExampleFortranFile(unittest.TestCase):
Expand All @@ -27,11 +27,13 @@ class TestExampleCFile(unittest.TestCase):

def test_c_comments(self):
rootdir = "./tests/comments/"
parser = file_parser.FileParser(os.path.join(rootdir, "continuation.cpp"))
parser = file_parser.FileParser(
os.path.join(rootdir, "continuation.cpp"),
)

tree = parser.parse_file()
self.assertEqual(tree.root.total_sloc, 25)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
48 changes: 16 additions & 32 deletions tests/compilers/test_compilers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Copyright (C) 2019-2024 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

import unittest
import logging
import unittest

from codebasin import config


class TestCompilers(unittest.TestCase):
"""
Test that a subset of common compilers and their arguments are recognized
Expand All @@ -16,41 +18,29 @@ def setUp(self):

def test_clang(self):
"""compilers/clang"""
args = [
"clang",
"-fsycl-is-device",
"test.cpp"
]
args = ["clang", "-fsycl-is-device", "test.cpp"]

compiler = config.recognize_compiler(args)
self.assertTrue(type(compiler) is config.ClangCompiler)

passes = compiler.get_passes()
self.assertEqual(passes, set(["default"]))
self.assertEqual(passes, {"default"})

self.assertTrue(compiler.has_implicit_behavior("default"))
defines = compiler.get_defines("default")
self.assertEqual(defines, ["__SYCL_DEVICE_ONLY__"])

def test_intel_sycl(self):
"""compilers/intel_sycl"""
args = [
"icpx",
"-fsycl",
"test.cpp"
]
args = ["icpx", "-fsycl", "test.cpp"]

compiler = config.recognize_compiler(args)
self.assertTrue(type(compiler) is config.IntelCompiler)

passes = compiler.get_passes()
self.assertEqual(passes, set(["default", "spir64"]))
self.assertEqual(passes, {"default", "spir64"})

expected = [
"__SYCL_DEVICE_ONLY__",
"__SPIR__",
"__SPIRV__"
]
expected = ["__SYCL_DEVICE_ONLY__", "__SPIR__", "__SPIRV__"]
self.assertTrue(compiler.has_implicit_behavior("spir64"))
defines = compiler.get_defines("spir64")
self.assertEqual(defines, expected)
Expand All @@ -62,24 +52,20 @@ def test_intel_targets(self):
"-fsycl",
"-fsycl-targets=spir64,x86_64",
"-fopenmp",
"test.cpp"
"test.cpp",
]

compiler = config.recognize_compiler(args)
self.assertTrue(type(compiler) is config.IntelCompiler)

passes = compiler.get_passes()
self.assertEqual(passes, set(["default", "spir64", "x86_64"]))
self.assertEqual(passes, {"default", "spir64", "x86_64"})

self.assertTrue(compiler.has_implicit_behavior("default"))
defines = compiler.get_defines("default")
self.assertEqual(defines, ["_OPENMP"])

expected = [
"__SYCL_DEVICE_ONLY__",
"__SPIR__",
"__SPIRV__"
]
expected = ["__SYCL_DEVICE_ONLY__", "__SPIR__", "__SPIRV__"]
self.assertTrue(compiler.has_implicit_behavior("spir64"))
defines = compiler.get_defines("spir64")
self.assertEqual(defines, expected)
Expand All @@ -93,19 +79,16 @@ def test_nvcc(self):
"nvcc",
"--gpu-architecture=compute_50",
"--gpu-code=compute_50,sm_50,sm_52",
"test.cpp"
"test.cpp",
]

compiler = config.recognize_compiler(args)
self.assertTrue(type(compiler) is config.NvccCompiler)

passes = compiler.get_passes()
self.assertEqual(passes, set(["default", "50", "52"]))
self.assertEqual(passes, {"default", "50", "52"})

defaults = [
"__NVCC__",
"__CUDACC__"
]
defaults = ["__NVCC__", "__CUDACC__"]
self.assertTrue(compiler.has_implicit_behavior("default"))
defines = compiler.get_defines("default")
self.assertEqual(defines, defaults)
Expand All @@ -118,5 +101,6 @@ def test_nvcc(self):
defines = compiler.get_defines("52")
self.assertEqual(defines, defaults + ["__CUDA_ARCH__=520"])

if __name__ == '__main__':

if __name__ == "__main__":
unittest.main()
9 changes: 7 additions & 2 deletions tests/failure/test_bignum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: BSD-3-Clause

import unittest

from codebasin import preprocessor


Expand All @@ -13,13 +14,17 @@ class TestBigNum(unittest.TestCase):
def test_oversized_constant(self):
"""oversized constant"""
with self.assertRaises(OverflowError):
tokens = preprocessor.Lexer("10000000000000000000000000000000000000").tokenize()
tokens = preprocessor.Lexer(
"10000000000000000000000000000000000000",
).tokenize()
preprocessor.ExpressionEvaluator(tokens).evaluate()

def test_overflow(self):
"""integer overflow"""
with self.assertRaises(OverflowError):
tokens = preprocessor.Lexer("0xFFFFFFFFFFFFFFFF * 0xFFFFFFFFFFFFFFFF").tokenize()
tokens = preprocessor.Lexer(
"0xFFFFFFFFFFFFFFFF * 0xFFFFFFFFFFFFFFFF",
).tokenize()
preprocessor.ExpressionEvaluator(tokens).evaluate()

def test_power(self):
Expand Down
62 changes: 53 additions & 9 deletions tests/lexer/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: BSD-3-Clause

import unittest

from codebasin import preprocessor


Expand All @@ -18,28 +19,56 @@ def test_character(self):

def test_numerical(self):
"""numbers"""
numbers = ["123", "123ul", "123.4", "123.4e+05", ".123", "0xFF", "0b10"]
numbers = [
"123",
"123ul",
"123.4",
"123.4e+05",
".123",
"0xFF",
"0b10",
]
for number in numbers:
tokens = preprocessor.Lexer(number).tokenize()
self.assertTrue(len(tokens) == 1)
self.assertTrue(isinstance(tokens[0], preprocessor.NumericalConstant))
self.assertTrue(
isinstance(tokens[0], preprocessor.NumericalConstant),
)

def test_string(self):
"""strings"""
tokens = preprocessor.Lexer("\"this is a string constant\"").tokenize()
tokens = preprocessor.Lexer('"this is a string constant"').tokenize()
self.assertTrue(len(tokens) == 1)
self.assertTrue(isinstance(tokens[0], preprocessor.StringConstant))

def test_identifier(self):
"""identifiers"""
tokens = preprocessor.Lexer("this is a string of words").tokenize()
self.assertTrue(len(tokens) == 6)
self.assertTrue(all([isinstance(t, preprocessor.Identifier) for t in tokens]))
self.assertTrue(
all([isinstance(t, preprocessor.Identifier) for t in tokens]),
)

def test_operator(self):
"""operators"""
operators = ["||", "&&", ">>", "<<", "!=", ">=", "<=", "==", "##"] + \
["-", "+", "!", "*", "/", "|", "&", "^", "<", ">", "?", ":", "~", "#", "=", "%"]
operators = ["||", "&&", ">>", "<<", "!=", ">=", "<=", "==", "##"] + [
"-",
"+",
"!",
"*",
"/",
"|",
"&",
"^",
"<",
">",
"?",
":",
"~",
"#",
"=",
"%",
]
for op in operators:
tokens = preprocessor.Lexer(op).tokenize()
self.assertTrue(len(tokens) == 1)
Expand All @@ -48,7 +77,20 @@ def test_operator(self):

def test_puncuator(self):
"""punctuators"""
punctuators = ["(", ")", "{", "}", "[", "]", ",", ".", ";", "'", "\"", "\\"]
punctuators = [
"(",
")",
"{",
"}",
"[",
"]",
",",
".",
";",
"'",
'"',
"\\",
]
for punc in punctuators:
tokens = preprocessor.Lexer(punc).tokenize()
self.assertTrue(len(tokens) == 1)
Expand All @@ -70,7 +112,9 @@ def test_expression(self):
self.assertTrue(isinstance(tokens[8], preprocessor.Operator))
self.assertTrue(isinstance(tokens[9], preprocessor.CharacterConstant))

tokens = preprocessor.Lexer("a > b ? \"true_string\" : \"false_string\"").tokenize()
tokens = preprocessor.Lexer(
'a > b ? "true_string" : "false_string"',
).tokenize()
self.assertTrue(len(tokens) == 7)
self.assertTrue(isinstance(tokens[0], preprocessor.Identifier))
self.assertTrue(isinstance(tokens[1], preprocessor.Operator))
Expand All @@ -81,5 +125,5 @@ def test_expression(self):
self.assertTrue(isinstance(tokens[6], preprocessor.StringConstant))


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
15 changes: 11 additions & 4 deletions tests/parsers/test_directive_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: BSD-3-Clause

import unittest

from codebasin import preprocessor


Expand Down Expand Up @@ -68,13 +69,17 @@ def test_undef(self):

def test_include(self):
"""include"""
tokens = preprocessor.Lexer("#include <path/to/system/header>").tokenize()
tokens = preprocessor.Lexer(
"#include <path/to/system/header>",
).tokenize()
node = preprocessor.DirectiveParser(tokens).parse()
self.assertTrue(isinstance(node, preprocessor.IncludeNode))
self.assertTrue(isinstance(node.value, preprocessor.IncludePath))
self.assertTrue(node.value.system)

tokens = preprocessor.Lexer("#include \"path/to/local/header\"").tokenize()
tokens = preprocessor.Lexer(
'#include "path/to/local/header"',
).tokenize()
node = preprocessor.DirectiveParser(tokens).parse()
self.assertTrue(isinstance(node, preprocessor.IncludeNode))
self.assertTrue(isinstance(node.value, preprocessor.IncludePath))
Expand Down Expand Up @@ -143,8 +148,10 @@ def test_unsupported(self):
for directive in ["#line", "#warning", "#error"]:
tokens = preprocessor.Lexer(directive).tokenize()
node = preprocessor.DirectiveParser(tokens).parse()
self.assertTrue(isinstance(node, preprocessor.UnrecognizedDirectiveNode))
self.assertTrue(
isinstance(node, preprocessor.UnrecognizedDirectiveNode),
)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
8 changes: 4 additions & 4 deletions tests/safe_write/test_safe_write.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright (C) 2019 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

import unittest
import tempfile
import shutil
import os
import shutil
import tempfile
import unittest

from codebasin import util

Expand Down Expand Up @@ -76,5 +76,5 @@ def test_create(self):
self.assertEqual(st.st_mode & 0o111, 0)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion tests/valid_path/test_valid_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ def test_line_feed(self):
self.assertFalse(util.valid_path("/invalid/\n/path/"))


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading