Test the US places' reach by the locality table's population column, read by its header

This commit is contained in:
2026-10-11 16:32:19 +02:00
committed by Lillem4n
parent 63a7ac156e
commit 121f9549f2
+23 -23
View File
@@ -4,6 +4,7 @@ import (
"io/fs" "io/fs"
"os" "os"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@@ -378,43 +379,42 @@ func TestShippedAddressIsOneNamedPick(t *testing.T) {
// TestShippedPlacesSpreadAddresses pins that a city ships a tenth of its streets, not ten, // TestShippedPlacesSpreadAddresses pins that a city ships a tenth of its streets, not ten,
// and that the US places reach below a town. // and that the US places reach below a town.
func TestShippedPlacesSpreadAddresses(t *testing.T) { func TestShippedPlacesSpreadAddresses(t *testing.T) {
cases := []struct { for country, city := range map[string]string{"SE": "Göteborg", "US": "3651000"} {
country, city string
smallest int
}{
{"SE", "Göteborg", 200},
{"US", "3651000", 2500},
}
for _, c := range cases {
streets := 0 streets := 0
for _, r := range geoRows(t, c.country, "street") { for _, r := range geoRows(t, country, "street") {
if r[1] == c.city { if r[1] == city {
streets++ streets++
} }
} }
if streets < 100 { 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) t.Errorf("geo.%s: %s ships %d streets, want a tenth of its streets, at least 100", country, 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)
} }
} }
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") b, err := os.ReadFile("data/geo/" + country + "/" + table + ".tsv")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
return strings.TrimSpace(string(b))
}
func geoRows(t *testing.T, country, table string) [][]string {
var out [][]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")) out = append(out, strings.Split(line, "\t"))
} }
return out return out