Skip to content

About

Simple particle physics class with googleTest for a private method unit test

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Particle class (simple particle physics helper)

This small project provides a Particle class with common particle properties used in particle physics simulations and analysis:

  • name (string)
  • PDG id (int)
  • mass (GeV/c^2)
  • charge (in units of e)
  • spin
  • lifetime (s)
  • four-momentum (px,py,pz,E) with helpers to set momentum and compute energy

Build (requires CMake and a C++17 toolchain):

mkdir -p build && cd build
cmake ..
cmake --build . -- -j2
./test_particle

The test_particle program creates an electron and a proton with simple momenta and prints properties.

Testing (GoogleTest)

This project integrates GoogleTest using CMake's FetchContent in CMakeLists.txt. The test executable particle_gtest is built and discovered via CTest.

To run tests locally:

cd build
ctest --verbose
# or run the binary directly:
./particle_gtest

Private-method unit testing and BUILD_TESTING

Design intent:

  • FourMomentum::invariantMass() is an implementation detail of the FourMomentum struct and has been made private to encourage use of the higher-level Particle API (Particle::invariantMass()). The Particle class is declared as a friend so it can call the private method internally.
  • Particle::willCollapseWith(...) is a private helper that contains simple logic to predict whether two particles would "collapse" (annihilate) — by default it checks whether two PDG IDs are negatives of each other (particle vs anti-particle). This logic is kept private to avoid exposing internal heuristics as part of the public API.

Testing private behavior:

  • We want unit tests to exercise the private willCollapseWith method without making it part of the production public API. To do this safely, the code provides a small test-only public wrapper called test_willCollapseWith(const Particle&) that simply delegates to the private method.
  • That wrapper is only compiled into the particle_gtest binary: CMake defines a compile-time macro BUILD_TESTING for the particle_gtest target. The wrapper is guarded with #if defined(BUILD_TESTING) in Particle.h so it does not appear in normal library builds.

Why this approach:

  • It keeps the production API clean while still allowing thorough unit testing of internal logic.
  • Alternatives would be using FRIEND_TEST from GoogleTest to friend a specific test, or making the method public. The chosen approach avoids polluting the public API and is explicit about the test-only nature of the accessor.

How to enable/see the test-only accessor in the build

  • The CMakeLists.txt already sets target_compile_definitions(particle_gtest PRIVATE BUILD_TESTING) so the test-only wrapper is compiled into the test binary. If you add other test targets and want them to access the wrapper, define BUILD_TESTING for those targets as well.

Example test behaviors added in test/test_particle_gtest.cpp:

  • ParticleBasic.EnergyFromMomentum — checks energy computed from momentum and mass.
  • ParticleSetMomentum.UpdatesEnergy — checks setMomentum updates energy and momentum magnitude.
  • FourMomentum.InvariantMassCalculation — verifies invariant mass via the Particle wrapper.
  • ParticlePrivateTest.WillCollapseWith — exercises the test-only test_willCollapseWith wrapper to assert that particle vs antiparticle returns true and identical particles return false.

If you prefer a different testing strategy (e.g., FRIEND_TEST or making the helper public in a dedicated detail namespace), tell me which approach you'd like and I can refactor accordingly.

About

Simple particle physics class with googleTest for a private method unit test

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages