Test the US places' reach by the locality table's population column, read by its header
This commit is contained in:
+23
-23
@@ -4,6 +4,7 @@ import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -378,43 +379,42 @@ func TestShippedAddressIsOneNamedPick(t *testing.T) {
|
||||
// TestShippedPlacesSpreadAddresses pins that a city ships a tenth of its streets, not ten,
|
||||
// and that the US places reach below a town.
|
||||
func TestShippedPlacesSpreadAddresses(t *testing.T) {
|
||||
cases := []struct {
|
||||
country, city string
|
||||
smallest int
|
||||
}{
|
||||
{"SE", "Göteborg", 200},
|
||||
{"US", "3651000", 2500},
|
||||
}
|
||||
for _, c := range cases {
|
||||
for country, city := range map[string]string{"SE": "Göteborg", "US": "3651000"} {
|
||||
streets := 0
|
||||
for _, r := range geoRows(t, c.country, "street") {
|
||||
if r[1] == c.city {
|
||||
for _, r := range geoRows(t, country, "street") {
|
||||
if r[1] == city {
|
||||
streets++
|
||||
}
|
||||
}
|
||||
if streets < 100 {
|
||||
t.Errorf("geo.%s: %s ships %d streets, want a tenth of its streets, at least 100", c.country, c.city, streets)
|
||||
}
|
||||
population := map[string]int{"SE": 2, "US": 3}[c.country]
|
||||
smallest := -1
|
||||
for _, r := range geoRows(t, c.country, "locality") {
|
||||
if n, err := strconv.Atoi(r[population]); err == nil && (smallest < 0 || n < smallest) {
|
||||
smallest = n
|
||||
}
|
||||
}
|
||||
if smallest < 0 || smallest > c.smallest {
|
||||
t.Errorf("geo.%s: the smallest locality holds %d people, want one of %d or fewer", c.country, smallest, c.smallest)
|
||||
t.Errorf("geo.%s: %s ships %d streets, want a tenth of its streets, at least 100", country, city, streets)
|
||||
}
|
||||
}
|
||||
header, _, _ := strings.Cut(geoTSV(t, "US", "locality"), "\n")
|
||||
population := slices.Index(strings.Split(header, "\t"), "population")
|
||||
for _, r := range geoRows(t, "US", "locality") {
|
||||
n, err := strconv.Atoi(r[population])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n < 2500 {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Error("geo.US: every locality holds 2500 people or more, want places drawn down to villages")
|
||||
}
|
||||
|
||||
func geoRows(t *testing.T, country, table string) [][]string {
|
||||
func geoTSV(t *testing.T, country, table string) string {
|
||||
b, err := os.ReadFile("data/geo/" + country + "/" + table + ".tsv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return strings.TrimSpace(string(b))
|
||||
}
|
||||
|
||||
func geoRows(t *testing.T, country, table string) [][]string {
|
||||
var out [][]string
|
||||
for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n")[1:] {
|
||||
for _, line := range strings.Split(geoTSV(t, country, table), "\n")[1:] {
|
||||
out = append(out, strings.Split(line, "\t"))
|
||||
}
|
||||
return out
|
||||
|
||||
Reference in New Issue
Block a user