add GTest infrastructure and port first mathematical tests from Java
- Add Google Test via CMake FetchContent (v1.14.0) - Add clausen.hpp: Clausen integral, Lobachevsky function, Im(Li2) - Add hyper_ideal_utility.hpp: generalized and ideal-vertex hyperbolic tetrahedron volume formulas using Eigen for the 4x4 Gram determinant - Port ClausenTest (5 tests) and HyperIdealUtilityTest (8 tests) from Java/JUnit — all 13 pass with same tolerances as the Java originals - Fix pre-existing VIEWER/viewer case mismatch in CMakeLists.txt Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17
code/tests/CMakeLists.txt
Normal file
17
code/tests/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
add_executable(conformallab_tests
|
||||
test_clausen.cpp
|
||||
test_hyper_ideal_utility.cpp
|
||||
)
|
||||
|
||||
target_include_directories(conformallab_tests SYSTEM PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/deps/eigen-3.4.0
|
||||
)
|
||||
|
||||
target_include_directories(conformallab_tests PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
target_link_libraries(conformallab_tests PRIVATE GTest::gtest_main)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(conformallab_tests DISCOVERY_TIMEOUT 60)
|
||||
35
code/tests/test_clausen.cpp
Normal file
35
code/tests/test_clausen.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// Port of de.varylab.discreteconformal.functional.ClausenTest (Java/JUnit).
|
||||
// Reference values computed with Mathematica.
|
||||
|
||||
#include "clausen.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <complex>
|
||||
|
||||
using conformallab::ImLi2;
|
||||
using C = std::complex<double>;
|
||||
|
||||
TEST(ClausenTest, ImLi2_case1) {
|
||||
double r = ImLi2(C(0.5, 0.5));
|
||||
EXPECT_NEAR(0.643767332889268748742017, r, 1e-15);
|
||||
}
|
||||
|
||||
TEST(ClausenTest, ImLi2_case2) {
|
||||
double r = ImLi2(C(1.234, -3.554));
|
||||
EXPECT_NEAR(-2.784709023190200241929031, r, 1e-15);
|
||||
}
|
||||
|
||||
TEST(ClausenTest, ImLi2_case3) {
|
||||
double r = ImLi2(C(-4.254, 2.965));
|
||||
EXPECT_NEAR(1.104092479314665907914598, r, 1e-15);
|
||||
}
|
||||
|
||||
TEST(ClausenTest, ImLi2_case4) {
|
||||
double r = ImLi2(C(-2.264, -1.05));
|
||||
EXPECT_NEAR(-0.540683148040955780529547, r, 1e-15);
|
||||
}
|
||||
|
||||
TEST(ClausenTest, ImLi2_case5) {
|
||||
double r = ImLi2(C(-4.0, 2.0));
|
||||
EXPECT_NEAR(0.7855694340809751306351005, r, 1e-15);
|
||||
}
|
||||
92
code/tests/test_hyper_ideal_utility.cpp
Normal file
92
code/tests/test_hyper_ideal_utility.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
// Port of de.varylab.discreteconformal.functional.HyperIdealUtilityTest (Java/JUnit).
|
||||
|
||||
#include "hyper_ideal_utility.hpp"
|
||||
#include "clausen.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
|
||||
using conformallab::calculateTetrahedronVolume;
|
||||
using conformallab::calculateTetrahedronVolumeWithIdealVertexAtGamma;
|
||||
using conformallab::Lobachevsky;
|
||||
|
||||
constexpr double PI = 3.14159265358979323846264338328;
|
||||
|
||||
// Regular tetrahedron at the Euclidean boundary (beta = arccos(1/3) for each
|
||||
// vertex angle) has volume 0 — it degenerates to a flat configuration.
|
||||
TEST(HyperIdealUtilityTest, VolumeEuclidean) {
|
||||
double b = std::acos(1.0 / 3.0);
|
||||
double V = calculateTetrahedronVolume(b, b, b, b, b, b);
|
||||
EXPECT_NEAR(0.0, V, 1e-7);
|
||||
}
|
||||
|
||||
// Regular ideal tetrahedron with all angles pi/3.
|
||||
// Formula: sum of Lobachevsky values at each angle.
|
||||
TEST(HyperIdealUtilityTest, VolumeRegularIdeal1) {
|
||||
double b = PI / 3.0;
|
||||
double Ve = Lobachevsky(b) + Lobachevsky(b) + Lobachevsky(b);
|
||||
double V = calculateTetrahedronVolume(b, b, b, b, b, b);
|
||||
EXPECT_NEAR(Ve, V, 1e-12);
|
||||
}
|
||||
|
||||
// Right-angled ideal tetrahedron (pi/2, pi/4, pi/4).
|
||||
TEST(HyperIdealUtilityTest, VolumeRegularIdeal2) {
|
||||
double bi = PI / 2.0, bj = PI / 4.0, bk = PI / 4.0;
|
||||
double Ve = Lobachevsky(bi) + Lobachevsky(bj) + Lobachevsky(bk);
|
||||
double V = calculateTetrahedronVolume(bi, bj, bk, bi, bj, bk);
|
||||
EXPECT_NEAR(Ve, V, 1e-12);
|
||||
}
|
||||
|
||||
// Hyperideal octahedron: all angles 0, volume = 8*Л(pi/4).
|
||||
TEST(HyperIdealUtilityTest, VolumeOctahedron) {
|
||||
double Ve = 8.0 * Lobachevsky(PI / 4.0);
|
||||
double V = calculateTetrahedronVolume(0, 0, 0, 0, 0, 0);
|
||||
EXPECT_NEAR(Ve, V, 1e-12);
|
||||
}
|
||||
|
||||
// Hyperideal tetrahedron with one hyperideal vertex.
|
||||
// Manual formula from the paper vs. general formula.
|
||||
TEST(HyperIdealUtilityTest, VolumeSingleHyperidealVertex) {
|
||||
double bi = PI / 5.0, bj = PI / 4.0, bk = PI / 4.0;
|
||||
double ai = (PI + bi - bj - bk) / 2.0;
|
||||
double aj = (PI + bj - bi - bk) / 2.0;
|
||||
double ak = (PI + bk - bi - bj) / 2.0;
|
||||
double aijk= (PI - bk - bi - bj) / 2.0;
|
||||
double Ve = 0.5 * (Lobachevsky(bi) + Lobachevsky(bj) + Lobachevsky(bk)
|
||||
+ Lobachevsky(ai) + Lobachevsky(aj) + Lobachevsky(ak)
|
||||
+ Lobachevsky(aijk));
|
||||
double V = calculateTetrahedronVolume(bi, bj, bk, ai, aj, ak);
|
||||
EXPECT_NEAR(Ve, V, 1e-12);
|
||||
}
|
||||
|
||||
// A degenerate triangle (angle = pi) must give volume 0 without NaN.
|
||||
TEST(HyperIdealUtilityTest, VolumeWithDegenerateTriangle) {
|
||||
double V = calculateTetrahedronVolume(0.0, PI, 0.0, 0.0, 0.0, PI);
|
||||
EXPECT_NEAR(0.0, V, 1e-12);
|
||||
EXPECT_FALSE(std::isnan(V));
|
||||
}
|
||||
|
||||
// The two volume formulas (general and ideal-vertex specialization) must agree
|
||||
// on the same input — numerical consistency check.
|
||||
TEST(HyperIdealUtilityTest, CompareGeneralAndIdealFormulaCase1) {
|
||||
constexpr double EPS = 0.1;
|
||||
double bi = PI / 3.0, bj = PI / 3.0, bk = PI / 3.0;
|
||||
double ai = PI / 3.0 - EPS, aj = PI / 3.0 - EPS, ak = PI / 3.0 - EPS;
|
||||
double Ve = calculateTetrahedronVolumeWithIdealVertexAtGamma(bi, bj, bk, ai, aj, ak);
|
||||
double V = calculateTetrahedronVolume(bi, bj, bk, ai, aj, ak);
|
||||
EXPECT_NEAR(Ve, V, 1e-12);
|
||||
}
|
||||
|
||||
// Second consistency check with non-symmetric angles that sum to pi.
|
||||
TEST(HyperIdealUtilityTest, CompareGeneralAndIdealFormulaCase2) {
|
||||
double bi = 0.6623267054958116;
|
||||
double bj = 1.437248992086214;
|
||||
double bk = 1.0420169560077686;
|
||||
double ai = 0.6896178197389236;
|
||||
double aj = 0.5195634857410114;
|
||||
double ak = 0.6304500578493993;
|
||||
EXPECT_NEAR(PI, bi + bj + bk, 1e-12);
|
||||
double Ve = calculateTetrahedronVolumeWithIdealVertexAtGamma(bi, bj, bk, ai, aj, ak);
|
||||
double V = calculateTetrahedronVolume(bi, bj, bk, ai, aj, ak);
|
||||
EXPECT_NEAR(Ve, V, 1e-12);
|
||||
}
|
||||
Reference in New Issue
Block a user