|
| 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 |
0 commit comments