Skip to content
Open
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
41 changes: 41 additions & 0 deletions QAG-recordpropulsuon.pynb
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,47 @@
"metadata": {}
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "unittest-hydrogen-coupled-saw"
},
"source": [
"import numpy as np\n",
"\n",
"def test_hydrogen_coupled_saw_propulsion():\n",
" print(\"Running UNIT TESTS for hydrogen_coupled_saw_propulsion...\")\n",
" \n",
" # Test scalar input (or single element array)\n",
" t_single = np.array([0.0])\n",
" res_single = hydrogen_coupled_saw_propulsion(t_single)\n",
" # sin(0) is 0, so result should be 0\n",
" assert np.isclose(res_single[0], 0.0), f\"Expected 0.0 at t=0, got {res_single[0]}\"\n",
" \n",
" # Test array input\n",
" t_arr = np.linspace(0, 10, 100)\n",
" res_arr = hydrogen_coupled_saw_propulsion(t_arr)\n",
" assert len(res_arr) == 100, \"Result array length mismatch\"\n",
" \n",
" # Test known value calculation\n",
" # psychon_mass_ug = 5400\n",
" # golden_freq_mhz = 0.70\n",
" # omega = 2 * np.pi * 0.70\n",
" # t = 1.0\n",
" # expected = 5400 * np.sin(omega * 1.0) * np.exp(0.05 * 1.0)\n",
" t_val = np.array([1.0])\n",
" res_val = hydrogen_coupled_saw_propulsion(t_val)\n",
" expected_val = 5400 * np.sin(2 * np.pi * 0.70 * 1.0) * np.exp(0.05 * 1.0)\n",
" assert np.isclose(res_val[0], expected_val), f\"Value mismatch at t=1.0: {res_val[0]} != {expected_val}\"\n",
Comment on lines +698 to +699
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test for a known value currently re-implements the logic from the hydrogen_coupled_saw_propulsion function. This is a testing anti-pattern, as a logical error in the original function could be replicated in the test, causing the test to pass incorrectly. It also duplicates magic numbers (5400, 0.70, 0.05), which makes the code harder to maintain. Tests are more robust when they validate against a pre-calculated, known-good value. This ensures the test is independent of the implementation.

    expected_val = -4590.54080255  # Pre-calculated value for t=1.0
    assert np.isclose(res_val[0], expected_val), f"Value mismatch at t=1.0: got {res_val[0]}, expected {expected_val}"

" \n",
" print(\"ALL UNIT TESTS PASSED!\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" test_hydrogen_coupled_saw_propulsion()"
Comment on lines +676 to +704
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Placing multiple independent tests within a single test function can be problematic. If an early assertion fails, the rest of the function is skipped, and subsequent tests are not executed. This can mask other failures. To improve test isolation and ensure all cases are checked, it's better to separate each logical test into its own function (e.g., test_zero_input, test_vectorization, test_known_value). Using a framework like pytest would automatically discover and run these separate tests, providing a more complete picture of the function's correctness.

],
"execution_count": null,
"outputs": []
}
]
}