SPB Git

Repository Public

ClustererTests.swift 8.5 KB 191 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Lou-Ka iOS9// File             : LouKaTests/ClustererTests.swift10// Description      : regroupement des points de carte — conservation des11//                    effectifs, bornes, performance.12// Module           : LouKaTests13// Repository       : https://git.spboucher.ai/lou-ka-ios-v314// Project Website  : https://www.lou-ka.com15// Created          : 2026-11-2516// Last Modified    : 2026-11-2817//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : None specified (no license file in this repository)83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation91import Testing92@testable import LouKa9394@Suite("Regroupement de carte")95struct ClustererTests {96    /// Points pseudo-aléatoires reproductibles autour de Montréal.97    static func points(_ n: Int, spread: Double = 0.25) -> [MapPoint] {98        var seed: UInt64 = 4299        func rnd() -> Double {100            seed = seed &* 6364136223846793005 &+ 1442695040888963407101            return Double(seed >> 33) / Double(1 << 31)102        }103        return (0..<n).map { i in104            MapPoint(uid: "p\(i)", lng: -73.6 + (rnd() - 0.5) * spread * 2, lat: 45.52 + (rnd() - 0.5) * spread,105                     price: 900 + rnd() * 1500, verdict: i % 7 == 0 ? .sous : (i % 11 == 0 ? .sur : nil))106        }107    }108109    static let montreal = MapBounds(west: -73.9, south: 45.38, east: -73.3, north: 45.68)110111    @Test("aucun logement visible n'est perdu")112    func conservesCounts() {113        let pts = Self.points(5000)114        let area = Self.montreal.padded(0.12)115        let visible = pts.filter { area.contains(lat: $0.lat, lng: $0.lng) }.count116        let items = Clusterer.cluster(pts, in: Self.montreal, columns: 5, aspect: 1.8)117        #expect(items.reduce(0) { $0 + $1.count } == visible)118        #expect(items.count <= 160)119        #expect(Set(items.map(\.id)).count == items.count, "identifiants uniques")120    }121122    @Test("peu de points : chaque logement reste individuel")123    func fewPointsStaySingle() {124        let pts = Self.points(40)125        let items = Clusterer.cluster(pts, in: MapBounds(west: -74.2, south: 45.2, east: -73.0, north: 45.9))126        #expect(items.count == 40)127        #expect(items.allSatisfy { if case .single = $0.kind { return true }; return false })128    }129130    @Test("même adresse : un seul marqueur regroupé")131    func coincidentPointsMerge() {132        let pts = (0..<6).map { MapPoint(uid: "u\($0)", lng: -73.57, lat: 45.5, price: 1000 + Double($0) * 100, verdict: $0 == 0 ? .sous : nil) }133        let items = Clusterer.cluster(pts, in: MapBounds(west: -73.6, south: 45.48, east: -73.54, north: 45.52))134        #expect(items.count == 1)135        guard case .cluster(let count, let good, let minPrice) = items[0].kind else { Issue.record("regroupement attendu"); return }136        #expect(count == 6 && good == 1 && minPrice == 1000)137    }138139    @Test("hors de la zone visible : rien")140    func outsideBounds() {141        #expect(Clusterer.cluster(Self.points(500), in: MapBounds(west: -71.4, south: 46.7, east: -71.1, north: 46.9)).isEmpty)142        #expect(Clusterer.cluster([], in: Self.montreal).isEmpty)143    }144145    @Test("les disques voisins sont fusionnés (pas de chevauchement)")146    func neighboursAreMerged() {147        let cols = 5, aspect = 1.8148        let items = Clusterer.cluster(Self.points(8000), in: Self.montreal, columns: cols, aspect: aspect)149        let area = Self.montreal.padded(0.12)150        let cw = (area.east - area.west) / Double(cols), ch = (area.north - area.south) / Double(Int((Double(cols) * aspect).rounded()))151        for (i, a) in items.enumerated() {152            for b in items[(i + 1)...] {153                let dx = (a.lng - b.lng) / cw, dy = (a.lat - b.lat) / ch154                #expect(dx * dx + dy * dy >= 0.5 * 0.5, "deux regroupements trop proches")155            }156        }157    }158159    @Test("zone englobante robuste aux coordonnées aberrantes")160    func robustBounds() throws {161        var pts = Self.points(1000, spread: 0.1)162        pts.append(MapPoint(uid: "loin", lng: -60, lat: 50, price: nil, verdict: nil))163        let b = try #require(Clusterer.robustBounds(pts))164        #expect(b.east < -73.0 && b.north < 46.0)165        #expect(Clusterer.robustBounds([]) == nil)166    }167168    @Test("40 000 points regroupés rapidement", .timeLimit(.minutes(1)))169    func performance() {170        let pts = Self.points(40_000, spread: 1.2)171        let start = Date()172        var total = 0173        for _ in 0..<10 { total += Clusterer.cluster(pts, in: MapBounds(west: -74.6, south: 45.0, east: -72.6, north: 46.1), columns: 5, aspect: 1.8).count }174        let perCall = Date().timeIntervalSince(start) / 10175        #expect(total > 0)176        #expect(perCall < 0.25, "regroupement trop lent : \(perCall) s")177    }178179    @Test("le logement sélectionné reste seul, jamais absorbé par un regroupement")180    func pinnedStaysSingle() {181        let pts = Self.points(5000)182        let area = Self.montreal.padded(0.12)183        let target = pts.first { area.contains(lat: $0.lat, lng: $0.lng) }!184        let items = Clusterer.cluster(pts, in: Self.montreal, columns: 5, aspect: 1.8, pinned: target.uid)185        let singles = items.filter { $0.id == target.uid }186        #expect(singles.count == 1)187        if case .single(let uid, _, _) = singles[0].kind { #expect(uid == target.uid) } else { Issue.record("étiquette individuelle attendue") }188        #expect(items.reduce(0) { $0 + $1.count } == pts.filter { area.contains(lat: $0.lat, lng: $0.lng) }.count)189    }190}191
Files main
Menu

Theme