From b6dc854a27352e6ebc7302646ff86beca00f1840 Mon Sep 17 00:00:00 2001 From: kad-dirc Date: Thu, 15 Feb 2024 14:54:15 +0100 Subject: [PATCH] dedupInnerOuters add unittest cases --- snap/snap.go | 6 +++ snap/snap_test.go | 134 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 132 insertions(+), 8 deletions(-) diff --git a/snap/snap.go b/snap/snap.go index 02c6e9d..1781207 100644 --- a/snap/snap.go +++ b/snap/snap.go @@ -173,6 +173,9 @@ func dedupeInnersOuters(outers [][][2]float64, inners [][][2]float64) ([][][2]fl lenOuters := len(outers) var indexesToDelete []int for i := 0; i < len(allRings); i++ { + if slices.Contains(indexesToDelete, i) { + continue + } iIsOuter := i < lenOuters var equalOutersIndexes []int var equalInnersIndexes []int @@ -183,6 +186,9 @@ func dedupeInnersOuters(outers [][][2]float64, inners [][][2]float64) ([][][2]fl } compareTwoRings: for j := i + 1; j < len(allRings); j++ { + if slices.Contains(indexesToDelete, j) { + continue + } jIsOuter := j < lenOuters // check length iLen := len(allRings[i]) diff --git a/snap/snap_test.go b/snap/snap_test.go index 22d4c11..faa0994 100644 --- a/snap/snap_test.go +++ b/snap/snap_test.go @@ -733,23 +733,123 @@ func Test_dedupeInnersOuters(t *testing.T) { wantOuters [][][2]float64 wantInners [][][2]float64 }{ + { + name: "#outer, #inner = 0, 0", + args: args{ + outers: squareRingArray(0, true), + inners: squareRingArray(0, false), + }, + wantOuters: squareRingArray(0, true), + wantInners: squareRingArray(0, false), + }, { name: "#outer, #inner = 1, 0", args: args{ - outers: [][][2]float64{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}, // square, counter clockwise - inners: [][][2]float64{{{}}}, + outers: squareRingArray(1, true), + inners: squareRingArray(0, false), }, - wantOuters: [][][2]float64{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}, - wantInners: [][][2]float64{{{}}}, + wantOuters: squareRingArray(1, true), + wantInners: squareRingArray(0, false), }, { name: "#outer, #inner = 1, 1", args: args{ - outers: [][][2]float64{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}, // square, counter clockwise - inners: [][][2]float64{{{0, 0}, {0, 1}, {1, 1}, {1, 0}}}, // square, clockwise + outers: squareRingArray(1, true), + inners: squareRingArray(1, false), + }, + wantOuters: squareRingArray(1, true), + wantInners: squareRingArray(1, false), + }, + { + name: "#outer, #inner = 2, 1", + args: args{ + outers: squareRingArray(2, true), + inners: squareRingArray(1, false), + }, + wantOuters: squareRingArray(1, true), + wantInners: squareRingArray(0, false), + }, + { + name: "#outer, #inner = 2, 2", + args: args{ + outers: squareRingArray(2, true), + inners: squareRingArray(2, false), + }, + wantOuters: squareRingArray(1, true), + wantInners: squareRingArray(1, false), + }, + { + name: "#outer, #inner = 0, 1", + args: args{ + outers: squareRingArray(0, true), + inners: squareRingArray(1, false), + }, + wantOuters: squareRingArray(0, true), + wantInners: squareRingArray(1, false), + }, + { + name: "#outer, #inner = 1, 2", + args: args{ + outers: squareRingArray(1, true), + inners: squareRingArray(2, false), + }, + wantOuters: squareRingArray(0, true), + wantInners: squareRingArray(1, false), + }, + { + name: "#outer, #inner = 2, 0", + args: args{ + outers: squareRingArray(2, true), + inners: squareRingArray(0, false), }, - wantOuters: [][][2]float64{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}, - wantInners: [][][2]float64{{{0, 0}, {0, 1}, {1, 1}, {1, 0}}}, + wantOuters: squareRingArray(2, true), + wantInners: squareRingArray(0, false), + }, + { + name: "#outer, #inner = 0, 2", + args: args{ + outers: squareRingArray(0, true), + inners: squareRingArray(2, false), + }, + wantOuters: squareRingArray(0, true), + wantInners: squareRingArray(2, false), + }, + { + name: "#outer, #inner = 3, 1", + args: args{ + outers: squareRingArray(3, true), + inners: squareRingArray(1, false), + }, + wantOuters: squareRingArray(2, true), + wantInners: squareRingArray(0, false), + }, + { + name: "#outer, #inner = 1, 3", + args: args{ + outers: squareRingArray(1, true), + inners: squareRingArray(3, false), + }, + wantOuters: squareRingArray(0, true), + wantInners: squareRingArray(2, false), + }, + // add different ring, which should be left alone + { + name: "#outer, #inner = 1, 1 + dummy ring", + args: args{ + outers: squareRingArray(1, true), + inners: append(squareRingArray(1, false), [][2]float64{{0, 0}, {1, 0}, {2, 1}}), + }, + wantOuters: squareRingArray(1, true), + wantInners: append(squareRingArray(1, false), [][2]float64{{0, 0}, {1, 0}, {2, 1}}), + }, + { + name: "#outer, #inner = 3, 1 + dummy ring", + args: args{ + outers: squareRingArray(3, true), + inners: append(squareRingArray(1, false), [][2]float64{{0, 0}, {1, 0}, {2, 1}}), + }, + wantOuters: squareRingArray(2, true), + wantInners: append(squareRingArray(0, false), [][2]float64{{0, 0}, {1, 0}, {2, 1}}), }, } for _, tt := range tests { @@ -831,3 +931,21 @@ func wktMustEncode(g geom.Geometry) (s string) { } return s } + +func squareRingArray(number int, isOuter bool) [][][2]float64 { + outerSquare := [][2]float64{{0, 0}, {1, 0}, {1, 1}, {0, 1}} // square, counter clockwise + innerSquare := [][2]float64{{0, 0}, {0, 1}, {1, 1}, {1, 0}} // square, clockwise + var squares = [][][2]float64{} + var square [][2]float64 + // outer or inner + if isOuter { + square = outerSquare + } else { + square = innerSquare + } + // add squares + for i := 0; i < number; i++ { + squares = append(squares, square) + } + return squares +}