Quantum Phenomena Explorer 7.9.8
Interactive quantum-phenomena Explorer Tutor — source documentation
Loading...
Searching...
No Matches
potentialscene2d_tests.cpp
Go to the documentation of this file.
1/**
2 * @file potentialscene2d_tests.cpp
3 * @brief Implements regression and invariant tests for potentialscene2d tests.
4 * @ingroup qpe_tests
5 *
6 * Source: @c tests/potentialscene2d_tests.cpp. This file is part of Quantum Phenomena Explorer (QPE),
7 * maintained by Officina Turini. See LICENSE_IT.txt / LICENSE.txt for the
8 * QPE freeware source-available terms and THIRD_PARTY_NOTICES.txt for
9 * third-party components.
10 */
11
13
16
17#include <cmath>
18#include <sstream>
19
20namespace qpe {
21namespace tests {
22namespace {
23
24/**
25 * @brief Performs near.
26 * @param a The a value used by the operation.
27 * @param b The b value used by the operation.
28 * @param eps The eps value used by the operation.
29 */
30bool near(float a, float b, float eps = 1.0e-5f)
31{
32 return std::fabs(a - b) <= eps;
33}
34
35/**
36 * @brief Performs fail.
37 * @param error Output diagnostic text on failure.
38 * @param message The message value used by the operation.
39 */
40bool fail(std::string &error, const char *message)
41{
42 error = message;
43 return false;
44}
45
46} // namespace
47
48/**
49 * @brief Runs potential scene2 d tests.
50 * @param errorMessage The error message value used by the operation.
51 */
52bool runPotentialScene2DTests(std::string &errorMessage)
53{
54 errorMessage.clear();
55
56 PotentialScene2D scene;
59 a.value = 2.0f;
60 a.centerX = 0.0f;
61 a.centerY = 0.0f;
62 a.width = 4.0f;
63 a.height = 4.0f;
64 const std::uint64_t idA = scene.add(a);
65 if (idA == 0 || scene.size() != 1) {
66 return fail(errorMessage, "PotentialScene2D: add failed");
67 }
68
70 b.value = 3.0f;
71 b.centerX = 1.0f;
72 const std::uint64_t idB = scene.add(b);
73 if (idB == 0 || idB == idA || scene.size() != 2) {
74 return fail(errorMessage, "PotentialScene2D: id allocation failed");
75 }
76
77 PotentialField2D field(16, 16, 1.0f, 1.0f);
78 scene.rasterize(field);
79
80 // x=0.5,y=0.5 is inside both rectangles on this even grid.
81 if (!near(field(8, 8), 5.0f)) {
82 std::ostringstream os;
83 os << "PotentialScene2D: additive overlap expected 5, got " << field(8, 8);
84 errorMessage = os.str();
85 return false;
86 }
87
88 PotentialObject2D *objB = scene.find(idB);
89 if (!objB) {
90 return fail(errorMessage, "PotentialScene2D: find failed");
91 }
92 objB->enabled = false;
93 scene.rasterize(field);
94 if (!near(field(8, 8), 2.0f)) {
95 return fail(errorMessage, "PotentialScene2D: disabled object was rasterized");
96 }
97
98 PotentialObject2D changed = a;
99 changed.value = 7.0f;
100 if (!scene.update(idA, changed)) {
101 return fail(errorMessage, "PotentialScene2D: update failed");
102 }
103 scene.rasterize(field);
104 if (!near(field(8, 8), 7.0f)) {
105 return fail(errorMessage, "PotentialScene2D: updated object not applied");
106 }
107
108 if (!scene.remove(idB) || scene.size() != 1) {
109 return fail(errorMessage, "PotentialScene2D: remove failed");
110 }
111
112 scene.clear();
113 PotentialObject2D gaussian;
115 gaussian.value = -4.0f;
116 gaussian.sigmaX = 1.0f;
117 gaussian.sigmaY = 1.0f;
118 scene.add(gaussian);
119 PotentialObject2D harmonic;
121 harmonic.value = 1.0f;
122 harmonic.curvatureX = 0.0f;
123 harmonic.curvatureY = 0.0f;
124 scene.add(harmonic);
125 PotentialField2D smoothField(101, 101, 0.1f, 0.1f);
126 scene.rasterize(smoothField);
127 if (!near(smoothField(50, 50), -3.0f, 1.0e-4f)) {
128 return fail(errorMessage, "PotentialScene2D: smooth additive composition failed");
129 }
130
131 PotentialObject2D invalid;
133 invalid.radius = 0.0f;
134 if (scene.add(invalid) != 0) {
135 return fail(errorMessage, "PotentialScene2D: invalid object accepted");
136 }
137
138 return true;
139}
140
141} // namespace tests
142} // namespace qpe
Implements the Potential Field2D component.
Implements the Potential Scene2D component.
std::uint64_t add(PotentialObject2D object)
Performs add.
void rasterize(PotentialField2D &target) const
Performs rasterize.
std::size_t size() const noexcept
Returns size.
PotentialObject2D * find(std::uint64_t id) noexcept
Performs find.
bool update(std::uint64_t id, const PotentialObject2D &object)
Performs update.
bool remove(std::uint64_t id)
Performs remove.
void clear() noexcept
Performs clear.
bool runPotentialScene2DTests(std::string &errorMessage)
Runs potential scene2 d tests.
Declares the potentialfield2d physics/numerical component used by QPE experiments.
Declares the potentialscene2d physics/numerical component used by QPE experiments.
Declares regression and invariant tests for potentialscene2d tests.
Data structure used by the Potential Object2D component.
PotentialObjectType2D type