SPB Git

Repository Public

ClustererTest.kt 8.1 KB 164 lines kotlin

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project          : Immo-Ka Android10 * File             : app/src/test/kotlin/com/spboucher/immoka/ClustererTest.kt11 * Description      : carte — fusion des regroupements du serveur (aucune12 *                    inscription perdue ni inventée, médiane gardée seulement13 *                    quand elle est exacte), zone à cadrer, zoom déduit de14 *                    l'emprise. Le regroupement des points isolés est fait par15 *                    le moteur KA Maps (voir MapTest pour la couche qu'on lui16 *                    donne).17 * Module           : test18 * Repository       : https://git.spboucher.ai/immo-ka-android19 * Project Website  : https://www.immo-ka.com20 * Created          : 2026-11-2921 * Last Modified    : 2026-12-0122 *23 * =============================================================================24 * AUTHOR & PROFESSIONAL AFFILIATION25 * =============================================================================26 *27 * Author           : Simon-Pierre Boucher28 * Position         : Professor of Finance29 * Institution      : Université du Québec en Outaouais (UQO)30 * Department       : Département des sciences administratives31 * Organization     : SP Boucher32 *33 * Website          : https://spboucher.ai34 * Git              : https://git.spboucher.ai35 *36 * =============================================================================37 * OFFICIAL CONTACT DIRECTORY38 * =============================================================================39 *40 * General          : info@spboucher.ai41 * Professional     : contact@spboucher.ai42 * Direct           : simon@spboucher.ai43 * Research         : research@spboucher.ai44 * Development      : dev@spboucher.ai45 * Support          : support@spboucher.ai46 * Security/Admin   : admin@spboucher.ai47 * Notifications    : notifications@spboucher.ai48 *49 * Preferred Contact: contact@spboucher.ai50 *51 * =============================================================================52 * COPYRIGHT & INTELLECTUAL PROPERTY53 * =============================================================================54 *55 * Copyright (c) 202656 * Simon-Pierre Boucher57 *58 * All rights reserved where applicable.59 *60 * This file is governed by the applicable project license,61 * copyright notices, institutional agreements and relevant law.62 *63 * Unless expressly authorized by the applicable license or64 * rights holder, copying, redistribution, sublicensing,65 * publication and commercial exploitation are not permitted.66 *67 * Existing third-party rights and notices remain unaffected.68 *69 * =============================================================================70 * CONFIDENTIALITY & SECURITY71 * =============================================================================72 *73 * Classification   : Public74 * Distribution     : Public repository; use is governed by the project license75 * Security Contact : admin@spboucher.ai76 *77 * Access to this source does not independently grant permission78 * to redistribute or disclose confidential materials.79 *80 * Do not embed credentials, private keys, access tokens,81 * certificates or confidential personal information.82 *83 * =============================================================================84 * LICENSE & MAINTENANCE85 * =============================================================================86 *87 * License          : None specified (no license file in this repository)88 * Maintainer       : Simon-Pierre Boucher89 * Technical Contact: dev@spboucher.ai90 *91 * =============================================================================92 * END OF SP BOUCHER SOURCE HEADER93 * =============================================================================94 */9596package com.spboucher.immoka9798import com.spboucher.immoka.core.map.Clusterer99import com.spboucher.immoka.core.map.MapBounds100import com.spboucher.immoka.core.model.MapPage101import com.spboucher.immoka.core.model.MapPoint102import com.spboucher.immoka.core.model.Position103import com.spboucher.immoka.core.model.ServerCluster104import org.junit.Assert.assertEquals105import org.junit.Assert.assertNull106import org.junit.Assert.assertTrue107import org.junit.Test108109class ClustererTest {110    private val montreal = MapBounds(-74.0, 45.3, -73.3, 45.8)111112    /** Points pseudo-aléatoires reproductibles (générateur congruentiel, graine 42) autour de Montréal. */113    private fun points(n: Int): List<MapPoint> {114        var seed = 42L115        fun next(): Double { seed = (seed * 1103515245 + 12345) and 0x7fffffff; return seed / 2147483647.0 }116        return (0 until n).map { i ->117            MapPoint("p$i", -74.2 + next() * 1.1, 45.2 + next() * 0.7, 200_000 + next() * 900_000, if (i % 5 == 0) Position.SOUS else Position.DANS)118        }119    }120121    @Test fun `regroupements du serveur - fusion sans perte ni invention`() {122        val page = MapPage.from(Fixtures.obj("v3-map-clusters"))123        val quebec = MapBounds(-80.5, 44.5, -56.5, 63.0)124        val items = Clusterer.merge(page.clusters, quebec, columns = 5, aspect = 1.8)125        assertTrue(items.size <= 160); assertTrue(items.size < page.clusters.size)126        assertEquals(page.clusters.sumOf { it.count }, items.sumOf { it.count })127        assertEquals(items.size, items.map { it.id }.toSet().size)128        val min = page.clusters.mapNotNull { it.minPrice?.takeIf { p -> p >= 10_000 } }.min()129        assertEquals(min, items.mapNotNull { it.minPrice }.min(), 0.0)130        // la médiane d'un regroupement fusionné n'est gardée que s'il vient d'une seule cellule du serveur131        val single = Clusterer.merge(page.clusters.take(1), quebec, columns = 5, aspect = 1.8).single()132        assertEquals(page.clusters[0].medianPrice?.takeIf { it >= 10_000 }, single.medianPrice)133        val two = listOf(ServerCluster(-73.5, 45.5, 10, 100_000.0, 300_000.0), ServerCluster(-73.5001, 45.5001, 30, 150_000.0, 500_000.0))134        val fused = Clusterer.merge(two, MapBounds(-74.0, 45.0, -73.0, 46.0), columns = 2, aspect = 1.0).single()135        assertEquals(40, fused.count); assertNull(fused.medianPrice); assertEquals(100_000.0, fused.minPrice!!, 0.0)136        assertTrue(Clusterer.merge(page.clusters, MapBounds(10.0, 10.0, 11.0, 11.0)).isEmpty())137        // au-delà de la borne, les petits rejoignent un voisin : la somme reste exacte138        val many = (0 until 900).map { ServerCluster(-80 + (it % 30) * 0.7, 45 + (it / 30) * 0.5, 1 + it % 7, 100_000.0 + it) }139        val capped = Clusterer.merge(many, quebec, columns = 40, aspect = 1.0, maxItems = 50)140        assertEquals(50, capped.size); assertEquals(many.sumOf { it.count }, capped.sumOf { it.count })141    }142143    @Test fun `zone a cadrer`() {144        val page = MapPage.from(Fixtures.obj("v3-map-clusters"))145        val b = Clusterer.robustBounds(emptyList(), page.clusters)!!146        assertTrue(b.west > -80.5 && b.north < 52 && b.west < b.east && b.south < b.north)147        assertNull(Clusterer.robustBounds(emptyList(), emptyList()))148        val one = Clusterer.robustBounds(listOf(MapPoint("a", -73.5, 45.5, null, null)), emptyList())!!149        assertEquals(one.west, one.east, 0.0)150        // un point aberrant (coordonnées erronées) est ignoré151        val pts = points(200).map { it.copy(lng = -73.9 + (it.lng + 74.2) / 3, lat = 45.4 + (it.lat - 45.2) / 3) } + MapPoint("x", -60.0, 60.0, null, null)152        val r = Clusterer.robustBounds(pts)!!153        assertTrue(r.east < -73 && r.north < 46)154    }155156    @Test fun `zoom deduit de l'emprise`() {157        assertEquals(2.0, Clusterer.zoom(MapBounds(-180.0, -85.0, 180.0, 85.0), 256.0), 0.0)158        val city = Clusterer.zoom(MapBounds(-75.9, 45.35, -75.55, 45.55), 402.0)159        assertTrue(city > 10 && city < 11)160        assertEquals(20.0, Clusterer.zoom(MapBounds(-73.5, 45.5, -73.5, 45.5), 402.0), 0.0)161        assertEquals("-73.50000,45.50000,-73.40000,45.60000", MapBounds(-73.5, 45.5, -73.4, 45.6).bbox)162    }163}164
Files main
Menu

Theme