Skip to content

Commit d3ba44d

Browse files
BotellaAclaude
andcommitted
test(Geometry): portable inputs and shared failure report in brute force tests
- absl::int128 instead of __int128 (not supported by MSVC) - random numbers from the generator output only: the standard distributions are implementation-defined, so platforms tested different inputs - record/check/hex/report shared in brute-force.hpp - the distance test collects failures with hexadecimal inputs instead of throwing on the first one Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e5ceb6e commit d3ba44d

5 files changed

Lines changed: 290 additions & 248 deletions

File tree

‎tests/geometry/brute-force.hpp‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (c) 2019 - 2026 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#pragma once
25+
26+
#include <cmath>
27+
#include <cstdint>
28+
#include <cstdio>
29+
#include <map>
30+
#include <random>
31+
#include <string>
32+
#include <string_view>
33+
34+
#include <absl/strings/str_cat.h>
35+
36+
#include <geode/basic/assert.hpp>
37+
#include <geode/basic/logger.hpp>
38+
#include <geode/basic/range.hpp>
39+
40+
#include <geode/geometry/point.hpp>
41+
42+
/*
43+
* Shared tools of the brute force tests: portable random numbers and report
44+
* of the failures with their exact (hexadecimal) inputs.
45+
*/
46+
namespace
47+
{
48+
/*
49+
* The generator output is portable, unlike the standard distributions
50+
* (implementation-defined): the same inputs are tested on all platforms.
51+
*/
52+
std::mt19937_64 generator{ 42 };
53+
54+
/*
55+
* Uniform double in [min, max) from the 53 high bits of the generator
56+
*/
57+
inline double uniform( double min, double max )
58+
{
59+
const auto unit =
60+
std::ldexp( static_cast< double >( generator() >> 11 ), -53 );
61+
return min + ( max - min ) * unit;
62+
}
63+
64+
/*
65+
* Uniform integer in [min, max] (the modulo bias is negligible for the
66+
* tested ranges)
67+
*/
68+
inline std::int64_t random_int( std::int64_t min, std::int64_t max )
69+
{
70+
const auto range = static_cast< std::uint64_t >( max - min ) + 1;
71+
return min + static_cast< std::int64_t >( generator() % range );
72+
}
73+
74+
std::map< std::string, geode::index_t > issues;
75+
std::string current_mode;
76+
77+
inline std::string hex( double value )
78+
{
79+
char buffer[32];
80+
std::snprintf( buffer, sizeof( buffer ), "%a", value );
81+
return buffer;
82+
}
83+
84+
template < geode::index_t dimension >
85+
std::string hex( const geode::Point< dimension >& point )
86+
{
87+
std::string result;
88+
for( const auto d : geode::LRange{ dimension } )
89+
{
90+
absl::StrAppend( &result, hex( point.value( d ) ), " " );
91+
}
92+
return result;
93+
}
94+
95+
/*
96+
* Counts the failure in its category and current mode, logs the first
97+
* two of each
98+
*/
99+
inline void record(
100+
const std::string& category, const std::string& details )
101+
{
102+
if( issues[category + current_mode]++ < 2 )
103+
{
104+
geode::Logger::warning( category, current_mode, " | ", details );
105+
}
106+
}
107+
108+
template < typename... Args >
109+
void check(
110+
bool condition, const std::string& category, const Args&... details )
111+
{
112+
if( !condition )
113+
{
114+
record( category, absl::StrCat( details... ) );
115+
}
116+
}
117+
118+
/*
119+
* Logs the failure counts, then fails if any
120+
*/
121+
inline void report_issues( std::string_view name )
122+
{
123+
for( const auto& [category, count] : issues )
124+
{
125+
geode::Logger::info( category, ": ", count );
126+
}
127+
geode::OpenGeodeGeometryException::test(
128+
issues.empty(), name, " issues found" );
129+
}
130+
} // namespace

‎tests/geometry/random-geometry.hpp‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <algorithm>
2727
#include <array>
2828
#include <cmath>
29-
#include <random>
3029

3130
#include <geode/basic/range.hpp>
3231

@@ -36,21 +35,15 @@
3635
#include <geode/geometry/point.hpp>
3736
#include <geode/geometry/vector.hpp>
3837

38+
#include "brute-force.hpp"
39+
3940
/*
4041
* Random geometric configurations for brute force tests: generic and nearly
4142
* degenerated objects (cap, needle and flat triangles, crossing, coplanar and
4243
* nearly parallel objects)
4344
*/
4445
namespace
4546
{
46-
std::mt19937 generator{ 42 };
47-
48-
double uniform( double min, double max )
49-
{
50-
return std::uniform_real_distribution< double >{ min, max }(
51-
generator );
52-
}
53-
5447
double log_uniform( double min_exponent, double max_exponent )
5548
{
5649
return std::pow( 10., uniform( min_exponent, max_exponent ) );
@@ -222,6 +215,7 @@ namespace
222215

223216
/*
224217
* Orthonormal axes of the plane orthogonal to the given unit normal
218+
* (independent of the library implementation on purpose)
225219
*/
226220
std::array< geode::Vector3D, 2 > plane_axes( const geode::Vector3D& normal )
227221
{

0 commit comments

Comments
 (0)