Skip to content

C UNIT TESTING!!! #3475

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

Closed
wants to merge 2 commits into from
Closed
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
87 changes: 87 additions & 0 deletions .github/workflows/build-ubuntu-ctest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# This workflow generates C code tests
# Note that for intrinsics, it only runs what gets compiled
# and would naturally run. It also is limited to what can run in
# a CI environment
# IMPORTANT: binaries are not to be uploaded from this workflow!

name: Ubuntu ctest

# Run CI only on changes to main branch, or any PR to main.
# Do not run CI on any other branch. Also, skip any non-source changes
# from running on CI
on:
push:
branches: main
paths-ignore:
- 'docs/**'
- 'examples/**'
- '.gitignore'
- '*.rst'
- '*.md'
- '.github/workflows/*.yml'
# only care about c code that changes
- 'src_py/**'
# re-include current file to not be excluded
- '!.github/workflows/build-ubuntu-ctest.yml'

pull_request:
branches: main
paths-ignore:
- 'docs/**'
- 'examples/**'
- '.gitignore'
- '*.rst'
- '*.md'
- '.github/workflows/*.yml'
# only care about c code that changes
- 'src_py/**'
# re-include current file to not be excluded
- '!.github/workflows/build-ubuntu-ctest.yml'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-ubuntu-ctest
cancel-in-progress: true

jobs:
gen_coverage:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false # if a particular matrix build fails, don't skip the rest
matrix:
os: [ubuntu-24.04]

env:
# Pip now forces us to either make a venv or set this flag, so we will do
# this
PIP_BREAK_SYSTEM_PACKAGES: 1
# We are using dependencies installed from apt
PG_DEPS_FROM_SYSTEM: 1

steps:
- uses: actions/[email protected]

- name: Install deps
# https://github.com/actions/runner-images/issues/7192
# https://github.com/orgs/community/discussions/47863
run: |
sudo apt-get update --fix-missing
sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libfreetype6-dev libportmidi-dev python3-dev -y

- name: Build with the ctest suite
id: build
run: |
python3 -m pip install . -Csetup-args="-Dctest=true" --break-system-packages

- name: Run tests
env:
SDL_VIDEODRIVER: "dummy"
SDL_AUDIODRIVER: "disk"
run: python3 -m pygame.tests -v --exclude opengl,music,timing --time_out 300

# We upload the generated files under github actions assets
- name: Upload ctest log
if: !cancelled()
uses: actions/upload-artifact@v4
with:
name: ctest_suite_log
path: ctest.log
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dist
__pycache__
_headers/*
buildconfig/win_dll_dirs.json
*.log

# cython generated files
src_c/_sdl2/*.c
Expand Down
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ repos:
| ^.*\.svg$
| ^.*\.sfd$
| docs/LGPL.txt
| subprojects/.*
)$
- id: trailing-whitespace
exclude: |
Expand All @@ -23,6 +24,7 @@ repos:
| ^.*\.svg$
| ^.*\.sfd$
| docs/LGPL.txt
| subprojects/.*
)$

- repo: https://github.com/astral-sh/ruff-pre-commit
Expand All @@ -47,4 +49,5 @@ repos:
| src_c/include/sse2neon.h
| src_c/include/pythoncapi_compat.h
| src_c/pypm.c
| subprojects/.*
)$
22 changes: 22 additions & 0 deletions ctest/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
unity_subproject = subproject('unity')
unity_dependency = unity_subproject.get_variable('unity_dep')

test_sources = files(
['../src_c/base.c',
'test_base.c']
)

inc = include_directories('../src_c')

test = executable('run_ctests',
sources: [
test_sources,
],
include_directories: [inc],
dependencies: [unity_dependency, py_dep, sdl_dep],
install: true,
install_dir: pg_dir
)

# test('test', test,
# should_fail: false)
82 changes: 82 additions & 0 deletions ctest/test_base.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "base.h"
#include "unity.h"

PyObject *base_module;

void setUp(void) {}

void tearDown(void) {}

/**
* @brief Tests _pg_is_int_tuple when passed a tuple of ints
*/
void test__pg_is_int_tuple_nominal(void) {
PyObject *arg1 = Py_BuildValue("(iii)", 1, 2, 3);
PyObject *arg2 = Py_BuildValue("(iii)", -1, -2, -3);
PyObject *arg3 = Py_BuildValue("(iii)", 1, -2, -3);

TEST_ASSERT_EQUAL(1, _pg_is_int_tuple(arg1));
TEST_ASSERT_EQUAL(1, _pg_is_int_tuple(arg2));
TEST_ASSERT_EQUAL(1, _pg_is_int_tuple(arg3));
}

/**
* @brief Tests _pg_is_int_tuple when passed a tuple of non-numeric values
*/
void test__pg_is_int_tuple_failureModes(void) {
PyObject *arg1 =
Py_BuildValue("(sss)", (char *)"Larry", (char *)"Moe", (char *)"Curly");
PyObject *arg2 = Py_BuildValue("(sss)", (char *)NULL, (char *)NULL,
(char *)NULL); // tuple of None's
PyObject *arg3 = Py_BuildValue("(OOO)", arg1, arg2, arg1);

TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg1));
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg2));
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg3));
}

/**
* @brief Tests _pg_is_int_tuple when passed a tuple of floats
*/
void test__pg_is_int_tuple_floats(void) {
PyObject *arg1 = Py_BuildValue("(ddd)", 1.0, 2.0, 3.0);
PyObject *arg2 = Py_BuildValue("(ddd)", -1.1, -2.2, -3.3);
PyObject *arg3 = Py_BuildValue("(ddd)", 1.0, -2.0, -3.1);

TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg1));
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg2));
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg3));
}

#define RUN_TEST(TestFunc, TestLineNum) \
{ \
Unity.CurrentTestName = #TestFunc; \
Unity.CurrentTestLineNumber = TestLineNum; \
Unity.NumberOfTests++; \
if (TEST_PROTECT()) { \
setUp(); \
TestFunc(); \
} \
if (TEST_PROTECT()) { \
tearDown(); \
} \
UnityConcludeTest(); \
}

/*=======Test Reset Option=====*/
void resetTest(void) {
tearDown();
setUp();
}

#undef main
/*=======MAIN=====*/
int main(void) {
Py_Initialize();
UnityBegin("test_base.c");
RUN_TEST(test__pg_is_int_tuple_nominal, 17);
RUN_TEST(test__pg_is_int_tuple_failureModes, 31);
RUN_TEST(test__pg_is_int_tuple_floats, 45);

return (UnityEnd());
}
11 changes: 11 additions & 0 deletions dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
]
COVERAGE_ARGS = ["-Csetup-args=-Dcoverage=true"]

CTEST_ARGS = ["-Csetup-args=-Dctest=true"]

# We assume this script works with any pip version above this.
PIP_MIN_VERSION = "23.1"

Expand Down Expand Up @@ -208,6 +210,7 @@ def cmd_build(self):
lax = self.args.get("lax", False)
sdl3 = self.args.get("sdl3", False)
coverage = self.args.get("coverage", False)
ctest = self.args.get("ctest", False)
if wheel_dir and coverage:
pprint("Cannot pass --wheel and --coverage together", Colors.RED)
sys.exit(1)
Expand All @@ -221,6 +224,8 @@ def cmd_build(self):
build_suffix += "-sdl3"
if coverage:
build_suffix += "-cov"
if ctest:
build_suffix += "-ctest"
install_args = [
"--no-build-isolation",
f"-Cbuild-dir=.mesonpy-build{build_suffix}",
Expand All @@ -245,6 +250,9 @@ def cmd_build(self):
if coverage:
install_args.extend(COVERAGE_ARGS)

if ctest:
install_args.extend(CTEST_ARGS)

info_str = f"with {debug=}, {lax=}, {sdl3=}, and {coverage=}"
if wheel_dir:
pprint(f"Building wheel at '{wheel_dir}' ({info_str})")
Expand Down Expand Up @@ -376,6 +384,9 @@ def parse_args(self):
"supported if the underlying compiler supports the --coverage argument"
),
)
build_parser.add_argument(
"--ctest", action="store_true", help="Build the C-direct unit tests"
)

# Docs command
docs_parser = subparsers.add_parser("docs", help="Generate docs")
Expand Down
5 changes: 5 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,9 @@ if not get_option('stripped')
subdir('buildconfig/stubs')
install_subdir('examples', install_dir: pg_dir, install_tag: 'pg-tag')
# TODO: install headers? not really important though

if get_option('ctest')
subproject('unity')
subdir('ctest')
endif
endif
10 changes: 7 additions & 3 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ option('midi', type: 'feature', value: 'enabled')
# Controls whether to make a "stripped" pygame install. Enabling this disables
# the bundling of docs/examples/tests/stubs in the wheels.
# The default behaviour is to bundle all of these.
option('stripped', type: 'boolean', value: 'false')
option('stripped', type: 'boolean', value: false)

# Controls whether to compile with -Werror (or its msvc equivalent). The default
# behaviour is to not do this by default
option('error_on_warns', type: 'boolean', value: 'false')
option('error_on_warns', type: 'boolean', value: false)

# Controls whether to error on build if generated docs are missing. Defaults to
# false.
option('error_docs_missing', type: 'boolean', value: 'false')
option('error_docs_missing', type: 'boolean', value: false)

# Controls whether to do a coverage build.
# This argument must be used together with the editable install.
option('coverage', type: 'boolean', value: false)

# Controls whether to do to a C unit test build. Defaults to false.
# If "stripped" is true, this is ignored.
option('ctest', type: 'boolean', value: false)

# Controls whether to use SDL3 instead of SDL2. The default is to use SDL2
option('sdl_api', type: 'integer', min: 2, max: 3, value: 2)
Loading
Loading