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_particleThe test_particle program creates an electron and a proton with simple momenta and prints properties.
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_gtestDesign intent:
FourMomentum::invariantMass()is an implementation detail of theFourMomentumstruct and has been made private to encourage use of the higher-levelParticleAPI (Particle::invariantMass()). TheParticleclass 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
willCollapseWithmethod without making it part of the production public API. To do this safely, the code provides a small test-only public wrapper calledtest_willCollapseWith(const Particle&)that simply delegates to the private method. - That wrapper is only compiled into the
particle_gtestbinary: CMake defines a compile-time macroBUILD_TESTINGfor theparticle_gtesttarget. The wrapper is guarded with#if defined(BUILD_TESTING)inParticle.hso 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_TESTfrom 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.txtalready setstarget_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, defineBUILD_TESTINGfor 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— checkssetMomentumupdates energy and momentum magnitude.FourMomentum.InvariantMassCalculation— verifies invariant mass via theParticlewrapper.ParticlePrivateTest.WillCollapseWith— exercises the test-onlytest_willCollapseWithwrapper 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.