Files
FreeCADWasmDev/tools/test_mesh_wasm_sort.py
wangdequan 9a7c4f0335 Save FreeCAD Web workspace sources and user validation evidence
IFC native binding remains incomplete; preserve accurate test results.
2026-09-15 22:01:58 +08:00

60 lines
2.0 KiB
Python

"""Check the Mesh sort policy without rebuilding a complete FreeCAD runtime."""
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
SOURCE = r'''
#include <cerrno>
#include <functional>
#include <numeric>
#include <pthread.h>
#include <random>
#include <vector>
#include "src/Mod/Mesh/App/Core/Functional.h"
static int threadAttempts = 0;
extern "C" int __wrap_pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)
{
++threadAttempts;
return EAGAIN;
}
int main()
{
std::mt19937 random(42);
for (const int size : {0, 1, 2, 36, 65536}) {
std::vector<int> original(size);
for (auto& value : original) value = static_cast<int>(random() % 1000);
for (const int threads : {0, 1, 2, 64}) {
auto expected = original;
auto actual = original;
std::sort(expected.begin(), expected.end(), std::greater<int>());
MeshCore::parallel_sort(actual.begin(), actual.end(), std::greater<int>(), threads);
if (actual != expected) return 1;
}
}
return threadAttempts ? 2 : 0;
}
'''
class MeshWasmSortTests(unittest.TestCase):
@unittest.skipUnless(shutil.which("c++"), "C++ compiler is required")
def test_wasm_sort_preserves_results_without_attempting_pthreads(self):
with tempfile.TemporaryDirectory(prefix="freecad-mesh-sort-") as directory:
source = Path(directory) / "sort.cpp"
binary = Path(directory) / "sort"
source.write_text(SOURCE)
subprocess.run(["c++", "-std=c++17", "-O2", "-D__EMSCRIPTEN__", "-pthread",
"-Wl,--wrap=pthread_create", "-I", str(ROOT / "freecad-source"),
str(source), "-o", str(binary)], check=True, capture_output=True, text=True, timeout=30)
subprocess.run([str(binary)], check=True, capture_output=True, text=True, timeout=5)
if __name__ == "__main__":
unittest.main()