Skip to content

Commit

Permalink
FIx incorrect parseTileLevelIndexWidth implementation and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roger2hk committed Jul 10, 2024
1 parent 60cf398 commit a7d3c8c
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 19 deletions.
40 changes: 21 additions & 19 deletions cmd/example-mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,35 @@ func main() {
// "/tile/0/x001/x234/067.p/8" means level 0, index 1234067 and width 8 of a partial tile.
func parseTileLevelIndexWidth(level, index string) (uint64, uint64, uint64, error) {
l, err := strconv.ParseUint(level, 10, 64)
if err != nil {
if l > 63 || err != nil {
return 0, 0, 0, fmt.Errorf("failed to parse tile level")
}

var i, w uint64
switch indexPaths := strings.Split(index, "/"); len(indexPaths) {
// Full tile = 3
// Partial tile = 4
case 3, 4:
indexPath := strings.Join(indexPaths[0:3], "")
indexPath = strings.ReplaceAll(indexPath, "x", "")
indexPath = strings.ReplaceAll(indexPath, ".p", "")
i, err = strconv.ParseUint(indexPath, 10, 64)
if err != nil {
i := uint64(0)
w := uint64(256)
indexPaths := strings.Split(index, "/")

if strings.Contains(index, ".p") {
w, err = strconv.ParseUint(indexPaths[len(indexPaths)-1], 10, 64)
if err != nil || w < 1 || w > 255 {
return 0, 0, 0, fmt.Errorf("failed to parse tile index")
}
indexPaths[len(indexPaths)-2] = strings.TrimSuffix(indexPaths[len(indexPaths)-2], ".p")
indexPaths = indexPaths[:len(indexPaths)-1]
}

w = 256
if len(indexPaths) == 4 {
w, err = strconv.ParseUint(indexPaths[3], 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to parse tile index")
}
}
default:
if strings.Count(index, "x") != len(indexPaths)-1 || strings.HasPrefix(indexPaths[len(indexPaths)-1], "x") {
return 0, 0, 0, fmt.Errorf("failed to parse tile index")
}

for _, indexPath := range indexPaths {
indexPath = strings.TrimPrefix(indexPath, "x")
n, err := strconv.ParseUint(indexPath, 10, 64)
if err != nil || n >= 1000 || len(indexPath) > 3 {
return 0, 0, 0, fmt.Errorf("failed to parse tile index")
}
i = i*1000 + n
}

return l, i, w, nil
}
148 changes: 148 additions & 0 deletions cmd/example-mysql/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2024 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"testing"
)

func TestParseTileLevelIndexWidth(t *testing.T) {
for _, test := range []struct {
pathLevel string
pathIndex string
wantLevel uint64
wantIndex uint64
wantWidth uint64
wantErr bool
}{
{
pathLevel: "0",
pathIndex: "x001/x234/067",
wantLevel: 0,
wantIndex: 1234067,
wantWidth: 256,
},
{
pathLevel: "0",
pathIndex: "x001/x234/067.p/89",
wantLevel: 0,
wantIndex: 1234067,
wantWidth: 89,
},
{
pathLevel: "63",
pathIndex: "x999/x999/x999/x999/x999/999.p/255",
wantLevel: 63,
wantIndex: 999999999999999999,
wantWidth: 255,
},
{
pathLevel: "0",
pathIndex: "001",
wantLevel: 0,
wantIndex: 1,
wantWidth: 256,
},
{
pathLevel: "0",
pathIndex: "x001/x234/067.p/",
wantErr: true,
},
{
pathLevel: "0",
pathIndex: "x001/x234/067.p",
wantErr: true,
},
{
pathLevel: "0",
pathIndex: "x001/x234/",
wantErr: true,
},
{
pathLevel: "0",
pathIndex: "x001/x234",
wantErr: true,
},
{
pathLevel: "0",
pathIndex: "x001/",
wantErr: true,
},
{
pathLevel: "0",
pathIndex: "x001",
wantErr: true,
},
{
pathLevel: "1",
pathIndex: "x001/.p/abc",
wantErr: true,
},
{
pathLevel: "64",
pathIndex: "x001/002",
wantErr: true,
},
{
pathLevel: "-1",
pathIndex: "x001/002",
wantErr: true,
},
{
pathLevel: "abc",
pathIndex: "x001/002",
wantErr: true,
},
{
pathLevel: "8",
pathIndex: "001/002",
wantErr: true,
},
{
pathLevel: "8",
pathIndex: "x001/0002",
wantErr: true,
},
{
pathLevel: "8",
pathIndex: "x001/-002",
wantErr: true,
},
{
pathLevel: "8",
pathIndex: "x001/002.p/256",
wantErr: true,
},
} {
desc := fmt.Sprintf("pathLevel: %q, pathIndex: %q", test.pathLevel, test.pathIndex)
t.Run(desc, func(t *testing.T) {
gotLevel, gotIndex, gotWidth, err := parseTileLevelIndexWidth(test.pathLevel, test.pathIndex)
if gotLevel != test.wantLevel {
t.Errorf("got level %d want %d", gotLevel, test.wantLevel)
}
if gotIndex != test.wantIndex {
t.Errorf("got index %d want %d", gotIndex, test.wantIndex)
}
if gotWidth != test.wantWidth {
t.Errorf("got width %d want %d", gotWidth, test.wantWidth)
}
gotErr := err != nil
if gotErr != test.wantErr {
t.Errorf("got err %v want %v", gotErr, test.wantErr)
}
})
}
}

0 comments on commit a7d3c8c

Please sign in to comment.