|
| 1 | +package utils |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestEncodePolyline_GoogleExample(t *testing.T) { |
| 6 | + // Canonical example from Google's polyline algorithm documentation. |
| 7 | + coords := [][]float64{ |
| 8 | + {38.5, -120.2}, |
| 9 | + {40.7, -120.95}, |
| 10 | + {43.252, -126.453}, |
| 11 | + } |
| 12 | + got := EncodePolyline(coords) |
| 13 | + want := "_p~iF~ps|U_ulLnnqC_mqNvxq`@" |
| 14 | + if got != want { |
| 15 | + t.Errorf("EncodePolyline() = %q, want %q", got, want) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func TestEncodePolyline_FloorsNotRounds(t *testing.T) { |
| 20 | + // 0.000019 * 1e5 = 1.9. Java floors to 1 (same as 0.00001); a rounding |
| 21 | + // encoder would yield 2 (same as 0.00002). Verify we floor. |
| 22 | + got := EncodePolyline([][]float64{{0.000019, 0}}) |
| 23 | + floored := EncodePolyline([][]float64{{0.00001, 0}}) |
| 24 | + rounded := EncodePolyline([][]float64{{0.00002, 0}}) |
| 25 | + if got != floored { |
| 26 | + t.Errorf("floor(0.000019*1e5) should match 0.00001 encoding; got %q want %q", got, floored) |
| 27 | + } |
| 28 | + if got == rounded { |
| 29 | + t.Errorf("floored encoding should differ from rounded 0.00002 encoding %q", rounded) |
| 30 | + } |
| 31 | + |
| 32 | + // Negative boundary: -0.000019 * 1e5 = -1.9. Java floors to -2 (same as |
| 33 | + // -0.00002); truncation or rounding would give -1 (same as -0.00001). |
| 34 | + gotNeg := EncodePolyline([][]float64{{-0.000019, 0}}) |
| 35 | + flooredNeg := EncodePolyline([][]float64{{-0.00002, 0}}) |
| 36 | + truncOrRoundNeg := EncodePolyline([][]float64{{-0.00001, 0}}) |
| 37 | + if gotNeg != flooredNeg { |
| 38 | + t.Errorf("floor(-0.000019*1e5) should match -0.00002 encoding; got %q want %q", gotNeg, flooredNeg) |
| 39 | + } |
| 40 | + if gotNeg == truncOrRoundNeg { |
| 41 | + t.Errorf("floored negative encoding should differ from -0.00001 (truncate/round) encoding %q", truncOrRoundNeg) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestEncodePolyline_PreservesDuplicates(t *testing.T) { |
| 46 | + // A consecutive duplicate point must still be encoded (delta 0,0), not dropped. |
| 47 | + coords := [][]float64{{1.0, 1.0}, {1.0, 1.0}, {2.0, 2.0}} |
| 48 | + withDup := EncodePolyline(coords) |
| 49 | + withoutDup := EncodePolyline([][]float64{{1.0, 1.0}, {2.0, 2.0}}) |
| 50 | + if withDup == withoutDup { |
| 51 | + t.Errorf("duplicate point should add a zero-delta segment; encodings should differ") |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestEncodePolyline_Empty(t *testing.T) { |
| 56 | + if got := EncodePolyline(nil); got != "" { |
| 57 | + t.Errorf("EncodePolyline(nil) = %q, want empty string", got) |
| 58 | + } |
| 59 | +} |
0 commit comments