Commit 69183570 authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

exp/locale/collate: Added test flag to maketables tool for comparing newly

against previously generated tables.

R=r
CC=golang-dev
https://golang.org/cl/6441098
parent 2b14a48d
...@@ -11,6 +11,7 @@ package main ...@@ -11,6 +11,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"exp/locale/collate" "exp/locale/collate"
"exp/locale/collate/build" "exp/locale/collate/build"
"flag" "flag"
...@@ -21,6 +22,7 @@ import ( ...@@ -21,6 +22,7 @@ import (
"os" "os"
"path" "path"
"regexp" "regexp"
"sort"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
...@@ -29,6 +31,9 @@ import ( ...@@ -29,6 +31,9 @@ import (
var ducet = flag.String("ducet", var ducet = flag.String("ducet",
"http://unicode.org/Public/UCA/"+unicode.Version+"/allkeys.txt", "http://unicode.org/Public/UCA/"+unicode.Version+"/allkeys.txt",
"URL of the Default Unicode Collation Element Table (DUCET).") "URL of the Default Unicode Collation Element Table (DUCET).")
var test = flag.Bool("test",
false,
"test existing tables; can be used to compare web data with package data")
var localFiles = flag.Bool("local", var localFiles = flag.Bool("local",
false, false,
"data files have been copied to the current directory; for debugging only") "data files have been copied to the current directory; for debugging only")
...@@ -124,6 +129,9 @@ func parseUCA(builder *build.Builder) { ...@@ -124,6 +129,9 @@ func parseUCA(builder *build.Builder) {
if len(part[1]) < n+3 || part[1][n+1] != '#' { if len(part[1]) < n+3 || part[1][n+1] != '#' {
log.Fatalf("%d: expected comment; found %s", i, part[1][n:]) log.Fatalf("%d: expected comment; found %s", i, part[1][n:])
} }
if *test {
testInput.add(string(lhs))
}
failOnError(builder.Add(lhs, rhs, vars)) failOnError(builder.Add(lhs, rhs, vars))
} }
} }
...@@ -137,6 +145,59 @@ func convHex(line int, s string) int { ...@@ -137,6 +145,59 @@ func convHex(line int, s string) int {
return int(r) return int(r)
} }
var testInput = stringSet{}
type stringSet struct {
set []string
}
func (ss *stringSet) add(s string) {
ss.set = append(ss.set, s)
}
func (ss *stringSet) values() []string {
ss.compact()
return ss.set
}
func (ss *stringSet) compact() {
a := ss.set
sort.Strings(a)
k := 0
for i := 1; i < len(a); i++ {
if a[k] != a[i] {
a[k+1] = a[i]
k++
}
}
ss.set = a[:k+1]
}
func testCollator(c *collate.Collator) {
c0 := collate.Root
// iterator over all characters for all locales and check
// whether Key is equal.
buf := collate.Buffer{}
// Add all common and not too uncommon runes to the test set.
for i := rune(0); i < 0x30000; i++ {
testInput.add(string(i))
}
for i := rune(0xE0000); i < 0xF0000; i++ {
testInput.add(string(i))
}
for _, str := range testInput.values() {
k0 := c0.KeyFromString(&buf, str)
k := c.KeyFromString(&buf, str)
if bytes.Compare(k0, k) != 0 {
failOnError(fmt.Errorf("test:%U: keys differ (%x vs %x)", []rune(str), k0, k))
}
buf.ResetKeys()
}
fmt.Println("PASS")
}
// TODO: move this functionality to exp/locale/collate/build. // TODO: move this functionality to exp/locale/collate/build.
func printCollators(c *collate.Collator) { func printCollators(c *collate.Collator) {
const name = "Root" const name = "Root"
...@@ -157,18 +218,22 @@ func main() { ...@@ -157,18 +218,22 @@ func main() {
c, err := b.Build() c, err := b.Build()
failOnError(err) failOnError(err)
fmt.Println("// Generated by running") if *test {
fmt.Printf("// maketables --ducet=%s\n", *ducet) testCollator(c)
fmt.Println("// DO NOT EDIT") } else {
fmt.Println("// TODO: implement more compact representation for sparse blocks.") fmt.Println("// Generated by running")
fmt.Println("") fmt.Printf("// maketables --ducet=%s\n", *ducet)
fmt.Println("package collate") fmt.Println("// DO NOT EDIT")
fmt.Println("") fmt.Println("// TODO: implement more compact representation for sparse blocks.")
fmt.Println(`import "exp/norm"`) fmt.Println("")
fmt.Println("") fmt.Println("package collate")
fmt.Println("")
fmt.Println(`import "exp/norm"`)
fmt.Println("")
printCollators(c) printCollators(c)
_, err = b.Print(os.Stdout) _, err = b.Print(os.Stdout)
failOnError(err) failOnError(err)
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment